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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package cloudbrain
  2. import (
  3. "errors"
  4. "code.gitea.io/gitea/modules/setting"
  5. "code.gitea.io/gitea/models"
  6. "code.gitea.io/gitea/modules/context"
  7. "code.gitea.io/gitea/modules/log"
  8. )
  9. const (
  10. 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"`
  11. CodeMountPath = "/code"
  12. DataSetMountPath = "/dataset"
  13. ModelMountPath = "/model"
  14. BenchMarkMountPath = "/benchmark"
  15. Snn4imagenetMountPath = "/snn4imagenet"
  16. BrainScoreMountPath = "/brainscore"
  17. TaskInfoName = "/taskInfo"
  18. SubTaskName = "task1"
  19. Success = "S000"
  20. )
  21. var (
  22. ResourceSpecs *models.ResourceSpecs
  23. )
  24. func isAdminOrOwnerOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  25. if err != nil {
  26. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin()
  27. } else {
  28. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  29. }
  30. }
  31. func CanDeleteDebugJob(ctx *context.Context, job *models.Cloudbrain) bool {
  32. if job.Status != string(models.JobStopped) && job.Status != string(models.JobFailed) && job.Status != string(models.ModelArtsStartFailed) && job.Status != string(models.ModelArtsCreateFailed) {
  33. return false
  34. }
  35. return isAdminOrOwnerOrJobCreater(ctx, job, nil)
  36. }
  37. func CanDeleteTrainJob(ctx *context.Context, job *models.Cloudbrain) bool {
  38. return isAdminOrOwnerOrJobCreater(ctx, job, nil)
  39. }
  40. func CanCreateOrDebugJob(ctx *context.Context) bool {
  41. return ctx.Repo.CanWrite(models.UnitTypeCloudBrain)
  42. }
  43. func CanModifyJob(ctx *context.Context, job *models.Cloudbrain) bool {
  44. return isAdminOrJobCreater(ctx, job, nil)
  45. }
  46. func isAdminOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  47. if err != nil {
  48. return ctx.IsUserSiteAdmin()
  49. } else {
  50. return ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  51. }
  52. }
  53. func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  54. var jobID = ctx.Params(":jobid")
  55. job, err := models.GetCloudbrainByJobID(jobID)
  56. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  57. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  58. }
  59. }
  60. func AdminOrJobCreaterRight(ctx *context.Context) {
  61. var jobID = ctx.Params(":jobid")
  62. job, err := models.GetCloudbrainByJobID(jobID)
  63. if !isAdminOrJobCreater(ctx, job, err) {
  64. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  65. }
  66. }
  67. func GenerateTask(ctx *context.Context, jobName, image, command, uuid, codePath, modelPath, benchmarkPath, snn4imagenetPath, brainScorePath, jobType, gpuQueue string, resourceSpecId int) error {
  68. dataActualPath := setting.Attachment.Minio.RealPath +
  69. setting.Attachment.Minio.Bucket + "/" +
  70. setting.Attachment.Minio.BasePath +
  71. models.AttachmentRelativePath(uuid) +
  72. uuid
  73. var resourceSpec *models.ResourceSpec
  74. for _, spec := range ResourceSpecs.ResourceSpec {
  75. if resourceSpecId == spec.Id {
  76. resourceSpec = spec
  77. }
  78. }
  79. if resourceSpec == nil {
  80. log.Error("no such resourceSpecId(%d)", resourceSpecId, ctx.Data["MsgID"])
  81. return errors.New("no such resourceSpec")
  82. }
  83. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  84. JobName: jobName,
  85. RetryCount: 1,
  86. GpuType: gpuQueue,
  87. Image: image,
  88. TaskRoles: []models.TaskRole{
  89. {
  90. Name: SubTaskName,
  91. TaskNumber: 1,
  92. MinSucceededTaskCount: 1,
  93. MinFailedTaskCount: 1,
  94. CPUNumber: resourceSpec.CpuNum,
  95. GPUNumber: resourceSpec.GpuNum,
  96. MemoryMB: resourceSpec.MemMiB,
  97. ShmMB: resourceSpec.ShareMemMiB,
  98. Command: command,
  99. NeedIBDevice: false,
  100. IsMainRole: false,
  101. UseNNI: false,
  102. },
  103. },
  104. Volumes: []models.Volume{
  105. {
  106. HostPath: models.StHostPath{
  107. Path: codePath,
  108. MountPath: CodeMountPath,
  109. ReadOnly: false,
  110. },
  111. },
  112. {
  113. HostPath: models.StHostPath{
  114. Path: dataActualPath,
  115. MountPath: DataSetMountPath,
  116. ReadOnly: true,
  117. },
  118. },
  119. {
  120. HostPath: models.StHostPath{
  121. Path: modelPath,
  122. MountPath: ModelMountPath,
  123. ReadOnly: false,
  124. },
  125. },
  126. {
  127. HostPath: models.StHostPath{
  128. Path: benchmarkPath,
  129. MountPath: BenchMarkMountPath,
  130. ReadOnly: true,
  131. },
  132. },
  133. {
  134. HostPath: models.StHostPath{
  135. Path: snn4imagenetPath,
  136. MountPath: Snn4imagenetMountPath,
  137. ReadOnly: true,
  138. },
  139. },
  140. {
  141. HostPath: models.StHostPath{
  142. Path: brainScorePath,
  143. MountPath: BrainScoreMountPath,
  144. ReadOnly: true,
  145. },
  146. },
  147. },
  148. })
  149. if err != nil {
  150. log.Error("CreateJob failed:", err.Error())
  151. return err
  152. }
  153. if jobResult.Code != Success {
  154. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg)
  155. return errors.New(jobResult.Msg)
  156. }
  157. var jobID = jobResult.Payload["jobId"].(string)
  158. err = models.CreateCloudbrain(&models.Cloudbrain{
  159. Status: string(models.JobWaiting),
  160. UserID: ctx.User.ID,
  161. RepoID: ctx.Repo.Repository.ID,
  162. JobID: jobID,
  163. JobName: jobName,
  164. SubTaskName: SubTaskName,
  165. JobType: jobType,
  166. Type: models.TypeCloudBrainOne,
  167. Uuid: uuid,
  168. })
  169. if err != nil {
  170. return err
  171. }
  172. return nil
  173. }