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 9.2 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
3 years ago
3 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. ctx.Cloudbrain = job
  66. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  67. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  68. }
  69. }
  70. func AdminOrJobCreaterRight(ctx *context.Context) {
  71. var jobID = ctx.Params(":jobid")
  72. job, err := models.GetCloudbrainByJobID(jobID)
  73. ctx.Cloudbrain = job
  74. if !isAdminOrJobCreater(ctx, job, err) {
  75. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  76. }
  77. }
  78. func GenerateTask(ctx *context.Context, jobName, image, command, uuid, codePath, modelPath, benchmarkPath, snn4imagenetPath, brainScorePath, jobType, gpuQueue string, resourceSpecId int) error {
  79. dataActualPath := setting.Attachment.Minio.RealPath +
  80. setting.Attachment.Minio.Bucket + "/" +
  81. setting.Attachment.Minio.BasePath +
  82. models.AttachmentRelativePath(uuid) +
  83. uuid
  84. var resourceSpec *models.ResourceSpec
  85. if ResourceSpecs == nil {
  86. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  87. }
  88. for _, spec := range ResourceSpecs.ResourceSpec {
  89. if resourceSpecId == spec.Id {
  90. resourceSpec = spec
  91. }
  92. }
  93. if resourceSpec == nil {
  94. log.Error("no such resourceSpecId(%d)", resourceSpecId, ctx.Data["MsgID"])
  95. return errors.New("no such resourceSpec")
  96. }
  97. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  98. JobName: jobName,
  99. RetryCount: 1,
  100. GpuType: gpuQueue,
  101. Image: image,
  102. TaskRoles: []models.TaskRole{
  103. {
  104. Name: SubTaskName,
  105. TaskNumber: 1,
  106. MinSucceededTaskCount: 1,
  107. MinFailedTaskCount: 1,
  108. CPUNumber: resourceSpec.CpuNum,
  109. GPUNumber: resourceSpec.GpuNum,
  110. MemoryMB: resourceSpec.MemMiB,
  111. ShmMB: resourceSpec.ShareMemMiB,
  112. Command: command,
  113. NeedIBDevice: false,
  114. IsMainRole: false,
  115. UseNNI: false,
  116. },
  117. },
  118. Volumes: []models.Volume{
  119. {
  120. HostPath: models.StHostPath{
  121. Path: codePath,
  122. MountPath: CodeMountPath,
  123. ReadOnly: false,
  124. },
  125. },
  126. {
  127. HostPath: models.StHostPath{
  128. Path: dataActualPath,
  129. MountPath: DataSetMountPath,
  130. ReadOnly: true,
  131. },
  132. },
  133. {
  134. HostPath: models.StHostPath{
  135. Path: modelPath,
  136. MountPath: ModelMountPath,
  137. ReadOnly: false,
  138. },
  139. },
  140. {
  141. HostPath: models.StHostPath{
  142. Path: benchmarkPath,
  143. MountPath: BenchMarkMountPath,
  144. ReadOnly: true,
  145. },
  146. },
  147. {
  148. HostPath: models.StHostPath{
  149. Path: snn4imagenetPath,
  150. MountPath: Snn4imagenetMountPath,
  151. ReadOnly: true,
  152. },
  153. },
  154. {
  155. HostPath: models.StHostPath{
  156. Path: brainScorePath,
  157. MountPath: BrainScoreMountPath,
  158. ReadOnly: true,
  159. },
  160. },
  161. },
  162. })
  163. if err != nil {
  164. log.Error("CreateJob failed:", err.Error(), ctx.Data["MsgID"])
  165. return err
  166. }
  167. if jobResult.Code != Success {
  168. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  169. return errors.New(jobResult.Msg)
  170. }
  171. var jobID = jobResult.Payload["jobId"].(string)
  172. err = models.CreateCloudbrain(&models.Cloudbrain{
  173. Status: string(models.JobWaiting),
  174. UserID: ctx.User.ID,
  175. RepoID: ctx.Repo.Repository.ID,
  176. JobID: jobID,
  177. JobName: jobName,
  178. SubTaskName: SubTaskName,
  179. JobType: jobType,
  180. Type: models.TypeCloudBrainOne,
  181. Uuid: uuid,
  182. Image: image,
  183. GpuQueue: gpuQueue,
  184. ResourceSpecId: resourceSpecId,
  185. ComputeResource: models.GPUResource,
  186. })
  187. if err != nil {
  188. return err
  189. }
  190. return nil
  191. }
  192. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newJobID *string) error {
  193. dataActualPath := setting.Attachment.Minio.RealPath +
  194. setting.Attachment.Minio.Bucket + "/" +
  195. setting.Attachment.Minio.BasePath +
  196. models.AttachmentRelativePath(task.Uuid) +
  197. task.Uuid
  198. jobName := task.JobName
  199. var resourceSpec *models.ResourceSpec
  200. if ResourceSpecs == nil {
  201. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  202. }
  203. for _, spec := range ResourceSpecs.ResourceSpec {
  204. if task.ResourceSpecId == spec.Id {
  205. resourceSpec = spec
  206. }
  207. }
  208. if resourceSpec == nil {
  209. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  210. return errors.New("no such resourceSpec")
  211. }
  212. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  213. JobName: jobName,
  214. RetryCount: 1,
  215. GpuType: task.GpuQueue,
  216. Image: task.Image,
  217. TaskRoles: []models.TaskRole{
  218. {
  219. Name: SubTaskName,
  220. TaskNumber: 1,
  221. MinSucceededTaskCount: 1,
  222. MinFailedTaskCount: 1,
  223. CPUNumber: resourceSpec.CpuNum,
  224. GPUNumber: resourceSpec.GpuNum,
  225. MemoryMB: resourceSpec.MemMiB,
  226. ShmMB: resourceSpec.ShareMemMiB,
  227. Command: Command,
  228. NeedIBDevice: false,
  229. IsMainRole: false,
  230. UseNNI: false,
  231. },
  232. },
  233. Volumes: []models.Volume{
  234. {
  235. HostPath: models.StHostPath{
  236. Path: storage.GetMinioPath(jobName, CodeMountPath + "/"),
  237. MountPath: CodeMountPath,
  238. ReadOnly: false,
  239. },
  240. },
  241. {
  242. HostPath: models.StHostPath{
  243. Path: dataActualPath,
  244. MountPath: DataSetMountPath,
  245. ReadOnly: true,
  246. },
  247. },
  248. {
  249. HostPath: models.StHostPath{
  250. Path: storage.GetMinioPath(jobName, ModelMountPath + "/"),
  251. MountPath: ModelMountPath,
  252. ReadOnly: false,
  253. },
  254. },
  255. {
  256. HostPath: models.StHostPath{
  257. Path: storage.GetMinioPath(jobName, BenchMarkMountPath + "/"),
  258. MountPath: BenchMarkMountPath,
  259. ReadOnly: true,
  260. },
  261. },
  262. {
  263. HostPath: models.StHostPath{
  264. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath + "/"),
  265. MountPath: Snn4imagenetMountPath,
  266. ReadOnly: true,
  267. },
  268. },
  269. {
  270. HostPath: models.StHostPath{
  271. Path: storage.GetMinioPath(jobName, BrainScoreMountPath + "/"),
  272. MountPath: BrainScoreMountPath,
  273. ReadOnly: true,
  274. },
  275. },
  276. },
  277. })
  278. if err != nil {
  279. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  280. return err
  281. }
  282. if jobResult.Code != Success {
  283. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  284. return errors.New(jobResult.Msg)
  285. }
  286. var jobID = jobResult.Payload["jobId"].(string)
  287. newTask := &models.Cloudbrain{
  288. Status: string(models.JobWaiting),
  289. UserID: task.UserID,
  290. RepoID: task.RepoID,
  291. JobID: jobID,
  292. JobName: task.JobName,
  293. SubTaskName: task.SubTaskName,
  294. JobType: task.JobType,
  295. Type: task.Type,
  296. Uuid: task.Uuid,
  297. Image: task.Image,
  298. GpuQueue: task.GpuQueue,
  299. ResourceSpecId: task.ResourceSpecId,
  300. ComputeResource: task.ComputeResource,
  301. }
  302. err = models.RestartCloudbrain(task, newTask)
  303. if err != nil {
  304. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  305. return err
  306. }
  307. *newJobID = jobID
  308. return nil
  309. }