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 20 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
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
3 years ago
3 years ago
4 years ago
4 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
3 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
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
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
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
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
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
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
4 years ago
3 years ago
4 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
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
4 years ago
3 years ago
4 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package modelarts
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "path"
  7. "strconv"
  8. "code.gitea.io/gitea/modules/timeutil"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/notification"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/storage"
  15. )
  16. const (
  17. //notebook
  18. storageTypeOBS = "obs"
  19. autoStopDuration = 4 * 60 * 60
  20. autoStopDurationMs = 4 * 60 * 60 * 1000
  21. DataSetMountPath = "/home/ma-user/work"
  22. NotebookEnv = "Python3"
  23. NotebookType = "Ascend"
  24. FlavorInfo = "Ascend: 1*Ascend 910 CPU: 24 核 96GiB (modelarts.kat1.xlarge)"
  25. //train-job
  26. // ResourcePools = "{\"resource_pool\":[{\"id\":\"pool1328035d\", \"value\":\"专属资源池\"}]}"
  27. // Engines = "{\"engine\":[{\"id\":1, \"value\":\"Ascend-Powered-Engine\"}]}"
  28. // EngineVersions = "{\"version\":[{\"id\":118,\"value\":\"MindSpore-1.0.0-c75-python3.7-euleros2.8-aarch64\"}," +
  29. // "{\"id\":119,\"value\":\"MindSpore-1.1.1-c76-python3.7-euleros2.8-aarch64\"}," +
  30. // "{\"id\":120,\"value\":\"MindSpore-1.1.1-c76-tr5-python3.7-euleros2.8-aarch64\"}," +
  31. // "{\"id\":117,\"value\":\"TF-1.15-c75-python3.7-euleros2.8-aarch64\"}" +
  32. // "]}"
  33. // TrainJobFlavorInfo = "{\"flavor\":[{\"code\":\"modelarts.bm.910.arm.public.2\",\"value\":\"Ascend : 2 * Ascend 910 CPU:48 核 512GiB\"}," +
  34. // "{\"code\":\"modelarts.bm.910.arm.public.8\",\"value\":\"Ascend : 8 * Ascend 910 CPU:192 核 2048GiB\"}," +
  35. // "{\"code\":\"modelarts.bm.910.arm.public.4\",\"value\":\"Ascend : 4 * Ascend 910 CPU:96 核 1024GiB\"}," +
  36. // "{\"code\":\"modelarts.bm.910.arm.public.1\",\"value\":\"Ascend : 1 * Ascend 910 CPU:24 核 256GiB\"}" +
  37. // "]}"
  38. CodePath = "/code/"
  39. OutputPath = "/output/"
  40. ResultPath = "/result/"
  41. LogPath = "/log/"
  42. JobPath = "/job/"
  43. OrderDesc = "desc" //向下查询
  44. OrderAsc = "asc" //向上查询
  45. Lines = 500
  46. TrainUrl = "train_url"
  47. DataUrl = "data_url"
  48. ResultUrl = "result_url"
  49. CkptUrl = "ckpt_url"
  50. DeviceTarget = "device_target"
  51. Ascend = "Ascend"
  52. PerPage = 10
  53. IsLatestVersion = "1"
  54. NotLatestVersion = "0"
  55. VersionCount = 1
  56. SortByCreateTime = "create_time"
  57. ConfigTypeCustom = "custom"
  58. TotalVersionCount = 1
  59. )
  60. var (
  61. poolInfos *models.PoolInfos
  62. FlavorInfos *models.FlavorInfos
  63. ImageInfos *models.ImageInfosModelArts
  64. )
  65. type GenerateTrainJobReq struct {
  66. JobName string
  67. DisplayJobName string
  68. Uuid string
  69. Description string
  70. CodeObsPath string
  71. BootFile string
  72. BootFileUrl string
  73. DataUrl string
  74. TrainUrl string
  75. FlavorCode string
  76. LogUrl string
  77. PoolID string
  78. WorkServerNumber int
  79. EngineID int64
  80. Parameters []models.Parameter
  81. CommitID string
  82. IsLatestVersion string
  83. Params string
  84. BranchName string
  85. PreVersionId int64
  86. PreVersionName string
  87. FlavorName string
  88. VersionCount int
  89. EngineName string
  90. TotalVersionCount int
  91. UserImageUrl string
  92. UserCommand string
  93. }
  94. type GenerateInferenceJobReq struct {
  95. JobName string
  96. DisplayJobName string
  97. Uuid string
  98. Description string
  99. CodeObsPath string
  100. BootFile string
  101. BootFileUrl string
  102. DataUrl string
  103. TrainUrl string
  104. FlavorCode string
  105. LogUrl string
  106. PoolID string
  107. WorkServerNumber int
  108. EngineID int64
  109. Parameters []models.Parameter
  110. CommitID string
  111. Params string
  112. BranchName string
  113. FlavorName string
  114. EngineName string
  115. LabelName string
  116. IsLatestVersion string
  117. VersionCount int
  118. TotalVersionCount int
  119. ModelName string
  120. ModelVersion string
  121. CkptName string
  122. ResultUrl string
  123. }
  124. type VersionInfo struct {
  125. Version []struct {
  126. ID int `json:"id"`
  127. Value string `json:"value"`
  128. } `json:"version"`
  129. }
  130. type Flavor struct {
  131. Info []struct {
  132. Code string `json:"code"`
  133. Value string `json:"value"`
  134. } `json:"flavor"`
  135. }
  136. type Engine struct {
  137. Info []struct {
  138. ID int `json:"id"`
  139. Value string `json:"value"`
  140. } `json:"engine"`
  141. }
  142. type ResourcePool struct {
  143. Info []struct {
  144. ID string `json:"id"`
  145. Value string `json:"value"`
  146. } `json:"resource_pool"`
  147. }
  148. // type Parameter struct {
  149. // Label string `json:"label"`
  150. // Value string `json:"value"`
  151. // }
  152. // type Parameters struct {
  153. // Parameter []Parameter `json:"parameter"`
  154. // }
  155. type Parameters struct {
  156. Parameter []struct {
  157. Label string `json:"label"`
  158. Value string `json:"value"`
  159. } `json:"parameter"`
  160. }
  161. func GenerateTask(ctx *context.Context, jobName, uuid, description, flavor string) error {
  162. var dataActualPath string
  163. if uuid != "" {
  164. dataActualPath = setting.Bucket + "/" + setting.BasePath + path.Join(uuid[0:1], uuid[1:2]) + "/" + uuid + "/"
  165. } else {
  166. userPath := setting.UserBasePath + ctx.User.Name + "/"
  167. isExist, err := storage.ObsHasObject(userPath)
  168. if err != nil {
  169. log.Error("ObsHasObject failed:%v", err.Error(), ctx.Data["MsgID"])
  170. return err
  171. }
  172. if !isExist {
  173. if err = storage.ObsCreateObject(userPath); err != nil {
  174. log.Error("ObsCreateObject failed:%v", err.Error(), ctx.Data["MsgID"])
  175. return err
  176. }
  177. }
  178. dataActualPath = setting.Bucket + "/" + userPath
  179. }
  180. if poolInfos == nil {
  181. json.Unmarshal([]byte(setting.PoolInfos), &poolInfos)
  182. }
  183. createTime := timeutil.TimeStampNow()
  184. jobResult, err := CreateJob(models.CreateNotebookParams{
  185. JobName: jobName,
  186. Description: description,
  187. ProfileID: setting.ProfileID,
  188. Flavor: flavor,
  189. Pool: models.Pool{
  190. ID: poolInfos.PoolInfo[0].PoolId,
  191. Name: poolInfos.PoolInfo[0].PoolName,
  192. Type: poolInfos.PoolInfo[0].PoolType,
  193. },
  194. Spec: models.Spec{
  195. Storage: models.Storage{
  196. Type: storageTypeOBS,
  197. Location: models.Location{
  198. Path: dataActualPath,
  199. },
  200. },
  201. AutoStop: models.AutoStop{
  202. Enable: true,
  203. Duration: autoStopDuration,
  204. },
  205. },
  206. })
  207. if err != nil {
  208. log.Error("CreateJob failed: %v", err.Error())
  209. return err
  210. }
  211. err = models.CreateCloudbrain(&models.Cloudbrain{
  212. Status: string(models.JobWaiting),
  213. UserID: ctx.User.ID,
  214. RepoID: ctx.Repo.Repository.ID,
  215. JobID: jobResult.ID,
  216. JobName: jobName,
  217. JobType: string(models.JobTypeDebug),
  218. Type: models.TypeCloudBrainTwo,
  219. Uuid: uuid,
  220. ComputeResource: models.NPUResource,
  221. CreatedUnix: createTime,
  222. UpdatedUnix: createTime,
  223. })
  224. if err != nil {
  225. return err
  226. }
  227. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, jobResult.ID, jobName, models.ActionCreateDebugNPUTask)
  228. return nil
  229. }
  230. func GenerateNotebook2(ctx *context.Context, displayJobName, jobName, uuid, description, flavor, imageId string) error {
  231. if poolInfos == nil {
  232. json.Unmarshal([]byte(setting.PoolInfos), &poolInfos)
  233. }
  234. imageName, err := GetNotebookImageName(imageId)
  235. if err != nil {
  236. log.Error("GetNotebookImageName failed: %v", err.Error())
  237. return err
  238. }
  239. createTime := timeutil.TimeStampNow()
  240. jobResult, err := createNotebook2(models.CreateNotebook2Params{
  241. JobName: jobName,
  242. Description: description,
  243. Flavor: flavor,
  244. Duration: autoStopDurationMs,
  245. ImageID: imageId,
  246. PoolID: poolInfos.PoolInfo[0].PoolId,
  247. Feature: models.NotebookFeature,
  248. Volume: models.VolumeReq{
  249. Capacity: setting.Capacity,
  250. Category: models.EVSCategory,
  251. Ownership: models.ManagedOwnership,
  252. },
  253. WorkspaceID: "0",
  254. })
  255. if err != nil {
  256. log.Error("createNotebook2 failed: %v", err.Error())
  257. return err
  258. }
  259. err = models.CreateCloudbrain(&models.Cloudbrain{
  260. Status: jobResult.Status,
  261. UserID: ctx.User.ID,
  262. RepoID: ctx.Repo.Repository.ID,
  263. JobID: jobResult.ID,
  264. JobName: jobName,
  265. FlavorCode: flavor,
  266. DisplayJobName: displayJobName,
  267. JobType: string(models.JobTypeDebug),
  268. Type: models.TypeCloudBrainTwo,
  269. Uuid: uuid,
  270. ComputeResource: models.NPUResource,
  271. Image: imageName,
  272. Description: description,
  273. CreatedUnix: createTime,
  274. UpdatedUnix: createTime,
  275. })
  276. if err != nil {
  277. return err
  278. }
  279. task, err := models.GetCloudbrainByName(jobName)
  280. if err != nil {
  281. log.Error("GetCloudbrainByName failed: %v", err.Error())
  282. return err
  283. }
  284. stringId := strconv.FormatInt(task.ID, 10)
  285. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateDebugNPUTask)
  286. return nil
  287. }
  288. func GenerateTrainJob(ctx *context.Context, req *GenerateTrainJobReq) (err error) {
  289. createTime := timeutil.TimeStampNow()
  290. jobResult, err := createTrainJob(models.CreateTrainJobParams{
  291. JobName: req.JobName,
  292. Description: req.Description,
  293. Config: models.Config{
  294. WorkServerNum: req.WorkServerNumber,
  295. AppUrl: req.CodeObsPath,
  296. BootFileUrl: req.BootFileUrl,
  297. DataUrl: req.DataUrl,
  298. EngineID: req.EngineID,
  299. TrainUrl: req.TrainUrl,
  300. LogUrl: req.LogUrl,
  301. PoolID: req.PoolID,
  302. CreateVersion: true,
  303. Flavor: models.Flavor{
  304. Code: req.FlavorCode,
  305. },
  306. Parameter: req.Parameters,
  307. },
  308. })
  309. if err != nil {
  310. log.Error("CreateJob failed: %v", err.Error())
  311. return err
  312. }
  313. attach, err := models.GetAttachmentByUUID(req.Uuid)
  314. if err != nil {
  315. log.Error("GetAttachmentByUUID(%s) failed:%v", strconv.FormatInt(jobResult.JobID, 10), err.Error())
  316. return err
  317. }
  318. jobId := strconv.FormatInt(jobResult.JobID, 10)
  319. err = models.CreateCloudbrain(&models.Cloudbrain{
  320. Status: TransTrainJobStatus(jobResult.Status),
  321. UserID: ctx.User.ID,
  322. RepoID: ctx.Repo.Repository.ID,
  323. JobID: jobId,
  324. JobName: req.JobName,
  325. DisplayJobName: req.DisplayJobName,
  326. JobType: string(models.JobTypeTrain),
  327. Type: models.TypeCloudBrainTwo,
  328. VersionID: jobResult.VersionID,
  329. VersionName: jobResult.VersionName,
  330. Uuid: req.Uuid,
  331. DatasetName: attach.Name,
  332. CommitID: req.CommitID,
  333. IsLatestVersion: req.IsLatestVersion,
  334. ComputeResource: models.NPUResource,
  335. EngineID: req.EngineID,
  336. TrainUrl: req.TrainUrl,
  337. BranchName: req.BranchName,
  338. Parameters: req.Params,
  339. BootFile: req.BootFile,
  340. DataUrl: req.DataUrl,
  341. LogUrl: req.LogUrl,
  342. FlavorCode: req.FlavorCode,
  343. Description: req.Description,
  344. WorkServerNumber: req.WorkServerNumber,
  345. FlavorName: req.FlavorName,
  346. EngineName: req.EngineName,
  347. VersionCount: req.VersionCount,
  348. TotalVersionCount: req.TotalVersionCount,
  349. CreatedUnix: createTime,
  350. UpdatedUnix: createTime,
  351. })
  352. if err != nil {
  353. log.Error("CreateCloudbrain(%s) failed:%v", req.DisplayJobName, err.Error())
  354. return err
  355. }
  356. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, jobId, req.DisplayJobName, models.ActionCreateTrainTask)
  357. return nil
  358. }
  359. func GenerateModelConvertTrainJob(req *GenerateTrainJobReq) (*models.CreateTrainJobResult, error) {
  360. return createTrainJobUserImage(models.CreateUserImageTrainJobParams{
  361. JobName: req.JobName,
  362. Description: req.Description,
  363. Config: models.UserImageConfig{
  364. WorkServerNum: req.WorkServerNumber,
  365. AppUrl: req.CodeObsPath,
  366. BootFileUrl: req.BootFileUrl,
  367. DataUrl: req.DataUrl,
  368. TrainUrl: req.TrainUrl,
  369. LogUrl: req.LogUrl,
  370. PoolID: req.PoolID,
  371. CreateVersion: true,
  372. Flavor: models.Flavor{
  373. Code: req.FlavorCode,
  374. },
  375. Parameter: req.Parameters,
  376. UserImageUrl: req.UserImageUrl,
  377. UserCommand: req.UserCommand,
  378. },
  379. })
  380. }
  381. func GenerateTrainJobVersion(ctx *context.Context, req *GenerateTrainJobReq, jobId string) (err error) {
  382. createTime := timeutil.TimeStampNow()
  383. jobResult, err := createTrainJobVersion(models.CreateTrainJobVersionParams{
  384. Description: req.Description,
  385. Config: models.TrainJobVersionConfig{
  386. WorkServerNum: req.WorkServerNumber,
  387. AppUrl: req.CodeObsPath,
  388. BootFileUrl: req.BootFileUrl,
  389. DataUrl: req.DataUrl,
  390. EngineID: req.EngineID,
  391. TrainUrl: req.TrainUrl,
  392. LogUrl: req.LogUrl,
  393. PoolID: req.PoolID,
  394. Flavor: models.Flavor{
  395. Code: req.FlavorCode,
  396. },
  397. Parameter: req.Parameters,
  398. PreVersionId: req.PreVersionId,
  399. },
  400. }, jobId)
  401. if err != nil {
  402. log.Error("CreateJob failed: %v", err.Error())
  403. return err
  404. }
  405. attach, err := models.GetAttachmentByUUID(req.Uuid)
  406. if err != nil {
  407. log.Error("GetAttachmentByUUID(%s) failed:%v", strconv.FormatInt(jobResult.JobID, 10), err.Error())
  408. return err
  409. }
  410. var jobTypes []string
  411. jobTypes = append(jobTypes, string(models.JobTypeTrain))
  412. repo := ctx.Repo.Repository
  413. VersionTaskList, VersionListCount, err := models.CloudbrainsVersionList(&models.CloudbrainsOptions{
  414. RepoID: repo.ID,
  415. Type: models.TypeCloudBrainTwo,
  416. JobTypes: jobTypes,
  417. JobID: strconv.FormatInt(jobResult.JobID, 10),
  418. })
  419. if err != nil {
  420. ctx.ServerError("Cloudbrain", err)
  421. return err
  422. }
  423. //将当前版本的isLatestVersion设置为"1"和任务数量更新,任务数量包括当前版本数VersionCount和历史创建的总版本数TotalVersionCount
  424. err = models.CreateCloudbrain(&models.Cloudbrain{
  425. Status: TransTrainJobStatus(jobResult.Status),
  426. UserID: ctx.User.ID,
  427. RepoID: ctx.Repo.Repository.ID,
  428. JobID: strconv.FormatInt(jobResult.JobID, 10),
  429. JobName: req.JobName,
  430. DisplayJobName: req.DisplayJobName,
  431. JobType: string(models.JobTypeTrain),
  432. Type: models.TypeCloudBrainTwo,
  433. VersionID: jobResult.VersionID,
  434. VersionName: jobResult.VersionName,
  435. Uuid: req.Uuid,
  436. DatasetName: attach.Name,
  437. CommitID: req.CommitID,
  438. IsLatestVersion: req.IsLatestVersion,
  439. PreVersionName: req.PreVersionName,
  440. ComputeResource: models.NPUResource,
  441. EngineID: req.EngineID,
  442. TrainUrl: req.TrainUrl,
  443. BranchName: req.BranchName,
  444. Parameters: req.Params,
  445. BootFile: req.BootFile,
  446. DataUrl: req.DataUrl,
  447. LogUrl: req.LogUrl,
  448. PreVersionId: req.PreVersionId,
  449. FlavorCode: req.FlavorCode,
  450. Description: req.Description,
  451. WorkServerNumber: req.WorkServerNumber,
  452. FlavorName: req.FlavorName,
  453. EngineName: req.EngineName,
  454. TotalVersionCount: VersionTaskList[0].TotalVersionCount + 1,
  455. VersionCount: VersionListCount + 1,
  456. CreatedUnix: createTime,
  457. UpdatedUnix: createTime,
  458. })
  459. if err != nil {
  460. log.Error("CreateCloudbrain(%s) failed:%v", req.JobName, err.Error())
  461. return err
  462. }
  463. //将训练任务的上一版本的isLatestVersion设置为"0"
  464. err = models.SetVersionCountAndLatestVersion(strconv.FormatInt(jobResult.JobID, 10), VersionTaskList[0].VersionName, VersionCount, NotLatestVersion, TotalVersionCount)
  465. if err != nil {
  466. ctx.ServerError("Update IsLatestVersion failed", err)
  467. return err
  468. }
  469. return err
  470. }
  471. func TransTrainJobStatus(status int) string {
  472. switch status {
  473. case 0:
  474. return "UNKNOWN"
  475. case 1:
  476. return "INIT"
  477. case 2:
  478. return "IMAGE_CREATING"
  479. case 3:
  480. return "IMAGE_FAILED"
  481. case 4:
  482. return "SUBMIT_TRYING"
  483. case 5:
  484. return "SUBMIT_FAILED"
  485. case 6:
  486. return "DELETE_FAILED"
  487. case 7:
  488. return "WAITING"
  489. case 8:
  490. return "RUNNING"
  491. case 9:
  492. return "KILLING"
  493. case 10:
  494. return "COMPLETED"
  495. case 11:
  496. return "FAILED"
  497. case 12:
  498. return "KILLED"
  499. case 13:
  500. return "CANCELED"
  501. case 14:
  502. return "LOST"
  503. case 15:
  504. return "SCALING"
  505. case 16:
  506. return "SUBMIT_MODEL_FAILED"
  507. case 17:
  508. return "DEPLOY_SERVICE_FAILED"
  509. case 18:
  510. return "CHECK_INIT"
  511. case 19:
  512. return "CHECK_RUNNING"
  513. case 20:
  514. return "CHECK_RUNNING_COMPLETED"
  515. case 21:
  516. return "CHECK_FAILED"
  517. default:
  518. return strconv.Itoa(status)
  519. }
  520. }
  521. func GetOutputPathByCount(TotalVersionCount int) (VersionOutputPath string) {
  522. talVersionCountToString := fmt.Sprintf("%04d", TotalVersionCount)
  523. VersionOutputPath = "V" + talVersionCountToString
  524. return VersionOutputPath
  525. }
  526. func GenerateInferenceJob(ctx *context.Context, req *GenerateInferenceJobReq) (err error) {
  527. createTime := timeutil.TimeStampNow()
  528. jobResult, err := createInferenceJob(models.CreateInferenceJobParams{
  529. JobName: req.JobName,
  530. Description: req.Description,
  531. InfConfig: models.InfConfig{
  532. WorkServerNum: req.WorkServerNumber,
  533. AppUrl: req.CodeObsPath,
  534. BootFileUrl: req.BootFileUrl,
  535. DataUrl: req.DataUrl,
  536. EngineID: req.EngineID,
  537. // TrainUrl: req.TrainUrl,
  538. LogUrl: req.LogUrl,
  539. PoolID: req.PoolID,
  540. CreateVersion: true,
  541. Flavor: models.Flavor{
  542. Code: req.FlavorCode,
  543. },
  544. Parameter: req.Parameters,
  545. },
  546. })
  547. if err != nil {
  548. log.Error("CreateJob failed: %v", err.Error())
  549. return err
  550. }
  551. attach, err := models.GetAttachmentByUUID(req.Uuid)
  552. if err != nil {
  553. log.Error("GetAttachmentByUUID(%s) failed:%v", strconv.FormatInt(jobResult.JobID, 10), err.Error())
  554. return err
  555. }
  556. jobID := strconv.FormatInt(jobResult.JobID, 10)
  557. err = models.CreateCloudbrain(&models.Cloudbrain{
  558. Status: TransTrainJobStatus(jobResult.Status),
  559. UserID: ctx.User.ID,
  560. RepoID: ctx.Repo.Repository.ID,
  561. JobID: jobID,
  562. JobName: req.JobName,
  563. DisplayJobName: req.DisplayJobName,
  564. JobType: string(models.JobTypeInference),
  565. Type: models.TypeCloudBrainTwo,
  566. VersionID: jobResult.VersionID,
  567. VersionName: jobResult.VersionName,
  568. Uuid: req.Uuid,
  569. DatasetName: attach.Name,
  570. CommitID: req.CommitID,
  571. EngineID: req.EngineID,
  572. TrainUrl: req.TrainUrl,
  573. BranchName: req.BranchName,
  574. Parameters: req.Params,
  575. BootFile: req.BootFile,
  576. DataUrl: req.DataUrl,
  577. LogUrl: req.LogUrl,
  578. FlavorCode: req.FlavorCode,
  579. Description: req.Description,
  580. WorkServerNumber: req.WorkServerNumber,
  581. FlavorName: req.FlavorName,
  582. EngineName: req.EngineName,
  583. LabelName: req.LabelName,
  584. IsLatestVersion: req.IsLatestVersion,
  585. ComputeResource: models.NPUResource,
  586. VersionCount: req.VersionCount,
  587. TotalVersionCount: req.TotalVersionCount,
  588. ModelName: req.ModelName,
  589. ModelVersion: req.ModelVersion,
  590. CkptName: req.CkptName,
  591. ResultUrl: req.ResultUrl,
  592. CreatedUnix: createTime,
  593. UpdatedUnix: createTime,
  594. })
  595. if err != nil {
  596. log.Error("CreateCloudbrain(%s) failed:%v", req.JobName, err.Error())
  597. return err
  598. }
  599. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, jobID, req.DisplayJobName, models.ActionCreateInferenceTask)
  600. return nil
  601. }
  602. func GetNotebookImageName(imageId string) (string, error) {
  603. var validImage = false
  604. var imageName = ""
  605. if ImageInfos == nil {
  606. json.Unmarshal([]byte(setting.ImageInfos), &ImageInfos)
  607. }
  608. for _, imageInfo := range ImageInfos.ImageInfo {
  609. if imageInfo.Id == imageId {
  610. validImage = true
  611. imageName = imageInfo.Value
  612. }
  613. }
  614. if !validImage {
  615. log.Error("the image id(%s) is invalid", imageId)
  616. return imageName, errors.New("the image id is invalid")
  617. }
  618. return imageName, nil
  619. }