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

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