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

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