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 36 kB

4 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
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
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
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
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
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
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
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
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
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
3 years ago
4 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
3 years ago
4 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
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
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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. package modelarts
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "path"
  7. "strconv"
  8. "strings"
  9. "code.gitea.io/gitea/modules/modelarts_cd"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/notification"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/storage"
  16. "code.gitea.io/gitea/modules/timeutil"
  17. )
  18. const (
  19. //notebook
  20. storageTypeOBS = "obs"
  21. autoStopDuration = 4 * 60 * 60
  22. autoStopDurationMs = 4 * 60 * 60 * 1000
  23. MORDELART_USER_IMAGE_ENGINE_ID = -1
  24. DataSetMountPath = "/home/ma-user/work"
  25. NotebookEnv = "Python3"
  26. NotebookType = "Ascend"
  27. FlavorInfo = "Ascend: 1*Ascend 910 CPU: 24 核 96GiB (modelarts.kat1.xlarge)"
  28. //train-job
  29. // ResourcePools = "{\"resource_pool\":[{\"id\":\"pool1328035d\", \"value\":\"专属资源池\"}]}"
  30. // Engines = "{\"engine\":[{\"id\":1, \"value\":\"Ascend-Powered-Engine\"}]}"
  31. // EngineVersions = "{\"version\":[{\"id\":118,\"value\":\"MindSpore-1.0.0-c75-python3.7-euleros2.8-aarch64\"}," +
  32. // "{\"id\":119,\"value\":\"MindSpore-1.1.1-c76-python3.7-euleros2.8-aarch64\"}," +
  33. // "{\"id\":120,\"value\":\"MindSpore-1.1.1-c76-tr5-python3.7-euleros2.8-aarch64\"}," +
  34. // "{\"id\":117,\"value\":\"TF-1.15-c75-python3.7-euleros2.8-aarch64\"}" +
  35. // "]}"
  36. // TrainJobFlavorInfo = "{\"flavor\":[{\"code\":\"modelarts.bm.910.arm.public.2\",\"value\":\"Ascend : 2 * Ascend 910 CPU:48 核 512GiB\"}," +
  37. // "{\"code\":\"modelarts.bm.910.arm.public.8\",\"value\":\"Ascend : 8 * Ascend 910 CPU:192 核 2048GiB\"}," +
  38. // "{\"code\":\"modelarts.bm.910.arm.public.4\",\"value\":\"Ascend : 4 * Ascend 910 CPU:96 核 1024GiB\"}," +
  39. // "{\"code\":\"modelarts.bm.910.arm.public.1\",\"value\":\"Ascend : 1 * Ascend 910 CPU:24 核 256GiB\"}" +
  40. // "]}"
  41. CodePath = "/code/"
  42. OutputPath = "/output/"
  43. ResultPath = "/result/"
  44. LogPath = "/log/"
  45. JobPath = "/job/"
  46. OrderDesc = "desc" //向下查询
  47. OrderAsc = "asc" //向上查询
  48. Lines = 500
  49. TrainUrl = "train_url"
  50. DataUrl = "data_url"
  51. MultiDataUrl = "multi_data_url"
  52. ResultUrl = "result_url"
  53. CkptUrl = "ckpt_url"
  54. DeviceTarget = "device_target"
  55. Ascend = "Ascend"
  56. PerPage = 10
  57. IsLatestVersion = "1"
  58. NotLatestVersion = "0"
  59. VersionCountOne = 1
  60. SortByCreateTime = "create_time"
  61. ConfigTypeCustom = "custom"
  62. TotalVersionCount = 1
  63. )
  64. var (
  65. poolInfos *models.PoolInfos
  66. TrainFlavorInfos *Flavor
  67. SpecialPools *models.SpecialPools
  68. MultiNodeConfig *MultiNodes
  69. )
  70. type GenerateTrainJobReq struct {
  71. JobName string
  72. DisplayJobName string
  73. Uuid string
  74. Description string
  75. CodeObsPath string
  76. BootFile string
  77. BootFileUrl string
  78. DataUrl string
  79. TrainUrl string
  80. LogUrl string
  81. PoolID string
  82. WorkServerNumber int
  83. EngineID int64
  84. Parameters []models.Parameter
  85. CommitID string
  86. IsLatestVersion string
  87. Params string
  88. BranchName string
  89. PreVersionId int64
  90. PreVersionName string
  91. FlavorCode string
  92. FlavorName string
  93. VersionCount int
  94. EngineName string
  95. TotalVersionCount int
  96. UserImageUrl string
  97. UserCommand string
  98. DatasetName string
  99. Spec *models.Specification
  100. ModelName string
  101. LabelName string
  102. CkptName string
  103. ModelVersion string
  104. PreTrainModelUrl string
  105. }
  106. type GenerateInferenceJobReq struct {
  107. JobName string
  108. DisplayJobName string
  109. Uuid string
  110. Description string
  111. CodeObsPath string
  112. BootFile string
  113. BootFileUrl string
  114. DataUrl string
  115. TrainUrl string
  116. LogUrl string
  117. PoolID string
  118. WorkServerNumber int
  119. EngineID int64
  120. Parameters []models.Parameter
  121. CommitID string
  122. Params string
  123. BranchName string
  124. FlavorName string
  125. EngineName string
  126. LabelName string
  127. IsLatestVersion string
  128. VersionCount int
  129. TotalVersionCount int
  130. ModelName string
  131. ModelVersion string
  132. CkptName string
  133. ResultUrl string
  134. Spec *models.Specification
  135. DatasetName string
  136. JobType string
  137. }
  138. type VersionInfo struct {
  139. Version []struct {
  140. ID int `json:"id"`
  141. Value string `json:"value"`
  142. Url string `json:"url"`
  143. } `json:"version"`
  144. }
  145. type Flavor struct {
  146. Info []struct {
  147. Code string `json:"code"`
  148. Value string `json:"value"`
  149. UnitPrice int64 `json:"unitPrice"`
  150. } `json:"flavor"`
  151. }
  152. type Engine struct {
  153. Info []struct {
  154. ID int `json:"id"`
  155. Value string `json:"value"`
  156. } `json:"engine"`
  157. }
  158. type ResourcePool struct {
  159. Info []struct {
  160. ID string `json:"id"`
  161. Value string `json:"value"`
  162. } `json:"resource_pool"`
  163. }
  164. type MultiNodes struct {
  165. Info []OrgMultiNode `json:"multinode"`
  166. }
  167. type OrgMultiNode struct {
  168. Org string `json:"org"`
  169. Node []int `json:"node"`
  170. }
  171. // type Parameter struct {
  172. // Label string `json:"label"`
  173. // Value string `json:"value"`
  174. // }
  175. // type Parameters struct {
  176. // Parameter []Parameter `json:"parameter"`
  177. // }
  178. type Parameters struct {
  179. Parameter []struct {
  180. Label string `json:"label"`
  181. Value string `json:"value"`
  182. } `json:"parameter"`
  183. }
  184. func GenerateTask(ctx *context.Context, jobName, uuid, description, flavor string) error {
  185. var dataActualPath string
  186. if uuid != "" {
  187. dataActualPath = setting.Bucket + "/" + setting.BasePath + path.Join(uuid[0:1], uuid[1:2]) + "/" + uuid + "/"
  188. } else {
  189. userPath := setting.UserBasePath + ctx.User.Name + "/"
  190. isExist, err := storage.ObsHasObject(userPath)
  191. if err != nil {
  192. log.Error("ObsHasObject failed:%v", err.Error(), ctx.Data["MsgID"])
  193. return err
  194. }
  195. if !isExist {
  196. if err = storage.ObsCreateObject(userPath); err != nil {
  197. log.Error("ObsCreateObject failed:%v", err.Error(), ctx.Data["MsgID"])
  198. return err
  199. }
  200. }
  201. dataActualPath = setting.Bucket + "/" + userPath
  202. }
  203. if poolInfos == nil {
  204. json.Unmarshal([]byte(setting.PoolInfos), &poolInfos)
  205. }
  206. createTime := timeutil.TimeStampNow()
  207. jobResult, err := CreateJob(models.CreateNotebookParams{
  208. JobName: jobName,
  209. Description: description,
  210. ProfileID: setting.ProfileID,
  211. Flavor: flavor,
  212. Pool: models.Pool{
  213. ID: poolInfos.PoolInfo[0].PoolId,
  214. Name: poolInfos.PoolInfo[0].PoolName,
  215. Type: poolInfos.PoolInfo[0].PoolType,
  216. },
  217. Spec: models.Spec{
  218. Storage: models.Storage{
  219. Type: storageTypeOBS,
  220. Location: models.Location{
  221. Path: dataActualPath,
  222. },
  223. },
  224. AutoStop: models.AutoStop{
  225. Enable: true,
  226. Duration: autoStopDuration,
  227. },
  228. },
  229. })
  230. if err != nil {
  231. log.Error("CreateJob failed: %v", err.Error())
  232. return err
  233. }
  234. err = models.CreateCloudbrain(&models.Cloudbrain{
  235. Status: string(models.JobWaiting),
  236. UserID: ctx.User.ID,
  237. RepoID: ctx.Repo.Repository.ID,
  238. JobID: jobResult.ID,
  239. JobName: jobName,
  240. JobType: string(models.JobTypeDebug),
  241. Type: models.TypeCloudBrainTwo,
  242. Uuid: uuid,
  243. ComputeResource: models.NPUResource,
  244. CreatedUnix: createTime,
  245. UpdatedUnix: createTime,
  246. })
  247. if err != nil {
  248. return err
  249. }
  250. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, jobResult.ID, jobName, models.ActionCreateDebugNPUTask)
  251. return nil
  252. }
  253. func GenerateNotebook2(ctx *context.Context, displayJobName, jobName, uuid, description, imageId string, spec *models.Specification) error {
  254. if poolInfos == nil {
  255. json.Unmarshal([]byte(setting.PoolInfos), &poolInfos)
  256. }
  257. imageName, err := GetNotebookImageName(imageId)
  258. if err != nil {
  259. log.Error("GetNotebookImageName failed: %v", err.Error())
  260. return err
  261. }
  262. createTime := timeutil.TimeStampNow()
  263. jobResult, err := createNotebook2(models.CreateNotebook2Params{
  264. JobName: jobName,
  265. Description: description,
  266. Flavor: spec.SourceSpecId,
  267. Duration: autoStopDurationMs,
  268. ImageID: imageId,
  269. PoolID: poolInfos.PoolInfo[0].PoolId,
  270. Feature: models.NotebookFeature,
  271. Volume: models.VolumeReq{
  272. Capacity: setting.Capacity,
  273. Category: models.EVSCategory,
  274. Ownership: models.ManagedOwnership,
  275. },
  276. WorkspaceID: "0",
  277. })
  278. if err != nil {
  279. log.Error("createNotebook2 failed: %v", err.Error())
  280. if strings.HasPrefix(err.Error(), UnknownErrorPrefix) {
  281. log.Info("(%s)unknown error, set temp status", displayJobName)
  282. errTemp := models.InsertCloudbrainTemp(&models.CloudbrainTemp{
  283. JobID: models.TempJobId,
  284. VersionID: models.TempVersionId,
  285. Status: models.TempJobStatus,
  286. Type: models.TypeCloudBrainTwo,
  287. JobName: jobName,
  288. JobType: string(models.JobTypeDebug),
  289. })
  290. if errTemp != nil {
  291. log.Error("InsertCloudbrainTemp failed: %v", errTemp.Error())
  292. return errTemp
  293. }
  294. }
  295. return err
  296. }
  297. task := &models.Cloudbrain{
  298. Status: jobResult.Status,
  299. UserID: ctx.User.ID,
  300. RepoID: ctx.Repo.Repository.ID,
  301. JobID: jobResult.ID,
  302. JobName: jobName,
  303. FlavorCode: spec.SourceSpecId,
  304. DisplayJobName: displayJobName,
  305. JobType: string(models.JobTypeDebug),
  306. Type: models.TypeCloudBrainTwo,
  307. Uuid: uuid,
  308. ComputeResource: models.NPUResource,
  309. Image: imageName,
  310. Description: description,
  311. CreatedUnix: createTime,
  312. UpdatedUnix: createTime,
  313. Spec: spec,
  314. }
  315. err = models.CreateCloudbrain(task)
  316. if err != nil {
  317. return err
  318. }
  319. stringId := strconv.FormatInt(task.ID, 10)
  320. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, displayJobName, models.ActionCreateDebugNPUTask)
  321. return nil
  322. }
  323. func GenerateTrainJob(ctx *context.Context, req *GenerateTrainJobReq) (err error) {
  324. createTime := timeutil.TimeStampNow()
  325. var jobResult *models.CreateTrainJobResult
  326. var createErr error
  327. if req.EngineID < 0 {
  328. jobResult, createErr = createTrainJobUserImage(models.CreateUserImageTrainJobParams{
  329. JobName: req.JobName,
  330. Description: req.Description,
  331. Config: models.UserImageConfig{
  332. WorkServerNum: req.WorkServerNumber,
  333. AppUrl: req.CodeObsPath,
  334. BootFileUrl: req.BootFileUrl,
  335. DataUrl: req.DataUrl,
  336. TrainUrl: req.TrainUrl,
  337. LogUrl: req.LogUrl,
  338. PoolID: req.PoolID,
  339. CreateVersion: true,
  340. Flavor: models.Flavor{
  341. Code: req.Spec.SourceSpecId,
  342. },
  343. Parameter: req.Parameters,
  344. UserImageUrl: req.UserImageUrl,
  345. UserCommand: req.UserCommand,
  346. },
  347. })
  348. } else {
  349. jobResult, createErr = createTrainJob(models.CreateTrainJobParams{
  350. JobName: req.JobName,
  351. Description: req.Description,
  352. Config: models.Config{
  353. WorkServerNum: req.WorkServerNumber,
  354. AppUrl: req.CodeObsPath,
  355. BootFileUrl: req.BootFileUrl,
  356. DataUrl: req.DataUrl,
  357. EngineID: req.EngineID,
  358. TrainUrl: req.TrainUrl,
  359. LogUrl: req.LogUrl,
  360. PoolID: req.PoolID,
  361. CreateVersion: true,
  362. Flavor: models.Flavor{
  363. Code: req.Spec.SourceSpecId,
  364. },
  365. Parameter: req.Parameters,
  366. },
  367. })
  368. }
  369. if createErr != nil {
  370. log.Error("createTrainJob failed: %v", createErr.Error())
  371. if strings.HasPrefix(createErr.Error(), UnknownErrorPrefix) {
  372. log.Info("(%s)unknown error, set temp status", req.DisplayJobName)
  373. errTemp := models.InsertCloudbrainTemp(&models.CloudbrainTemp{
  374. JobID: models.TempJobId,
  375. VersionID: models.TempVersionId,
  376. Status: models.TempJobStatus,
  377. Type: models.TypeCloudBrainTwo,
  378. JobName: req.JobName,
  379. JobType: string(models.JobTypeTrain),
  380. })
  381. if errTemp != nil {
  382. log.Error("InsertCloudbrainTemp failed: %v", errTemp.Error())
  383. return errTemp
  384. }
  385. }
  386. return createErr
  387. }
  388. jobId := strconv.FormatInt(jobResult.JobID, 10)
  389. createErr = models.CreateCloudbrain(&models.Cloudbrain{
  390. Status: TransTrainJobStatus(jobResult.Status),
  391. UserID: ctx.User.ID,
  392. RepoID: ctx.Repo.Repository.ID,
  393. JobID: jobId,
  394. JobName: req.JobName,
  395. DisplayJobName: req.DisplayJobName,
  396. JobType: string(models.JobTypeTrain),
  397. Type: models.TypeCloudBrainTwo,
  398. VersionID: jobResult.VersionID,
  399. VersionName: jobResult.VersionName,
  400. Uuid: req.Uuid,
  401. DatasetName: req.DatasetName,
  402. CommitID: req.CommitID,
  403. IsLatestVersion: req.IsLatestVersion,
  404. ComputeResource: models.NPUResource,
  405. EngineID: req.EngineID,
  406. TrainUrl: req.TrainUrl,
  407. BranchName: req.BranchName,
  408. Parameters: req.Params,
  409. BootFile: req.BootFile,
  410. DataUrl: req.DataUrl,
  411. LogUrl: req.LogUrl,
  412. FlavorCode: req.Spec.SourceSpecId,
  413. Description: req.Description,
  414. WorkServerNumber: req.WorkServerNumber,
  415. FlavorName: req.FlavorName,
  416. EngineName: req.EngineName,
  417. VersionCount: req.VersionCount,
  418. TotalVersionCount: req.TotalVersionCount,
  419. CreatedUnix: createTime,
  420. UpdatedUnix: createTime,
  421. Spec: req.Spec,
  422. ModelName: req.ModelName,
  423. ModelVersion: req.ModelVersion,
  424. LabelName: req.LabelName,
  425. PreTrainModelUrl: req.PreTrainModelUrl,
  426. CkptName: req.CkptName,
  427. })
  428. if createErr != nil {
  429. log.Error("CreateCloudbrain(%s) failed:%v", req.DisplayJobName, createErr.Error())
  430. return createErr
  431. }
  432. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, jobId, req.DisplayJobName, models.ActionCreateTrainTask)
  433. return nil
  434. }
  435. func GenerateModelConvertTrainJob(req *GenerateTrainJobReq) (*models.CreateTrainJobResult, error) {
  436. return createTrainJobUserImage(models.CreateUserImageTrainJobParams{
  437. JobName: req.JobName,
  438. Description: req.Description,
  439. Config: models.UserImageConfig{
  440. WorkServerNum: req.WorkServerNumber,
  441. AppUrl: req.CodeObsPath,
  442. BootFileUrl: req.BootFileUrl,
  443. DataUrl: req.DataUrl,
  444. TrainUrl: req.TrainUrl,
  445. LogUrl: req.LogUrl,
  446. PoolID: req.PoolID,
  447. CreateVersion: true,
  448. Flavor: models.Flavor{
  449. Code: req.FlavorCode,
  450. },
  451. Parameter: req.Parameters,
  452. UserImageUrl: req.UserImageUrl,
  453. UserCommand: req.UserCommand,
  454. },
  455. })
  456. }
  457. func GenerateTrainJobVersion(ctx *context.Context, req *GenerateTrainJobReq, jobId string) (err error) {
  458. createTime := timeutil.TimeStampNow()
  459. var jobResult *models.CreateTrainJobResult
  460. var createErr error
  461. if req.EngineID < 0 {
  462. jobResult, createErr = createTrainJobVersionUserImage(models.CreateTrainJobVersionUserImageParams{
  463. Description: req.Description,
  464. Config: models.TrainJobVersionUserImageConfig{
  465. WorkServerNum: req.WorkServerNumber,
  466. AppUrl: req.CodeObsPath,
  467. BootFileUrl: req.BootFileUrl,
  468. DataUrl: req.DataUrl,
  469. TrainUrl: req.TrainUrl,
  470. LogUrl: req.LogUrl,
  471. PoolID: req.PoolID,
  472. Flavor: models.Flavor{
  473. Code: req.Spec.SourceSpecId,
  474. },
  475. Parameter: req.Parameters,
  476. PreVersionId: req.PreVersionId,
  477. UserImageUrl: req.UserImageUrl,
  478. UserCommand: req.UserCommand,
  479. },
  480. }, jobId)
  481. } else {
  482. jobResult, createErr = createTrainJobVersion(models.CreateTrainJobVersionParams{
  483. Description: req.Description,
  484. Config: models.TrainJobVersionConfig{
  485. WorkServerNum: req.WorkServerNumber,
  486. AppUrl: req.CodeObsPath,
  487. BootFileUrl: req.BootFileUrl,
  488. DataUrl: req.DataUrl,
  489. EngineID: req.EngineID,
  490. TrainUrl: req.TrainUrl,
  491. LogUrl: req.LogUrl,
  492. PoolID: req.PoolID,
  493. Flavor: models.Flavor{
  494. Code: req.Spec.SourceSpecId,
  495. },
  496. Parameter: req.Parameters,
  497. PreVersionId: req.PreVersionId,
  498. },
  499. }, jobId)
  500. }
  501. if createErr != nil {
  502. log.Error("createTrainJobVersion failed: %v", createErr.Error())
  503. if strings.HasPrefix(createErr.Error(), UnknownErrorPrefix) {
  504. log.Info("(%s)unknown error, set temp status", req.DisplayJobName)
  505. errTemp := models.InsertCloudbrainTemp(&models.CloudbrainTemp{
  506. JobID: jobId,
  507. VersionID: models.TempVersionId,
  508. Status: models.TempJobStatus,
  509. Type: models.TypeCloudBrainTwo,
  510. JobName: req.JobName,
  511. JobType: string(models.JobTypeTrain),
  512. })
  513. if errTemp != nil {
  514. log.Error("InsertCloudbrainTemp failed: %v", errTemp.Error())
  515. return errTemp
  516. }
  517. }
  518. return createErr
  519. }
  520. var jobTypes []string
  521. jobTypes = append(jobTypes, string(models.JobTypeTrain))
  522. repo := ctx.Repo.Repository
  523. VersionTaskList, VersionListCount, createErr := models.CloudbrainsVersionList(&models.CloudbrainsOptions{
  524. RepoID: repo.ID,
  525. Type: models.TypeCloudBrainTwo,
  526. JobTypes: jobTypes,
  527. JobID: strconv.FormatInt(jobResult.JobID, 10),
  528. })
  529. if createErr != nil {
  530. ctx.ServerError("Cloudbrain", createErr)
  531. return createErr
  532. }
  533. //将当前版本的isLatestVersion设置为"1"和任务数量更新,任务数量包括当前版本数VersionCount和历史创建的总版本数TotalVersionCount
  534. createErr = models.CreateCloudbrain(&models.Cloudbrain{
  535. Status: TransTrainJobStatus(jobResult.Status),
  536. UserID: ctx.User.ID,
  537. RepoID: ctx.Repo.Repository.ID,
  538. JobID: strconv.FormatInt(jobResult.JobID, 10),
  539. JobName: req.JobName,
  540. DisplayJobName: req.DisplayJobName,
  541. JobType: string(models.JobTypeTrain),
  542. Type: models.TypeCloudBrainTwo,
  543. VersionID: jobResult.VersionID,
  544. VersionName: jobResult.VersionName,
  545. Uuid: req.Uuid,
  546. DatasetName: req.DatasetName,
  547. CommitID: req.CommitID,
  548. IsLatestVersion: req.IsLatestVersion,
  549. PreVersionName: req.PreVersionName,
  550. ComputeResource: models.NPUResource,
  551. EngineID: req.EngineID,
  552. TrainUrl: req.TrainUrl,
  553. BranchName: req.BranchName,
  554. Parameters: req.Params,
  555. BootFile: req.BootFile,
  556. DataUrl: req.DataUrl,
  557. LogUrl: req.LogUrl,
  558. PreVersionId: req.PreVersionId,
  559. FlavorCode: req.Spec.SourceSpecId,
  560. Description: req.Description,
  561. WorkServerNumber: req.WorkServerNumber,
  562. FlavorName: req.FlavorName,
  563. EngineName: req.EngineName,
  564. TotalVersionCount: VersionTaskList[0].TotalVersionCount + 1,
  565. VersionCount: VersionListCount + 1,
  566. CreatedUnix: createTime,
  567. UpdatedUnix: createTime,
  568. Spec: req.Spec,
  569. ModelName: req.ModelName,
  570. ModelVersion: req.ModelVersion,
  571. LabelName: req.LabelName,
  572. PreTrainModelUrl: req.PreTrainModelUrl,
  573. CkptName: req.CkptName,
  574. })
  575. if createErr != nil {
  576. log.Error("CreateCloudbrain(%s) failed:%v", req.JobName, createErr.Error())
  577. return createErr
  578. }
  579. //将训练任务的上一版本的isLatestVersion设置为"0"
  580. createErr = models.SetVersionCountAndLatestVersion(strconv.FormatInt(jobResult.JobID, 10), VersionTaskList[0].VersionName, VersionCountOne, NotLatestVersion, TotalVersionCount)
  581. if createErr != nil {
  582. ctx.ServerError("Update IsLatestVersion failed", createErr)
  583. return createErr
  584. }
  585. return createErr
  586. }
  587. func TransTrainJobStatus(status int) string {
  588. switch status {
  589. case 0:
  590. return "UNKNOWN"
  591. case 1:
  592. return "INIT"
  593. case 2:
  594. return "IMAGE_CREATING"
  595. case 3:
  596. return "IMAGE_FAILED"
  597. case 4:
  598. return "SUBMIT_TRYING"
  599. case 5:
  600. return "SUBMIT_FAILED"
  601. case 6:
  602. return "DELETE_FAILED"
  603. case 7:
  604. return "WAITING"
  605. case 8:
  606. return "RUNNING"
  607. case 9:
  608. return "KILLING"
  609. case 10:
  610. return "COMPLETED"
  611. case 11:
  612. return "FAILED"
  613. case 12:
  614. return "KILLED"
  615. case 13:
  616. return "CANCELED"
  617. case 14:
  618. return "LOST"
  619. case 15:
  620. return "SCALING"
  621. case 16:
  622. return "SUBMIT_MODEL_FAILED"
  623. case 17:
  624. return "DEPLOY_SERVICE_FAILED"
  625. case 18:
  626. return "CHECK_INIT"
  627. case 19:
  628. return "CHECK_RUNNING"
  629. case 20:
  630. return "CHECK_RUNNING_COMPLETED"
  631. case 21:
  632. return "CHECK_FAILED"
  633. default:
  634. return strconv.Itoa(status)
  635. }
  636. }
  637. func GetOutputPathByCount(TotalVersionCount int) (VersionOutputPath string) {
  638. talVersionCountToString := fmt.Sprintf("%04d", TotalVersionCount)
  639. VersionOutputPath = "V" + talVersionCountToString
  640. return VersionOutputPath
  641. }
  642. func GenerateInferenceJob(ctx *context.Context, req *GenerateInferenceJobReq) (err error) {
  643. createTime := timeutil.TimeStampNow()
  644. jobResult, err := createInferenceJob(models.CreateInferenceJobParams{
  645. JobName: req.JobName,
  646. Description: req.Description,
  647. InfConfig: models.InfConfig{
  648. WorkServerNum: req.WorkServerNumber,
  649. AppUrl: req.CodeObsPath,
  650. BootFileUrl: req.BootFileUrl,
  651. DataUrl: req.DataUrl,
  652. EngineID: req.EngineID,
  653. // TrainUrl: req.TrainUrl,
  654. LogUrl: req.LogUrl,
  655. PoolID: req.PoolID,
  656. CreateVersion: true,
  657. Flavor: models.Flavor{
  658. Code: req.Spec.SourceSpecId,
  659. },
  660. Parameter: req.Parameters,
  661. },
  662. })
  663. if err != nil {
  664. log.Error("createInferenceJob failed: %v", err.Error())
  665. if strings.HasPrefix(err.Error(), UnknownErrorPrefix) {
  666. log.Info("(%s)unknown error, set temp status", req.DisplayJobName)
  667. err = models.InsertCloudbrainTemp(&models.CloudbrainTemp{
  668. JobID: models.TempJobId,
  669. VersionID: models.TempVersionId,
  670. Status: models.TempJobStatus,
  671. Type: models.TypeCloudBrainTwo,
  672. JobName: req.JobName,
  673. JobType: req.JobType,
  674. })
  675. if err != nil {
  676. log.Error("InsertCloudbrainTemp failed: %v", err.Error())
  677. return err
  678. }
  679. }
  680. return err
  681. }
  682. // attach, err := models.GetAttachmentByUUID(req.Uuid)
  683. // if err != nil {
  684. // log.Error("GetAttachmentByUUID(%s) failed:%v", strconv.FormatInt(jobResult.JobID, 10), err.Error())
  685. // return err
  686. // }
  687. jobID := strconv.FormatInt(jobResult.JobID, 10)
  688. err = models.CreateCloudbrain(&models.Cloudbrain{
  689. Status: TransTrainJobStatus(jobResult.Status),
  690. UserID: ctx.User.ID,
  691. RepoID: ctx.Repo.Repository.ID,
  692. JobID: jobID,
  693. JobName: req.JobName,
  694. DisplayJobName: req.DisplayJobName,
  695. JobType: req.JobType,
  696. Type: models.TypeCloudBrainTwo,
  697. VersionID: jobResult.VersionID,
  698. VersionName: jobResult.VersionName,
  699. Uuid: req.Uuid,
  700. DatasetName: req.DatasetName,
  701. CommitID: req.CommitID,
  702. EngineID: req.EngineID,
  703. TrainUrl: req.TrainUrl,
  704. BranchName: req.BranchName,
  705. Parameters: req.Params,
  706. BootFile: req.BootFile,
  707. DataUrl: req.DataUrl,
  708. LogUrl: req.LogUrl,
  709. FlavorCode: req.Spec.SourceSpecId,
  710. Description: req.Description,
  711. WorkServerNumber: req.WorkServerNumber,
  712. FlavorName: req.FlavorName,
  713. EngineName: req.EngineName,
  714. LabelName: req.LabelName,
  715. IsLatestVersion: req.IsLatestVersion,
  716. ComputeResource: models.NPUResource,
  717. VersionCount: req.VersionCount,
  718. TotalVersionCount: req.TotalVersionCount,
  719. ModelName: req.ModelName,
  720. ModelVersion: req.ModelVersion,
  721. CkptName: req.CkptName,
  722. ResultUrl: req.ResultUrl,
  723. CreatedUnix: createTime,
  724. UpdatedUnix: createTime,
  725. Spec: req.Spec,
  726. })
  727. if err != nil {
  728. log.Error("CreateCloudbrain(%s) failed:%v", req.JobName, err.Error())
  729. return err
  730. }
  731. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, jobID, req.DisplayJobName, models.ActionCreateInferenceTask)
  732. return nil
  733. }
  734. func GetNotebookImageName(imageId string) (string, error) {
  735. var validImage = false
  736. var imageName = ""
  737. for _, imageInfo := range setting.StImageInfos.ImageInfo {
  738. if imageInfo.Id == imageId {
  739. validImage = true
  740. imageName = imageInfo.Value
  741. }
  742. }
  743. if !validImage {
  744. log.Error("the image id(%s) is invalid", imageId)
  745. return imageName, errors.New("the image id is invalid")
  746. }
  747. return imageName, nil
  748. }
  749. func InitSpecialPool() {
  750. if SpecialPools == nil && setting.ModelArtsSpecialPools != "" {
  751. json.Unmarshal([]byte(setting.ModelArtsSpecialPools), &SpecialPools)
  752. }
  753. }
  754. func InitMultiNode() {
  755. if MultiNodeConfig == nil && setting.ModelArtsMultiNode != "" {
  756. json.Unmarshal([]byte(setting.ModelArtsMultiNode), &MultiNodeConfig)
  757. }
  758. }
  759. func HandleTrainJobInfo(task *models.Cloudbrain) error {
  760. result, err := GetTrainJob(task.JobID, strconv.FormatInt(task.VersionID, 10))
  761. if err != nil {
  762. log.Error("GetTrainJob(%s) failed:%v", task.DisplayJobName, err)
  763. return err
  764. }
  765. if result != nil {
  766. oldStatus := task.Status
  767. task.Status = TransTrainJobStatus(result.IntStatus)
  768. task.Duration = result.Duration / 1000
  769. task.TrainJobDuration = result.TrainJobDuration
  770. if task.StartTime == 0 && result.StartTime > 0 {
  771. task.StartTime = timeutil.TimeStamp(result.StartTime / 1000)
  772. }
  773. task.TrainJobDuration = models.ConvertDurationToStr(task.Duration)
  774. if task.EndTime == 0 && models.IsTrainJobTerminal(task.Status) && task.StartTime > 0 {
  775. task.EndTime = task.StartTime.Add(task.Duration)
  776. }
  777. task.CorrectCreateUnix()
  778. if oldStatus != task.Status {
  779. notification.NotifyChangeCloudbrainStatus(task, oldStatus)
  780. }
  781. err = models.UpdateJob(task)
  782. if err != nil {
  783. log.Error("UpdateJob(%s) failed:%v", task.JobName, err)
  784. return err
  785. }
  786. }
  787. return nil
  788. }
  789. func HandleNotebookInfo(task *models.Cloudbrain) error {
  790. var result *models.GetNotebook2Result
  791. var err error
  792. if task.Type == models.TypeCloudBrainTwo {
  793. result, err = GetNotebook2(task.JobID)
  794. } else if task.Type == models.TypeCDCenter {
  795. result, err = modelarts_cd.GetNotebook(task.JobID)
  796. }
  797. if err != nil {
  798. log.Error("GetNotebook2(%s) failed:%v", task.DisplayJobName, err)
  799. return err
  800. }
  801. if result != nil {
  802. oldStatus := task.Status
  803. task.Status = result.Status
  804. if task.StartTime == 0 && result.Lease.UpdateTime > 0 {
  805. task.StartTime = timeutil.TimeStamp(result.Lease.UpdateTime / 1000)
  806. }
  807. if task.EndTime == 0 && models.IsModelArtsDebugJobTerminal(task.Status) {
  808. task.EndTime = timeutil.TimeStampNow()
  809. }
  810. task.CorrectCreateUnix()
  811. task.ComputeAndSetDuration()
  812. if oldStatus != task.Status {
  813. notification.NotifyChangeCloudbrainStatus(task, oldStatus)
  814. }
  815. if task.FlavorCode == "" {
  816. task.FlavorCode = result.Flavor
  817. }
  818. err = models.UpdateJob(task)
  819. if err != nil {
  820. log.Error("UpdateJob(%s) failed:%v", task.DisplayJobName, err)
  821. return err
  822. }
  823. }
  824. return nil
  825. }
  826. func SyncTempStatusJob() {
  827. jobs, err := models.GetCloudBrainTempJobs()
  828. if err != nil {
  829. log.Error("GetCloudBrainTempJobs failed:%v", err.Error())
  830. return
  831. }
  832. for _, temp := range jobs {
  833. log.Info("start to handle record: %s", temp.JobName)
  834. if temp.Type == models.TypeCloudBrainTwo {
  835. if temp.JobType == string(models.JobTypeDebug) {
  836. err = handleNotebook(temp)
  837. if err != nil {
  838. log.Error("handleNotebook falied:%v", err)
  839. break
  840. }
  841. } else if temp.JobType == string(models.JobTypeTrain) || temp.JobType == string(models.JobTypeInference) {
  842. _, err = models.GetCloudbrainByJobID(temp.JobID)
  843. if err != nil {
  844. //one version
  845. err = handleTrainJob(temp)
  846. if err != nil {
  847. log.Error("handleTrainJob falied:%v", err)
  848. break
  849. }
  850. } else {
  851. //multi version
  852. err = handleTrainJobMultiVersion(temp)
  853. if err != nil {
  854. log.Error("handleTrainJobMultiVersion falied:%v", err)
  855. break
  856. }
  857. }
  858. }
  859. }
  860. }
  861. return
  862. }
  863. func handleNotebook(temp *models.CloudbrainTemp) error {
  864. if temp.Status == models.TempJobStatus {
  865. err := handleTempNotebook(temp)
  866. if err != nil {
  867. log.Error("handleTempNotebook failed:%v", err)
  868. return err
  869. }
  870. } else if temp.Status == string(models.ModelArtsStopping) {
  871. res, err := GetNotebook2(temp.JobID)
  872. if err != nil {
  873. log.Error("GetNotebook2 failed:%v", err)
  874. return err
  875. }
  876. temp.Status = res.Status
  877. if temp.Status == string(models.ModelArtsStopped) {
  878. err = models.UpdateCloudbrainTemp(temp)
  879. if err != nil {
  880. log.Error("UpdateCloudbrainTemp failed:%v", err)
  881. return err
  882. }
  883. _, err := DelNotebook2(temp.JobID)
  884. if err != nil {
  885. log.Error("DelNotebook2 failed:%v", err)
  886. return err
  887. }
  888. temp.Status = string(models.ModelArtsDeleted)
  889. err = models.UpdateCloudbrainTemp(temp)
  890. if err != nil {
  891. log.Error("UpdateCloudbrainTemp failed:%v", err)
  892. return err
  893. }
  894. }
  895. }
  896. return nil
  897. }
  898. func handleTempNotebook(temp *models.CloudbrainTemp) error {
  899. var err error
  900. var isExist bool
  901. for {
  902. result, err := GetNotebookList(1000, 0, "createTime", "DESC", temp.JobName)
  903. if err != nil {
  904. log.Error("GetNotebookList failed:%v", err)
  905. break
  906. }
  907. temp.QueryTimes++
  908. err = models.UpdateCloudbrainTemp(temp)
  909. if err != nil {
  910. log.Error("UpdateCloudbrainTemp failed:%v", err)
  911. }
  912. if result != nil {
  913. for _, notebook := range result.NotebookList {
  914. if temp.JobID == models.TempJobId {
  915. //new notebook
  916. if notebook.JobName == temp.JobName {
  917. isExist = true
  918. temp.Status = notebook.Status
  919. temp.JobID = notebook.JobID
  920. break
  921. }
  922. } else {
  923. //restart: always can find one record
  924. if notebook.JobName == temp.JobName {
  925. if notebook.Status != string(models.ModelArtsStopped) {
  926. isExist = true
  927. temp.Status = notebook.Status
  928. temp.JobID = notebook.JobID
  929. break
  930. }
  931. }
  932. }
  933. }
  934. if isExist {
  935. log.Info("find the record(%s), status(%s)", temp.JobName, temp.Status)
  936. if temp.Status == string(models.ModelArtsCreateFailed) {
  937. err = models.UpdateCloudbrainTemp(temp)
  938. if err != nil {
  939. log.Error("UpdateCloudbrainTemp failed:%v", err)
  940. break
  941. }
  942. _, err := DelNotebook2(temp.JobID)
  943. if err != nil {
  944. log.Error("DelNotebook2(%s) failed:%v", temp.JobName, err)
  945. break
  946. }
  947. temp.Status = string(models.ModelArtsDeleted)
  948. } else {
  949. _, err := ManageNotebook2(temp.JobID, models.NotebookAction{Action: models.ActionStop})
  950. if err != nil {
  951. log.Error("ManageNotebook2(%s) failed:%v", temp.JobName, err)
  952. break
  953. }
  954. temp.Status = string(models.ModelArtsStopping)
  955. }
  956. models.UpdateCloudbrainTemp(temp)
  957. } else {
  958. log.Error("can not find the record(%s) till now", temp.JobName)
  959. err = errors.New("not found")
  960. break
  961. }
  962. } else {
  963. log.Error("can not find the record(%s) till now", temp.JobName)
  964. err = errors.New("not found")
  965. break
  966. }
  967. break
  968. }
  969. if temp.QueryTimes >= setting.MaxTempQueryTimes && !isExist {
  970. log.Info("reach MaxTempQueryTimes, set the job failed")
  971. temp.Status = string(models.ModelArtsTrainJobFailed)
  972. err = models.UpdateCloudbrainTemp(temp)
  973. if err != nil {
  974. log.Error("UpdateCloudbrainTemp(%s) failed:%v", temp.JobName, err)
  975. return err
  976. }
  977. }
  978. return err
  979. }
  980. func handleTrainJob(temp *models.CloudbrainTemp) error {
  981. if temp.Status == models.TempJobStatus {
  982. err := handleTempTrainJob(temp)
  983. if err != nil {
  984. log.Error("handleTempTrainJob failed:%v", err)
  985. return err
  986. }
  987. } else if temp.Status == string(models.ModelArtsTrainJobKilling) {
  988. res, err := GetTrainJob(temp.JobID, temp.VersionID)
  989. if err != nil {
  990. log.Error("GetTrainJob failed:%v", err)
  991. return err
  992. }
  993. temp.Status = TransTrainJobStatus(res.IntStatus)
  994. if temp.Status == string(models.ModelArtsTrainJobKilled) {
  995. err = models.UpdateCloudbrainTemp(temp)
  996. if err != nil {
  997. log.Error("UpdateCloudbrainTemp failed:%v", err)
  998. return err
  999. }
  1000. _, err := DelTrainJob(temp.JobID)
  1001. if err != nil {
  1002. log.Error("DelTrainJob failed:%v", err)
  1003. return err
  1004. }
  1005. temp.Status = string(models.ModelArtsDeleted)
  1006. err = models.UpdateCloudbrainTemp(temp)
  1007. if err != nil {
  1008. log.Error("UpdateCloudbrainTemp failed:%v", err)
  1009. return err
  1010. }
  1011. }
  1012. }
  1013. return nil
  1014. }
  1015. func handleTrainJobMultiVersion(temp *models.CloudbrainTemp) error {
  1016. if temp.Status == models.TempJobStatus {
  1017. err := handleTempTrainJobMultiVersion(temp)
  1018. if err != nil {
  1019. log.Error("handleTempTrainJobMultiVersion failed:%v", err)
  1020. return err
  1021. }
  1022. } else if temp.Status == string(models.ModelArtsTrainJobKilling) {
  1023. res, err := GetTrainJob(temp.JobID, temp.VersionID)
  1024. if err != nil {
  1025. log.Error("GetTrainJob failed:%v", err)
  1026. return err
  1027. }
  1028. temp.Status = TransTrainJobStatus(res.IntStatus)
  1029. if temp.Status == string(models.ModelArtsTrainJobKilled) {
  1030. err = models.UpdateCloudbrainTemp(temp)
  1031. if err != nil {
  1032. log.Error("UpdateCloudbrainTemp failed:%v", err)
  1033. return err
  1034. }
  1035. _, err := DelTrainJobVersion(temp.JobID, temp.VersionID)
  1036. if err != nil {
  1037. log.Error("DelTrainJob failed:%v", err)
  1038. return err
  1039. }
  1040. temp.Status = string(models.ModelArtsDeleted)
  1041. err = models.UpdateCloudbrainTemp(temp)
  1042. if err != nil {
  1043. log.Error("UpdateCloudbrainTemp failed:%v", err)
  1044. return err
  1045. }
  1046. }
  1047. }
  1048. return nil
  1049. }
  1050. func handleTempTrainJobMultiVersion(temp *models.CloudbrainTemp) error {
  1051. var err error
  1052. var isExist bool
  1053. for {
  1054. result, err := GetTrainJobVersionList(1000, 1, temp.JobID)
  1055. if err != nil {
  1056. log.Error("GetTrainJobVersionList failed:%v", err)
  1057. break
  1058. }
  1059. temp.QueryTimes++
  1060. err = models.UpdateCloudbrainTemp(temp)
  1061. if err != nil {
  1062. log.Error("UpdateCloudbrainTemp failed:%v", err)
  1063. }
  1064. if result != nil {
  1065. count, _ := models.GetCloudbrainCountByJobName(temp.JobName, temp.JobType, temp.Type)
  1066. if result.VersionCount == int64(count+1) {
  1067. isExist = true
  1068. temp.Status = TransTrainJobStatus(result.JobVersionList[0].IntStatus)
  1069. temp.VersionID = strconv.FormatInt(result.JobVersionList[0].VersionID, 10)
  1070. log.Info("find the record(%s), status(%s)", temp.JobName, temp.Status)
  1071. _, err := StopTrainJob(temp.JobID, temp.VersionID)
  1072. if err != nil {
  1073. log.Error("StopTrainJob failed:%v", err)
  1074. break
  1075. }
  1076. temp.Status = string(models.ModelArtsTrainJobKilling)
  1077. err = models.UpdateCloudbrainTemp(temp)
  1078. if err != nil {
  1079. log.Error("UpdateCloudbrainTemp(%s) failed:%v", temp.JobName, err)
  1080. break
  1081. }
  1082. } else {
  1083. log.Error("can not find the record(%s) till now", temp.JobName)
  1084. err = errors.New("not found")
  1085. break
  1086. }
  1087. }
  1088. break
  1089. }
  1090. if temp.QueryTimes >= setting.MaxTempQueryTimes && !isExist {
  1091. log.Info("reach MaxTempQueryTimes, set the job failed")
  1092. temp.Status = string(models.ModelArtsTrainJobFailed)
  1093. err = models.UpdateCloudbrainTemp(temp)
  1094. if err != nil {
  1095. log.Error("UpdateCloudbrainTemp(%s) failed:%v", temp.JobName, err)
  1096. return err
  1097. }
  1098. }
  1099. return err
  1100. }
  1101. func handleTempTrainJob(temp *models.CloudbrainTemp) error {
  1102. var err error
  1103. var isExist bool
  1104. for {
  1105. result, err := GetTrainJobList(1000, 1, "create_time", "desc", temp.JobName)
  1106. if err != nil {
  1107. log.Error("GetTrainJobList failed:%v", err)
  1108. break
  1109. }
  1110. temp.QueryTimes++
  1111. err = models.UpdateCloudbrainTemp(temp)
  1112. if err != nil {
  1113. log.Error("UpdateCloudbrainTemp failed:%v", err)
  1114. }
  1115. if result != nil {
  1116. for _, job := range result.JobList {
  1117. if temp.JobName == job.JobName && TransTrainJobStatus(job.IntStatus) != string(models.ModelArtsTrainJobFailed) {
  1118. isExist = true
  1119. temp.Status = TransTrainJobStatus(job.IntStatus)
  1120. temp.JobID = strconv.FormatInt(job.JobID, 10)
  1121. temp.VersionID = strconv.FormatInt(job.VersionID, 10)
  1122. log.Info("find the record(%s), status(%s)", temp.JobName, temp.Status)
  1123. _, err = StopTrainJob(temp.JobID, temp.VersionID)
  1124. if err != nil {
  1125. log.Error("StopTrainJob(%s) failed:%v", temp.JobName, err)
  1126. break
  1127. }
  1128. temp.Status = string(models.ModelArtsTrainJobKilling)
  1129. err = models.UpdateCloudbrainTemp(temp)
  1130. if err != nil {
  1131. log.Error("UpdateCloudbrainTemp(%s) failed:%v", temp.JobName, err)
  1132. break
  1133. }
  1134. }
  1135. }
  1136. if !isExist {
  1137. log.Error("can not find the record(%s) till now", temp.JobName)
  1138. err = errors.New("not found")
  1139. break
  1140. }
  1141. }
  1142. break
  1143. }
  1144. if temp.QueryTimes >= setting.MaxTempQueryTimes && !isExist {
  1145. log.Info("reach MaxTempQueryTimes, set the job failed")
  1146. temp.Status = string(models.ModelArtsTrainJobFailed)
  1147. err = models.UpdateCloudbrainTemp(temp)
  1148. if err != nil {
  1149. log.Error("UpdateCloudbrainTemp(%s) failed:%v", temp.JobName, err)
  1150. return err
  1151. }
  1152. }
  1153. return err
  1154. }