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

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