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