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

3 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
3 years ago
3 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
3 years ago
3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package cloudbrain
  2. import (
  3. "code.gitea.io/gitea/modules/storage"
  4. "encoding/json"
  5. "errors"
  6. "strconv"
  7. "code.gitea.io/gitea/modules/setting"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. const (
  13. 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"`
  14. CodeMountPath = "/code"
  15. DataSetMountPath = "/dataset"
  16. ModelMountPath = "/model"
  17. BenchMarkMountPath = "/benchmark"
  18. Snn4imagenetMountPath = "/snn4imagenet"
  19. BrainScoreMountPath = "/brainscore"
  20. TaskInfoName = "/taskInfo"
  21. SubTaskName = "task1"
  22. Success = "S000"
  23. )
  24. var (
  25. ResourceSpecs *models.ResourceSpecs
  26. )
  27. func isAdminOrOwnerOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  28. if !ctx.IsSigned {
  29. return false
  30. }
  31. log.Info("is repo owner:" + strconv.FormatBool(ctx.IsUserRepoOwner()))
  32. log.Info("is user admin:" + strconv.FormatBool(ctx.IsUserSiteAdmin()))
  33. if err != nil {
  34. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin()
  35. } else {
  36. log.Info("is job creator:" + strconv.FormatBool(ctx.User.ID == job.UserID))
  37. return ctx.IsUserRepoOwner() || ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  38. }
  39. }
  40. func CanDeleteJob(ctx *context.Context, job *models.Cloudbrain) bool {
  41. return isAdminOrOwnerOrJobCreater(ctx, job, nil)
  42. }
  43. func CanCreateOrDebugJob(ctx *context.Context) bool {
  44. if !ctx.IsSigned {
  45. return false
  46. }
  47. return ctx.Repo.CanWrite(models.UnitTypeCloudBrain)
  48. }
  49. func CanModifyJob(ctx *context.Context, job *models.Cloudbrain) bool {
  50. return isAdminOrJobCreater(ctx, job, nil)
  51. }
  52. func isAdminOrJobCreater(ctx *context.Context, job *models.Cloudbrain, err error) bool {
  53. if !ctx.IsSigned {
  54. return false
  55. }
  56. if err != nil {
  57. return ctx.IsUserSiteAdmin()
  58. } else {
  59. return ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  60. }
  61. }
  62. func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  63. var jobID = ctx.Params(":jobid")
  64. job, err := models.GetCloudbrainByJobID(jobID)
  65. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  66. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  67. }
  68. }
  69. func AdminOrJobCreaterRight(ctx *context.Context) {
  70. var jobID = ctx.Params(":jobid")
  71. job, err := models.GetCloudbrainByJobID(jobID)
  72. if !isAdminOrJobCreater(ctx, job, err) {
  73. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  74. }
  75. }
  76. func GenerateTask(ctx *context.Context, jobName, image, command, uuid, codePath, modelPath, benchmarkPath, snn4imagenetPath, brainScorePath, jobType, gpuQueue string, resourceSpecId int) error {
  77. dataActualPath := setting.Attachment.Minio.RealPath +
  78. setting.Attachment.Minio.Bucket + "/" +
  79. setting.Attachment.Minio.BasePath +
  80. models.AttachmentRelativePath(uuid) +
  81. uuid
  82. var resourceSpec *models.ResourceSpec
  83. if ResourceSpecs == nil {
  84. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  85. }
  86. for _, spec := range ResourceSpecs.ResourceSpec {
  87. if resourceSpecId == spec.Id {
  88. resourceSpec = spec
  89. }
  90. }
  91. if resourceSpec == nil {
  92. log.Error("no such resourceSpecId(%d)", resourceSpecId, ctx.Data["MsgID"])
  93. return errors.New("no such resourceSpec")
  94. }
  95. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  96. JobName: jobName,
  97. RetryCount: 1,
  98. GpuType: gpuQueue,
  99. Image: image,
  100. TaskRoles: []models.TaskRole{
  101. {
  102. Name: SubTaskName,
  103. TaskNumber: 1,
  104. MinSucceededTaskCount: 1,
  105. MinFailedTaskCount: 1,
  106. CPUNumber: resourceSpec.CpuNum,
  107. GPUNumber: resourceSpec.GpuNum,
  108. MemoryMB: resourceSpec.MemMiB,
  109. ShmMB: resourceSpec.ShareMemMiB,
  110. Command: command,
  111. NeedIBDevice: false,
  112. IsMainRole: false,
  113. UseNNI: false,
  114. },
  115. },
  116. Volumes: []models.Volume{
  117. {
  118. HostPath: models.StHostPath{
  119. Path: codePath,
  120. MountPath: CodeMountPath,
  121. ReadOnly: false,
  122. },
  123. },
  124. {
  125. HostPath: models.StHostPath{
  126. Path: dataActualPath,
  127. MountPath: DataSetMountPath,
  128. ReadOnly: true,
  129. },
  130. },
  131. {
  132. HostPath: models.StHostPath{
  133. Path: modelPath,
  134. MountPath: ModelMountPath,
  135. ReadOnly: false,
  136. },
  137. },
  138. {
  139. HostPath: models.StHostPath{
  140. Path: benchmarkPath,
  141. MountPath: BenchMarkMountPath,
  142. ReadOnly: true,
  143. },
  144. },
  145. {
  146. HostPath: models.StHostPath{
  147. Path: snn4imagenetPath,
  148. MountPath: Snn4imagenetMountPath,
  149. ReadOnly: true,
  150. },
  151. },
  152. {
  153. HostPath: models.StHostPath{
  154. Path: brainScorePath,
  155. MountPath: BrainScoreMountPath,
  156. ReadOnly: true,
  157. },
  158. },
  159. },
  160. })
  161. if err != nil {
  162. log.Error("CreateJob failed:", err.Error(), ctx.Data["MsgID"])
  163. return err
  164. }
  165. if jobResult.Code != Success {
  166. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  167. return errors.New(jobResult.Msg)
  168. }
  169. var jobID = jobResult.Payload["jobId"].(string)
  170. err = models.CreateCloudbrain(&models.Cloudbrain{
  171. Status: string(models.JobWaiting),
  172. UserID: ctx.User.ID,
  173. RepoID: ctx.Repo.Repository.ID,
  174. JobID: jobID,
  175. JobName: jobName,
  176. SubTaskName: SubTaskName,
  177. JobType: jobType,
  178. Type: models.TypeCloudBrainOne,
  179. Uuid: uuid,
  180. Image: image,
  181. GpuQueue: gpuQueue,
  182. ResourceSpecId: resourceSpecId,
  183. })
  184. if err != nil {
  185. return err
  186. }
  187. return nil
  188. }
  189. func RestartTask(ctx *context.Context, task *models.Cloudbrain) error {
  190. dataActualPath := setting.Attachment.Minio.RealPath +
  191. setting.Attachment.Minio.Bucket + "/" +
  192. setting.Attachment.Minio.BasePath +
  193. models.AttachmentRelativePath(task.Uuid) +
  194. task.Uuid
  195. jobName := task.JobName
  196. var resourceSpec *models.ResourceSpec
  197. if ResourceSpecs == nil {
  198. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  199. }
  200. for _, spec := range ResourceSpecs.ResourceSpec {
  201. if task.ResourceSpecId == spec.Id {
  202. resourceSpec = spec
  203. }
  204. }
  205. if resourceSpec == nil {
  206. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  207. return errors.New("no such resourceSpec")
  208. }
  209. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  210. JobName: jobName,
  211. RetryCount: 1,
  212. GpuType: task.GpuQueue,
  213. Image: task.Image,
  214. TaskRoles: []models.TaskRole{
  215. {
  216. Name: SubTaskName,
  217. TaskNumber: 1,
  218. MinSucceededTaskCount: 1,
  219. MinFailedTaskCount: 1,
  220. CPUNumber: resourceSpec.CpuNum,
  221. GPUNumber: resourceSpec.GpuNum,
  222. MemoryMB: resourceSpec.MemMiB,
  223. ShmMB: resourceSpec.ShareMemMiB,
  224. Command: Command,
  225. NeedIBDevice: false,
  226. IsMainRole: false,
  227. UseNNI: false,
  228. },
  229. },
  230. Volumes: []models.Volume{
  231. {
  232. HostPath: models.StHostPath{
  233. Path: storage.GetMinioPath(jobName, CodeMountPath + "/"),
  234. MountPath: CodeMountPath,
  235. ReadOnly: false,
  236. },
  237. },
  238. {
  239. HostPath: models.StHostPath{
  240. Path: dataActualPath,
  241. MountPath: DataSetMountPath,
  242. ReadOnly: true,
  243. },
  244. },
  245. {
  246. HostPath: models.StHostPath{
  247. Path: storage.GetMinioPath(jobName, ModelMountPath + "/"),
  248. MountPath: ModelMountPath,
  249. ReadOnly: false,
  250. },
  251. },
  252. {
  253. HostPath: models.StHostPath{
  254. Path: storage.GetMinioPath(jobName, BenchMarkMountPath + "/"),
  255. MountPath: BenchMarkMountPath,
  256. ReadOnly: true,
  257. },
  258. },
  259. {
  260. HostPath: models.StHostPath{
  261. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath + "/"),
  262. MountPath: Snn4imagenetMountPath,
  263. ReadOnly: true,
  264. },
  265. },
  266. {
  267. HostPath: models.StHostPath{
  268. Path: storage.GetMinioPath(jobName, BrainScoreMountPath + "/"),
  269. MountPath: BrainScoreMountPath,
  270. ReadOnly: true,
  271. },
  272. },
  273. },
  274. })
  275. if err != nil {
  276. log.Error("CreateJob failed:", err.Error(), ctx.Data["MsgID"])
  277. return err
  278. }
  279. if jobResult.Code != Success {
  280. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  281. return errors.New(jobResult.Msg)
  282. }
  283. var jobID = jobResult.Payload["jobId"].(string)
  284. task.JobID = jobID
  285. task.Status = string(models.JobWaiting)
  286. err = models.UpdateJob(task)
  287. if err != nil {
  288. log.Error("UpdateJob(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  289. return err
  290. }
  291. return nil
  292. }