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

4 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
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
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
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
4 years ago
3 years ago
4 years ago
4 years ago
3 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
4 years ago
4 years ago
4 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
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/cloudbrain"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/modelarts"
  15. "code.gitea.io/gitea/modules/storage"
  16. "code.gitea.io/gitea/modules/timeutil"
  17. routerRepo "code.gitea.io/gitea/routers/repo"
  18. )
  19. func GetModelArtsNotebook(ctx *context.APIContext) {
  20. var (
  21. err error
  22. )
  23. jobID := ctx.Params(":jobid")
  24. repoID := ctx.Repo.Repository.ID
  25. job, err := models.GetRepoCloudBrainByJobID(repoID, jobID)
  26. if err != nil {
  27. ctx.NotFound(err)
  28. return
  29. }
  30. result, err := modelarts.GetJob(jobID)
  31. if err != nil {
  32. ctx.NotFound(err)
  33. return
  34. }
  35. job.Status = result.Status
  36. err = models.UpdateJob(job)
  37. if err != nil {
  38. log.Error("UpdateJob failed:", err)
  39. }
  40. ctx.JSON(http.StatusOK, map[string]interface{}{
  41. "JobID": jobID,
  42. "JobStatus": result.Status,
  43. })
  44. }
  45. func GetModelArtsNotebook2(ctx *context.APIContext) {
  46. var (
  47. err error
  48. )
  49. ID := ctx.Params(":id")
  50. job, err := models.GetCloudbrainByID(ID)
  51. if err != nil {
  52. ctx.NotFound(err)
  53. return
  54. }
  55. result, err := modelarts.GetNotebook2(job.JobID)
  56. if err != nil {
  57. ctx.NotFound(err)
  58. return
  59. }
  60. if job.StartTime == 0 && result.Lease.UpdateTime > 0 {
  61. job.StartTime = timeutil.TimeStamp(result.Lease.UpdateTime / 1000)
  62. }
  63. job.Status = result.Status
  64. if job.EndTime == 0 && models.IsModelArtsDebugJobTerminal(job.Status) {
  65. job.EndTime = timeutil.TimeStampNow()
  66. }
  67. job.CorrectCreateUnix()
  68. job.ComputeAndSetDuration()
  69. err = models.UpdateJob(job)
  70. if err != nil {
  71. log.Error("UpdateJob failed:", err)
  72. }
  73. ctx.JSON(http.StatusOK, map[string]interface{}{
  74. "ID": ID,
  75. "JobName": job.JobName,
  76. "JobStatus": result.Status,
  77. })
  78. }
  79. func GetModelArtsTrainJob(ctx *context.APIContext) {
  80. var (
  81. err error
  82. )
  83. jobID := ctx.Params(":jobid")
  84. repoID := ctx.Repo.Repository.ID
  85. job, err := models.GetRepoCloudBrainByJobID(repoID, jobID)
  86. if err != nil {
  87. ctx.NotFound(err)
  88. return
  89. }
  90. result, err := modelarts.GetTrainJob(jobID, strconv.FormatInt(job.VersionID, 10))
  91. if err != nil {
  92. ctx.NotFound(err)
  93. return
  94. }
  95. job.Status = modelarts.TransTrainJobStatus(result.IntStatus)
  96. job.Duration = result.Duration
  97. job.TrainJobDuration = result.TrainJobDuration
  98. err = models.UpdateJob(job)
  99. if err != nil {
  100. log.Error("UpdateJob failed:", err)
  101. }
  102. ctx.JSON(http.StatusOK, map[string]interface{}{
  103. "JobID": jobID,
  104. "JobStatus": job.Status,
  105. "JobDuration": job.Duration,
  106. })
  107. }
  108. func GetModelArtsTrainJobVersion(ctx *context.APIContext) {
  109. var (
  110. err error
  111. )
  112. jobID := ctx.Params(":jobid")
  113. versionName := ctx.Query("version_name")
  114. job, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  115. if err != nil {
  116. ctx.NotFound(err)
  117. return
  118. }
  119. if job.Type == models.TypeCloudBrainOne {
  120. jobResult, err := cloudbrain.GetJob(job.JobID)
  121. if err != nil {
  122. ctx.NotFound(err)
  123. log.Error("GetJob failed:", err)
  124. return
  125. }
  126. result, err := models.ConvertToJobResultPayload(jobResult.Payload)
  127. if err != nil {
  128. ctx.NotFound(err)
  129. log.Error("ConvertToJobResultPayload failed:", err)
  130. return
  131. }
  132. job.Status = result.JobStatus.State
  133. if result.JobStatus.State != string(models.JobWaiting) && result.JobStatus.State != string(models.JobFailed) {
  134. taskRoles := result.TaskRoles
  135. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  136. job.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  137. job.ContainerID = taskRes.TaskStatuses[0].ContainerID
  138. job.Status = taskRes.TaskStatuses[0].State
  139. }
  140. if result.JobStatus.State != string(models.JobWaiting) {
  141. models.ParseAndSetDurationFromCloudBrainOne(result, job)
  142. err = models.UpdateJob(job)
  143. if err != nil {
  144. log.Error("UpdateJob failed:", err)
  145. }
  146. }
  147. } else {
  148. result, err := modelarts.GetTrainJob(jobID, strconv.FormatInt(job.VersionID, 10))
  149. if err != nil {
  150. ctx.NotFound(err)
  151. return
  152. }
  153. if job.StartTime == 0 && result.StartTime > 0 {
  154. job.StartTime = timeutil.TimeStamp(result.StartTime / 1000)
  155. }
  156. job.Status = modelarts.TransTrainJobStatus(result.IntStatus)
  157. job.Duration = result.Duration / 1000
  158. job.TrainJobDuration = models.ConvertDurationToStr(job.Duration)
  159. if job.EndTime == 0 && models.IsTrainJobTerminal(job.Status) && job.StartTime > 0 {
  160. job.EndTime = job.StartTime.Add(job.Duration)
  161. }
  162. job.CorrectCreateUnix()
  163. err = models.UpdateTrainJobVersion(job)
  164. if err != nil {
  165. log.Error("UpdateJob failed:", err)
  166. }
  167. }
  168. ctx.JSON(http.StatusOK, map[string]interface{}{
  169. "JobID": jobID,
  170. "JobStatus": job.Status,
  171. "JobDuration": job.TrainJobDuration,
  172. })
  173. }
  174. func TrainJobForModelConvertGetLog(ctx *context.APIContext) {
  175. var (
  176. err error
  177. )
  178. var jobID = ctx.Params(":id")
  179. var baseLine = ctx.Query("base_line")
  180. var order = ctx.Query("order")
  181. var lines = ctx.Query("lines")
  182. lines_int, err := strconv.Atoi(lines)
  183. if err != nil {
  184. log.Error("change lines(%d) string to int failed", lines_int)
  185. }
  186. if order != modelarts.OrderDesc && order != modelarts.OrderAsc {
  187. log.Error("order(%s) check failed", order)
  188. ctx.JSON(http.StatusBadRequest, map[string]interface{}{
  189. "err_msg": "order check failed",
  190. })
  191. return
  192. }
  193. resultLogFile, result, err := trainJobForModelConvertGetLogContent(jobID, baseLine, order, lines_int)
  194. if err != nil {
  195. log.Error("trainJobGetLog(%s) failed:%v", jobID, err.Error())
  196. // ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobShow, nil)
  197. return
  198. }
  199. ctx.Data["log_file_name"] = resultLogFile.LogFileList[0]
  200. ctx.JSON(http.StatusOK, map[string]interface{}{
  201. "JobID": jobID,
  202. "LogFileName": resultLogFile.LogFileList[0],
  203. "StartLine": result.StartLine,
  204. "EndLine": result.EndLine,
  205. "Content": result.Content,
  206. "Lines": result.Lines,
  207. })
  208. }
  209. func trainJobForModelConvertGetLogContent(jobID string, baseLine string, order string, lines int) (*models.GetTrainJobLogFileNamesResult, *models.GetTrainJobLogResult, error) {
  210. task, err := models.QueryModelConvertById(jobID)
  211. if err != nil {
  212. log.Error("GetCloudbrainByJobID(%s) failed:%v", jobID, err.Error())
  213. return nil, nil, err
  214. }
  215. resultLogFile, err := modelarts.GetTrainJobLogFileNames(task.CloudBrainTaskId, task.ModelArtsVersionId)
  216. if err != nil {
  217. log.Error("GetTrainJobLogFileNames(%s) failed:%v", task.CloudBrainTaskId, err.Error())
  218. return nil, nil, err
  219. }
  220. result, err := modelarts.GetTrainJobLog(task.CloudBrainTaskId, task.ModelArtsVersionId, baseLine, resultLogFile.LogFileList[0], order, lines)
  221. if err != nil {
  222. log.Error("GetTrainJobLog(%s) failed:%v", task.CloudBrainTaskId, err.Error())
  223. return nil, nil, err
  224. }
  225. return resultLogFile, result, err
  226. }
  227. func TrainJobGetLog(ctx *context.APIContext) {
  228. var (
  229. err error
  230. )
  231. var jobID = ctx.Params(":jobid")
  232. var versionName = ctx.Query("version_name")
  233. var baseLine = ctx.Query("base_line")
  234. var order = ctx.Query("order")
  235. var lines = ctx.Query("lines")
  236. lines_int, err := strconv.Atoi(lines)
  237. if err != nil {
  238. log.Error("change lines(%d) string to int failed", lines_int)
  239. }
  240. if order != modelarts.OrderDesc && order != modelarts.OrderAsc {
  241. log.Error("order(%s) check failed", order)
  242. ctx.JSON(http.StatusBadRequest, map[string]interface{}{
  243. "err_msg": "order check failed",
  244. })
  245. return
  246. }
  247. resultLogFile, result, err := trainJobGetLogContent(jobID, versionName, baseLine, order, lines_int)
  248. if err != nil {
  249. log.Error("trainJobGetLog(%s) failed:%v", jobID, err.Error())
  250. // ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobShow, nil)
  251. return
  252. }
  253. ctx.Data["log_file_name"] = resultLogFile.LogFileList[0]
  254. ctx.JSON(http.StatusOK, map[string]interface{}{
  255. "JobID": jobID,
  256. "LogFileName": resultLogFile.LogFileList[0],
  257. "StartLine": result.StartLine,
  258. "EndLine": result.EndLine,
  259. "Content": result.Content,
  260. "Lines": result.Lines,
  261. })
  262. }
  263. func trainJobGetLogContent(jobID string, versionName string, baseLine string, order string, lines int) (*models.GetTrainJobLogFileNamesResult, *models.GetTrainJobLogResult, error) {
  264. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  265. if err != nil {
  266. log.Error("GetCloudbrainByJobID(%s) failed:%v", jobID, err.Error())
  267. return nil, nil, err
  268. }
  269. resultLogFile, err := modelarts.GetTrainJobLogFileNames(jobID, strconv.FormatInt(task.VersionID, 10))
  270. if err != nil {
  271. log.Error("GetTrainJobLogFileNames(%s) failed:%v", jobID, err.Error())
  272. return nil, nil, err
  273. }
  274. result, err := modelarts.GetTrainJobLog(jobID, strconv.FormatInt(task.VersionID, 10), baseLine, resultLogFile.LogFileList[0], order, lines)
  275. if err != nil {
  276. log.Error("GetTrainJobLog(%s) failed:%v", jobID, err.Error())
  277. return nil, nil, err
  278. }
  279. return resultLogFile, result, err
  280. }
  281. func DelTrainJobVersion(ctx *context.APIContext) {
  282. var (
  283. err error
  284. )
  285. var jobID = ctx.Params(":jobid")
  286. var versionName = ctx.Query("version_name")
  287. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  288. if err != nil {
  289. log.Error("GetCloudbrainByJobID(%s) failed:%v", task.JobName, err.Error())
  290. ctx.NotFound(err)
  291. return
  292. }
  293. //删除modelarts上的记录
  294. _, err = modelarts.DelTrainJobVersion(jobID, strconv.FormatInt(task.VersionID, 10))
  295. if err != nil {
  296. log.Error("DelTrainJobVersion(%s) failed:%v", task.JobName, err.Error())
  297. ctx.NotFound(err)
  298. return
  299. }
  300. //删除数据库记录
  301. err = models.DeleteJob(task)
  302. if err != nil {
  303. ctx.ServerError("DeleteJob failed", err)
  304. ctx.NotFound(err)
  305. return
  306. }
  307. //获取删除后的版本数量
  308. var jobTypes []string
  309. jobTypes = append(jobTypes, string(models.JobTypeTrain))
  310. repo := ctx.Repo.Repository
  311. VersionTaskList, VersionListCount, err := models.CloudbrainsVersionList(&models.CloudbrainsOptions{
  312. RepoID: repo.ID,
  313. Type: models.TypeCloudBrainTwo,
  314. JobTypes: jobTypes,
  315. JobID: jobID,
  316. })
  317. if err != nil {
  318. ctx.ServerError("get VersionListCount failed", err)
  319. return
  320. }
  321. if VersionListCount > 0 {
  322. // 判断当前删掉的任务是否是最新版本,若是,将排序后的TotalVersionCount置为删掉的最新版本的TotalVersionCount,若不是,按时间排序后的版本列表的第一个版本设置为最新版本,TotalVersionCount不变
  323. if task.IsLatestVersion == modelarts.IsLatestVersion {
  324. err = models.SetVersionCountAndLatestVersion(jobID, VersionTaskList[0].Cloudbrain.VersionName, VersionListCount, modelarts.IsLatestVersion, task.TotalVersionCount)
  325. if err != nil {
  326. ctx.ServerError("UpdateJobVersionCount failed", err)
  327. return
  328. }
  329. } else {
  330. err = models.SetVersionCountAndLatestVersion(jobID, VersionTaskList[0].VersionName, VersionListCount, modelarts.IsLatestVersion, VersionTaskList[0].Cloudbrain.TotalVersionCount)
  331. if err != nil {
  332. ctx.ServerError("UpdateJobVersionCount failed", err)
  333. return
  334. }
  335. }
  336. } else { //已删除该任务下的所有版本
  337. routerRepo.DeleteJobStorage(task.JobName)
  338. }
  339. ctx.JSON(http.StatusOK, map[string]interface{}{
  340. "JobID": jobID,
  341. "VersionName": versionName,
  342. "StatusOK": 0,
  343. "VersionListCount": VersionListCount,
  344. })
  345. }
  346. func StopTrainJobVersion(ctx *context.APIContext) {
  347. var (
  348. err error
  349. )
  350. var jobID = ctx.Params(":jobid")
  351. var versionName = ctx.Query("version_name")
  352. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  353. if err != nil {
  354. log.Error("GetCloudbrainByJobID(%s) failed:%v", task.JobName, err.Error())
  355. return
  356. }
  357. _, err = modelarts.StopTrainJob(jobID, strconv.FormatInt(task.VersionID, 10))
  358. if err != nil {
  359. log.Error("StopTrainJob(%s) failed:%v", task.JobName, err.Error())
  360. return
  361. }
  362. ctx.JSON(http.StatusOK, map[string]interface{}{
  363. "JobID": jobID,
  364. "VersionName": versionName,
  365. "StatusOK": 0,
  366. })
  367. }
  368. func ModelList(ctx *context.APIContext) {
  369. var (
  370. err error
  371. )
  372. var jobID = ctx.Params(":jobid")
  373. var versionName = ctx.Query("version_name")
  374. parentDir := ctx.Query("parentDir")
  375. dirArray := strings.Split(parentDir, "/")
  376. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  377. if err != nil {
  378. log.Error("GetCloudbrainByJobID(%s) failed:%v", task.JobName, err.Error())
  379. return
  380. }
  381. models, err := storage.GetObsListObject(task.JobName, "output/", parentDir, versionName)
  382. if err != nil {
  383. log.Info("get TrainJobListModel failed:", err)
  384. ctx.ServerError("GetObsListObject:", err)
  385. return
  386. }
  387. ctx.JSON(http.StatusOK, map[string]interface{}{
  388. "JobID": jobID,
  389. "VersionName": versionName,
  390. "StatusOK": 0,
  391. "Path": dirArray,
  392. "Dirs": models,
  393. "task": task,
  394. "PageIsCloudBrain": true,
  395. })
  396. }
  397. func GetModelArtsInferenceJob(ctx *context.APIContext) {
  398. var (
  399. err error
  400. )
  401. jobID := ctx.Params(":jobid")
  402. job, err := models.GetCloudbrainByJobID(jobID)
  403. if err != nil {
  404. ctx.NotFound(err)
  405. return
  406. }
  407. result, err := modelarts.GetTrainJob(jobID, strconv.FormatInt(job.VersionID, 10))
  408. if err != nil {
  409. ctx.NotFound(err)
  410. return
  411. }
  412. if job.StartTime == 0 && result.StartTime > 0 {
  413. job.StartTime = timeutil.TimeStamp(result.StartTime / 1000)
  414. }
  415. job.Status = modelarts.TransTrainJobStatus(result.IntStatus)
  416. job.Duration = result.Duration / 1000
  417. job.TrainJobDuration = models.ConvertDurationToStr(job.Duration)
  418. if job.EndTime == 0 && models.IsTrainJobTerminal(job.Status) && job.StartTime > 0 {
  419. job.EndTime = job.StartTime.Add(job.Duration)
  420. }
  421. job.CorrectCreateUnix()
  422. err = models.UpdateInferenceJob(job)
  423. if err != nil {
  424. log.Error("UpdateJob failed:", err)
  425. }
  426. ctx.JSON(http.StatusOK, map[string]interface{}{
  427. "JobID": jobID,
  428. "JobStatus": job.Status,
  429. "JobDuration": job.TrainJobDuration,
  430. })
  431. }
  432. func ResultList(ctx *context.APIContext) {
  433. var (
  434. err error
  435. )
  436. var jobID = ctx.Params(":jobid")
  437. var versionName = ctx.Query("version_name")
  438. parentDir := ctx.Query("parentDir")
  439. dirArray := strings.Split(parentDir, "/")
  440. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  441. if err != nil {
  442. log.Error("GetCloudbrainByJobID(%s) failed:%v", task.JobName, err.Error())
  443. return
  444. }
  445. models, err := storage.GetObsListObject(task.JobName, "result/", parentDir, versionName)
  446. if err != nil {
  447. log.Info("get TrainJobListModel failed:", err)
  448. ctx.ServerError("GetObsListObject:", err)
  449. return
  450. }
  451. ctx.JSON(http.StatusOK, map[string]interface{}{
  452. "JobID": jobID,
  453. "VersionName": versionName,
  454. "StatusOK": 0,
  455. "Path": dirArray,
  456. "Dirs": models,
  457. "task": task,
  458. "PageIsCloudBrain": true,
  459. })
  460. }
  461. func TrainJobGetMetricStatistic(ctx *context.APIContext) {
  462. var (
  463. err error
  464. )
  465. var jobID = ctx.Params(":jobid")
  466. var versionName = ctx.Query("version_name")
  467. result, err := trainJobGetMetricStatistic(jobID, versionName)
  468. if err != nil {
  469. log.Error("trainJobGetMetricStatistic(%s) failed:%v", jobID, err.Error())
  470. return
  471. }
  472. ctx.JSON(http.StatusOK, map[string]interface{}{
  473. "JobID": jobID,
  474. "Interval": result.Interval,
  475. "MetricsInfo": result.MetricsInfo,
  476. })
  477. }
  478. func trainJobGetMetricStatistic(jobID string, versionName string) (*models.GetTrainJobMetricStatisticResult, error) {
  479. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  480. if err != nil {
  481. log.Error("GetCloudbrainByJobIDAndVersionName(%s) failed:%v", jobID, err.Error())
  482. return nil, err
  483. }
  484. resultLogFile, err := modelarts.GetTrainJobLogFileNames(jobID, strconv.FormatInt(task.VersionID, 10))
  485. if err != nil {
  486. log.Error("GetTrainJobLogFileNames(%s) failed:%v", jobID, err.Error())
  487. return nil, err
  488. }
  489. result, err := modelarts.GetTrainJobMetricStatistic(jobID, strconv.FormatInt(task.VersionID, 10), resultLogFile.LogFileList[0])
  490. if err != nil {
  491. log.Error("GetTrainJobMetricStatistic(%s) failed:%v", jobID, err.Error())
  492. return nil, err
  493. }
  494. return result, err
  495. }