You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cloudbrain.go 12 kB

5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. package cloudbrain
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strconv"
  6. "code.gitea.io/gitea/modules/storage"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/notification"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. const (
  14. Command = `pip3 install jupyterlab==2.2.5 -i https://pypi.tuna.tsinghua.edu.cn/simple;service ssh stop;jupyter lab --no-browser --ip=0.0.0.0 --allow-root --notebook-dir="/code" --port=80 --LabApp.token="" --LabApp.allow_origin="self https://cloudbrain.pcl.ac.cn"`
  15. //CommandBenchmark = `echo "start benchmark";python /code/test.py;echo "end benchmark"`
  16. CommandBenchmark = `echo "start benchmark";cd /benchmark && bash run_bk.sh;echo "end benchmark"`
  17. CodeMountPath = "/code"
  18. DataSetMountPath = "/dataset"
  19. ModelMountPath = "/model"
  20. LogFile = "log.txt"
  21. BenchMarkMountPath = "/benchmark"
  22. BenchMarkResourceID = 1
  23. Snn4imagenetMountPath = "/snn4imagenet"
  24. BrainScoreMountPath = "/brainscore"
  25. TaskInfoName = "/taskInfo"
  26. SubTaskName = "task1"
  27. Success = "S000"
  28. DefaultBranchName = "master"
  29. )
  30. var (
  31. ResourceSpecs *models.ResourceSpecs
  32. TrainResourceSpecs *models.ResourceSpecs
  33. )
  34. func isAdminOrOwnerOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  35. if !ctx.IsSigned {
  36. return false
  37. }
  38. log.Info("is repo owner:" + strconv.FormatBool(ctx.IsUserRepoOwner()))
  39. log.Info("is user admin:" + strconv.FormatBool(ctx.IsUserSiteAdmin()))
  40. if err != nil {
  41. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin()
  42. } else {
  43. log.Info("is job creator:" + strconv.FormatBool(ctx.User.ID == job.UserID))
  44. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  45. }
  46. }
  47. func CanDeleteJob(ctx *context.Context, job *models.Cloudbrain) bool {
  48. return isAdminOrOwnerOrJobCreater(ctx, job, nil)
  49. }
  50. func CanCreateOrDebugJob(ctx *context.Context) bool {
  51. if !ctx.IsSigned {
  52. return false
  53. }
  54. return ctx.Repo.CanWrite(models.UnitTypeCloudBrain)
  55. }
  56. func CanModifyJob(ctx *context.Context, job *models.Cloudbrain) bool {
  57. return isAdminOrJobCreater(ctx, job, nil)
  58. }
  59. func isAdminOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  60. if !ctx.IsSigned {
  61. return false
  62. }
  63. if err != nil {
  64. return ctx.IsUserSiteAdmin()
  65. } else {
  66. return ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  67. }
  68. }
  69. func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  70. var ID = ctx.Params(":id")
  71. job, err := models.GetCloudbrainByID(ID)
  72. if err != nil {
  73. log.Error("GetCloudbrainByID failed:%v", err.Error())
  74. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  75. }
  76. ctx.Cloudbrain = job
  77. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  78. log.Error("!isAdminOrOwnerOrJobCreater error:%v", err.Error())
  79. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  80. }
  81. }
  82. func AdminOrJobCreaterRight(ctx *context.Context) {
  83. var ID = ctx.Params(":id")
  84. job, err := models.GetCloudbrainByID(ID)
  85. if err != nil {
  86. log.Error("GetCloudbrainByID failed:%v", err.Error())
  87. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  88. }
  89. ctx.Cloudbrain = job
  90. if !isAdminOrJobCreater(ctx, job, err) {
  91. log.Error("!isAdminOrJobCreater error:%v", err.Error())
  92. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  93. }
  94. }
  95. func AdminOrOwnerOrJobCreaterRightForTrain(ctx *context.Context) {
  96. var jobID = ctx.Params(":jobid")
  97. job, err := models.GetCloudbrainByJobID(jobID)
  98. if err != nil {
  99. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  100. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  101. }
  102. ctx.Cloudbrain = job
  103. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  104. log.Error("!isAdminOrOwnerOrJobCreater failed:%v", err.Error())
  105. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  106. }
  107. }
  108. func AdminOrJobCreaterRightForTrain(ctx *context.Context) {
  109. var jobID = ctx.Params(":jobid")
  110. job, err := models.GetCloudbrainByJobID(jobID)
  111. if err != nil {
  112. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  113. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  114. }
  115. ctx.Cloudbrain = job
  116. if !isAdminOrJobCreater(ctx, job, err) {
  117. log.Error("!isAdminOrJobCreater errot:%v", err.Error())
  118. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  119. }
  120. }
  121. func GenerateTask(ctx *context.Context, displayJobName, jobName, image, command, uuid, codePath, modelPath, benchmarkPath, snn4imagenetPath, brainScorePath, jobType, gpuQueue, description, branchName string, benchmarkTypeID, benchmarkChildTypeID, resourceSpecId int) error {
  122. dataActualPath := setting.Attachment.Minio.RealPath +
  123. setting.Attachment.Minio.Bucket + "/" +
  124. setting.Attachment.Minio.BasePath +
  125. models.AttachmentRelativePath(uuid) +
  126. uuid
  127. var resourceSpec *models.ResourceSpec
  128. var versionCount int
  129. if jobType == string(models.JobTypeTrain) {
  130. versionCount = 1
  131. if TrainResourceSpecs == nil {
  132. json.Unmarshal([]byte(setting.TrainResourceSpecs), &TrainResourceSpecs)
  133. }
  134. for _, spec := range TrainResourceSpecs.ResourceSpec {
  135. if resourceSpecId == spec.Id {
  136. resourceSpec = spec
  137. }
  138. }
  139. } else {
  140. if ResourceSpecs == nil {
  141. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  142. }
  143. for _, spec := range ResourceSpecs.ResourceSpec {
  144. if resourceSpecId == spec.Id {
  145. resourceSpec = spec
  146. }
  147. }
  148. }
  149. if resourceSpec == nil {
  150. log.Error("no such resourceSpecId(%d)", resourceSpecId, ctx.Data["MsgID"])
  151. return errors.New("no such resourceSpec")
  152. }
  153. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  154. JobName: jobName,
  155. RetryCount: 1,
  156. GpuType: gpuQueue,
  157. Image: image,
  158. TaskRoles: []models.TaskRole{
  159. {
  160. Name: SubTaskName,
  161. TaskNumber: 1,
  162. MinSucceededTaskCount: 1,
  163. MinFailedTaskCount: 1,
  164. CPUNumber: resourceSpec.CpuNum,
  165. GPUNumber: resourceSpec.GpuNum,
  166. MemoryMB: resourceSpec.MemMiB,
  167. ShmMB: resourceSpec.ShareMemMiB,
  168. Command: command,
  169. NeedIBDevice: false,
  170. IsMainRole: false,
  171. UseNNI: false,
  172. },
  173. },
  174. Volumes: []models.Volume{
  175. {
  176. HostPath: models.StHostPath{
  177. Path: codePath,
  178. MountPath: CodeMountPath,
  179. ReadOnly: false,
  180. },
  181. },
  182. {
  183. HostPath: models.StHostPath{
  184. Path: dataActualPath,
  185. MountPath: DataSetMountPath,
  186. ReadOnly: true,
  187. },
  188. },
  189. {
  190. HostPath: models.StHostPath{
  191. Path: modelPath,
  192. MountPath: ModelMountPath,
  193. ReadOnly: false,
  194. },
  195. },
  196. {
  197. HostPath: models.StHostPath{
  198. Path: benchmarkPath,
  199. MountPath: BenchMarkMountPath,
  200. ReadOnly: true,
  201. },
  202. },
  203. {
  204. HostPath: models.StHostPath{
  205. Path: snn4imagenetPath,
  206. MountPath: Snn4imagenetMountPath,
  207. ReadOnly: true,
  208. },
  209. },
  210. {
  211. HostPath: models.StHostPath{
  212. Path: brainScorePath,
  213. MountPath: BrainScoreMountPath,
  214. ReadOnly: true,
  215. },
  216. },
  217. },
  218. })
  219. if err != nil {
  220. log.Error("CreateJob failed:", err.Error(), ctx.Data["MsgID"])
  221. return err
  222. }
  223. if jobResult.Code != Success {
  224. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  225. return errors.New(jobResult.Msg)
  226. }
  227. var jobID = jobResult.Payload["jobId"].(string)
  228. err = models.CreateCloudbrain(&models.Cloudbrain{
  229. Status: string(models.JobWaiting),
  230. UserID: ctx.User.ID,
  231. RepoID: ctx.Repo.Repository.ID,
  232. JobID: jobID,
  233. JobName: jobName,
  234. DisplayJobName: displayJobName,
  235. SubTaskName: SubTaskName,
  236. JobType: jobType,
  237. Type: models.TypeCloudBrainOne,
  238. Uuid: uuid,
  239. Image: image,
  240. GpuQueue: gpuQueue,
  241. ResourceSpecId: resourceSpecId,
  242. ComputeResource: models.GPUResource,
  243. BenchmarkTypeID: benchmarkTypeID,
  244. BenchmarkChildTypeID: benchmarkChildTypeID,
  245. Description: description,
  246. IsLatestVersion: "1",
  247. VersionCount: versionCount,
  248. BranchName: branchName,
  249. })
  250. if err != nil {
  251. return err
  252. }
  253. task, err := models.GetCloudbrainByName(jobName)
  254. if err != nil {
  255. log.Error("GetCloudbrainByName failed: %v", err.Error())
  256. return err
  257. }
  258. stringId := strconv.FormatInt(task.ID, 10)
  259. if string(models.JobTypeBenchmark) == jobType {
  260. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateBenchMarkTask)
  261. } else if string(models.JobTypeTrain) == jobType {
  262. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateGPUTrainTask)
  263. } else {
  264. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateDebugGPUTask)
  265. }
  266. return nil
  267. }
  268. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newID *string) error {
  269. dataActualPath := setting.Attachment.Minio.RealPath +
  270. setting.Attachment.Minio.Bucket + "/" +
  271. setting.Attachment.Minio.BasePath +
  272. models.AttachmentRelativePath(task.Uuid) +
  273. task.Uuid
  274. jobName := task.JobName
  275. var resourceSpec *models.ResourceSpec
  276. if ResourceSpecs == nil {
  277. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  278. }
  279. for _, spec := range ResourceSpecs.ResourceSpec {
  280. if task.ResourceSpecId == spec.Id {
  281. resourceSpec = spec
  282. }
  283. }
  284. if resourceSpec == nil {
  285. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  286. return errors.New("no such resourceSpec")
  287. }
  288. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  289. JobName: jobName,
  290. RetryCount: 1,
  291. GpuType: task.GpuQueue,
  292. Image: task.Image,
  293. TaskRoles: []models.TaskRole{
  294. {
  295. Name: SubTaskName,
  296. TaskNumber: 1,
  297. MinSucceededTaskCount: 1,
  298. MinFailedTaskCount: 1,
  299. CPUNumber: resourceSpec.CpuNum,
  300. GPUNumber: resourceSpec.GpuNum,
  301. MemoryMB: resourceSpec.MemMiB,
  302. ShmMB: resourceSpec.ShareMemMiB,
  303. Command: Command,
  304. NeedIBDevice: false,
  305. IsMainRole: false,
  306. UseNNI: false,
  307. },
  308. },
  309. Volumes: []models.Volume{
  310. {
  311. HostPath: models.StHostPath{
  312. Path: storage.GetMinioPath(jobName, CodeMountPath+"/"),
  313. MountPath: CodeMountPath,
  314. ReadOnly: false,
  315. },
  316. },
  317. {
  318. HostPath: models.StHostPath{
  319. Path: dataActualPath,
  320. MountPath: DataSetMountPath,
  321. ReadOnly: true,
  322. },
  323. },
  324. {
  325. HostPath: models.StHostPath{
  326. Path: storage.GetMinioPath(jobName, ModelMountPath+"/"),
  327. MountPath: ModelMountPath,
  328. ReadOnly: false,
  329. },
  330. },
  331. {
  332. HostPath: models.StHostPath{
  333. Path: storage.GetMinioPath(jobName, BenchMarkMountPath+"/"),
  334. MountPath: BenchMarkMountPath,
  335. ReadOnly: true,
  336. },
  337. },
  338. {
  339. HostPath: models.StHostPath{
  340. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath+"/"),
  341. MountPath: Snn4imagenetMountPath,
  342. ReadOnly: true,
  343. },
  344. },
  345. {
  346. HostPath: models.StHostPath{
  347. Path: storage.GetMinioPath(jobName, BrainScoreMountPath+"/"),
  348. MountPath: BrainScoreMountPath,
  349. ReadOnly: true,
  350. },
  351. },
  352. },
  353. })
  354. if err != nil {
  355. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  356. return err
  357. }
  358. if jobResult.Code != Success {
  359. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  360. return errors.New(jobResult.Msg)
  361. }
  362. var jobID = jobResult.Payload["jobId"].(string)
  363. newTask := &models.Cloudbrain{
  364. Status: string(models.JobWaiting),
  365. UserID: task.UserID,
  366. RepoID: task.RepoID,
  367. JobID: jobID,
  368. JobName: task.JobName,
  369. DisplayJobName: task.DisplayJobName,
  370. SubTaskName: task.SubTaskName,
  371. JobType: task.JobType,
  372. Type: task.Type,
  373. Uuid: task.Uuid,
  374. Image: task.Image,
  375. GpuQueue: task.GpuQueue,
  376. ResourceSpecId: task.ResourceSpecId,
  377. ComputeResource: task.ComputeResource,
  378. }
  379. err = models.RestartCloudbrain(task, newTask)
  380. if err != nil {
  381. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  382. return err
  383. }
  384. idString := strconv.FormatInt(newTask.ID, 10)
  385. *newID = idString
  386. return nil
  387. }