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

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