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

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