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