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