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