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
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
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 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 ResourceSpecs == nil {
  128. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  129. }
  130. for _, spec := range ResourceSpecs.ResourceSpec {
  131. if resourceSpecId == spec.Id {
  132. resourceSpec = spec
  133. }
  134. }
  135. if resourceSpec == nil {
  136. log.Error("no such resourceSpecId(%d)", resourceSpecId, ctx.Data["MsgID"])
  137. return errors.New("no such resourceSpec")
  138. }
  139. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  140. JobName: jobName,
  141. RetryCount: 1,
  142. GpuType: gpuQueue,
  143. Image: image,
  144. TaskRoles: []models.TaskRole{
  145. {
  146. Name: SubTaskName,
  147. TaskNumber: 1,
  148. MinSucceededTaskCount: 1,
  149. MinFailedTaskCount: 1,
  150. CPUNumber: resourceSpec.CpuNum,
  151. GPUNumber: resourceSpec.GpuNum,
  152. MemoryMB: resourceSpec.MemMiB,
  153. ShmMB: resourceSpec.ShareMemMiB,
  154. Command: command,
  155. NeedIBDevice: false,
  156. IsMainRole: false,
  157. UseNNI: false,
  158. },
  159. },
  160. Volumes: []models.Volume{
  161. {
  162. HostPath: models.StHostPath{
  163. Path: codePath,
  164. MountPath: CodeMountPath,
  165. ReadOnly: false,
  166. },
  167. },
  168. {
  169. HostPath: models.StHostPath{
  170. Path: dataActualPath,
  171. MountPath: DataSetMountPath,
  172. ReadOnly: true,
  173. },
  174. },
  175. {
  176. HostPath: models.StHostPath{
  177. Path: modelPath,
  178. MountPath: ModelMountPath,
  179. ReadOnly: false,
  180. },
  181. },
  182. {
  183. HostPath: models.StHostPath{
  184. Path: benchmarkPath,
  185. MountPath: BenchMarkMountPath,
  186. ReadOnly: true,
  187. },
  188. },
  189. {
  190. HostPath: models.StHostPath{
  191. Path: snn4imagenetPath,
  192. MountPath: Snn4imagenetMountPath,
  193. ReadOnly: true,
  194. },
  195. },
  196. {
  197. HostPath: models.StHostPath{
  198. Path: brainScorePath,
  199. MountPath: BrainScoreMountPath,
  200. ReadOnly: true,
  201. },
  202. },
  203. },
  204. })
  205. if err != nil {
  206. log.Error("CreateJob failed:", err.Error(), ctx.Data["MsgID"])
  207. return err
  208. }
  209. if jobResult.Code != Success {
  210. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  211. return errors.New(jobResult.Msg)
  212. }
  213. var jobID = jobResult.Payload["jobId"].(string)
  214. err = models.CreateCloudbrain(&models.Cloudbrain{
  215. Status: string(models.JobWaiting),
  216. UserID: ctx.User.ID,
  217. RepoID: ctx.Repo.Repository.ID,
  218. JobID: jobID,
  219. JobName: jobName,
  220. DisplayJobName: displayJobName,
  221. SubTaskName: SubTaskName,
  222. JobType: jobType,
  223. Type: models.TypeCloudBrainOne,
  224. Uuid: uuid,
  225. Image: image,
  226. GpuQueue: gpuQueue,
  227. ResourceSpecId: resourceSpecId,
  228. ComputeResource: models.GPUResource,
  229. BenchmarkTypeID: benchmarkTypeID,
  230. BenchmarkChildTypeID: benchmarkChildTypeID,
  231. Description: description,
  232. })
  233. if err != nil {
  234. return err
  235. }
  236. task, err := models.GetCloudbrainByName(jobName)
  237. if err != nil {
  238. log.Error("GetCloudbrainByName failed: %v", err.Error())
  239. return err
  240. }
  241. stringId := strconv.FormatInt(task.ID, 10)
  242. if string(models.JobTypeBenchmark) == jobType {
  243. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateBenchMarkTask)
  244. } else {
  245. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateDebugGPUTask)
  246. }
  247. return nil
  248. }
  249. func RestartTask(ctx *context.Context, task *models.Cloudbrain, newID *string) error {
  250. dataActualPath := setting.Attachment.Minio.RealPath +
  251. setting.Attachment.Minio.Bucket + "/" +
  252. setting.Attachment.Minio.BasePath +
  253. models.AttachmentRelativePath(task.Uuid) +
  254. task.Uuid
  255. jobName := task.JobName
  256. var resourceSpec *models.ResourceSpec
  257. if ResourceSpecs == nil {
  258. json.Unmarshal([]byte(setting.ResourceSpecs), &ResourceSpecs)
  259. }
  260. for _, spec := range ResourceSpecs.ResourceSpec {
  261. if task.ResourceSpecId == spec.Id {
  262. resourceSpec = spec
  263. }
  264. }
  265. if resourceSpec == nil {
  266. log.Error("no such resourceSpecId(%d)", task.ResourceSpecId, ctx.Data["MsgID"])
  267. return errors.New("no such resourceSpec")
  268. }
  269. jobResult, err := CreateJob(jobName, models.CreateJobParams{
  270. JobName: jobName,
  271. RetryCount: 1,
  272. GpuType: task.GpuQueue,
  273. Image: task.Image,
  274. TaskRoles: []models.TaskRole{
  275. {
  276. Name: SubTaskName,
  277. TaskNumber: 1,
  278. MinSucceededTaskCount: 1,
  279. MinFailedTaskCount: 1,
  280. CPUNumber: resourceSpec.CpuNum,
  281. GPUNumber: resourceSpec.GpuNum,
  282. MemoryMB: resourceSpec.MemMiB,
  283. ShmMB: resourceSpec.ShareMemMiB,
  284. Command: Command,
  285. NeedIBDevice: false,
  286. IsMainRole: false,
  287. UseNNI: false,
  288. },
  289. },
  290. Volumes: []models.Volume{
  291. {
  292. HostPath: models.StHostPath{
  293. Path: storage.GetMinioPath(jobName, CodeMountPath+"/"),
  294. MountPath: CodeMountPath,
  295. ReadOnly: false,
  296. },
  297. },
  298. {
  299. HostPath: models.StHostPath{
  300. Path: dataActualPath,
  301. MountPath: DataSetMountPath,
  302. ReadOnly: true,
  303. },
  304. },
  305. {
  306. HostPath: models.StHostPath{
  307. Path: storage.GetMinioPath(jobName, ModelMountPath+"/"),
  308. MountPath: ModelMountPath,
  309. ReadOnly: false,
  310. },
  311. },
  312. {
  313. HostPath: models.StHostPath{
  314. Path: storage.GetMinioPath(jobName, BenchMarkMountPath+"/"),
  315. MountPath: BenchMarkMountPath,
  316. ReadOnly: true,
  317. },
  318. },
  319. {
  320. HostPath: models.StHostPath{
  321. Path: storage.GetMinioPath(jobName, Snn4imagenetMountPath+"/"),
  322. MountPath: Snn4imagenetMountPath,
  323. ReadOnly: true,
  324. },
  325. },
  326. {
  327. HostPath: models.StHostPath{
  328. Path: storage.GetMinioPath(jobName, BrainScoreMountPath+"/"),
  329. MountPath: BrainScoreMountPath,
  330. ReadOnly: true,
  331. },
  332. },
  333. },
  334. })
  335. if err != nil {
  336. log.Error("CreateJob failed:%v", err.Error(), ctx.Data["MsgID"])
  337. return err
  338. }
  339. if jobResult.Code != Success {
  340. log.Error("CreateJob(%s) failed:%s", jobName, jobResult.Msg, ctx.Data["MsgID"])
  341. return errors.New(jobResult.Msg)
  342. }
  343. var jobID = jobResult.Payload["jobId"].(string)
  344. newTask := &models.Cloudbrain{
  345. Status: string(models.JobWaiting),
  346. UserID: task.UserID,
  347. RepoID: task.RepoID,
  348. JobID: jobID,
  349. JobName: task.JobName,
  350. DisplayJobName: task.DisplayJobName,
  351. SubTaskName: task.SubTaskName,
  352. JobType: task.JobType,
  353. Type: task.Type,
  354. Uuid: task.Uuid,
  355. Image: task.Image,
  356. GpuQueue: task.GpuQueue,
  357. ResourceSpecId: task.ResourceSpecId,
  358. ComputeResource: task.ComputeResource,
  359. }
  360. err = models.RestartCloudbrain(task, newTask)
  361. if err != nil {
  362. log.Error("RestartCloudbrain(%s) failed:%v", jobName, err.Error(), ctx.Data["MsgID"])
  363. return err
  364. }
  365. idString := strconv.FormatInt(newTask.ID, 10)
  366. *newID = idString
  367. return nil
  368. }