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.

modelarts.go 12 kB

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
4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 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
4 years ago
4 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package modelarts
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "path"
  6. "strconv"
  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/setting"
  11. "code.gitea.io/gitea/modules/storage"
  12. )
  13. const (
  14. //notebook
  15. storageTypeOBS = "obs"
  16. autoStopDuration = 4 * 60 * 60
  17. DataSetMountPath = "/home/ma-user/work"
  18. NotebookEnv = "Python3"
  19. NotebookType = "Ascend"
  20. FlavorInfo = "Ascend: 1*Ascend 910 CPU: 24 核 96GiB (modelarts.kat1.xlarge)"
  21. //train-job
  22. // ResourcePools = "{\"resource_pool\":[{\"id\":\"pool1328035d\", \"value\":\"专属资源池\"}]}"
  23. // Engines = "{\"engine\":[{\"id\":1, \"value\":\"Ascend-Powered-Engine\"}]}"
  24. // EngineVersions = "{\"version\":[{\"id\":118,\"value\":\"MindSpore-1.0.0-c75-python3.7-euleros2.8-aarch64\"}," +
  25. // "{\"id\":119,\"value\":\"MindSpore-1.1.1-c76-python3.7-euleros2.8-aarch64\"}," +
  26. // "{\"id\":120,\"value\":\"MindSpore-1.1.1-c76-tr5-python3.7-euleros2.8-aarch64\"}," +
  27. // "{\"id\":117,\"value\":\"TF-1.15-c75-python3.7-euleros2.8-aarch64\"}" +
  28. // "]}"
  29. // TrainJobFlavorInfo = "{\"flavor\":[{\"code\":\"modelarts.bm.910.arm.public.2\",\"value\":\"Ascend : 2 * Ascend 910 CPU:48 核 512GiB\"}," +
  30. // "{\"code\":\"modelarts.bm.910.arm.public.8\",\"value\":\"Ascend : 8 * Ascend 910 CPU:192 核 2048GiB\"}," +
  31. // "{\"code\":\"modelarts.bm.910.arm.public.4\",\"value\":\"Ascend : 4 * Ascend 910 CPU:96 核 1024GiB\"}," +
  32. // "{\"code\":\"modelarts.bm.910.arm.public.1\",\"value\":\"Ascend : 1 * Ascend 910 CPU:24 核 256GiB\"}" +
  33. // "]}"
  34. CodePath = "/code/"
  35. OutputPath = "/output/"
  36. LogPath = "/log/"
  37. JobPath = "/job/"
  38. OrderDesc = "desc" //向下查询
  39. OrderAsc = "asc" //向上查询
  40. Lines = 500
  41. TrainUrl = "train_url"
  42. DataUrl = "data_url"
  43. PerPage = 10
  44. IsLatestVersion = "1"
  45. NotLatestVersion = "0"
  46. ComputeResource = "NPU"
  47. NPUResource = "NPU"
  48. GPUResource = "CPU/GPU"
  49. DebugType = -1
  50. VersionCount = 1
  51. SortByCreateTime = "create_time"
  52. ConfigTypeCustom = "custom"
  53. TotalVersionCount = 1
  54. )
  55. var (
  56. poolInfos *models.PoolInfos
  57. FlavorInfos *models.FlavorInfos
  58. )
  59. type GenerateTrainJobReq struct {
  60. JobName string
  61. Uuid string
  62. Description string
  63. CodeObsPath string
  64. BootFile string
  65. BootFileUrl string
  66. DataUrl string
  67. TrainUrl string
  68. FlavorCode string
  69. LogUrl string
  70. PoolID string
  71. WorkServerNumber int
  72. EngineID int64
  73. Parameters []models.Parameter
  74. CommitID string
  75. IsLatestVersion string
  76. Params string
  77. BranchName string
  78. PreVersionId int64
  79. PreVersionName string
  80. FlavorName string
  81. VersionCount int
  82. EngineName string
  83. TotalVersionCount int
  84. }
  85. type GenerateTrainJobVersionReq struct {
  86. JobName string
  87. Uuid string
  88. Description string
  89. CodeObsPath string
  90. BootFile string
  91. BootFileUrl string
  92. DataUrl string
  93. TrainUrl string
  94. FlavorCode string
  95. LogUrl string
  96. PoolID string
  97. WorkServerNumber int
  98. EngineID int64
  99. Parameters []models.Parameter
  100. Params string
  101. PreVersionId int64
  102. CommitID string
  103. BranchName string
  104. FlavorName string
  105. EngineName string
  106. PreVersionName string
  107. TotalVersionCount int
  108. }
  109. type VersionInfo struct {
  110. Version []struct {
  111. ID int `json:"id"`
  112. Value string `json:"value"`
  113. } `json:"version"`
  114. }
  115. type Flavor struct {
  116. Info []struct {
  117. Code string `json:"code"`
  118. Value string `json:"value"`
  119. } `json:"flavor"`
  120. }
  121. type Engine struct {
  122. Info []struct {
  123. ID int `json:"id"`
  124. Value string `json:"value"`
  125. } `json:"engine"`
  126. }
  127. type ResourcePool struct {
  128. Info []struct {
  129. ID string `json:"id"`
  130. Value string `json:"value"`
  131. } `json:"resource_pool"`
  132. }
  133. // type Parameter struct {
  134. // Label string `json:"label"`
  135. // Value string `json:"value"`
  136. // }
  137. // type Parameters struct {
  138. // Parameter []Parameter `json:"parameter"`
  139. // }
  140. type Parameters struct {
  141. Parameter []struct {
  142. Label string `json:"label"`
  143. Value string `json:"value"`
  144. } `json:"parameter"`
  145. }
  146. func GenerateTask(ctx *context.Context, jobName, uuid, description, flavor string) error {
  147. var dataActualPath string
  148. if uuid != "" {
  149. dataActualPath = setting.Bucket + "/" + setting.BasePath + path.Join(uuid[0:1], uuid[1:2]) + "/" + uuid + "/"
  150. } else {
  151. userPath := setting.UserBasePath + ctx.User.Name + "/"
  152. isExist, err := storage.ObsHasObject(userPath)
  153. if err != nil {
  154. log.Error("ObsHasObject failed:%v", err.Error(), ctx.Data["MsgID"])
  155. return err
  156. }
  157. if !isExist {
  158. if err = storage.ObsCreateObject(userPath); err != nil {
  159. log.Error("ObsCreateObject failed:%v", err.Error(), ctx.Data["MsgID"])
  160. return err
  161. }
  162. }
  163. dataActualPath = setting.Bucket + "/" + userPath
  164. }
  165. if poolInfos == nil {
  166. json.Unmarshal([]byte(setting.PoolInfos), &poolInfos)
  167. }
  168. jobResult, err := CreateJob(models.CreateNotebookParams{
  169. JobName: jobName,
  170. Description: description,
  171. ProfileID: setting.ProfileID,
  172. Flavor: flavor,
  173. Pool: models.Pool{
  174. ID: poolInfos.PoolInfo[0].PoolId,
  175. Name: poolInfos.PoolInfo[0].PoolName,
  176. Type: poolInfos.PoolInfo[0].PoolType,
  177. },
  178. Spec: models.Spec{
  179. Storage: models.Storage{
  180. Type: storageTypeOBS,
  181. Location: models.Location{
  182. Path: dataActualPath,
  183. },
  184. },
  185. AutoStop: models.AutoStop{
  186. Enable: true,
  187. Duration: autoStopDuration,
  188. },
  189. },
  190. })
  191. if err != nil {
  192. log.Error("CreateJob failed: %v", err.Error())
  193. return err
  194. }
  195. err = models.CreateCloudbrain(&models.Cloudbrain{
  196. Status: string(models.JobWaiting),
  197. UserID: ctx.User.ID,
  198. RepoID: ctx.Repo.Repository.ID,
  199. JobID: jobResult.ID,
  200. JobName: jobName,
  201. JobType: string(models.JobTypeDebug),
  202. Type: models.TypeCloudBrainTwo,
  203. Uuid: uuid,
  204. })
  205. if err != nil {
  206. return err
  207. }
  208. return nil
  209. }
  210. func GenerateTrainJob(ctx *context.Context, req *GenerateTrainJobReq) (err error) {
  211. jobResult, err := createTrainJob(models.CreateTrainJobParams{
  212. JobName: req.JobName,
  213. Description: req.Description,
  214. Config: models.Config{
  215. WorkServerNum: req.WorkServerNumber,
  216. AppUrl: req.CodeObsPath,
  217. BootFileUrl: req.BootFileUrl,
  218. DataUrl: req.DataUrl,
  219. EngineID: req.EngineID,
  220. TrainUrl: req.TrainUrl,
  221. LogUrl: req.LogUrl,
  222. PoolID: req.PoolID,
  223. CreateVersion: true,
  224. Flavor: models.Flavor{
  225. Code: req.FlavorCode,
  226. },
  227. Parameter: req.Parameters,
  228. },
  229. })
  230. if err != nil {
  231. log.Error("CreateJob failed: %v", err.Error())
  232. return err
  233. }
  234. attach, err := models.GetAttachmentByUUID(req.Uuid)
  235. if err != nil {
  236. log.Error("GetAttachmentByUUID(%s) failed:%v", strconv.FormatInt(jobResult.JobID, 10), err.Error())
  237. return err
  238. }
  239. err = models.CreateCloudbrain(&models.Cloudbrain{
  240. Status: TransTrainJobStatus(jobResult.Status),
  241. UserID: ctx.User.ID,
  242. RepoID: ctx.Repo.Repository.ID,
  243. JobID: strconv.FormatInt(jobResult.JobID, 10),
  244. JobName: req.JobName,
  245. JobType: string(models.JobTypeTrain),
  246. Type: models.TypeCloudBrainTwo,
  247. VersionID: jobResult.VersionID,
  248. VersionName: jobResult.VersionName,
  249. Uuid: req.Uuid,
  250. DatasetName: attach.Name,
  251. CommitID: req.CommitID,
  252. IsLatestVersion: req.IsLatestVersion,
  253. ComputeResource: NPUResource,
  254. EngineID: req.EngineID,
  255. TrainUrl: req.TrainUrl,
  256. BranchName: req.BranchName,
  257. Parameters: req.Params,
  258. BootFile: req.BootFile,
  259. DataUrl: req.DataUrl,
  260. LogUrl: req.LogUrl,
  261. FlavorCode: req.FlavorCode,
  262. Description: req.Description,
  263. WorkServerNumber: req.WorkServerNumber,
  264. FlavorName: req.FlavorName,
  265. EngineName: req.EngineName,
  266. VersionCount: req.VersionCount,
  267. TotalVersionCount: req.TotalVersionCount,
  268. })
  269. if err != nil {
  270. log.Error("CreateCloudbrain(%s) failed:%v", req.JobName, err.Error())
  271. return err
  272. }
  273. return nil
  274. }
  275. func GenerateTrainJobVersion(ctx *context.Context, req *GenerateTrainJobReq, jobId string) (err error) {
  276. jobResult, err := createTrainJobVersion(models.CreateTrainJobVersionParams{
  277. Description: req.Description,
  278. Config: models.TrainJobVersionConfig{
  279. WorkServerNum: req.WorkServerNumber,
  280. AppUrl: req.CodeObsPath,
  281. BootFileUrl: req.BootFileUrl,
  282. DataUrl: req.DataUrl,
  283. EngineID: req.EngineID,
  284. TrainUrl: req.TrainUrl,
  285. LogUrl: req.LogUrl,
  286. PoolID: req.PoolID,
  287. Flavor: models.Flavor{
  288. Code: req.FlavorCode,
  289. },
  290. Parameter: req.Parameters,
  291. PreVersionId: req.PreVersionId,
  292. },
  293. }, jobId)
  294. if err != nil {
  295. log.Error("CreateJob failed: %v", err.Error())
  296. return err
  297. }
  298. attach, err := models.GetAttachmentByUUID(req.Uuid)
  299. if err != nil {
  300. log.Error("GetAttachmentByUUID(%s) failed:%v", strconv.FormatInt(jobResult.JobID, 10), err.Error())
  301. return err
  302. }
  303. repo := ctx.Repo.Repository
  304. VersionTaskList, VersionListCount, err := models.CloudbrainsVersionList(&models.CloudbrainsOptions{
  305. RepoID: repo.ID,
  306. Type: models.TypeCloudBrainTwo,
  307. JobType: string(models.JobTypeTrain),
  308. JobID: strconv.FormatInt(jobResult.JobID, 10),
  309. })
  310. if err != nil {
  311. ctx.ServerError("Cloudbrain", err)
  312. return err
  313. }
  314. //将当前版本的isLatestVersion设置为"1"和任务数量更新,任务数量包括当前版本数VersionCount和历史创建的总版本数TotalVersionCount
  315. err = models.CreateCloudbrain(&models.Cloudbrain{
  316. Status: TransTrainJobStatus(jobResult.Status),
  317. UserID: ctx.User.ID,
  318. RepoID: ctx.Repo.Repository.ID,
  319. JobID: strconv.FormatInt(jobResult.JobID, 10),
  320. JobName: req.JobName,
  321. JobType: string(models.JobTypeTrain),
  322. Type: models.TypeCloudBrainTwo,
  323. VersionID: jobResult.VersionID,
  324. VersionName: jobResult.VersionName,
  325. Uuid: req.Uuid,
  326. DatasetName: attach.Name,
  327. CommitID: req.CommitID,
  328. IsLatestVersion: req.IsLatestVersion,
  329. PreVersionName: req.PreVersionName,
  330. ComputeResource: NPUResource,
  331. EngineID: req.EngineID,
  332. TrainUrl: req.TrainUrl,
  333. BranchName: req.BranchName,
  334. Parameters: req.Params,
  335. BootFile: req.BootFile,
  336. DataUrl: req.DataUrl,
  337. LogUrl: req.LogUrl,
  338. PreVersionId: req.PreVersionId,
  339. FlavorCode: req.FlavorCode,
  340. Description: req.Description,
  341. WorkServerNumber: req.WorkServerNumber,
  342. FlavorName: req.FlavorName,
  343. EngineName: req.EngineName,
  344. TotalVersionCount: VersionTaskList[0].TotalVersionCount + 1,
  345. VersionCount: VersionListCount + 1,
  346. })
  347. if err != nil {
  348. log.Error("CreateCloudbrain(%s) failed:%v", req.JobName, err.Error())
  349. return err
  350. }
  351. //将训练任务的上一版本的isLatestVersion设置为"0"
  352. err = models.SetVersionCountAndLatestVersion(strconv.FormatInt(jobResult.JobID, 10), VersionTaskList[0].VersionName, VersionCount, NotLatestVersion, TotalVersionCount)
  353. if err != nil {
  354. ctx.ServerError("Update IsLatestVersion failed", err)
  355. return err
  356. }
  357. return err
  358. }
  359. func TransTrainJobStatus(status int) string {
  360. switch status {
  361. case 0:
  362. return "UNKNOWN"
  363. case 1:
  364. return "INIT"
  365. case 2:
  366. return "IMAGE_CREATING"
  367. case 3:
  368. return "IMAGE_FAILED"
  369. case 4:
  370. return "SUBMIT_TRYING"
  371. case 5:
  372. return "SUBMIT_FAILED"
  373. case 6:
  374. return "DELETE_FAILED"
  375. case 7:
  376. return "WAITING"
  377. case 8:
  378. return "RUNNING"
  379. case 9:
  380. return "KILLING"
  381. case 10:
  382. return "COMPLETED"
  383. case 11:
  384. return "FAILED"
  385. case 12:
  386. return "KILLED"
  387. case 13:
  388. return "CANCELED"
  389. case 14:
  390. return "LOST"
  391. case 15:
  392. return "SCALING"
  393. case 16:
  394. return "SUBMIT_MODEL_FAILED"
  395. case 17:
  396. return "DEPLOY_SERVICE_FAILED"
  397. case 18:
  398. return "CHECK_INIT"
  399. case 19:
  400. return "CHECK_RUNNING"
  401. case 20:
  402. return "CHECK_RUNNING_COMPLETED"
  403. case 21:
  404. return "CHECK_FAILED"
  405. default:
  406. return strconv.Itoa(status)
  407. }
  408. }
  409. func GetVersionOutputPathByTotalVersionCount(TotalVersionCount int) (VersionOutputPath string) {
  410. talVersionCountToString := fmt.Sprintf("%04d", TotalVersionCount)
  411. VersionOutputPath = "V" + talVersionCountToString
  412. return VersionOutputPath
  413. }