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