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.5 kB

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