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
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 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
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. ComputeResource: models.GPUResource,
  184. })
  185. if err != nil {
  186. return err
  187. }
  188. return nil
  189. }
  190. func RestartTask(ctx *context.Context, task *models.Cloudbrain) error {
  191. dataActualPath := setting.Attachment.Minio.RealPath +
  192. setting.Attachment.Minio.Bucket + "/" +
  193. setting.Attachment.Minio.BasePath +
  194. models.AttachmentRelativePath(task.Uuid) +
  195. task.Uuid
  196. jobName := task.JobName
  197. var resourceSpec *models.ResourceSpec
  198. if ResourceSpecs == nil {
  199. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  200. }
  201. for _, spec := range ResourceSpecs.ResourceSpec {
  202. if task.ResourceSpecId == spec.Id {
  203. resourceSpec = spec
  204. }
  205. }
  206. if resourceSpec == nil {
  207. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  208. return errors.New("no such resourceSpec")
  209. }
  210. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  211. JobName: jobName,
  212. RetryCount: 1,
  213. GpuType: task.GpuQueue,
  214. Image: task.Image,
  215. TaskRoles: []models.TaskRole{
  216. {
  217. Name: SubTaskName,
  218. TaskNumber: 1,
  219. MinSucceededTaskCount: 1,
  220. MinFailedTaskCount: 1,
  221. CPUNumber: resourceSpec.CpuNum,
  222. GPUNumber: resourceSpec.GpuNum,
  223. MemoryMB: resourceSpec.MemMiB,
  224. ShmMB: resourceSpec.ShareMemMiB,
  225. Command: Command,
  226. NeedIBDevice: false,
  227. IsMainRole: false,
  228. UseNNI: false,
  229. },
  230. },
  231. Volumes: []models.Volume{
  232. {
  233. HostPath: models.StHostPath{
  234. Path: storage.GetMinioPath(jobName, CodeMountPath + "/"),
  235. MountPath: CodeMountPath,
  236. ReadOnly: false,
  237. },
  238. },
  239. {
  240. HostPath: models.StHostPath{
  241. Path: dataActualPath,
  242. MountPath: DataSetMountPath,
  243. ReadOnly: true,
  244. },
  245. },
  246. {
  247. HostPath: models.StHostPath{
  248. Path: storage.GetMinioPath(jobName, ModelMountPath + "/"),
  249. MountPath: ModelMountPath,
  250. ReadOnly: false,
  251. },
  252. },
  253. {
  254. HostPath: models.StHostPath{
  255. Path: storage.GetMinioPath(jobName, BenchMarkMountPath + "/"),
  256. MountPath: BenchMarkMountPath,
  257. ReadOnly: true,
  258. },
  259. },
  260. {
  261. HostPath: models.StHostPath{
  262. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath + "/"),
  263. MountPath: Snn4imagenetMountPath,
  264. ReadOnly: true,
  265. },
  266. },
  267. {
  268. HostPath: models.StHostPath{
  269. Path: storage.GetMinioPath(jobName, BrainScoreMountPath + "/"),
  270. MountPath: BrainScoreMountPath,
  271. ReadOnly: true,
  272. },
  273. },
  274. },
  275. })
  276. if err != nil {
  277. log.Error("CreateJob failed:", err.Error(), ctx.Data["MsgID"])
  278. return err
  279. }
  280. if jobResult.Code != Success {
  281. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  282. return errors.New(jobResult.Msg)
  283. }
  284. var jobID = jobResult.Payload["jobId"].(string)
  285. task.JobID = jobID
  286. task.Status = string(models.JobWaiting)
  287. err = models.UpdateJob(task)
  288. if err != nil {
  289. log.Error("UpdateJob(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  290. return err
  291. }
  292. return nil
  293. }