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.

grampus.go 8.7 kB

3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 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
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package grampus
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "code.gitea.io/gitea/models"
  6. "code.gitea.io/gitea/modules/cloudbrain"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/notification"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. )
  13. const (
  14. JobPath = "job/"
  15. ProcessorTypeNPU = "npu.huawei.com/NPU"
  16. ProcessorTypeGPU = "nvidia.com/gpu"
  17. GpuWorkDir = "/tmp/"
  18. NpuWorkDir = "/cache/"
  19. NpuLocalLogUrl = "/tmp/train.log"
  20. CommandPrepareScriptNpu = ";mkdir -p output;mkdir -p code;mkdir -p dataset;mkdir -p pretrainmodel;"
  21. CodeArchiveName = "master.zip"
  22. BucketRemote = "grampus"
  23. RemoteModelPath = "/output/" + models.ModelSuffix
  24. )
  25. var (
  26. poolInfos *models.PoolInfos
  27. FlavorInfos *setting.StFlavorInfos
  28. ImageInfos *setting.StImageInfosModelArts
  29. SpecialPools *models.SpecialPools
  30. CommandPrepareScriptGpu = ";mkdir -p output;mkdir -p code;mkdir -p dataset;mkdir -p pretrainmodel;echo \"start loading script\";wget -q https://git.openi.org.cn/OpenIOSSG/%s/archive/master.zip;" +
  31. "echo \"finish loading script\";unzip -q master.zip;cd %s;chmod 777 downloader_for_obs uploader_for_npu downloader_for_minio uploader_for_gpu;"
  32. )
  33. type GenerateTrainJobReq struct {
  34. JobName string
  35. Command string
  36. ImageUrl string //与image_id二选一,都有的情况下优先image_url
  37. ImageId string
  38. DisplayJobName string
  39. Uuid string
  40. Description string
  41. CodeObsPath string
  42. BootFile string
  43. BootFileUrl string
  44. DataUrl string
  45. TrainUrl string
  46. WorkServerNumber int
  47. EngineID int64
  48. CommitID string
  49. IsLatestVersion string
  50. BranchName string
  51. PreVersionId int64
  52. PreVersionName string
  53. VersionCount int
  54. EngineName string
  55. TotalVersionCount int
  56. ComputeResource string
  57. ProcessType string
  58. DatasetNames string
  59. DatasetInfos map[string]models.DatasetInfo
  60. Params string
  61. ModelName string
  62. LabelName string
  63. CkptName string
  64. ModelVersion string
  65. PreTrainModelPath string
  66. PreTrainModelUrl string
  67. Spec *models.Specification
  68. CodeName string
  69. }
  70. func getEndPoint() string {
  71. index := strings.Index(setting.Endpoint, "//")
  72. endpoint := setting.Endpoint[index+2:]
  73. return endpoint
  74. }
  75. func getDatasetGrampus(datasetInfos map[string]models.DatasetInfo) []models.GrampusDataset {
  76. var datasetGrampus []models.GrampusDataset
  77. endPoint := getEndPoint()
  78. for _, datasetInfo := range datasetInfos {
  79. datasetGrampus = append(datasetGrampus, models.GrampusDataset{
  80. Name: datasetInfo.FullName,
  81. Bucket: setting.Bucket,
  82. EndPoint: endPoint,
  83. ObjectKey: datasetInfo.DataLocalPath + datasetInfo.FullName,
  84. })
  85. }
  86. return datasetGrampus
  87. }
  88. func GenerateTrainJob(ctx *context.Context, req *GenerateTrainJobReq) (err error) {
  89. createTime := timeutil.TimeStampNow()
  90. centerID, centerName := getCentersParamter(ctx, req)
  91. var datasetGrampus, modelGrampus []models.GrampusDataset
  92. var codeGrampus models.GrampusDataset
  93. if ProcessorTypeNPU == req.ProcessType {
  94. datasetGrampus = getDatasetGrampus(req.DatasetInfos)
  95. if len(req.ModelName) != 0 {
  96. modelGrampus = []models.GrampusDataset{
  97. {
  98. Name: req.ModelName,
  99. Bucket: setting.Bucket,
  100. EndPoint: getEndPoint(),
  101. ObjectKey: req.PreTrainModelPath,
  102. },
  103. }
  104. }
  105. codeGrampus = models.GrampusDataset{
  106. Name: req.CodeName,
  107. Bucket: setting.Bucket,
  108. EndPoint: getEndPoint(),
  109. ObjectKey: req.CodeObsPath + cloudbrain.DefaultBranchName + ".zip",
  110. }
  111. }
  112. jobResult, err := createJob(models.CreateGrampusJobRequest{
  113. Name: req.JobName,
  114. Tasks: []models.GrampusTasks{
  115. {
  116. Name: req.JobName,
  117. Command: req.Command,
  118. ResourceSpecId: req.Spec.SourceSpecId,
  119. ImageId: req.ImageId,
  120. ImageUrl: req.ImageUrl,
  121. CenterID: centerID,
  122. CenterName: centerName,
  123. ReplicaNum: 1,
  124. Datasets: datasetGrampus,
  125. Models: modelGrampus,
  126. Code: codeGrampus,
  127. BootFile: req.BootFile,
  128. },
  129. },
  130. })
  131. if err != nil {
  132. log.Error("createJob failed: %v", err.Error())
  133. return err
  134. }
  135. jobID := jobResult.JobInfo.JobID
  136. err = models.CreateCloudbrain(&models.Cloudbrain{
  137. Status: TransTrainJobStatus(jobResult.JobInfo.Status),
  138. UserID: ctx.User.ID,
  139. RepoID: ctx.Repo.Repository.ID,
  140. JobID: jobID,
  141. JobName: req.JobName,
  142. DisplayJobName: req.DisplayJobName,
  143. JobType: string(models.JobTypeTrain),
  144. Type: models.TypeC2Net,
  145. Uuid: req.Uuid,
  146. DatasetName: req.DatasetNames,
  147. CommitID: req.CommitID,
  148. IsLatestVersion: req.IsLatestVersion,
  149. ComputeResource: req.ComputeResource,
  150. ImageID: req.ImageId,
  151. TrainUrl: req.TrainUrl,
  152. BranchName: req.BranchName,
  153. Parameters: req.Params,
  154. BootFile: req.BootFile,
  155. DataUrl: req.DataUrl,
  156. Description: req.Description,
  157. WorkServerNumber: req.WorkServerNumber,
  158. EngineName: req.EngineName,
  159. VersionCount: req.VersionCount,
  160. TotalVersionCount: req.TotalVersionCount,
  161. CreatedUnix: createTime,
  162. UpdatedUnix: createTime,
  163. Spec: req.Spec,
  164. ModelName: req.ModelName,
  165. ModelVersion: req.ModelVersion,
  166. LabelName: req.LabelName,
  167. PreTrainModelUrl: req.PreTrainModelUrl,
  168. CkptName: req.CkptName,
  169. })
  170. if err != nil {
  171. log.Error("CreateCloudbrain(%s) failed:%v", req.DisplayJobName, err.Error())
  172. return err
  173. }
  174. var actionType models.ActionType
  175. if req.ComputeResource == models.NPUResource {
  176. actionType = models.ActionCreateGrampusNPUTrainTask
  177. } else if req.ComputeResource == models.GPUResource {
  178. actionType = models.ActionCreateGrampusGPUTrainTask
  179. }
  180. notification.NotifyOtherTask(ctx.User, ctx.Repo.Repository, jobID, req.DisplayJobName, actionType)
  181. return nil
  182. }
  183. func getCentersParamter(ctx *context.Context, req *GenerateTrainJobReq) ([]string, []string) {
  184. var centerID []string
  185. var centerName []string
  186. includeCenters := make(map[string]string)
  187. excludeCenters := make(map[string]string)
  188. if SpecialPools != nil {
  189. for _, pool := range SpecialPools.Pools {
  190. if !pool.IsExclusive && strings.Contains(req.ComputeResource, pool.Type) {
  191. org, _ := models.GetOrgByName(pool.Org)
  192. if org != nil {
  193. isOrgMember, _ := models.IsOrganizationMember(org.ID, ctx.User.ID)
  194. if isOrgMember {
  195. for _, info := range pool.Pool {
  196. includeCenters[info.Queue] = info.Value
  197. }
  198. } else {
  199. for _, info := range pool.Pool {
  200. excludeCenters[info.Queue] = info.Value
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. if len(includeCenters) > 0 {
  208. //如果有专属资源池,根据专属资源池指定智算中心
  209. for k, v := range includeCenters {
  210. centerID = append(centerID, k)
  211. centerName = append(centerName, v)
  212. }
  213. } else if len(excludeCenters) > 0 {
  214. //否则,有要排除的中心,先获取所有中心,删除其中的排除中心,得到指定的智算中心
  215. allCenters := make(map[string]string)
  216. specs, err := GetResourceSpecs(req.ProcessType)
  217. if err == nil {
  218. for _, info := range specs.Infos {
  219. for _, center := range info.Centers {
  220. allCenters[center.ID] = center.Name
  221. }
  222. }
  223. }
  224. for k, _ := range excludeCenters {
  225. delete(allCenters, k)
  226. }
  227. for k, v := range allCenters {
  228. centerID = append(centerID, k)
  229. centerName = append(centerName, v)
  230. }
  231. }
  232. return centerID, centerName
  233. }
  234. func TransTrainJobStatus(status string) string {
  235. if status == models.GrampusStatusPending {
  236. status = models.GrampusStatusWaiting
  237. }
  238. return strings.ToUpper(status)
  239. }
  240. func InitSpecialPool() {
  241. if SpecialPools == nil && setting.Grampus.SpecialPools != "" {
  242. json.Unmarshal([]byte(setting.Grampus.SpecialPools), &SpecialPools)
  243. }
  244. }
  245. func GetNpuModelRemoteObsUrl(jobName string) string {
  246. return "s3:///" + BucketRemote + "/" + GetNpuModelObjectKey(jobName)
  247. }
  248. func GetNpuModelObjectKey(jobName string) string {
  249. return setting.CodePathPrefix + jobName + RemoteModelPath
  250. }
  251. func GetRemoteEndPoint(aiCenterID string) string {
  252. var endPoint string
  253. for _, info := range setting.CenterInfos.Info {
  254. if info.CenterID == aiCenterID {
  255. endPoint = info.Endpoint
  256. break
  257. }
  258. }
  259. return endPoint
  260. }
  261. func GetCenterProxy(aiCenterID string) string {
  262. var proxy string
  263. for _, info := range setting.CenterInfos.Info {
  264. if info.CenterID == aiCenterID {
  265. proxy = info.StorageProxyServer
  266. break
  267. }
  268. }
  269. return proxy
  270. }