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 10 kB

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