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 12 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
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
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
4 years ago
5 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
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 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
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 isAdminOrImageCreater(ctx *context.Context, image *models.Image, err error) bool {
  69. if !ctx.IsSigned {
  70. return false
  71. }
  72. if err != nil {
  73. return ctx.IsUserSiteAdmin()
  74. } else {
  75. return ctx.IsUserSiteAdmin() || ctx.User.ID == image.UID
  76. }
  77. }
  78. func AdminOrOwnerOrJobCreaterRight(ctx *context.Context) {
  79. var ID = ctx.Params(":id")
  80. job, err := models.GetCloudbrainByID(ID)
  81. if err != nil {
  82. log.Error("GetCloudbrainByID failed:%v", err.Error())
  83. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  84. }
  85. ctx.Cloudbrain = job
  86. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  87. log.Error("!isAdminOrOwnerOrJobCreater error:%v", err.Error())
  88. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  89. }
  90. }
  91. func AdminOrJobCreaterRight(ctx *context.Context) {
  92. var ID = ctx.Params(":id")
  93. job, err := models.GetCloudbrainByID(ID)
  94. if err != nil {
  95. log.Error("GetCloudbrainByID failed:%v", err.Error())
  96. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  97. }
  98. ctx.Cloudbrain = job
  99. if !isAdminOrJobCreater(ctx, job, err) {
  100. log.Error("!isAdminOrJobCreater error:%v", err.Error())
  101. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  102. }
  103. }
  104. func AdminOrOwnerOrJobCreaterRightForTrain(ctx *context.Context) {
  105. var jobID = ctx.Params(":jobid")
  106. job, err := models.GetCloudbrainByJobID(jobID)
  107. if err != nil {
  108. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  109. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  110. }
  111. ctx.Cloudbrain = job
  112. if !isAdminOrOwnerOrJobCreater(ctx, job, err) {
  113. log.Error("!isAdminOrOwnerOrJobCreater failed:%v", err.Error())
  114. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  115. }
  116. }
  117. func AdminOrJobCreaterRightForTrain(ctx *context.Context) {
  118. var jobID = ctx.Params(":jobid")
  119. job, err := models.GetCloudbrainByJobID(jobID)
  120. if err != nil {
  121. log.Error("GetCloudbrainByJobID failed:%v", err.Error())
  122. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  123. }
  124. ctx.Cloudbrain = job
  125. if !isAdminOrJobCreater(ctx, job, err) {
  126. log.Error("!isAdminOrJobCreater errot:%v", err.Error())
  127. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  128. }
  129. }
  130. func AdminOrImageCreaterRight(ctx *context.Context) {
  131. id, err := strconv.ParseInt(ctx.Params(":id"), 10, 64)
  132. var image *models.Image
  133. if err != nil {
  134. log.Error("Get Image by ID failed:%v", err.Error())
  135. } else {
  136. image, err = models.GetImageByID(id)
  137. if err != nil {
  138. log.Error("Get Image by ID failed:%v", err.Error())
  139. return
  140. }
  141. }
  142. if !isAdminOrImageCreater(ctx, image, err) {
  143. log.Error("!isAdminOrImageCreater error:%v", err.Error())
  144. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  145. }
  146. }
  147. func GenerateTask(ctx *context.Context, displayJobName, jobName, image, command, uuid, codePath, modelPath, benchmarkPath, snn4imagenetPath, brainScorePath, jobType, gpuQueue, description string, benchmarkTypeID, benchmarkChildTypeID, resourceSpecId int) error {
  148. dataActualPath := setting.Attachment.Minio.RealPath +
  149. setting.Attachment.Minio.Bucket + "/" +
  150. setting.Attachment.Minio.BasePath +
  151. models.AttachmentRelativePath(uuid) +
  152. uuid
  153. var resourceSpec *models.ResourceSpec
  154. if ResourceSpecs == nil {
  155. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  156. }
  157. for _, spec := range ResourceSpecs.ResourceSpec {
  158. if resourceSpecId == spec.Id {
  159. resourceSpec = spec
  160. }
  161. }
  162. if resourceSpec == nil {
  163. log.Error("no such resourceSpecId(%d)", resourceSpecId, ctx.Data["MsgID"])
  164. return errors.New("no such resourceSpec")
  165. }
  166. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  167. JobName: jobName,
  168. RetryCount: 1,
  169. GpuType: gpuQueue,
  170. Image: image,
  171. TaskRoles: []models.TaskRole{
  172. {
  173. Name: SubTaskName,
  174. TaskNumber: 1,
  175. MinSucceededTaskCount: 1,
  176. MinFailedTaskCount: 1,
  177. CPUNumber: resourceSpec.CpuNum,
  178. GPUNumber: resourceSpec.GpuNum,
  179. MemoryMB: resourceSpec.MemMiB,
  180. ShmMB: resourceSpec.ShareMemMiB,
  181. Command: command,
  182. NeedIBDevice: false,
  183. IsMainRole: false,
  184. UseNNI: false,
  185. },
  186. },
  187. Volumes: []models.Volume{
  188. {
  189. HostPath: models.StHostPath{
  190. Path: codePath,
  191. MountPath: CodeMountPath,
  192. ReadOnly: false,
  193. },
  194. },
  195. {
  196. HostPath: models.StHostPath{
  197. Path: dataActualPath,
  198. MountPath: DataSetMountPath,
  199. ReadOnly: true,
  200. },
  201. },
  202. {
  203. HostPath: models.StHostPath{
  204. Path: modelPath,
  205. MountPath: ModelMountPath,
  206. ReadOnly: false,
  207. },
  208. },
  209. {
  210. HostPath: models.StHostPath{
  211. Path: benchmarkPath,
  212. MountPath: BenchMarkMountPath,
  213. ReadOnly: true,
  214. },
  215. },
  216. {
  217. HostPath: models.StHostPath{
  218. Path: snn4imagenetPath,
  219. MountPath: Snn4imagenetMountPath,
  220. ReadOnly: true,
  221. },
  222. },
  223. {
  224. HostPath: models.StHostPath{
  225. Path: brainScorePath,
  226. MountPath: BrainScoreMountPath,
  227. ReadOnly: true,
  228. },
  229. },
  230. },
  231. })
  232. if err != nil {
  233. log.Error("CreateJob failed:", err.Error(), ctx.Data["MsgID"])
  234. return err
  235. }
  236. if jobResult.Code != Success {
  237. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  238. return errors.New(jobResult.Msg)
  239. }
  240. var jobID = jobResult.Payload["jobId"].(string)
  241. err = models.CreateCloudbrain(&models.Cloudbrain{
  242. Status: string(models.JobWaiting),
  243. UserID: ctx.User.ID,
  244. RepoID: ctx.Repo.Repository.ID,
  245. JobID: jobID,
  246. JobName: jobName,
  247. DisplayJobName: displayJobName,
  248. SubTaskName: SubTaskName,
  249. JobType: jobType,
  250. Type: models.TypeCloudBrainOne,
  251. Uuid: uuid,
  252. Image: image,
  253. GpuQueue: gpuQueue,
  254. ResourceSpecId: resourceSpecId,
  255. ComputeResource: models.GPUResource,
  256. BenchmarkTypeID: benchmarkTypeID,
  257. BenchmarkChildTypeID: benchmarkChildTypeID,
  258. Description: description,
  259. })
  260. if err != nil {
  261. return err
  262. }
  263. task, err := models.GetCloudbrainByName(jobName)
  264. if err != nil {
  265. log.Error("GetCloudbrainByName failed: %v", err.Error())
  266. return err
  267. }
  268. stringId := strconv.FormatInt(task.ID, 10)
  269. if string(models.JobTypeBenchmark) == jobType {
  270. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateBenchMarkTask)
  271. } else {
  272. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateDebugGPUTask)
  273. }
  274. return nil
  275. }
  276. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newID *string) error {
  277. dataActualPath := setting.Attachment.Minio.RealPath +
  278. setting.Attachment.Minio.Bucket + "/" +
  279. setting.Attachment.Minio.BasePath +
  280. models.AttachmentRelativePath(task.Uuid) +
  281. task.Uuid
  282. jobName := task.JobName
  283. var resourceSpec *models.ResourceSpec
  284. if ResourceSpecs == nil {
  285. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  286. }
  287. for _, spec := range ResourceSpecs.ResourceSpec {
  288. if task.ResourceSpecId == spec.Id {
  289. resourceSpec = spec
  290. }
  291. }
  292. if resourceSpec == nil {
  293. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  294. return errors.New("no such resourceSpec")
  295. }
  296. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  297. JobName: jobName,
  298. RetryCount: 1,
  299. GpuType: task.GpuQueue,
  300. Image: task.Image,
  301. TaskRoles: []models.TaskRole{
  302. {
  303. Name: SubTaskName,
  304. TaskNumber: 1,
  305. MinSucceededTaskCount: 1,
  306. MinFailedTaskCount: 1,
  307. CPUNumber: resourceSpec.CpuNum,
  308. GPUNumber: resourceSpec.GpuNum,
  309. MemoryMB: resourceSpec.MemMiB,
  310. ShmMB: resourceSpec.ShareMemMiB,
  311. Command: Command,
  312. NeedIBDevice: false,
  313. IsMainRole: false,
  314. UseNNI: false,
  315. },
  316. },
  317. Volumes: []models.Volume{
  318. {
  319. HostPath: models.StHostPath{
  320. Path: storage.GetMinioPath(jobName, CodeMountPath+"/"),
  321. MountPath: CodeMountPath,
  322. ReadOnly: false,
  323. },
  324. },
  325. {
  326. HostPath: models.StHostPath{
  327. Path: dataActualPath,
  328. MountPath: DataSetMountPath,
  329. ReadOnly: true,
  330. },
  331. },
  332. {
  333. HostPath: models.StHostPath{
  334. Path: storage.GetMinioPath(jobName, ModelMountPath+"/"),
  335. MountPath: ModelMountPath,
  336. ReadOnly: false,
  337. },
  338. },
  339. {
  340. HostPath: models.StHostPath{
  341. Path: storage.GetMinioPath(jobName, BenchMarkMountPath+"/"),
  342. MountPath: BenchMarkMountPath,
  343. ReadOnly: true,
  344. },
  345. },
  346. {
  347. HostPath: models.StHostPath{
  348. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath+"/"),
  349. MountPath: Snn4imagenetMountPath,
  350. ReadOnly: true,
  351. },
  352. },
  353. {
  354. HostPath: models.StHostPath{
  355. Path: storage.GetMinioPath(jobName, BrainScoreMountPath+"/"),
  356. MountPath: BrainScoreMountPath,
  357. ReadOnly: true,
  358. },
  359. },
  360. },
  361. })
  362. if err != nil {
  363. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  364. return err
  365. }
  366. if jobResult.Code != Success {
  367. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  368. return errors.New(jobResult.Msg)
  369. }
  370. var jobID = jobResult.Payload["jobId"].(string)
  371. newTask := &models.Cloudbrain{
  372. Status: string(models.JobWaiting),
  373. UserID: task.UserID,
  374. RepoID: task.RepoID,
  375. JobID: jobID,
  376. JobName: task.JobName,
  377. DisplayJobName: task.DisplayJobName,
  378. SubTaskName: task.SubTaskName,
  379. JobType: task.JobType,
  380. Type: task.Type,
  381. Uuid: task.Uuid,
  382. Image: task.Image,
  383. GpuQueue: task.GpuQueue,
  384. ResourceSpecId: task.ResourceSpecId,
  385. ComputeResource: task.ComputeResource,
  386. }
  387. err = models.RestartCloudbrain(task, newTask)
  388. if err != nil {
  389. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  390. return err
  391. }
  392. idString := strconv.FormatInt(newTask.ID, 10)
  393. *newID = idString
  394. return nil
  395. }