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

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