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

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
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
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
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
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
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
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
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
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
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
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
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
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
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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. package repo
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "path"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/modules/auth"
  15. "code.gitea.io/gitea/modules/base"
  16. "code.gitea.io/gitea/modules/context"
  17. "code.gitea.io/gitea/modules/git"
  18. "code.gitea.io/gitea/modules/log"
  19. "code.gitea.io/gitea/modules/modelarts"
  20. "code.gitea.io/gitea/modules/obs"
  21. "code.gitea.io/gitea/modules/setting"
  22. "code.gitea.io/gitea/modules/storage"
  23. "github.com/unknwon/com"
  24. )
  25. const (
  26. tplModelArtsNotebookIndex base.TplName = "repo/modelarts/notebook/index"
  27. tplModelArtsNotebookNew base.TplName = "repo/modelarts/notebook/new"
  28. tplModelArtsNotebookShow base.TplName = "repo/modelarts/notebook/show"
  29. tplModelArtsTrainJobIndex base.TplName = "repo/modelarts/trainjob/index"
  30. tplModelArtsTrainJobNew base.TplName = "repo/modelarts/trainjob/new"
  31. tplModelArtsTrainJobShow base.TplName = "repo/modelarts/trainjob/show"
  32. tplModelArtsTrainJobVersionNew base.TplName = "repo/modelarts/trainjob/version_new"
  33. )
  34. // MustEnableDataset check if repository enable internal cb
  35. func MustEnableModelArts(ctx *context.Context) {
  36. if !ctx.Repo.CanRead(models.UnitTypeCloudBrain) {
  37. ctx.NotFound("MustEnableCloudbrain", nil)
  38. return
  39. }
  40. }
  41. func NotebookIndex(ctx *context.Context) {
  42. MustEnableModelArts(ctx)
  43. repo := ctx.Repo.Repository
  44. page := ctx.QueryInt("page")
  45. if page <= 0 {
  46. page = 1
  47. }
  48. ciTasks, count, err := models.Cloudbrains(&models.CloudbrainsOptions{
  49. ListOptions: models.ListOptions{
  50. Page: page,
  51. PageSize: setting.UI.IssuePagingNum,
  52. },
  53. RepoID: repo.ID,
  54. Type: models.TypeCloudBrainTwo,
  55. JobType: string(models.JobTypeDebug),
  56. })
  57. if err != nil {
  58. ctx.ServerError("Cloudbrain", err)
  59. return
  60. }
  61. for i, task := range ciTasks {
  62. if task.Status == string(models.JobRunning) {
  63. ciTasks[i].CanDebug = true
  64. } else {
  65. ciTasks[i].CanDebug = false
  66. }
  67. }
  68. pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5)
  69. pager.SetDefaultParams(ctx)
  70. ctx.Data["Page"] = pager
  71. ctx.Data["PageIsCloudBrain"] = true
  72. ctx.Data["Tasks"] = ciTasks
  73. ctx.HTML(200, tplModelArtsNotebookIndex)
  74. }
  75. func NotebookNew(ctx *context.Context) {
  76. ctx.Data["PageIsCloudBrain"] = true
  77. t := time.Now()
  78. var jobName = jobNamePrefixValid(cutString(ctx.User.Name, 5)) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[5:]
  79. ctx.Data["job_name"] = jobName
  80. attachs, err := models.GetModelArtsUserAttachments(ctx.User.ID)
  81. if err != nil {
  82. ctx.ServerError("GetAllUserAttachments failed:", err)
  83. return
  84. }
  85. ctx.Data["attachments"] = attachs
  86. ctx.Data["dataset_path"] = modelarts.DataSetMountPath
  87. ctx.Data["env"] = modelarts.NotebookEnv
  88. ctx.Data["notebook_type"] = modelarts.NotebookType
  89. if modelarts.FlavorInfos == nil {
  90. json.Unmarshal([]byte(setting.FlavorInfos), &modelarts.FlavorInfos)
  91. }
  92. ctx.Data["flavors"] = modelarts.FlavorInfos.FlavorInfo
  93. ctx.HTML(200, tplModelArtsNotebookNew)
  94. }
  95. func NotebookCreate(ctx *context.Context, form auth.CreateModelArtsNotebookForm) {
  96. ctx.Data["PageIsNotebook"] = true
  97. jobName := form.JobName
  98. uuid := form.Attachment
  99. description := form.Description
  100. flavor := form.Flavor
  101. err := modelarts.GenerateTask(ctx, jobName, uuid, description, flavor)
  102. if err != nil {
  103. ctx.RenderWithErr(err.Error(), tplModelArtsNotebookNew, &form)
  104. return
  105. }
  106. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts/notebook")
  107. }
  108. func NotebookShow(ctx *context.Context) {
  109. ctx.Data["PageIsCloudBrain"] = true
  110. var jobID = ctx.Params(":jobid")
  111. task, err := models.GetCloudbrainByJobID(jobID)
  112. if err != nil {
  113. ctx.Data["error"] = err.Error()
  114. ctx.RenderWithErr(err.Error(), tplModelArtsNotebookShow, nil)
  115. return
  116. }
  117. result, err := modelarts.GetJob(jobID)
  118. if err != nil {
  119. ctx.Data["error"] = err.Error()
  120. ctx.RenderWithErr(err.Error(), tplModelArtsNotebookShow, nil)
  121. return
  122. }
  123. if result != nil {
  124. task.Status = result.Status
  125. err = models.UpdateJob(task)
  126. if err != nil {
  127. ctx.Data["error"] = err.Error()
  128. ctx.RenderWithErr(err.Error(), tplModelArtsNotebookShow, nil)
  129. return
  130. }
  131. createTime, _ := com.StrTo(result.CreationTimestamp).Int64()
  132. result.CreateTime = time.Unix(int64(createTime/1000), 0).Format("2006-01-02 15:04:05")
  133. endTime, _ := com.StrTo(result.LatestUpdateTimestamp).Int64()
  134. result.LatestUpdateTime = time.Unix(int64(endTime/1000), 0).Format("2006-01-02 15:04:05")
  135. result.QueuingInfo.BeginTime = time.Unix(int64(result.QueuingInfo.BeginTimestamp/1000), 0).Format("2006-01-02 15:04:05")
  136. result.QueuingInfo.EndTime = time.Unix(int64(result.QueuingInfo.EndTimestamp/1000), 0).Format("2006-01-02 15:04:05")
  137. }
  138. ctx.Data["task"] = task
  139. ctx.Data["jobID"] = jobID
  140. ctx.Data["result"] = result
  141. ctx.HTML(200, tplModelArtsNotebookShow)
  142. }
  143. func NotebookDebug(ctx *context.Context) {
  144. var jobID = ctx.Params(":jobid")
  145. _, err := models.GetCloudbrainByJobID(jobID)
  146. if err != nil {
  147. ctx.ServerError("GetCloudbrainByJobID failed", err)
  148. return
  149. }
  150. result, err := modelarts.GetJob(jobID)
  151. if err != nil {
  152. ctx.RenderWithErr(err.Error(), tplModelArtsNotebookIndex, nil)
  153. return
  154. }
  155. res, err := modelarts.GetJobToken(jobID)
  156. if err != nil {
  157. ctx.RenderWithErr(err.Error(), tplModelArtsNotebookIndex, nil)
  158. return
  159. }
  160. urls := strings.Split(result.Spec.Annotations.Url, "/")
  161. urlPrefix := result.Spec.Annotations.TargetDomain
  162. for i, url := range urls {
  163. if i > 2 {
  164. urlPrefix += "/" + url
  165. }
  166. }
  167. debugUrl := urlPrefix + "?token=" + res.Token
  168. ctx.Redirect(debugUrl)
  169. }
  170. func NotebookStop(ctx *context.Context) {
  171. var jobID = ctx.Params(":jobid")
  172. log.Info(jobID)
  173. task, err := models.GetCloudbrainByJobID(jobID)
  174. if err != nil {
  175. ctx.ServerError("GetCloudbrainByJobID failed", err)
  176. return
  177. }
  178. if task.Status != string(models.JobRunning) {
  179. log.Error("the job(%s) is not running", task.JobName)
  180. ctx.ServerError("the job is not running", errors.New("the job is not running"))
  181. return
  182. }
  183. param := models.NotebookAction{
  184. Action: models.ActionStop,
  185. }
  186. res, err := modelarts.StopJob(jobID, param)
  187. if err != nil {
  188. log.Error("StopJob(%s) failed:%v", task.JobName, err.Error())
  189. ctx.ServerError("StopJob failed", err)
  190. return
  191. }
  192. task.Status = res.CurrentStatus
  193. err = models.UpdateJob(task)
  194. if err != nil {
  195. ctx.ServerError("UpdateJob failed", err)
  196. return
  197. }
  198. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts/notebook")
  199. }
  200. func NotebookDel(ctx *context.Context) {
  201. var jobID = ctx.Params(":jobid")
  202. task, err := models.GetCloudbrainByJobID(jobID)
  203. if err != nil {
  204. ctx.ServerError("GetCloudbrainByJobID failed", err)
  205. return
  206. }
  207. if task.Status != string(models.JobStopped) {
  208. log.Error("the job(%s) has not been stopped", task.JobName)
  209. ctx.ServerError("the job has not been stopped", errors.New("the job has not been stopped"))
  210. return
  211. }
  212. _, err = modelarts.DelNotebook(jobID)
  213. if err != nil {
  214. log.Error("DelJob(%s) failed:%v", task.JobName, err.Error())
  215. ctx.ServerError("DelJob failed", err)
  216. return
  217. }
  218. err = models.DeleteJob(task)
  219. if err != nil {
  220. ctx.ServerError("DeleteJob failed", err)
  221. return
  222. }
  223. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts/notebook")
  224. }
  225. func TrainJobIndex(ctx *context.Context) {
  226. MustEnableModelArts(ctx)
  227. repo := ctx.Repo.Repository
  228. page := ctx.QueryInt("page")
  229. if page <= 0 {
  230. page = 1
  231. }
  232. tasks, count, err := models.Cloudbrains(&models.CloudbrainsOptions{
  233. ListOptions: models.ListOptions{
  234. Page: page,
  235. PageSize: setting.UI.IssuePagingNum,
  236. },
  237. RepoID: repo.ID,
  238. Type: models.TypeCloudBrainTwo,
  239. JobType: string(models.JobTypeTrain),
  240. IsLatestVersion: modelarts.IsLatestVersion,
  241. })
  242. if err != nil {
  243. ctx.ServerError("Cloudbrain", err)
  244. return
  245. }
  246. pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5)
  247. pager.SetDefaultParams(ctx)
  248. ctx.Data["Page"] = pager
  249. ctx.Data["PageIsCloudBrain"] = true
  250. ctx.Data["Tasks"] = tasks
  251. ctx.HTML(200, tplModelArtsTrainJobIndex)
  252. }
  253. func TrainJobNew(ctx *context.Context) {
  254. err := trainJobNewDataPrepare(ctx)
  255. if err != nil {
  256. ctx.ServerError("get new train-job info failed", err)
  257. return
  258. }
  259. ctx.HTML(200, tplModelArtsTrainJobNew)
  260. }
  261. func trainJobNewDataPrepare(ctx *context.Context) error {
  262. ctx.Data["PageIsCloudBrain"] = true
  263. //can, err := canUserCreateTrainJob(ctx.User.ID)
  264. //if err != nil {
  265. // ctx.ServerError("canUserCreateTrainJob", err)
  266. // return
  267. //}
  268. //
  269. //if !can {
  270. // log.Error("the user can not create train-job")
  271. // ctx.ServerError("the user can not create train-job", fmt.Errorf("the user can not create train-job"))
  272. // return
  273. //}
  274. t := time.Now()
  275. var jobName = cutString(ctx.User.Name, 5) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[5:]
  276. ctx.Data["job_name"] = jobName
  277. attachs, err := models.GetModelArtsUserAttachments(ctx.User.ID)
  278. if err != nil {
  279. ctx.ServerError("GetAllUserAttachments failed:", err)
  280. return err
  281. }
  282. ctx.Data["attachments"] = attachs
  283. var resourcePools modelarts.ResourcePool
  284. if err = json.Unmarshal([]byte(setting.ResourcePools), &resourcePools); err != nil {
  285. ctx.ServerError("json.Unmarshal failed:", err)
  286. return err
  287. }
  288. ctx.Data["resource_pools"] = resourcePools.Info
  289. var engines modelarts.Engine
  290. if err = json.Unmarshal([]byte(setting.Engines), &engines); err != nil {
  291. ctx.ServerError("json.Unmarshal failed:", err)
  292. return err
  293. }
  294. ctx.Data["engines"] = engines.Info
  295. var versionInfos modelarts.VersionInfo
  296. if err = json.Unmarshal([]byte(setting.EngineVersions), &versionInfos); err != nil {
  297. ctx.ServerError("json.Unmarshal failed:", err)
  298. return err
  299. }
  300. ctx.Data["engine_versions"] = versionInfos.Version
  301. var flavorInfos modelarts.Flavor
  302. if err = json.Unmarshal([]byte(setting.TrainJobFLAVORINFOS), &flavorInfos); err != nil {
  303. ctx.ServerError("json.Unmarshal failed:", err)
  304. return err
  305. }
  306. ctx.Data["flavor_infos"] = flavorInfos.Info
  307. outputObsPath := "/" + setting.Bucket + modelarts.JobPath + jobName + modelarts.OutputPath
  308. ctx.Data["train_url"] = outputObsPath
  309. Branches, err := ctx.Repo.GitRepo.GetBranches()
  310. if err != nil {
  311. ctx.ServerError("GetBranches error:", err)
  312. return err
  313. }
  314. ctx.Data["Branches"] = Branches
  315. ctx.Data["BranchesCount"] = len(Branches)
  316. configList, err := getConfigList(modelarts.PerPage, 1, modelarts.SortByCreateTime, "desc", "", modelarts.ConfigTypeCustom)
  317. if err != nil {
  318. ctx.ServerError("getConfigList failed:", err)
  319. return err
  320. }
  321. ctx.Data["config_list"] = configList.ParaConfigs
  322. return nil
  323. }
  324. func TrainJobNewVersion(ctx *context.Context) {
  325. err := trainJobNewVersionDataPrepare(ctx)
  326. if err != nil {
  327. ctx.ServerError("get new train-job info failed", err)
  328. return
  329. }
  330. ctx.HTML(200, tplModelArtsTrainJobVersionNew)
  331. }
  332. func trainJobNewVersionDataPrepare(ctx *context.Context) error {
  333. ctx.Data["PageIsCloudBrain"] = true
  334. var jobID = ctx.Params(":jobid")
  335. // var versionName = ctx.Params(":version-name")
  336. var versionName = ctx.Query("version_name")
  337. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  338. if err != nil {
  339. log.Error("GetCloudbrainByJobIDAndVersionName(%s) failed:%v", jobID, err.Error())
  340. return err
  341. }
  342. t := time.Now()
  343. var jobName = cutString(ctx.User.Name, 5) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[5:]
  344. ctx.Data["job_name"] = task.JobName
  345. attachs, err := models.GetModelArtsUserAttachments(ctx.User.ID)
  346. if err != nil {
  347. ctx.ServerError("GetAllUserAttachments failed:", err)
  348. return err
  349. }
  350. ctx.Data["attachments"] = attachs
  351. var resourcePools modelarts.ResourcePool
  352. if err = json.Unmarshal([]byte(setting.ResourcePools), &resourcePools); err != nil {
  353. ctx.ServerError("json.Unmarshal failed:", err)
  354. return err
  355. }
  356. ctx.Data["resource_pools"] = resourcePools.Info
  357. var engines modelarts.Engine
  358. if err = json.Unmarshal([]byte(setting.Engines), &engines); err != nil {
  359. ctx.ServerError("json.Unmarshal failed:", err)
  360. return err
  361. }
  362. ctx.Data["engines"] = engines.Info
  363. var versionInfos modelarts.VersionInfo
  364. if err = json.Unmarshal([]byte(setting.EngineVersions), &versionInfos); err != nil {
  365. ctx.ServerError("json.Unmarshal failed:", err)
  366. return err
  367. }
  368. ctx.Data["engine_versions"] = versionInfos.Version
  369. var flavorInfos modelarts.Flavor
  370. if err = json.Unmarshal([]byte(setting.TrainJobFLAVORINFOS), &flavorInfos); err != nil {
  371. ctx.ServerError("json.Unmarshal failed:", err)
  372. return err
  373. }
  374. ctx.Data["flavor_infos"] = flavorInfos.Info
  375. var Parameters modelarts.Parameters
  376. if err = json.Unmarshal([]byte(task.Parameters), &Parameters); err != nil {
  377. ctx.ServerError("json.Unmarshal failed:", err)
  378. return err
  379. }
  380. ctx.Data["params"] = Parameters.Parameter
  381. outputObsPath := "/" + setting.Bucket + modelarts.JobPath + jobName + modelarts.OutputPath
  382. ctx.Data["train_url"] = outputObsPath
  383. Branches, err := ctx.Repo.GitRepo.GetBranches()
  384. if err != nil {
  385. ctx.ServerError("GetBranches error:", err)
  386. return err
  387. }
  388. ctx.Data["branches"] = Branches
  389. ctx.Data["branch_name"] = task.BranchName
  390. ctx.Data["description"] = task.Description
  391. ctx.Data["boot_file"] = task.BootFile
  392. ctx.Data["dataset_name"] = task.DatasetName
  393. ctx.Data["work_server_number"] = task.WorkServerNumber
  394. ctx.Data["flavor_name"] = task.FlavorName
  395. ctx.Data["engine_name"] = task.EngineName
  396. ctx.Data["uuid"] = task.Uuid
  397. ctx.Data["flavor_code"] = task.FlavorCode
  398. ctx.Data["engine_id"] = task.EngineID
  399. configList, err := getConfigList(modelarts.PerPage, 1, modelarts.SortByCreateTime, "desc", "", modelarts.ConfigTypeCustom)
  400. if err != nil {
  401. ctx.ServerError("getConfigList failed:", err)
  402. return err
  403. }
  404. ctx.Data["config_list"] = configList.ParaConfigs
  405. return nil
  406. }
  407. func TrainJobCreate(ctx *context.Context, form auth.CreateModelArtsTrainJobForm) {
  408. ctx.Data["PageIsTrainJob"] = true
  409. VersionOutputPath := modelarts.GetVersionOutputPathByTotalVersionCount(modelarts.TotalVersionCount)
  410. jobName := form.JobName
  411. uuid := form.Attachment
  412. description := form.Description
  413. workServerNumber := form.WorkServerNumber
  414. engineID := form.EngineID
  415. bootFile := form.BootFile
  416. flavorCode := form.Flavor
  417. params := form.Params
  418. poolID := form.PoolID
  419. isSaveParam := form.IsSaveParam
  420. repo := ctx.Repo.Repository
  421. codeLocalPath := setting.JobPath + jobName + modelarts.CodePath
  422. codeObsPath := "/" + setting.Bucket + modelarts.JobPath + jobName + modelarts.CodePath
  423. outputObsPath := "/" + setting.Bucket + modelarts.JobPath + jobName + modelarts.OutputPath + VersionOutputPath + "/"
  424. logObsPath := "/" + setting.Bucket + modelarts.JobPath + jobName + modelarts.LogPath + VersionOutputPath + "/"
  425. dataPath := "/" + setting.Bucket + "/" + setting.BasePath + path.Join(uuid[0:1], uuid[1:2]) + "/" + uuid + uuid + "/"
  426. branch_name := form.BranchName
  427. isLatestVersion := modelarts.IsLatestVersion
  428. FlavorName := form.FlavorName
  429. VersionCount := modelarts.VersionCount
  430. EngineName := form.EngineName
  431. if err := paramCheckCreateTrainJob(form); err != nil {
  432. log.Error("paramCheckCreateTrainJob failed:(%v)", err)
  433. trainJobNewDataPrepare(ctx)
  434. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobNew, &form)
  435. return
  436. }
  437. attach, err := models.GetAttachmentByUUID(uuid)
  438. if err != nil {
  439. log.Error("GetAttachmentByUUID(%s) failed:%v", uuid, err.Error())
  440. return
  441. }
  442. //todo: del the codeLocalPath
  443. _, err = ioutil.ReadDir(codeLocalPath)
  444. if err == nil {
  445. os.RemoveAll(codeLocalPath)
  446. }
  447. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  448. commitID, _ := gitRepo.GetBranchCommitID(branch_name)
  449. if err := git.Clone(repo.RepoPath(), codeLocalPath, git.CloneRepoOptions{
  450. Branch: branch_name,
  451. }); err != nil {
  452. log.Error("创建任务失败,服务器超时!: %s (%v)", repo.FullName(), err)
  453. trainJobNewDataPrepare(ctx)
  454. ctx.Data["bootFile"] = form.BootFile
  455. ctx.Data["uuid"] = form.Attachment
  456. ctx.Data["datasetName"] = attach.Name
  457. ctx.Data["params"] = form.Params
  458. ctx.Data["branch_name"] = branch_name
  459. trainJobNewDataPrepare(ctx)
  460. ctx.RenderWithErr("创建任务失败,服务器超时!", tplModelArtsTrainJobNew, &form)
  461. return
  462. }
  463. //todo: upload code (send to file_server todo this work?)
  464. if err := obsMkdir(setting.CodePathPrefix + jobName + modelarts.OutputPath + VersionOutputPath + "/"); err != nil {
  465. log.Error("Failed to obsMkdir_output: %s (%v)", repo.FullName(), err)
  466. trainJobNewDataPrepare(ctx)
  467. ctx.RenderWithErr("Failed to obsMkdir_output", tplModelArtsTrainJobNew, &form)
  468. return
  469. }
  470. if err := obsMkdir(setting.CodePathPrefix + jobName + modelarts.LogPath + VersionOutputPath + "/"); err != nil {
  471. log.Error("Failed to obsMkdir_log: %s (%v)", repo.FullName(), err)
  472. trainJobNewDataPrepare(ctx)
  473. ctx.RenderWithErr("Failed to obsMkdir_log", tplModelArtsTrainJobNew, &form)
  474. return
  475. }
  476. if err := uploadCodeToObs(codeLocalPath, jobName, ""); err != nil {
  477. log.Error("Failed to uploadCodeToObs: %s (%v)", repo.FullName(), err)
  478. trainJobNewDataPrepare(ctx)
  479. ctx.RenderWithErr("Failed to uploadCodeToObs", tplModelArtsTrainJobNew, &form)
  480. return
  481. }
  482. //todo: del local code?
  483. var parameters models.Parameters
  484. param := make([]models.Parameter, 0)
  485. param = append(param, models.Parameter{
  486. Label: modelarts.TrainUrl,
  487. Value: outputObsPath,
  488. }, models.Parameter{
  489. Label: modelarts.DataUrl,
  490. Value: dataPath,
  491. })
  492. if len(params) != 0 {
  493. err := json.Unmarshal([]byte(params), &parameters)
  494. if err != nil {
  495. log.Error("Failed to Unmarshal params: %s (%v)", params, err)
  496. trainJobNewDataPrepare(ctx)
  497. ctx.RenderWithErr("运行参数错误", tplModelArtsTrainJobNew, &form)
  498. return
  499. }
  500. for _, parameter := range parameters.Parameter {
  501. if parameter.Label != modelarts.TrainUrl && parameter.Label != modelarts.DataUrl {
  502. param = append(param, models.Parameter{
  503. Label: parameter.Label,
  504. Value: parameter.Value,
  505. })
  506. }
  507. }
  508. }
  509. //save param config
  510. if isSaveParam == "on" {
  511. if form.ParameterTemplateName == "" {
  512. log.Error("ParameterTemplateName is empty")
  513. trainJobNewDataPrepare(ctx)
  514. ctx.RenderWithErr("保存作业参数时,作业参数名称不能为空", tplModelArtsTrainJobNew, &form)
  515. return
  516. }
  517. _, err := modelarts.CreateTrainJobConfig(models.CreateConfigParams{
  518. ConfigName: form.ParameterTemplateName,
  519. Description: form.PrameterDescription,
  520. DataUrl: dataPath,
  521. AppUrl: codeObsPath,
  522. BootFileUrl: codeObsPath + bootFile,
  523. TrainUrl: outputObsPath,
  524. Flavor: models.Flavor{
  525. Code: flavorCode,
  526. },
  527. WorkServerNum: workServerNumber,
  528. EngineID: int64(engineID),
  529. LogUrl: logObsPath,
  530. PoolID: poolID,
  531. Parameter: param,
  532. })
  533. if err != nil {
  534. log.Error("Failed to CreateTrainJobConfig: %v", err)
  535. trainJobNewDataPrepare(ctx)
  536. ctx.RenderWithErr("保存作业参数失败:"+err.Error(), tplModelArtsTrainJobNew, &form)
  537. return
  538. }
  539. }
  540. req := &modelarts.GenerateTrainJobReq{
  541. JobName: jobName,
  542. DataUrl: dataPath,
  543. Description: description,
  544. CodeObsPath: codeObsPath,
  545. BootFileUrl: codeObsPath + bootFile,
  546. BootFile: bootFile,
  547. TrainUrl: outputObsPath,
  548. FlavorCode: flavorCode,
  549. WorkServerNumber: workServerNumber,
  550. EngineID: int64(engineID),
  551. LogUrl: logObsPath,
  552. PoolID: poolID,
  553. Uuid: uuid,
  554. Parameters: parameters.Parameter,
  555. CommitID: commitID,
  556. IsLatestVersion: isLatestVersion,
  557. BranchName: branch_name,
  558. Params: form.Params,
  559. FlavorName: FlavorName,
  560. EngineName: EngineName,
  561. VersionCount: VersionCount,
  562. TotalVersionCount: modelarts.TotalVersionCount,
  563. }
  564. err = modelarts.GenerateTrainJob(ctx, req)
  565. if err != nil {
  566. log.Error("GenerateTrainJob failed:%v", err.Error())
  567. trainJobNewDataPrepare(ctx)
  568. ctx.Data["bootFile"] = form.BootFile
  569. ctx.Data["uuid"] = form.Attachment
  570. ctx.Data["datasetName"] = attach.Name
  571. ctx.Data["params"] = form.Params
  572. ctx.Data["branch_name"] = branch_name
  573. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobNew, &form)
  574. return
  575. }
  576. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts/train-job")
  577. }
  578. func TrainJobCreateVersion(ctx *context.Context, form auth.CreateModelArtsTrainJobForm) {
  579. ctx.Data["PageIsTrainJob"] = true
  580. var jobID = ctx.Params(":jobid")
  581. latestTask, err := models.GetCloudbrainByJobIDAndIsLatestVersion(jobID, modelarts.IsLatestVersion)
  582. if err != nil {
  583. ctx.ServerError("GetCloudbrainByJobIDAndIsLatestVersion faild:", err)
  584. return
  585. }
  586. VersionOutputPath := modelarts.GetVersionOutputPathByTotalVersionCount(latestTask.TotalVersionCount + 1)
  587. jobName := form.JobName
  588. uuid := form.Attachment
  589. description := form.Description
  590. workServerNumber := form.WorkServerNumber
  591. engineID := form.EngineID
  592. bootFile := form.BootFile
  593. flavorCode := form.Flavor
  594. params := form.Params
  595. poolID := form.PoolID
  596. isSaveParam := form.IsSaveParam
  597. repo := ctx.Repo.Repository
  598. codeLocalPath := setting.JobPath + jobName + modelarts.CodePath
  599. codeObsPath := "/" + setting.Bucket + modelarts.JobPath + jobName + modelarts.CodePath
  600. outputObsPath := "/" + setting.Bucket + modelarts.JobPath + jobName + modelarts.OutputPath + VersionOutputPath + "/"
  601. logObsPath := "/" + setting.Bucket + modelarts.JobPath + jobName + modelarts.LogPath + VersionOutputPath + "/"
  602. dataPath := "/" + setting.Bucket + "/" + setting.BasePath + path.Join(uuid[0:1], uuid[1:2]) + "/" + uuid + uuid + "/"
  603. branch_name := form.BranchName
  604. PreVersionName := form.VersionName
  605. FlavorName := form.FlavorName
  606. EngineName := form.EngineName
  607. isLatestVersion := modelarts.IsLatestVersion
  608. if err := paramCheckCreateTrainJob(form); err != nil {
  609. log.Error("paramCheckCreateTrainJob failed:(%v)", err)
  610. trainJobNewVersionDataPrepare(ctx)
  611. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobVersionNew, &form)
  612. return
  613. }
  614. attach, err := models.GetAttachmentByUUID(uuid)
  615. if err != nil {
  616. log.Error("GetAttachmentByUUID(%s) failed:%v", uuid, err.Error())
  617. return
  618. }
  619. //todo: del the codeLocalPath
  620. _, err = ioutil.ReadDir(codeLocalPath)
  621. if err == nil {
  622. os.RemoveAll(codeLocalPath)
  623. }
  624. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  625. commitID, _ := gitRepo.GetBranchCommitID(branch_name)
  626. if err := git.Clone(repo.RepoPath(), codeLocalPath, git.CloneRepoOptions{
  627. Branch: branch_name,
  628. }); err != nil {
  629. log.Error("创建任务失败,任务名称已存在!: %s (%v)", repo.FullName(), err)
  630. trainJobNewVersionDataPrepare(ctx)
  631. ctx.Data["bootFile"] = form.BootFile
  632. ctx.Data["uuid"] = form.Attachment
  633. ctx.Data["datasetName"] = attach.Name
  634. ctx.Data["params"] = form.Params
  635. ctx.Data["branch_name"] = branch_name
  636. ctx.RenderWithErr("创建任务失败,任务名称已存在!", tplModelArtsTrainJobVersionNew, &form)
  637. return
  638. }
  639. //todo: upload code (send to file_server todo this work?)
  640. if err := obsMkdir(setting.CodePathPrefix + jobName + modelarts.OutputPath + VersionOutputPath + "/"); err != nil {
  641. log.Error("Failed to obsMkdir_output: %s (%v)", repo.FullName(), err)
  642. trainJobNewVersionDataPrepare(ctx)
  643. ctx.RenderWithErr("Failed to obsMkdir_output", tplModelArtsTrainJobVersionNew, &form)
  644. return
  645. }
  646. if err := obsMkdir(setting.CodePathPrefix + jobName + modelarts.LogPath + VersionOutputPath + "/"); err != nil {
  647. log.Error("Failed to obsMkdir_log: %s (%v)", repo.FullName(), err)
  648. trainJobNewVersionDataPrepare(ctx)
  649. ctx.RenderWithErr("Failed to obsMkdir_log", tplModelArtsTrainJobVersionNew, &form)
  650. return
  651. }
  652. if err := uploadCodeToObs(codeLocalPath, jobName, ""); err != nil {
  653. log.Error("Failed to uploadCodeToObs: %s (%v)", repo.FullName(), err)
  654. trainJobNewVersionDataPrepare(ctx)
  655. ctx.RenderWithErr("Failed to uploadCodeToObs", tplModelArtsTrainJobVersionNew, &form)
  656. return
  657. }
  658. //todo: del local code?
  659. var parameters models.Parameters
  660. param := make([]models.Parameter, 0)
  661. param = append(param, models.Parameter{
  662. Label: modelarts.TrainUrl,
  663. Value: outputObsPath,
  664. }, models.Parameter{
  665. Label: modelarts.DataUrl,
  666. Value: dataPath,
  667. })
  668. if len(params) != 0 {
  669. err := json.Unmarshal([]byte(params), &parameters)
  670. if err != nil {
  671. log.Error("Failed to Unmarshal params: %s (%v)", params, err)
  672. trainJobNewVersionDataPrepare(ctx)
  673. ctx.RenderWithErr("运行参数错误", tplModelArtsTrainJobVersionNew, &form)
  674. return
  675. }
  676. for _, parameter := range parameters.Parameter {
  677. if parameter.Label != modelarts.TrainUrl && parameter.Label != modelarts.DataUrl {
  678. param = append(param, models.Parameter{
  679. Label: parameter.Label,
  680. Value: parameter.Value,
  681. })
  682. }
  683. }
  684. }
  685. //save param config
  686. if isSaveParam == "on" {
  687. if form.ParameterTemplateName == "" {
  688. log.Error("ParameterTemplateName is empty")
  689. trainJobNewVersionDataPrepare(ctx)
  690. ctx.RenderWithErr("保存作业参数时,作业参数名称不能为空", tplModelArtsTrainJobVersionNew, &form)
  691. return
  692. }
  693. _, err := modelarts.CreateTrainJobConfig(models.CreateConfigParams{
  694. ConfigName: form.ParameterTemplateName,
  695. Description: form.PrameterDescription,
  696. DataUrl: dataPath,
  697. AppUrl: codeObsPath,
  698. BootFileUrl: codeObsPath + bootFile,
  699. TrainUrl: outputObsPath,
  700. Flavor: models.Flavor{
  701. Code: flavorCode,
  702. },
  703. WorkServerNum: workServerNumber,
  704. EngineID: int64(engineID),
  705. LogUrl: logObsPath,
  706. PoolID: poolID,
  707. Parameter: parameters.Parameter,
  708. })
  709. if err != nil {
  710. log.Error("Failed to CreateTrainJobConfig: %v", err)
  711. trainJobNewVersionDataPrepare(ctx)
  712. ctx.RenderWithErr("保存作业参数失败:"+err.Error(), tplModelArtsTrainJobVersionNew, &form)
  713. return
  714. }
  715. }
  716. if err != nil {
  717. log.Error("getFlavorNameByEngineID(%s) failed:%v", engineID, err.Error())
  718. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobVersionNew, &form)
  719. return
  720. }
  721. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, PreVersionName)
  722. if err != nil {
  723. log.Error("GetCloudbrainByJobIDAndVersionName(%s) failed:%v", jobID, err.Error())
  724. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobVersionNew, &form)
  725. return
  726. }
  727. req := &modelarts.GenerateTrainJobReq{
  728. JobName: task.JobName,
  729. DataUrl: dataPath,
  730. Description: description,
  731. CodeObsPath: codeObsPath,
  732. BootFileUrl: codeObsPath + bootFile,
  733. BootFile: bootFile,
  734. TrainUrl: outputObsPath,
  735. FlavorCode: flavorCode,
  736. WorkServerNumber: workServerNumber,
  737. IsLatestVersion: isLatestVersion,
  738. EngineID: int64(engineID),
  739. LogUrl: logObsPath,
  740. PoolID: poolID,
  741. Uuid: uuid,
  742. Params: form.Params,
  743. Parameters: parameters.Parameter,
  744. PreVersionId: task.VersionID,
  745. CommitID: commitID,
  746. BranchName: branch_name,
  747. FlavorName: FlavorName,
  748. EngineName: EngineName,
  749. PreVersionName: PreVersionName,
  750. TotalVersionCount: latestTask.TotalVersionCount + 1,
  751. }
  752. err = modelarts.GenerateTrainJobVersion(ctx, req, jobID)
  753. if err != nil {
  754. log.Error("GenerateTrainJob failed:%v", err.Error())
  755. trainJobNewVersionDataPrepare(ctx)
  756. ctx.Data["bootFile"] = form.BootFile
  757. ctx.Data["uuid"] = form.Attachment
  758. ctx.Data["datasetName"] = attach.Name
  759. ctx.Data["params"] = form.Params
  760. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobVersionNew, &form)
  761. return
  762. }
  763. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts/train-job")
  764. // ctx.HTML(http.StatusOK, tplModelArtsTrainJobShow)
  765. }
  766. // readDir reads the directory named by dirname and returns
  767. // a list of directory entries sorted by filename.
  768. func readDir(dirname string) ([]os.FileInfo, error) {
  769. f, err := os.Open(dirname)
  770. if err != nil {
  771. return nil, err
  772. }
  773. list, err := f.Readdir(100)
  774. f.Close()
  775. if err != nil {
  776. //todo: can not upload empty folder
  777. if err == io.EOF {
  778. return nil, nil
  779. }
  780. return nil, err
  781. }
  782. //sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
  783. return list, nil
  784. }
  785. func uploadCodeToObs(codePath, jobName, parentDir string) error {
  786. files, err := readDir(codePath)
  787. if err != nil {
  788. log.Error("readDir(%s) failed: %s", codePath, err.Error())
  789. return err
  790. }
  791. for _, file := range files {
  792. if file.IsDir() {
  793. input := &obs.PutObjectInput{}
  794. input.Bucket = setting.Bucket
  795. input.Key = parentDir + file.Name() + "/"
  796. _, err = storage.ObsCli.PutObject(input)
  797. if err != nil {
  798. log.Error("PutObject(%s) failed: %s", input.Key, err.Error())
  799. return err
  800. }
  801. if err = uploadCodeToObs(codePath+file.Name()+"/", jobName, parentDir+file.Name()+"/"); err != nil {
  802. log.Error("uploadCodeToObs(%s) failed: %s", file.Name(), err.Error())
  803. return err
  804. }
  805. } else {
  806. input := &obs.PutFileInput{}
  807. input.Bucket = setting.Bucket
  808. input.Key = setting.CodePathPrefix + jobName + "/code/" + parentDir + file.Name()
  809. input.SourceFile = codePath + file.Name()
  810. _, err = storage.ObsCli.PutFile(input)
  811. if err != nil {
  812. log.Error("PutFile(%s) failed: %s", input.SourceFile, err.Error())
  813. return err
  814. }
  815. }
  816. }
  817. return nil
  818. }
  819. func obsMkdir(dir string) error {
  820. input := &obs.PutObjectInput{}
  821. input.Bucket = setting.Bucket
  822. input.Key = dir
  823. _, err := storage.ObsCli.PutObject(input)
  824. if err != nil {
  825. log.Error("PutObject(%s) failed: %s", input.Key, err.Error())
  826. return err
  827. }
  828. return nil
  829. }
  830. func paramCheckCreateTrainJob(form auth.CreateModelArtsTrainJobForm) error {
  831. if !strings.HasSuffix(form.BootFile, ".py") {
  832. log.Error("the boot file(%s) must be a python file", form.BootFile)
  833. return errors.New("启动文件必须是python文件")
  834. }
  835. if form.WorkServerNumber > 25 || form.WorkServerNumber < 1 {
  836. log.Error("the WorkServerNumber(%d) must be in (1,25)", form.WorkServerNumber)
  837. return errors.New("计算节点数必须在1-25之间")
  838. }
  839. return nil
  840. }
  841. func TrainJobShow(ctx *context.Context) {
  842. ctx.Data["PageIsCloudBrain"] = true
  843. var jobID = ctx.Params(":jobid")
  844. repo := ctx.Repo.Repository
  845. page := ctx.QueryInt("page")
  846. if page <= 0 {
  847. page = 1
  848. }
  849. VersionListTasks, VersionListCount, err := models.CloudbrainsVersionList(&models.CloudbrainsOptions{
  850. ListOptions: models.ListOptions{
  851. Page: page,
  852. PageSize: setting.UI.IssuePagingNum,
  853. },
  854. RepoID: repo.ID,
  855. Type: models.TypeCloudBrainTwo,
  856. JobType: string(models.JobTypeTrain),
  857. JobID: jobID,
  858. })
  859. if err != nil {
  860. log.Error("GetVersionListTasks(%s) failed:%v", jobID, err.Error())
  861. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobShow, nil)
  862. return
  863. }
  864. //将运行参数转化为epoch_size = 3, device_target = Ascend的格式
  865. for i, _ := range VersionListTasks {
  866. var parameters models.Parameters
  867. err := json.Unmarshal([]byte(VersionListTasks[i].Parameters), &parameters)
  868. if err != nil {
  869. log.Error("Failed to Unmarshal Parameters: %s (%v)", VersionListTasks[i].Parameters, err)
  870. trainJobNewDataPrepare(ctx)
  871. return
  872. }
  873. if len(parameters.Parameter) > 0 {
  874. paramTemp := ""
  875. for _, Parameter := range parameters.Parameter {
  876. param := Parameter.Label + " = " + Parameter.Value + ", "
  877. paramTemp = paramTemp + param
  878. }
  879. VersionListTasks[i].Parameters = paramTemp[:len(paramTemp)-2]
  880. } else {
  881. VersionListTasks[i].Parameters = ""
  882. }
  883. }
  884. ctx.Data["jobID"] = jobID
  885. ctx.Data["jobName"] = VersionListTasks[0].JobName
  886. ctx.Data["version_list_task"] = VersionListTasks
  887. ctx.Data["version_list_count"] = VersionListCount
  888. ctx.HTML(http.StatusOK, tplModelArtsTrainJobShow)
  889. }
  890. func TrainJobGetLog(ctx *context.Context) {
  891. ctx.Data["PageIsTrainJob"] = true
  892. var jobID = ctx.Params(":jobid")
  893. var logFileName = ctx.Query("file_name")
  894. var baseLine = ctx.Query("base_line")
  895. var order = ctx.Query("order")
  896. if order != modelarts.OrderDesc && order != modelarts.OrderAsc {
  897. log.Error("order(%s) check failed", order)
  898. ctx.HTML(http.StatusBadRequest, tplModelArtsTrainJobShow)
  899. return
  900. }
  901. task, err := models.GetCloudbrainByJobID(jobID)
  902. if err != nil {
  903. log.Error("GetCloudbrainByJobID(%s) failed:%v", jobID, err.Error())
  904. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobShow, nil)
  905. return
  906. }
  907. result, err := modelarts.GetTrainJobLog(jobID, strconv.FormatInt(task.VersionID, 10), baseLine, logFileName, order, modelarts.Lines)
  908. if err != nil {
  909. log.Error("GetTrainJobLog(%s) failed:%v", jobID, err.Error())
  910. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobShow, nil)
  911. return
  912. }
  913. ctx.Data["log"] = result
  914. //ctx.HTML(http.StatusOK, tplModelArtsTrainJobShow)
  915. }
  916. func trainJobGetLog(jobID string) (*models.GetTrainJobLogFileNamesResult, *models.GetTrainJobLogResult, error) {
  917. task, err := models.GetCloudbrainByJobID(jobID)
  918. if err != nil {
  919. log.Error("GetCloudbrainByJobID(%s) failed:%v", jobID, err.Error())
  920. return nil, nil, err
  921. }
  922. resultLogFile, err := modelarts.GetTrainJobLogFileNames(jobID, strconv.FormatInt(task.VersionID, 10))
  923. if err != nil {
  924. log.Error("GetTrainJobLogFileNames(%s) failed:%v", jobID, err.Error())
  925. return nil, nil, err
  926. }
  927. result, err := modelarts.GetTrainJobLog(jobID, strconv.FormatInt(task.VersionID, 10), "", resultLogFile.LogFileList[0], modelarts.OrderDesc, modelarts.Lines)
  928. if err != nil {
  929. log.Error("GetTrainJobLog(%s) failed:%v", jobID, err.Error())
  930. return nil, nil, err
  931. }
  932. return resultLogFile, result, err
  933. }
  934. func TrainJobDel(ctx *context.Context) {
  935. var jobID = ctx.Params(":jobid")
  936. repo := ctx.Repo.Repository
  937. VersionListTasks, _, err := models.CloudbrainsVersionList(&models.CloudbrainsOptions{
  938. RepoID: repo.ID,
  939. Type: models.TypeCloudBrainTwo,
  940. JobType: string(models.JobTypeTrain),
  941. JobID: jobID,
  942. })
  943. if err != nil {
  944. ctx.ServerError("get VersionListTasks failed", err)
  945. return
  946. }
  947. //删除modelarts上的任务记录
  948. _, err = modelarts.DelTrainJob(jobID)
  949. if err != nil {
  950. log.Error("DelTrainJob(%s) failed:%v", jobID, err.Error())
  951. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobIndex, nil)
  952. return
  953. }
  954. //删除数据库Cloudbrain表的记录
  955. for _, task := range VersionListTasks {
  956. err = models.DeleteJob(&task.Cloudbrain)
  957. if err != nil {
  958. ctx.ServerError("DeleteJob failed", err)
  959. return
  960. }
  961. }
  962. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts/train-job")
  963. }
  964. func TrainJobStop(ctx *context.Context) {
  965. var jobID = ctx.Params(":jobid")
  966. task, err := models.GetCloudbrainByJobID(jobID)
  967. if err != nil {
  968. log.Error("GetCloudbrainByJobID(%s) failed:%v", task.JobName, err.Error())
  969. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobIndex, nil)
  970. return
  971. }
  972. _, err = modelarts.StopTrainJob(jobID, strconv.FormatInt(task.VersionID, 10))
  973. if err != nil {
  974. log.Error("StopTrainJob(%s) failed:%v", task.JobName, err.Error())
  975. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobIndex, nil)
  976. return
  977. }
  978. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts/train-job")
  979. }
  980. func canUserCreateTrainJob(uid int64) (bool, error) {
  981. org, err := models.GetOrgByName(setting.AllowedOrg)
  982. if err != nil {
  983. log.Error("get allowed org failed: ", setting.AllowedOrg)
  984. return false, err
  985. }
  986. return org.IsOrgMember(uid)
  987. }
  988. func TrainJobGetConfigList(ctx *context.Context) {
  989. ctx.Data["PageIsTrainJob"] = true
  990. var jobID = ctx.Params(":jobid")
  991. var logFileName = ctx.Query("file_name")
  992. var baseLine = ctx.Query("base_line")
  993. var order = ctx.Query("order")
  994. if order != modelarts.OrderDesc && order != modelarts.OrderAsc {
  995. log.Error("order(%s) check failed", order)
  996. ctx.HTML(http.StatusBadRequest, tplModelArtsTrainJobShow)
  997. return
  998. }
  999. task, err := models.GetCloudbrainByJobID(jobID)
  1000. if err != nil {
  1001. log.Error("GetCloudbrainByJobID(%s) failed:%v", jobID, err.Error())
  1002. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobShow, nil)
  1003. return
  1004. }
  1005. result, err := modelarts.GetTrainJobLog(jobID, strconv.FormatInt(task.VersionID, 10), baseLine, logFileName, order, modelarts.Lines)
  1006. if err != nil {
  1007. log.Error("GetTrainJobLog(%s) failed:%v", jobID, err.Error())
  1008. ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobShow, nil)
  1009. return
  1010. }
  1011. ctx.Data["log"] = result
  1012. //ctx.HTML(http.StatusOK, tplModelArtsTrainJobShow)
  1013. }
  1014. func getConfigList(perPage, page int, sortBy, order, searchContent, configType string) (*models.GetConfigListResult, error) {
  1015. var result models.GetConfigListResult
  1016. list, err := modelarts.GetConfigList(perPage, page, sortBy, order, searchContent, configType)
  1017. if err != nil {
  1018. log.Error("GetConfigList failed:", err)
  1019. return &result, err
  1020. }
  1021. for _, config := range list.ParaConfigs {
  1022. paraConfig, err := modelarts.GetParaConfig(config.ConfigName, configType)
  1023. if err != nil {
  1024. log.Error("GetParaConfig failed:", err)
  1025. return &result, err
  1026. }
  1027. config.Result = paraConfig
  1028. }
  1029. return list, nil
  1030. }