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.

resource_specification.go 21 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. package resource
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/cloudbrain"
  11. "code.gitea.io/gitea/modules/convert"
  12. "code.gitea.io/gitea/modules/grampus"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/modelarts"
  15. "code.gitea.io/gitea/modules/setting"
  16. api "code.gitea.io/gitea/modules/structs"
  17. "code.gitea.io/gitea/routers/response"
  18. "code.gitea.io/gitea/services/admin/operate_log"
  19. )
  20. func AddResourceSpecification(doerId int64, req models.ResourceSpecificationReq) error {
  21. if req.Status == 0 {
  22. req.Status = models.SpecNotVerified
  23. }
  24. spec := req.ToDTO()
  25. if _, err := models.InsertResourceSpecification(spec); err != nil {
  26. return err
  27. }
  28. return nil
  29. }
  30. func UpdateSpecUnitPrice(doerId int64, specId int64, unitPrice int) *response.BizError {
  31. oldSpec, err := models.GetResourceSpecification(&models.ResourceSpecification{ID: specId})
  32. if err != nil {
  33. return response.NewBizError(err)
  34. }
  35. if oldSpec == nil {
  36. return response.SPECIFICATION_NOT_EXIST
  37. }
  38. err = models.UpdateSpecUnitPriceById(specId, unitPrice)
  39. if err != nil {
  40. return response.NewBizError(err)
  41. }
  42. if oldSpec.UnitPrice != unitPrice {
  43. AddSpecOperateLog(doerId, "edit", operate_log.NewLogValues().Add("unitPrice", unitPrice), operate_log.NewLogValues().Add("unitPrice", oldSpec.UnitPrice), specId, fmt.Sprintf("修改资源规格单价从%d积分到%d积分", oldSpec.UnitPrice, unitPrice))
  44. }
  45. return nil
  46. }
  47. func SyncGrampusSpecs(doerId int64) error {
  48. r, err := grampus.GetResourceSpecs("")
  49. if err != nil {
  50. return err
  51. }
  52. log.Info("SyncGrampusSpecs result = %+v", r)
  53. specUpdateList := make([]models.ResourceSpecification, 0)
  54. specInsertList := make([]models.ResourceSpecification, 0)
  55. existIds := make([]int64, 0)
  56. for _, spec := range r.Infos {
  57. for _, c := range spec.Centers {
  58. computeResource := models.ParseComputeResourceFormGrampus(spec.SpecInfo.AccDeviceKind)
  59. if computeResource == "" {
  60. continue
  61. }
  62. accCardType := strings.ToUpper(spec.SpecInfo.AccDeviceModel)
  63. memGiB, err := models.ParseMemSizeFromGrampus(spec.SpecInfo.MemorySize)
  64. gpuMemGiB, err := models.ParseMemSizeFromGrampus(spec.SpecInfo.AccDeviceMemory)
  65. if err != nil {
  66. log.Error("ParseMemSizeFromGrampus error. MemorySize=%s AccDeviceMemory=%s", spec.SpecInfo.MemorySize, spec.SpecInfo.AccDeviceMemory)
  67. }
  68. // get resource queue.if queue not exist,skip it
  69. r, err := models.GetResourceQueue(&models.ResourceQueue{
  70. Cluster: models.C2NetCluster,
  71. AiCenterCode: c.ID,
  72. ComputeResource: computeResource,
  73. AccCardType: accCardType,
  74. })
  75. if err != nil || r == nil {
  76. continue
  77. }
  78. //Determine if this specification already exists.if exist,update params
  79. //if not exist,insert a new record and status is SpecNotVerified
  80. oldSpec, err := models.GetResourceSpecification(&models.ResourceSpecification{
  81. QueueId: r.ID,
  82. SourceSpecId: spec.ID,
  83. })
  84. if err != nil {
  85. return err
  86. }
  87. if oldSpec == nil {
  88. specInsertList = append(specInsertList, models.ResourceSpecification{
  89. QueueId: r.ID,
  90. SourceSpecId: spec.ID,
  91. AccCardsNum: spec.SpecInfo.AccDeviceNum,
  92. CpuCores: spec.SpecInfo.CpuCoreNum,
  93. MemGiB: memGiB,
  94. GPUMemGiB: gpuMemGiB,
  95. Status: models.SpecNotVerified,
  96. IsAutomaticSync: true,
  97. IsAvailable: true,
  98. CreatedBy: doerId,
  99. UpdatedBy: doerId,
  100. })
  101. } else {
  102. existIds = append(existIds, oldSpec.ID)
  103. specUpdateList = append(specUpdateList, models.ResourceSpecification{
  104. ID: oldSpec.ID,
  105. AccCardsNum: spec.SpecInfo.AccDeviceNum,
  106. CpuCores: spec.SpecInfo.CpuCoreNum,
  107. MemGiB: memGiB,
  108. GPUMemGiB: gpuMemGiB,
  109. IsAvailable: true,
  110. UpdatedBy: doerId,
  111. })
  112. }
  113. }
  114. }
  115. return models.SyncGrampusSpecs(specUpdateList, specInsertList, existIds)
  116. }
  117. //GetResourceSpecificationList returns specification and queue
  118. func GetResourceSpecificationList(opts models.SearchResourceSpecificationOptions) (*models.ResourceSpecAndQueueListRes, error) {
  119. n, r, err := models.SearchResourceSpecification(opts)
  120. if err != nil {
  121. return nil, err
  122. }
  123. return models.NewResourceSpecAndQueueListRes(n, r), nil
  124. }
  125. //GetAllDistinctResourceSpecification returns specification and queue after distinct
  126. //totalSize is always 0 here
  127. func GetAllDistinctResourceSpecification(opts models.SearchResourceSpecificationOptions) (*models.ResourceSpecAndQueueListRes, error) {
  128. opts.Page = 0
  129. opts.PageSize = 1000
  130. _, r, err := models.SearchResourceSpecification(opts)
  131. if err != nil {
  132. return nil, err
  133. }
  134. nr := distinctResourceSpecAndQueue(r)
  135. return models.NewResourceSpecAndQueueListRes(0, nr), nil
  136. }
  137. func distinctResourceSpecAndQueue(r []models.ResourceSpecAndQueue) []models.ResourceSpecAndQueue {
  138. specs := make([]models.ResourceSpecAndQueue, 0, len(r))
  139. sourceSpecIdMap := make(map[string]models.ResourceSpecAndQueue, 0)
  140. for i := 0; i < len(r); i++ {
  141. spec := r[i]
  142. if spec.SourceSpecId == "" {
  143. specs = append(specs, spec)
  144. continue
  145. }
  146. if _, has := sourceSpecIdMap[spec.SourceSpecId]; has {
  147. //prefer to use on-shelf spec
  148. if sourceSpecIdMap[spec.SourceSpecId].Status != spec.Status && spec.Status == models.SpecOnShelf {
  149. for k, v := range specs {
  150. if v.ResourceSpecification.ID == sourceSpecIdMap[spec.SourceSpecId].ResourceSpecification.ID {
  151. specs[k] = spec
  152. }
  153. }
  154. }
  155. continue
  156. }
  157. specs = append(specs, spec)
  158. sourceSpecIdMap[spec.SourceSpecId] = spec
  159. }
  160. return specs
  161. }
  162. func GetResourceSpecificationScenes(specId int64) ([]models.ResourceSceneBriefRes, error) {
  163. r, err := models.GetSpecScenes(specId)
  164. if err != nil {
  165. return nil, err
  166. }
  167. return r, nil
  168. }
  169. func ResourceSpecOnShelf(doerId int64, id int64, unitPrice int) *response.BizError {
  170. spec, err := models.GetResourceSpecification(&models.ResourceSpecification{ID: id})
  171. if err != nil {
  172. return response.NewBizError(err)
  173. }
  174. if spec == nil {
  175. return response.SPECIFICATION_NOT_EXIST
  176. }
  177. if q, err := models.GetResourceQueue(&models.ResourceQueue{ID: spec.QueueId}); err != nil || q == nil {
  178. return response.RESOURCE_QUEUE_NOT_AVAILABLE
  179. }
  180. if !spec.IsAvailable {
  181. return response.SPECIFICATION_NOT_AVAILABLE
  182. }
  183. err = models.ResourceSpecOnShelf(id, unitPrice)
  184. if err != nil {
  185. return response.NewBizError(err)
  186. }
  187. if spec.UnitPrice != unitPrice {
  188. AddSpecOperateLog(doerId, "on-shelf", operate_log.NewLogValues().Add("UnitPrice", unitPrice), operate_log.NewLogValues().Add("UnitPrice", spec.UnitPrice), id, fmt.Sprintf("定价上架资源规格,单价为%d", unitPrice))
  189. } else {
  190. AddSpecOperateLog(doerId, "on-shelf", nil, nil, id, "上架资源规格")
  191. }
  192. return nil
  193. }
  194. func ResourceSpecOffShelf(doerId int64, id int64) *response.BizError {
  195. _, err := models.ResourceSpecOffShelf(id)
  196. if err != nil {
  197. return response.NewBizError(err)
  198. }
  199. AddSpecOperateLog(doerId, "off-shelf", nil, nil, id, "下架资源规格")
  200. return nil
  201. }
  202. func AddSpecOperateLog(doerId int64, operateType string, newValue, oldValue *models.LogValues, specId int64, comment string) {
  203. var newString = ""
  204. var oldString = ""
  205. if newValue != nil {
  206. newString = newValue.JsonString()
  207. }
  208. if oldValue != nil {
  209. oldString = oldValue.JsonString()
  210. }
  211. operate_log.Log(models.AdminOperateLog{
  212. BizType: "SpecOperate",
  213. OperateType: operateType,
  214. OldValue: oldString,
  215. NewValue: newString,
  216. RelatedId: fmt.Sprint(specId),
  217. CreatedBy: doerId,
  218. Comment: comment,
  219. })
  220. }
  221. func FindAvailableSpecs(userId int64, opts models.FindSpecsOptions) ([]*models.Specification, error) {
  222. opts.SpecStatus = models.SpecOnShelf
  223. r, err := models.FindSpecs(opts)
  224. if err != nil {
  225. log.Error("FindAvailableSpecs error.%v", err)
  226. return nil, err
  227. }
  228. //filter exclusive specs
  229. specs := filterExclusiveSpecs(r, userId)
  230. //distinct by sourceSpecId
  231. specs = distinctSpecs(specs)
  232. return specs, err
  233. }
  234. func FindAvailableSpecs4Show(userId int64, opts models.FindSpecsOptions) ([]*api.SpecificationShow, error) {
  235. specs, err := FindAvailableSpecs(userId, opts)
  236. if err != nil {
  237. return nil, err
  238. }
  239. result := make([]*api.SpecificationShow, len(specs))
  240. for i, v := range specs {
  241. result[i] = convert.ToSpecification(v)
  242. }
  243. return result, nil
  244. }
  245. func filterExclusiveSpecs(r []*models.Specification, userId int64) []*models.Specification {
  246. specs := make([]*models.Specification, 0, len(r))
  247. specMap := make(map[int64]string, 0)
  248. for i := 0; i < len(r); i++ {
  249. spec := r[i]
  250. if _, has := specMap[spec.ID]; has {
  251. continue
  252. }
  253. if !spec.IsExclusive {
  254. specs = append(specs, spec)
  255. specMap[spec.ID] = ""
  256. continue
  257. }
  258. orgs := strings.Split(spec.ExclusiveOrg, ";")
  259. for _, org := range orgs {
  260. isMember, _ := models.IsOrganizationMemberByOrgName(org, userId)
  261. if isMember {
  262. specs = append(specs, spec)
  263. specMap[spec.ID] = ""
  264. break
  265. }
  266. }
  267. }
  268. return specs
  269. }
  270. func distinctSpecs(r []*models.Specification) []*models.Specification {
  271. specs := make([]*models.Specification, 0, len(r))
  272. sourceSpecIdMap := make(map[string]string, 0)
  273. for i := 0; i < len(r); i++ {
  274. spec := r[i]
  275. if spec.SourceSpecId == "" {
  276. specs = append(specs, spec)
  277. continue
  278. }
  279. if _, has := sourceSpecIdMap[spec.SourceSpecId]; has {
  280. continue
  281. }
  282. specs = append(specs, spec)
  283. sourceSpecIdMap[spec.SourceSpecId] = ""
  284. }
  285. return specs
  286. }
  287. func GetAndCheckSpec(userId int64, specId int64, opts models.FindSpecsOptions) (*models.Specification, error) {
  288. if specId == 0 {
  289. return nil, nil
  290. }
  291. opts.SpecId = specId
  292. r, err := FindAvailableSpecs(userId, opts)
  293. if err != nil {
  294. return nil, err
  295. }
  296. if r == nil || len(r) == 0 {
  297. return nil, nil
  298. }
  299. return r[0], nil
  300. }
  301. func InsertCloudbrainSpec(cloudbrainId int64, s *models.Specification) error {
  302. c := models.CloudbrainSpec{
  303. CloudbrainID: cloudbrainId,
  304. SpecId: s.ID,
  305. SourceSpecId: s.SourceSpecId,
  306. AccCardsNum: s.AccCardsNum,
  307. AccCardType: s.AccCardType,
  308. CpuCores: s.CpuCores,
  309. MemGiB: s.MemGiB,
  310. GPUMemGiB: s.GPUMemGiB,
  311. ShareMemGiB: s.ShareMemGiB,
  312. ComputeResource: s.ComputeResource,
  313. UnitPrice: s.UnitPrice,
  314. QueueId: s.QueueId,
  315. QueueCode: s.QueueCode,
  316. Cluster: s.Cluster,
  317. AiCenterCode: s.AiCenterCode,
  318. AiCenterName: s.AiCenterName,
  319. IsExclusive: s.IsExclusive,
  320. ExclusiveOrg: s.ExclusiveOrg,
  321. }
  322. _, err := models.InsertCloudbrainSpec(c)
  323. if err != nil {
  324. log.Error("InsertCloudbrainSpec error.CloudbrainSpec=%v. err=%v", c, err)
  325. return err
  326. }
  327. return nil
  328. }
  329. func GetCloudbrainSpec(cloudbrainId int64) (*models.Specification, error) {
  330. c, err := models.GetCloudbrainSpecByID(cloudbrainId)
  331. if err != nil {
  332. return nil, err
  333. }
  334. if c == nil {
  335. return nil, nil
  336. }
  337. return c.ConvertToSpecification(), nil
  338. }
  339. func RefreshHistorySpec(scopeAll bool, ids []int64) (int64, int64, error) {
  340. var success int64
  341. var total int64
  342. if !scopeAll {
  343. if ids == nil || len(ids) == 0 {
  344. return 0, 0, nil
  345. }
  346. total = int64(len(ids))
  347. tasks, err := models.GetCloudbrainWithDeletedByIDs(ids)
  348. if err != nil {
  349. return total, 0, err
  350. }
  351. for _, task := range tasks {
  352. err = RefreshOneHistorySpec(task)
  353. if err != nil {
  354. log.Error("RefreshOneHistorySpec error.%v", err)
  355. continue
  356. }
  357. success++
  358. }
  359. } else {
  360. page := 1
  361. pageSize := 100
  362. n, err := models.CountNoSpecHistoricTask()
  363. if err != nil {
  364. log.Error("FindNoSpecHistoricTask CountNoSpecHistoricTask error. e=%v", err)
  365. return 0, 0, err
  366. }
  367. total = n
  368. for i := 0; i < 500; i++ {
  369. list, err := models.FindCloudbrainTask(page, pageSize)
  370. page++
  371. if err != nil {
  372. log.Error("FindCloudbrainTask error.page=%d pageSize=%d e=%v", page, pageSize, err)
  373. return total, success, err
  374. }
  375. if len(list) == 0 {
  376. log.Info("RefreshHistorySpec. list is empty")
  377. break
  378. }
  379. for _, task := range list {
  380. s, err := GetCloudbrainSpec(task.ID)
  381. if err != nil {
  382. log.Error("RefreshHistorySpec GetCloudbrainSpec error.%v", err)
  383. continue
  384. }
  385. if s != nil {
  386. continue
  387. }
  388. err = RefreshOneHistorySpec(task)
  389. if err != nil {
  390. log.Error("RefreshOneHistorySpec error.%v", err)
  391. continue
  392. }
  393. success++
  394. }
  395. if len(list) < pageSize {
  396. log.Info("RefreshHistorySpec. list < pageSize")
  397. break
  398. }
  399. }
  400. }
  401. return total, success, nil
  402. }
  403. func RefreshOneHistorySpec(task *models.Cloudbrain) error {
  404. var spec *models.Specification
  405. var err error
  406. switch task.Type {
  407. case models.TypeCloudBrainOne:
  408. spec, err = getCloudbrainOneSpec(task)
  409. case models.TypeCloudBrainTwo:
  410. spec, err = getCloudbrainTwoSpec(task)
  411. case models.TypeC2Net:
  412. spec, err = getGrampusSpec(task)
  413. }
  414. if err != nil {
  415. log.Error("find spec error,task.ID=%d err=%v", task.ID, err)
  416. return err
  417. }
  418. if spec == nil {
  419. log.Error("find spec failed,task.ID=%d", task.ID)
  420. return errors.New("find spec failed")
  421. }
  422. return InsertCloudbrainSpec(task.ID, spec)
  423. }
  424. func getCloudbrainOneSpec(task *models.Cloudbrain) (*models.Specification, error) {
  425. if task.GpuQueue == "" {
  426. log.Info("gpu queue is empty.task.ID = %d", task.ID)
  427. return nil, nil
  428. }
  429. //find from config
  430. spec, err := findCloudbrainOneSpecFromConfig(task)
  431. if err != nil {
  432. log.Error("getCloudbrainOneSpec findCloudbrainOneSpecFromConfig error.%v", err)
  433. return nil, err
  434. }
  435. if spec != nil {
  436. return spec, nil
  437. }
  438. //find from remote
  439. return findCloudbrainOneSpecFromRemote(task)
  440. }
  441. func findCloudbrainOneSpecFromRemote(task *models.Cloudbrain) (*models.Specification, error) {
  442. time.Sleep(200 * time.Millisecond)
  443. log.Info("start findCloudbrainOneSpecFromRemote")
  444. result, err := cloudbrain.GetJob(task.JobID)
  445. if err != nil {
  446. log.Error("getCloudbrainOneSpec error. %v", err)
  447. return nil, err
  448. }
  449. if result == nil {
  450. log.Info("findCloudbrainOneSpecFromRemote failed,result is empty.task.ID=%d", task.ID)
  451. return nil, nil
  452. }
  453. jobRes, _ := models.ConvertToJobResultPayload(result.Payload)
  454. memSize, _ := models.ParseMemSizeFromGrampus(jobRes.Resource.Memory)
  455. if task.ComputeResource == "CPU/GPU" {
  456. task.ComputeResource = models.GPU
  457. }
  458. var shmMB float32
  459. if jobRes.Config.TaskRoles != nil && len(jobRes.Config.TaskRoles) > 0 {
  460. shmMB = float32(jobRes.Config.TaskRoles[0].ShmMB) / 1024
  461. if jobRes.Config.TaskRoles[0].ShmMB == 103600 {
  462. shmMB = 100
  463. } else if jobRes.Config.TaskRoles[0].ShmMB == 51800 {
  464. shmMB = 50
  465. }
  466. }
  467. opt := models.FindSpecsOptions{
  468. ComputeResource: task.ComputeResource,
  469. Cluster: models.OpenICluster,
  470. AiCenterCode: models.AICenterOfCloudBrainOne,
  471. QueueCode: task.GpuQueue,
  472. AccCardsNum: jobRes.Resource.NvidiaComGpu,
  473. UseAccCardsNum: true,
  474. CpuCores: jobRes.Resource.CPU,
  475. UseCpuCores: true,
  476. MemGiB: memSize,
  477. UseMemGiB: memSize > 0,
  478. ShareMemGiB: shmMB,
  479. UseShareMemGiB: shmMB > 0,
  480. RequestAll: true,
  481. }
  482. specs, err := models.FindSpecs(opt)
  483. if err != nil {
  484. log.Error("getCloudbrainOneSpec from remote error,%v", err)
  485. return nil, err
  486. }
  487. if len(specs) == 1 {
  488. return specs[0], nil
  489. }
  490. if len(specs) == 0 {
  491. s, err := InitQueueAndSpec(opt, "云脑一", "处理历史云脑任务时自动添加")
  492. if err != nil {
  493. log.Error("getCloudbrainOneSpec InitQueueAndSpec error.err=%v", err)
  494. return nil, nil
  495. }
  496. return s, nil
  497. }
  498. log.Error("Too many results matched.size=%d opt=%+v", len(specs), opt)
  499. return nil, nil
  500. }
  501. func findCloudbrainOneSpecFromConfig(task *models.Cloudbrain) (*models.Specification, error) {
  502. //find from config
  503. var specConfig *models.ResourceSpec
  504. hasSpec := false
  505. if task.JobType == string(models.JobTypeTrain) {
  506. if cloudbrain.TrainResourceSpecs == nil {
  507. json.Unmarshal([]byte(setting.TrainResourceSpecs), &cloudbrain.TrainResourceSpecs)
  508. }
  509. for _, tmp := range cloudbrain.TrainResourceSpecs.ResourceSpec {
  510. if tmp.Id == task.ResourceSpecId {
  511. hasSpec = true
  512. specConfig = tmp
  513. break
  514. }
  515. }
  516. } else if task.JobType == string(models.JobTypeInference) {
  517. if cloudbrain.InferenceResourceSpecs == nil {
  518. json.Unmarshal([]byte(setting.InferenceResourceSpecs), &cloudbrain.InferenceResourceSpecs)
  519. }
  520. for _, tmp := range cloudbrain.InferenceResourceSpecs.ResourceSpec {
  521. if tmp.Id == task.ResourceSpecId {
  522. hasSpec = true
  523. specConfig = tmp
  524. break
  525. }
  526. }
  527. } else {
  528. if cloudbrain.ResourceSpecs == nil {
  529. json.Unmarshal([]byte(setting.ResourceSpecs), &cloudbrain.ResourceSpecs)
  530. }
  531. for _, tmp := range cloudbrain.ResourceSpecs.ResourceSpec {
  532. if tmp.Id == task.ResourceSpecId {
  533. hasSpec = true
  534. specConfig = tmp
  535. break
  536. }
  537. }
  538. }
  539. if !hasSpec && cloudbrain.SpecialPools != nil {
  540. for _, specialPool := range cloudbrain.SpecialPools.Pools {
  541. if specialPool.ResourceSpec != nil {
  542. for _, spec := range specialPool.ResourceSpec {
  543. if task.ResourceSpecId == spec.Id {
  544. hasSpec = true
  545. specConfig = spec
  546. break
  547. }
  548. }
  549. }
  550. }
  551. }
  552. if specConfig == nil {
  553. log.Error("getCloudbrainOneSpec from config failed,task.ResourceSpecId=%d", task.ResourceSpecId)
  554. return nil, nil
  555. }
  556. if task.ComputeResource == "CPU/GPU" {
  557. task.ComputeResource = models.GPU
  558. }
  559. shareMemMiB := float32(specConfig.ShareMemMiB) / 1024
  560. if specConfig.ShareMemMiB == 103600 {
  561. shareMemMiB = 100
  562. } else if specConfig.ShareMemMiB == 51800 {
  563. shareMemMiB = 50
  564. }
  565. opt := models.FindSpecsOptions{
  566. JobType: models.JobType(task.JobType),
  567. ComputeResource: task.ComputeResource,
  568. Cluster: models.OpenICluster,
  569. AiCenterCode: models.AICenterOfCloudBrainOne,
  570. QueueCode: task.GpuQueue,
  571. AccCardsNum: specConfig.GpuNum,
  572. UseAccCardsNum: true,
  573. CpuCores: specConfig.CpuNum,
  574. UseCpuCores: true,
  575. MemGiB: float32(specConfig.MemMiB) / 1024,
  576. UseMemGiB: true,
  577. ShareMemGiB: shareMemMiB,
  578. UseShareMemGiB: true,
  579. RequestAll: true,
  580. }
  581. specs, err := models.FindSpecs(opt)
  582. if err != nil {
  583. log.Error("getCloudbrainOneSpec from config error,%v", err)
  584. return nil, err
  585. }
  586. if len(specs) > 1 {
  587. log.Error("Too many results matched.size=%d opt=%+v", len(specs), opt)
  588. return nil, nil
  589. }
  590. if len(specs) == 0 {
  591. s, err := InitQueueAndSpec(opt, "云脑一", "处理历史云脑任务时自动添加")
  592. if err != nil {
  593. log.Error("getCloudbrainOneSpec InitQueueAndSpec error.err=%v", err)
  594. return nil, nil
  595. }
  596. return s, nil
  597. }
  598. return specs[0], nil
  599. }
  600. func getCloudbrainTwoSpec(task *models.Cloudbrain) (*models.Specification, error) {
  601. specMap, err := models.GetCloudbrainTwoSpecs()
  602. if err != nil {
  603. log.Error("InitCloudbrainTwoSpecs err.%v", err)
  604. return nil, err
  605. }
  606. if task.FlavorCode != "" {
  607. return specMap[task.FlavorCode], nil
  608. }
  609. time.Sleep(200 * time.Millisecond)
  610. log.Info("start getCloudbrainTwoSpec FromRemote")
  611. if task.JobType == string(models.JobTypeDebug) {
  612. result, err := modelarts.GetNotebook2(task.JobID)
  613. if err != nil {
  614. log.Error("getCloudbrainTwoSpec GetNotebook2 error.%v", err)
  615. return nil, err
  616. }
  617. if result != nil {
  618. return specMap[result.Flavor], nil
  619. }
  620. } else if task.JobType == string(models.JobTypeTrain) || task.JobType == string(models.JobTypeInference) {
  621. result, err := modelarts.GetTrainJob(task.JobID, strconv.FormatInt(task.VersionID, 10))
  622. if err != nil {
  623. log.Error("getCloudbrainTwoSpec GetTrainJob error:%v", task.JobName, err)
  624. return nil, err
  625. }
  626. if result != nil {
  627. return specMap[result.Flavor.Code], nil
  628. }
  629. }
  630. return nil, nil
  631. }
  632. func getGrampusSpec(task *models.Cloudbrain) (*models.Specification, error) {
  633. specMap, err := models.GetGrampusSpecs()
  634. if err != nil {
  635. log.Error("GetGrampusSpecs err.%v", err)
  636. return nil, err
  637. }
  638. if task.AiCenter != "" {
  639. c := strings.Split(task.AiCenter, "+")
  640. spec := specMap[task.FlavorCode+"_"+c[0]]
  641. if spec != nil {
  642. return spec, nil
  643. }
  644. }
  645. return specMap[task.FlavorCode], nil
  646. }
  647. func InitQueueAndSpec(opt models.FindSpecsOptions, aiCenterName string, remark string) (*models.Specification, error) {
  648. return models.InitQueueAndSpec(models.ResourceQueue{
  649. QueueCode: opt.QueueCode,
  650. Cluster: opt.Cluster,
  651. AiCenterCode: opt.AiCenterCode,
  652. AiCenterName: aiCenterName,
  653. ComputeResource: opt.ComputeResource,
  654. AccCardType: models.GetCloudbrainOneAccCardType(opt.QueueCode),
  655. Remark: remark,
  656. }, models.ResourceSpecification{
  657. AccCardsNum: opt.AccCardsNum,
  658. CpuCores: opt.CpuCores,
  659. MemGiB: opt.MemGiB,
  660. GPUMemGiB: opt.GPUMemGiB,
  661. ShareMemGiB: opt.ShareMemGiB,
  662. Status: models.SpecOffShelf,
  663. IsAvailable: true,
  664. })
  665. }