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 13 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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  71. var ID = ctx.Params(":id")
  72. job, err := models.GetCloudbrainByID(ID)
  73. if err != nil {
  74. log.Error("GetCloudbrainByID failed:%v", err.Error())
  75. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  76. }
  77. ctx.Cloudbrain = job
  78. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  79. log.Error("!isAdminOrOwnerOrJobCreater error:%v", err.Error())
  80. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  81. }
  82. }
  83. func AdminOrJobCreaterRight(ctx *context.Context) {
  84. var ID = ctx.Params(":id")
  85. job, err := models.GetCloudbrainByID(ID)
  86. if err != nil {
  87. log.Error("GetCloudbrainByID failed:%v", err.Error())
  88. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  89. }
  90. ctx.Cloudbrain = job
  91. if !isAdminOrJobCreater(ctx, job, err) {
  92. log.Error("!isAdminOrJobCreater error:%v", err.Error())
  93. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  94. }
  95. }
  96. func AdminOrOwnerOrJobCreaterRightForTrain(ctx *context.Context) {
  97. var jobID = ctx.Params(":jobid")
  98. job, err := models.GetCloudbrainByJobID(jobID)
  99. if err != nil {
  100. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  101. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  102. }
  103. ctx.Cloudbrain = job
  104. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  105. log.Error("!isAdminOrOwnerOrJobCreater failed:%v", err.Error())
  106. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  107. }
  108. }
  109. func AdminOrJobCreaterRightForTrain(ctx *context.Context) {
  110. var jobID = ctx.Params(":jobid")
  111. job, err := models.GetCloudbrainByJobID(jobID)
  112. if err != nil {
  113. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  114. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  115. }
  116. ctx.Cloudbrain = job
  117. if !isAdminOrJobCreater(ctx, job, err) {
  118. log.Error("!isAdminOrJobCreater errot:%v", err.Error())
  119. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  120. }
  121. }
  122. 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 {
  123. dataActualPath := setting.Attachment.Minio.RealPath +
  124. setting.Attachment.Minio.Bucket + "/" +
  125. setting.Attachment.Minio.BasePath +
  126. models.AttachmentRelativePath(uuid) +
  127. uuid
  128. var resourceSpec *models.ResourceSpec
  129. var versionCount int
  130. if jobType == string(models.JobTypeTrain) {
  131. versionCount = 1
  132. if TrainResourceSpecs == nil {
  133. json.Unmarshal([]byte(setting.TrainResourceSpecs), &TrainResourceSpecs)
  134. }
  135. for _, spec := range TrainResourceSpecs.ResourceSpec {
  136. if resourceSpecId == spec.Id {
  137. resourceSpec = spec
  138. }
  139. }
  140. } else {
  141. if ResourceSpecs == nil {
  142. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  143. }
  144. for _, spec := range ResourceSpecs.ResourceSpec {
  145. if resourceSpecId == spec.Id {
  146. resourceSpec = spec
  147. }
  148. }
  149. }
  150. if resourceSpec == nil {
  151. log.Error("no such resourceSpecId(%d)", resourceSpecId, ctx.Data["MsgID"])
  152. return errors.New("no such resourceSpec")
  153. }
  154. var datasetName string
  155. attach, err := models.GetAttachmentByUUID(uuid)
  156. if err != nil {
  157. //for benchmark, do not return error
  158. log.Error("GetAttachmentByUUID failed:%v", err)
  159. } else {
  160. datasetName = attach.Name
  161. }
  162. createTime := timeutil.TimeStampNow()
  163. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  164. JobName: jobName,
  165. RetryCount: 1,
  166. GpuType: gpuQueue,
  167. Image: image,
  168. TaskRoles: []models.TaskRole{
  169. {
  170. Name: SubTaskName,
  171. TaskNumber: 1,
  172. MinSucceededTaskCount: 1,
  173. MinFailedTaskCount: 1,
  174. CPUNumber: resourceSpec.CpuNum,
  175. GPUNumber: resourceSpec.GpuNum,
  176. MemoryMB: resourceSpec.MemMiB,
  177. ShmMB: resourceSpec.ShareMemMiB,
  178. Command: command,
  179. NeedIBDevice: false,
  180. IsMainRole: false,
  181. UseNNI: false,
  182. },
  183. },
  184. Volumes: []models.Volume{
  185. {
  186. HostPath: models.StHostPath{
  187. Path: codePath,
  188. MountPath: CodeMountPath,
  189. ReadOnly: false,
  190. },
  191. },
  192. {
  193. HostPath: models.StHostPath{
  194. Path: dataActualPath,
  195. MountPath: DataSetMountPath,
  196. ReadOnly: true,
  197. },
  198. },
  199. {
  200. HostPath: models.StHostPath{
  201. Path: modelPath,
  202. MountPath: ModelMountPath,
  203. ReadOnly: false,
  204. },
  205. },
  206. {
  207. HostPath: models.StHostPath{
  208. Path: benchmarkPath,
  209. MountPath: BenchMarkMountPath,
  210. ReadOnly: true,
  211. },
  212. },
  213. {
  214. HostPath: models.StHostPath{
  215. Path: snn4imagenetPath,
  216. MountPath: Snn4imagenetMountPath,
  217. ReadOnly: true,
  218. },
  219. },
  220. {
  221. HostPath: models.StHostPath{
  222. Path: brainScorePath,
  223. MountPath: BrainScoreMountPath,
  224. ReadOnly: true,
  225. },
  226. },
  227. },
  228. })
  229. if err != nil {
  230. log.Error("CreateJob failed:", err.Error(), ctx.Data["MsgID"])
  231. return err
  232. }
  233. if jobResult.Code != Success {
  234. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  235. return errors.New(jobResult.Msg)
  236. }
  237. var jobID = jobResult.Payload["jobId"].(string)
  238. err = models.CreateCloudbrain(&models.Cloudbrain{
  239. Status: string(models.JobWaiting),
  240. UserID: ctx.User.ID,
  241. RepoID: ctx.Repo.Repository.ID,
  242. JobID: jobID,
  243. JobName: jobName,
  244. DisplayJobName: displayJobName,
  245. SubTaskName: SubTaskName,
  246. JobType: jobType,
  247. Type: models.TypeCloudBrainOne,
  248. Uuid: uuid,
  249. Image: image,
  250. GpuQueue: gpuQueue,
  251. ResourceSpecId: resourceSpecId,
  252. ComputeResource: models.GPUResource,
  253. BenchmarkTypeID: benchmarkTypeID,
  254. BenchmarkChildTypeID: benchmarkChildTypeID,
  255. Description: description,
  256. IsLatestVersion: "1",
  257. VersionCount: versionCount,
  258. BranchName: branchName,
  259. BootFile: bootFile,
  260. DatasetName: datasetName,
  261. Parameters: params,
  262. CreatedUnix: createTime,
  263. UpdatedUnix: createTime,
  264. })
  265. if err != nil {
  266. return err
  267. }
  268. task, err := models.GetCloudbrainByJobID(jobID)
  269. if err != nil {
  270. log.Error("GetCloudbrainByName failed: %v", err.Error())
  271. return err
  272. }
  273. stringId := strconv.FormatInt(task.ID, 10)
  274. if string(models.JobTypeBenchmark) == jobType {
  275. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateBenchMarkTask)
  276. } else if string(models.JobTypeTrain) == jobType {
  277. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, jobID, displayJobName, models.ActionCreateGPUTrainTask)
  278. } else {
  279. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateDebugGPUTask)
  280. }
  281. return nil
  282. }
  283. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newID *string) error {
  284. dataActualPath := setting.Attachment.Minio.RealPath +
  285. setting.Attachment.Minio.Bucket + "/" +
  286. setting.Attachment.Minio.BasePath +
  287. models.AttachmentRelativePath(task.Uuid) +
  288. task.Uuid
  289. jobName := task.JobName
  290. var resourceSpec *models.ResourceSpec
  291. if ResourceSpecs == nil {
  292. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  293. }
  294. for _, spec := range ResourceSpecs.ResourceSpec {
  295. if task.ResourceSpecId == spec.Id {
  296. resourceSpec = spec
  297. }
  298. }
  299. if resourceSpec == nil {
  300. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  301. return errors.New("no such resourceSpec")
  302. }
  303. createTime := timeutil.TimeStampNow()
  304. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  305. JobName: jobName,
  306. RetryCount: 1,
  307. GpuType: task.GpuQueue,
  308. Image: task.Image,
  309. TaskRoles: []models.TaskRole{
  310. {
  311. Name: SubTaskName,
  312. TaskNumber: 1,
  313. MinSucceededTaskCount: 1,
  314. MinFailedTaskCount: 1,
  315. CPUNumber: resourceSpec.CpuNum,
  316. GPUNumber: resourceSpec.GpuNum,
  317. MemoryMB: resourceSpec.MemMiB,
  318. ShmMB: resourceSpec.ShareMemMiB,
  319. Command: Command,
  320. NeedIBDevice: false,
  321. IsMainRole: false,
  322. UseNNI: false,
  323. },
  324. },
  325. Volumes: []models.Volume{
  326. {
  327. HostPath: models.StHostPath{
  328. Path: storage.GetMinioPath(jobName, CodeMountPath+"/"),
  329. MountPath: CodeMountPath,
  330. ReadOnly: false,
  331. },
  332. },
  333. {
  334. HostPath: models.StHostPath{
  335. Path: dataActualPath,
  336. MountPath: DataSetMountPath,
  337. ReadOnly: true,
  338. },
  339. },
  340. {
  341. HostPath: models.StHostPath{
  342. Path: storage.GetMinioPath(jobName, ModelMountPath+"/"),
  343. MountPath: ModelMountPath,
  344. ReadOnly: false,
  345. },
  346. },
  347. {
  348. HostPath: models.StHostPath{
  349. Path: storage.GetMinioPath(jobName, BenchMarkMountPath+"/"),
  350. MountPath: BenchMarkMountPath,
  351. ReadOnly: true,
  352. },
  353. },
  354. {
  355. HostPath: models.StHostPath{
  356. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath+"/"),
  357. MountPath: Snn4imagenetMountPath,
  358. ReadOnly: true,
  359. },
  360. },
  361. {
  362. HostPath: models.StHostPath{
  363. Path: storage.GetMinioPath(jobName, BrainScoreMountPath+"/"),
  364. MountPath: BrainScoreMountPath,
  365. ReadOnly: true,
  366. },
  367. },
  368. },
  369. })
  370. if err != nil {
  371. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  372. return err
  373. }
  374. if jobResult.Code != Success {
  375. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  376. return errors.New(jobResult.Msg)
  377. }
  378. var jobID = jobResult.Payload["jobId"].(string)
  379. newTask := &models.Cloudbrain{
  380. Status: string(models.JobWaiting),
  381. UserID: task.UserID,
  382. RepoID: task.RepoID,
  383. JobID: jobID,
  384. JobName: task.JobName,
  385. DisplayJobName: task.DisplayJobName,
  386. SubTaskName: task.SubTaskName,
  387. JobType: task.JobType,
  388. Type: task.Type,
  389. Uuid: task.Uuid,
  390. Image: task.Image,
  391. GpuQueue: task.GpuQueue,
  392. ResourceSpecId: task.ResourceSpecId,
  393. ComputeResource: task.ComputeResource,
  394. CreatedUnix: createTime,
  395. UpdatedUnix: createTime,
  396. }
  397. err = models.RestartCloudbrain(task, newTask)
  398. if err != nil {
  399. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  400. return err
  401. }
  402. stringId := strconv.FormatInt(newTask.ID, 10)
  403. *newID = stringId
  404. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, task.DisplayJobName, models.ActionCreateDebugGPUTask)
  405. return nil
  406. }