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