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 14 kB

4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. package cloudbrain
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strconv"
  6. "code.gitea.io/gitea/modules/timeutil"
  7. "code.gitea.io/gitea/modules/storage"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/notification"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. const (
  15. 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"`
  16. //CommandBenchmark = `echo "start benchmark";python /code/test.py;echo "end benchmark"`
  17. CommandBenchmark = `echo "start benchmark";cd /benchmark && bash run_bk.sh;echo "end benchmark"`
  18. CodeMountPath = "/code"
  19. DataSetMountPath = "/dataset"
  20. ModelMountPath = "/model"
  21. LogFile = "log.txt"
  22. BenchMarkMountPath = "/benchmark"
  23. BenchMarkResourceID = 1
  24. Snn4imagenetMountPath = "/snn4imagenet"
  25. BrainScoreMountPath = "/brainscore"
  26. TaskInfoName = "/taskInfo"
  27. Snn4imagenetCommand = `/opt/conda/bin/python /snn4imagenet/testSNN_script.py --modelname '%s' --modelpath '/dataset' --modeldescription '%s'`
  28. BrainScoreCommand = `bash /brainscore/brainscore_test_par4shSrcipt.sh -b '%s' -n '%s' -p '/dataset' -d '%s'`
  29. SubTaskName = "task1"
  30. Success = "S000"
  31. DefaultBranchName = "master"
  32. )
  33. var (
  34. ResourceSpecs *models.ResourceSpecs
  35. TrainResourceSpecs *models.ResourceSpecs
  36. )
  37. func isAdminOrOwnerOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  38. if !ctx.IsSigned {
  39. return false
  40. }
  41. if err != nil {
  42. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin()
  43. } else {
  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 isAdminOrImageCreater(ctx *context.Context, image *models.Image, err error) bool {
  70. if !ctx.IsSigned {
  71. return false
  72. }
  73. if err != nil {
  74. return ctx.IsUserSiteAdmin()
  75. } else {
  76. return ctx.IsUserSiteAdmin() || ctx.User.ID == image.UID
  77. }
  78. }
  79. func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  80. var ID = ctx.Params(":id")
  81. job, err := models.GetCloudbrainByID(ID)
  82. if err != nil {
  83. log.Error("GetCloudbrainByID failed:%v", err.Error())
  84. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  85. }
  86. ctx.Cloudbrain = job
  87. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  88. log.Error("!isAdminOrOwnerOrJobCreater error:%v", err.Error())
  89. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  90. }
  91. }
  92. func AdminOrJobCreaterRight(ctx *context.Context) {
  93. var ID = ctx.Params(":id")
  94. job, err := models.GetCloudbrainByID(ID)
  95. if err != nil {
  96. log.Error("GetCloudbrainByID failed:%v", err.Error())
  97. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  98. }
  99. ctx.Cloudbrain = job
  100. if !isAdminOrJobCreater(ctx, job, err) {
  101. log.Error("!isAdminOrJobCreater error:%v", err.Error())
  102. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  103. }
  104. }
  105. func AdminOrOwnerOrJobCreaterRightForTrain(ctx *context.Context) {
  106. var jobID = ctx.Params(":jobid")
  107. job, err := models.GetCloudbrainByJobID(jobID)
  108. if err != nil {
  109. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  110. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  111. }
  112. ctx.Cloudbrain = job
  113. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  114. log.Error("!isAdminOrOwnerOrJobCreater failed:%v", err.Error())
  115. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  116. }
  117. }
  118. func AdminOrJobCreaterRightForTrain(ctx *context.Context) {
  119. var jobID = ctx.Params(":jobid")
  120. job, err := models.GetCloudbrainByJobID(jobID)
  121. if err != nil {
  122. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  123. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  124. }
  125. ctx.Cloudbrain = job
  126. if !isAdminOrJobCreater(ctx, job, err) {
  127. log.Error("!isAdminOrJobCreater errot:%v", err.Error())
  128. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  129. }
  130. }
  131. func AdminOrImageCreaterRight(ctx *context.Context) {
  132. id, err := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  133. var image *models.Image
  134. if err != nil {
  135. log.Error("Get Image by ID failed:%v", err.Error())
  136. } else {
  137. image, err = models.GetImageByID(id)
  138. if err != nil {
  139. log.Error("Get Image by ID failed:%v", err.Error())
  140. return
  141. }
  142. }
  143. if !isAdminOrImageCreater(ctx, image, err) {
  144. log.Error("!isAdminOrImageCreater error:%v", err.Error())
  145. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  146. }
  147. }
  148. func GenerateTask(ctx *context.Context, displayJobName, jobName, image, command, uuid, codePath, modelPath, benchmarkPath, snn4imagenetPath, brainScorePath, jobType, gpuQueue, description, branchName, bootFile, params, commitID string, benchmarkTypeID, benchmarkChildTypeID, resourceSpecId int) error {
  149. dataActualPath := setting.Attachment.Minio.RealPath +
  150. setting.Attachment.Minio.Bucket + "/" +
  151. setting.Attachment.Minio.BasePath +
  152. models.AttachmentRelativePath(uuid) +
  153. uuid
  154. var resourceSpec *models.ResourceSpec
  155. var versionCount int
  156. if jobType == string(models.JobTypeTrain) {
  157. versionCount = 1
  158. if TrainResourceSpecs == nil {
  159. json.Unmarshal([]byte(setting.TrainResourceSpecs), &TrainResourceSpecs)
  160. }
  161. for _, spec := range TrainResourceSpecs.ResourceSpec {
  162. if resourceSpecId == spec.Id {
  163. resourceSpec = spec
  164. }
  165. }
  166. } else {
  167. if ResourceSpecs == nil {
  168. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  169. }
  170. for _, spec := range ResourceSpecs.ResourceSpec {
  171. if resourceSpecId == spec.Id {
  172. resourceSpec = spec
  173. }
  174. }
  175. }
  176. if resourceSpec == nil {
  177. log.Error("no such resourceSpecId(%d)", resourceSpecId, ctx.Data["MsgID"])
  178. return errors.New("no such resourceSpec")
  179. }
  180. var datasetName string
  181. attach, err := models.GetAttachmentByUUID(uuid)
  182. if err != nil {
  183. //for benchmark, do not return error
  184. log.Error("GetAttachmentByUUID failed:%v", err)
  185. } else {
  186. datasetName = attach.Name
  187. }
  188. createTime := timeutil.TimeStampNow()
  189. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  190. JobName: jobName,
  191. RetryCount: 1,
  192. GpuType: gpuQueue,
  193. Image: image,
  194. TaskRoles: []models.TaskRole{
  195. {
  196. Name: SubTaskName,
  197. TaskNumber: 1,
  198. MinSucceededTaskCount: 1,
  199. MinFailedTaskCount: 1,
  200. CPUNumber: resourceSpec.CpuNum,
  201. GPUNumber: resourceSpec.GpuNum,
  202. MemoryMB: resourceSpec.MemMiB,
  203. ShmMB: resourceSpec.ShareMemMiB,
  204. Command: command,
  205. NeedIBDevice: false,
  206. IsMainRole: false,
  207. UseNNI: false,
  208. },
  209. },
  210. Volumes: []models.Volume{
  211. {
  212. HostPath: models.StHostPath{
  213. Path: codePath,
  214. MountPath: CodeMountPath,
  215. ReadOnly: false,
  216. },
  217. },
  218. {
  219. HostPath: models.StHostPath{
  220. Path: dataActualPath,
  221. MountPath: DataSetMountPath,
  222. ReadOnly: true,
  223. },
  224. },
  225. {
  226. HostPath: models.StHostPath{
  227. Path: modelPath,
  228. MountPath: ModelMountPath,
  229. ReadOnly: false,
  230. },
  231. },
  232. {
  233. HostPath: models.StHostPath{
  234. Path: benchmarkPath,
  235. MountPath: BenchMarkMountPath,
  236. ReadOnly: true,
  237. },
  238. },
  239. {
  240. HostPath: models.StHostPath{
  241. Path: snn4imagenetPath,
  242. MountPath: Snn4imagenetMountPath,
  243. ReadOnly: true,
  244. },
  245. },
  246. {
  247. HostPath: models.StHostPath{
  248. Path: brainScorePath,
  249. MountPath: BrainScoreMountPath,
  250. ReadOnly: true,
  251. },
  252. },
  253. },
  254. })
  255. if err != nil {
  256. log.Error("CreateJob failed:", err.Error(), ctx.Data["MsgID"])
  257. return err
  258. }
  259. if jobResult.Code != Success {
  260. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  261. return errors.New(jobResult.Msg)
  262. }
  263. var jobID = jobResult.Payload["jobId"].(string)
  264. err = models.CreateCloudbrain(&models.Cloudbrain{
  265. Status: string(models.JobWaiting),
  266. UserID: ctx.User.ID,
  267. RepoID: ctx.Repo.Repository.ID,
  268. JobID: jobID,
  269. JobName: jobName,
  270. DisplayJobName: displayJobName,
  271. SubTaskName: SubTaskName,
  272. JobType: jobType,
  273. Type: models.TypeCloudBrainOne,
  274. Uuid: uuid,
  275. Image: image,
  276. GpuQueue: gpuQueue,
  277. ResourceSpecId: resourceSpecId,
  278. ComputeResource: models.GPUResource,
  279. BenchmarkTypeID: benchmarkTypeID,
  280. BenchmarkChildTypeID: benchmarkChildTypeID,
  281. Description: description,
  282. IsLatestVersion: "1",
  283. VersionCount: versionCount,
  284. BranchName: branchName,
  285. BootFile: bootFile,
  286. DatasetName: datasetName,
  287. Parameters: params,
  288. CreatedUnix: createTime,
  289. UpdatedUnix: createTime,
  290. CommitID: commitID,
  291. })
  292. if err != nil {
  293. return err
  294. }
  295. task, err := models.GetCloudbrainByJobID(jobID)
  296. if err != nil {
  297. log.Error("GetCloudbrainByName failed: %v", err.Error())
  298. return err
  299. }
  300. stringId := strconv.FormatInt(task.ID, 10)
  301. if IsBenchmarkJob(jobType) {
  302. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateBenchMarkTask)
  303. } else if string(models.JobTypeTrain) == jobType {
  304. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, jobID, displayJobName, models.ActionCreateGPUTrainTask)
  305. } else {
  306. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateDebugGPUTask)
  307. }
  308. return nil
  309. }
  310. func IsBenchmarkJob(jobType string) bool {
  311. return string(models.JobTypeBenchmark) == jobType || string(models.JobTypeBrainScore) == jobType || string(models.JobTypeSnn4imagenet) == jobType
  312. }
  313. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newID *string) error {
  314. dataActualPath := setting.Attachment.Minio.RealPath +
  315. setting.Attachment.Minio.Bucket + "/" +
  316. setting.Attachment.Minio.BasePath +
  317. models.AttachmentRelativePath(task.Uuid) +
  318. task.Uuid
  319. jobName := task.JobName
  320. var resourceSpec *models.ResourceSpec
  321. if ResourceSpecs == nil {
  322. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  323. }
  324. for _, spec := range ResourceSpecs.ResourceSpec {
  325. if task.ResourceSpecId == spec.Id {
  326. resourceSpec = spec
  327. }
  328. }
  329. if resourceSpec == nil {
  330. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  331. return errors.New("no such resourceSpec")
  332. }
  333. createTime := timeutil.TimeStampNow()
  334. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  335. JobName: jobName,
  336. RetryCount: 1,
  337. GpuType: task.GpuQueue,
  338. Image: task.Image,
  339. TaskRoles: []models.TaskRole{
  340. {
  341. Name: SubTaskName,
  342. TaskNumber: 1,
  343. MinSucceededTaskCount: 1,
  344. MinFailedTaskCount: 1,
  345. CPUNumber: resourceSpec.CpuNum,
  346. GPUNumber: resourceSpec.GpuNum,
  347. MemoryMB: resourceSpec.MemMiB,
  348. ShmMB: resourceSpec.ShareMemMiB,
  349. Command: Command,
  350. NeedIBDevice: false,
  351. IsMainRole: false,
  352. UseNNI: false,
  353. },
  354. },
  355. Volumes: []models.Volume{
  356. {
  357. HostPath: models.StHostPath{
  358. Path: storage.GetMinioPath(jobName, CodeMountPath+"/"),
  359. MountPath: CodeMountPath,
  360. ReadOnly: false,
  361. },
  362. },
  363. {
  364. HostPath: models.StHostPath{
  365. Path: dataActualPath,
  366. MountPath: DataSetMountPath,
  367. ReadOnly: true,
  368. },
  369. },
  370. {
  371. HostPath: models.StHostPath{
  372. Path: storage.GetMinioPath(jobName, ModelMountPath+"/"),
  373. MountPath: ModelMountPath,
  374. ReadOnly: false,
  375. },
  376. },
  377. {
  378. HostPath: models.StHostPath{
  379. Path: storage.GetMinioPath(jobName, BenchMarkMountPath+"/"),
  380. MountPath: BenchMarkMountPath,
  381. ReadOnly: true,
  382. },
  383. },
  384. {
  385. HostPath: models.StHostPath{
  386. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath+"/"),
  387. MountPath: Snn4imagenetMountPath,
  388. ReadOnly: true,
  389. },
  390. },
  391. {
  392. HostPath: models.StHostPath{
  393. Path: storage.GetMinioPath(jobName, BrainScoreMountPath+"/"),
  394. MountPath: BrainScoreMountPath,
  395. ReadOnly: true,
  396. },
  397. },
  398. },
  399. })
  400. if err != nil {
  401. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  402. return err
  403. }
  404. if jobResult.Code != Success {
  405. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  406. return errors.New(jobResult.Msg)
  407. }
  408. var jobID = jobResult.Payload["jobId"].(string)
  409. newTask := &models.Cloudbrain{
  410. Status: string(models.JobWaiting),
  411. UserID: task.UserID,
  412. RepoID: task.RepoID,
  413. JobID: jobID,
  414. JobName: task.JobName,
  415. DisplayJobName: task.DisplayJobName,
  416. SubTaskName: task.SubTaskName,
  417. JobType: task.JobType,
  418. Type: task.Type,
  419. Uuid: task.Uuid,
  420. Image: task.Image,
  421. GpuQueue: task.GpuQueue,
  422. ResourceSpecId: task.ResourceSpecId,
  423. ComputeResource: task.ComputeResource,
  424. CreatedUnix: createTime,
  425. UpdatedUnix: createTime,
  426. BranchName: task.BranchName,
  427. }
  428. err = models.RestartCloudbrain(task, newTask)
  429. if err != nil {
  430. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  431. return err
  432. }
  433. stringId := strconv.FormatInt(newTask.ID, 10)
  434. *newID = stringId
  435. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, task.DisplayJobName, models.ActionCreateDebugGPUTask)
  436. return nil
  437. }