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

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