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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package modelarts_cd
  2. import (
  3. "errors"
  4. "strconv"
  5. "strings"
  6. "code.gitea.io/gitea/modules/cloudbrain"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/notification"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/timeutil"
  13. )
  14. const (
  15. //notebook
  16. storageTypeOBS = "obs"
  17. autoStopDuration = 4 * 60 * 60
  18. autoStopDurationMs = 4 * 60 * 60 * 1000
  19. MORDELART_USER_IMAGE_ENGINE_ID = -1
  20. DataSetMountPath = "/home/ma-user/work"
  21. NotebookEnv = "Python3"
  22. NotebookType = "Ascend"
  23. FlavorInfo = "Ascend: 1*Ascend 910 CPU: 24 核 96GiB (modelarts.kat1.xlarge)"
  24. //train-job
  25. CodePath = "/code/"
  26. OutputPath = "/output/"
  27. ResultPath = "/result/"
  28. LogPath = "/log/"
  29. JobPath = "/job/"
  30. OrderDesc = "desc" //向下查询
  31. OrderAsc = "asc" //向上查询
  32. Lines = 500
  33. TrainUrl = "train_url"
  34. DataUrl = "data_url"
  35. MultiDataUrl = "multi_data_url"
  36. ResultUrl = "result_url"
  37. CkptUrl = "ckpt_url"
  38. DeviceTarget = "device_target"
  39. Ascend = "Ascend"
  40. PerPage = 10
  41. IsLatestVersion = "1"
  42. NotLatestVersion = "0"
  43. VersionCountOne = 1
  44. SortByCreateTime = "create_time"
  45. ConfigTypeCustom = "custom"
  46. TotalVersionCount = 1
  47. )
  48. var ()
  49. type VersionInfo struct {
  50. Version []struct {
  51. ID int `json:"id"`
  52. Value string `json:"value"`
  53. Url string `json:"url"`
  54. } `json:"version"`
  55. }
  56. type Flavor struct {
  57. Info []struct {
  58. Code string `json:"code"`
  59. Value string `json:"value"`
  60. } `json:"flavor"`
  61. }
  62. type Engine struct {
  63. Info []struct {
  64. ID int `json:"id"`
  65. Value string `json:"value"`
  66. } `json:"engine"`
  67. }
  68. type ResourcePool struct {
  69. Info []struct {
  70. ID string `json:"id"`
  71. Value string `json:"value"`
  72. } `json:"resource_pool"`
  73. }
  74. type Parameters struct {
  75. Parameter []struct {
  76. Label string `json:"label"`
  77. Value string `json:"value"`
  78. } `json:"parameter"`
  79. }
  80. func GenerateNotebook(ctx *context.Context, req cloudbrain.GenerateModelArtsNotebookReq) (string, error) {
  81. imageName, err := GetNotebookImageName(req.ImageId)
  82. if err != nil {
  83. log.Error("GetNotebookImageName failed: %v", err.Error())
  84. return "", err
  85. }
  86. createTime := timeutil.TimeStampNow()
  87. jobResult, err := createNotebook(models.CreateNotebookWithoutPoolParams{
  88. JobName: req.JobName,
  89. Description: req.Description,
  90. Flavor: req.Spec.SourceSpecId,
  91. Duration: req.AutoStopDurationMs,
  92. ImageID: req.ImageId,
  93. Feature: models.NotebookFeature,
  94. Volume: models.VolumeReq{
  95. Capacity: setting.Capacity,
  96. Category: models.EVSCategory,
  97. Ownership: models.ManagedOwnership,
  98. },
  99. WorkspaceID: "0",
  100. })
  101. if err != nil {
  102. log.Error("createNotebook failed: %v", err.Error())
  103. if strings.HasPrefix(err.Error(), UnknownErrorPrefix) {
  104. log.Info("(%s)unknown error, set temp status", req.DisplayJobName)
  105. errTemp := models.InsertCloudbrainTemp(&models.CloudbrainTemp{
  106. JobID: models.TempJobId,
  107. VersionID: models.TempVersionId,
  108. Status: models.TempJobStatus,
  109. Type: models.TypeCDCenter,
  110. JobName: req.JobName,
  111. JobType: string(models.JobTypeDebug),
  112. })
  113. if errTemp != nil {
  114. log.Error("InsertCloudbrainTemp failed: %v", errTemp.Error())
  115. return "", errTemp
  116. }
  117. }
  118. return "", err
  119. }
  120. task := &models.Cloudbrain{
  121. Status: jobResult.Status,
  122. UserID: ctx.User.ID,
  123. RepoID: ctx.Repo.Repository.ID,
  124. JobID: jobResult.ID,
  125. JobName: req.JobName,
  126. FlavorCode: req.Spec.SourceSpecId,
  127. DisplayJobName: req.DisplayJobName,
  128. JobType: string(models.JobTypeDebug),
  129. Type: models.TypeCDCenter,
  130. Uuid: req.Uuid,
  131. ComputeResource: models.NPUResource,
  132. Image: imageName,
  133. Description: req.Description,
  134. CreatedUnix: createTime,
  135. UpdatedUnix: createTime,
  136. Spec: req.Spec,
  137. BootFile: req.BootFile,
  138. BranchName: req.BranchName,
  139. ModelName: req.ModelName,
  140. ModelVersion: req.ModelVersion,
  141. LabelName: req.LabelName,
  142. PreTrainModelUrl: req.PreTrainModelUrl,
  143. CkptName: req.CkptName,
  144. }
  145. err = models.CreateCloudbrain(task)
  146. if err != nil {
  147. return "", err
  148. }
  149. stringId := strconv.FormatInt(task.ID, 10)
  150. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, stringId, req.DisplayJobName, models.ActionCreateDebugNPUTask)
  151. return jobResult.ID, nil
  152. }
  153. func GetNotebookImageName(imageId string) (string, error) {
  154. var validImage = false
  155. var imageName = ""
  156. for _, imageInfo := range setting.StImageInfos.ImageInfo {
  157. if imageInfo.Id == imageId {
  158. validImage = true
  159. imageName = imageInfo.Value
  160. }
  161. }
  162. if !validImage {
  163. log.Error("the image id(%s) is invalid", imageId)
  164. return imageName, errors.New("the image id is invalid")
  165. }
  166. return imageName, nil
  167. }