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

5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
5 years ago
5 years ago
3 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
5 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
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 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
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 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
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 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 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. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  189. JobName: jobName,
  190. RetryCount: 1,
  191. GpuType: gpuQueue,
  192. Image: image,
  193. TaskRoles: []models.TaskRole{
  194. {
  195. Name: SubTaskName,
  196. TaskNumber: 1,
  197. MinSucceededTaskCount: 1,
  198. MinFailedTaskCount: 1,
  199. CPUNumber: resourceSpec.CpuNum,
  200. GPUNumber: resourceSpec.GpuNum,
  201. MemoryMB: resourceSpec.MemMiB,
  202. ShmMB: resourceSpec.ShareMemMiB,
  203. Command: command,
  204. NeedIBDevice: false,
  205. IsMainRole: false,
  206. UseNNI: false,
  207. },
  208. },
  209. Volumes: []models.Volume{
  210. {
  211. HostPath: models.StHostPath{
  212. Path: codePath,
  213. MountPath: CodeMountPath,
  214. ReadOnly: false,
  215. },
  216. },
  217. {
  218. HostPath: models.StHostPath{
  219. Path: dataActualPath,
  220. MountPath: DataSetMountPath,
  221. ReadOnly: true,
  222. },
  223. },
  224. {
  225. HostPath: models.StHostPath{
  226. Path: modelPath,
  227. MountPath: ModelMountPath,
  228. ReadOnly: false,
  229. },
  230. },
  231. {
  232. HostPath: models.StHostPath{
  233. Path: benchmarkPath,
  234. MountPath: BenchMarkMountPath,
  235. ReadOnly: true,
  236. },
  237. },
  238. {
  239. HostPath: models.StHostPath{
  240. Path: snn4imagenetPath,
  241. MountPath: Snn4imagenetMountPath,
  242. ReadOnly: true,
  243. },
  244. },
  245. {
  246. HostPath: models.StHostPath{
  247. Path: brainScorePath,
  248. MountPath: BrainScoreMountPath,
  249. ReadOnly: true,
  250. },
  251. },
  252. },
  253. })
  254. if err != nil {
  255. log.Error("CreateJob failed:", err.Error(), ctx.Data["MsgID"])
  256. return err
  257. }
  258. if jobResult.Code != Success {
  259. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  260. return errors.New(jobResult.Msg)
  261. }
  262. var jobID = jobResult.Payload["jobId"].(string)
  263. err = models.CreateCloudbrain(&models.Cloudbrain{
  264. Status: string(models.JobWaiting),
  265. UserID: ctx.User.ID,
  266. RepoID: ctx.Repo.Repository.ID,
  267. JobID: jobID,
  268. JobName: jobName,
  269. DisplayJobName: displayJobName,
  270. SubTaskName: SubTaskName,
  271. JobType: jobType,
  272. Type: models.TypeCloudBrainOne,
  273. Uuid: uuid,
  274. Image: image,
  275. GpuQueue: gpuQueue,
  276. ResourceSpecId: resourceSpecId,
  277. ComputeResource: models.GPUResource,
  278. BenchmarkTypeID: benchmarkTypeID,
  279. BenchmarkChildTypeID: benchmarkChildTypeID,
  280. Description: description,
  281. IsLatestVersion: "1",
  282. VersionCount: versionCount,
  283. BranchName: branchName,
  284. BootFile: bootFile,
  285. DatasetName: datasetName,
  286. Parameters: params,
  287. })
  288. if err != nil {
  289. return err
  290. }
  291. task, err := models.GetCloudbrainByJobID(jobID)
  292. if err != nil {
  293. log.Error("GetCloudbrainByName failed: %v", err.Error())
  294. return err
  295. }
  296. stringId := strconv.FormatInt(task.ID, 10)
  297. if string(models.JobTypeBenchmark) == jobType {
  298. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateBenchMarkTask)
  299. } else if string(models.JobTypeTrain) == jobType {
  300. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, jobID, displayJobName, models.ActionCreateGPUTrainTask)
  301. } else {
  302. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateDebugGPUTask)
  303. }
  304. return nil
  305. }
  306. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newID *string) error {
  307. dataActualPath := setting.Attachment.Minio.RealPath +
  308. setting.Attachment.Minio.Bucket + "/" +
  309. setting.Attachment.Minio.BasePath +
  310. models.AttachmentRelativePath(task.Uuid) +
  311. task.Uuid
  312. jobName := task.JobName
  313. var resourceSpec *models.ResourceSpec
  314. if ResourceSpecs == nil {
  315. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  316. }
  317. for _, spec := range ResourceSpecs.ResourceSpec {
  318. if task.ResourceSpecId == spec.Id {
  319. resourceSpec = spec
  320. }
  321. }
  322. if resourceSpec == nil {
  323. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  324. return errors.New("no such resourceSpec")
  325. }
  326. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  327. JobName: jobName,
  328. RetryCount: 1,
  329. GpuType: task.GpuQueue,
  330. Image: task.Image,
  331. TaskRoles: []models.TaskRole{
  332. {
  333. Name: SubTaskName,
  334. TaskNumber: 1,
  335. MinSucceededTaskCount: 1,
  336. MinFailedTaskCount: 1,
  337. CPUNumber: resourceSpec.CpuNum,
  338. GPUNumber: resourceSpec.GpuNum,
  339. MemoryMB: resourceSpec.MemMiB,
  340. ShmMB: resourceSpec.ShareMemMiB,
  341. Command: Command,
  342. NeedIBDevice: false,
  343. IsMainRole: false,
  344. UseNNI: false,
  345. },
  346. },
  347. Volumes: []models.Volume{
  348. {
  349. HostPath: models.StHostPath{
  350. Path: storage.GetMinioPath(jobName, CodeMountPath+"/"),
  351. MountPath: CodeMountPath,
  352. ReadOnly: false,
  353. },
  354. },
  355. {
  356. HostPath: models.StHostPath{
  357. Path: dataActualPath,
  358. MountPath: DataSetMountPath,
  359. ReadOnly: true,
  360. },
  361. },
  362. {
  363. HostPath: models.StHostPath{
  364. Path: storage.GetMinioPath(jobName, ModelMountPath+"/"),
  365. MountPath: ModelMountPath,
  366. ReadOnly: false,
  367. },
  368. },
  369. {
  370. HostPath: models.StHostPath{
  371. Path: storage.GetMinioPath(jobName, BenchMarkMountPath+"/"),
  372. MountPath: BenchMarkMountPath,
  373. ReadOnly: true,
  374. },
  375. },
  376. {
  377. HostPath: models.StHostPath{
  378. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath+"/"),
  379. MountPath: Snn4imagenetMountPath,
  380. ReadOnly: true,
  381. },
  382. },
  383. {
  384. HostPath: models.StHostPath{
  385. Path: storage.GetMinioPath(jobName, BrainScoreMountPath+"/"),
  386. MountPath: BrainScoreMountPath,
  387. ReadOnly: true,
  388. },
  389. },
  390. },
  391. })
  392. if err != nil {
  393. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  394. return err
  395. }
  396. if jobResult.Code != Success {
  397. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  398. return errors.New(jobResult.Msg)
  399. }
  400. var jobID = jobResult.Payload["jobId"].(string)
  401. newTask := &models.Cloudbrain{
  402. Status: string(models.JobWaiting),
  403. UserID: task.UserID,
  404. RepoID: task.RepoID,
  405. JobID: jobID,
  406. JobName: task.JobName,
  407. DisplayJobName: task.DisplayJobName,
  408. SubTaskName: task.SubTaskName,
  409. JobType: task.JobType,
  410. Type: task.Type,
  411. Uuid: task.Uuid,
  412. Image: task.Image,
  413. GpuQueue: task.GpuQueue,
  414. ResourceSpecId: task.ResourceSpecId,
  415. ComputeResource: task.ComputeResource,
  416. }
  417. err = models.RestartCloudbrain(task, newTask)
  418. if err != nil {
  419. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  420. return err
  421. }
  422. stringId := strconv.FormatInt(newTask.ID, 10)
  423. *newID = stringId
  424. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, task.DisplayJobName, models.ActionCreateDebugGPUTask)
  425. return nil
  426. }