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

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