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