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
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. func GetResourceSpecificationScenes(specId int64) ([]models.ResourceSceneBriefRes, error) {
  126. r, err := models.GetSpecScenes(specId)
  127. if err != nil {
  128. return nil, err
  129. }
  130. return r, nil
  131. }
  132. func ResourceSpecOnShelf(doerId int64, id int64, unitPrice int) *response.BizError {
  133. spec, err := models.GetResourceSpecification(&models.ResourceSpecification{ID: id})
  134. if err != nil {
  135. return response.NewBizError(err)
  136. }
  137. if spec == nil {
  138. return response.SPECIFICATION_NOT_EXIST
  139. }
  140. if q, err := models.GetResourceQueue(&models.ResourceQueue{ID: spec.QueueId}); err != nil || q == nil {
  141. return response.RESOURCE_QUEUE_NOT_AVAILABLE
  142. }
  143. if !spec.IsAvailable {
  144. return response.SPECIFICATION_NOT_AVAILABLE
  145. }
  146. err = models.ResourceSpecOnShelf(id, unitPrice)
  147. if err != nil {
  148. return response.NewBizError(err)
  149. }
  150. if spec.UnitPrice != unitPrice {
  151. AddSpecOperateLog(doerId, "on-shelf", operate_log.NewLogValues().Add("UnitPrice", unitPrice), operate_log.NewLogValues().Add("UnitPrice", spec.UnitPrice), id, fmt.Sprintf("定价上架资源规格,单价为%d", unitPrice))
  152. } else {
  153. AddSpecOperateLog(doerId, "on-shelf", nil, nil, id, "上架资源规格")
  154. }
  155. return nil
  156. }
  157. func ResourceSpecOffShelf(doerId int64, id int64) *response.BizError {
  158. _, err := models.ResourceSpecOffShelf(id)
  159. if err != nil {
  160. return response.NewBizError(err)
  161. }
  162. AddSpecOperateLog(doerId, "off-shelf", nil, nil, id, "下架资源规格")
  163. return nil
  164. }
  165. func AddSpecOperateLog(doerId int64, operateType string, newValue, oldValue *models.LogValues, specId int64, comment string) {
  166. var newString = ""
  167. var oldString = ""
  168. if newValue != nil {
  169. newString = newValue.JsonString()
  170. }
  171. if oldValue != nil {
  172. oldString = oldValue.JsonString()
  173. }
  174. operate_log.Log(models.AdminOperateLog{
  175. BizType: "SpecOperate",
  176. OperateType: operateType,
  177. OldValue: oldString,
  178. NewValue: newString,
  179. RelatedId: fmt.Sprint(specId),
  180. CreatedBy: doerId,
  181. Comment: comment,
  182. })
  183. }
  184. func FindAvailableSpecs(userId int64, opts models.FindSpecsOptions) ([]*models.Specification, error) {
  185. opts.SpecStatus = models.SpecOnShelf
  186. r, err := models.FindSpecs(opts)
  187. if err != nil {
  188. log.Error("FindAvailableSpecs error.%v", err)
  189. return nil, err
  190. }
  191. //filter exclusive specs
  192. specs := filterExclusiveSpecs(r, userId)
  193. //distinct by sourceSpecId
  194. specs = distinctSpecs(specs)
  195. return specs, err
  196. }
  197. func FindAvailableSpecs4Show(userId int64, opts models.FindSpecsOptions) ([]*api.SpecificationShow, error) {
  198. specs, err := FindAvailableSpecs(userId, opts)
  199. if err != nil {
  200. return nil, err
  201. }
  202. result := make([]*api.SpecificationShow, len(specs))
  203. for i, v := range specs {
  204. result[i] = convert.ToSpecification(v)
  205. }
  206. return result, nil
  207. }
  208. func filterExclusiveSpecs(r []*models.Specification, userId int64) []*models.Specification {
  209. specs := make([]*models.Specification, 0, len(r))
  210. specMap := make(map[int64]string, 0)
  211. for i := 0; i < len(r); i++ {
  212. spec := r[i]
  213. if _, has := specMap[spec.ID]; has {
  214. continue
  215. }
  216. if !spec.IsExclusive {
  217. specs = append(specs, spec)
  218. specMap[spec.ID] = ""
  219. continue
  220. }
  221. orgs := strings.Split(spec.ExclusiveOrg, ";")
  222. for _, org := range orgs {
  223. isMember, _ := models.IsOrganizationMemberByOrgName(org, userId)
  224. if isMember {
  225. specs = append(specs, spec)
  226. specMap[spec.ID] = ""
  227. break
  228. }
  229. }
  230. }
  231. return specs
  232. }
  233. func distinctSpecs(r []*models.Specification) []*models.Specification {
  234. specs := make([]*models.Specification, 0, len(r))
  235. sourceSpecIdMap := make(map[string]string, 0)
  236. for i := 0; i < len(r); i++ {
  237. spec := r[i]
  238. if spec.SourceSpecId == "" {
  239. specs = append(specs, spec)
  240. continue
  241. }
  242. if _, has := sourceSpecIdMap[spec.SourceSpecId]; has {
  243. continue
  244. }
  245. specs = append(specs, spec)
  246. sourceSpecIdMap[spec.SourceSpecId] = ""
  247. }
  248. return specs
  249. }
  250. func GetAndCheckSpec(userId int64, specId int64, opts models.FindSpecsOptions) (*models.Specification, error) {
  251. if specId == 0 {
  252. return nil, nil
  253. }
  254. opts.SpecId = specId
  255. r, err := FindAvailableSpecs(userId, opts)
  256. if err != nil {
  257. return nil, err
  258. }
  259. if r == nil || len(r) == 0 {
  260. return nil, nil
  261. }
  262. return r[0], nil
  263. }
  264. func InsertCloudbrainSpec(cloudbrainId int64, s *models.Specification) error {
  265. c := models.CloudbrainSpec{
  266. CloudbrainID: cloudbrainId,
  267. SpecId: s.ID,
  268. SourceSpecId: s.SourceSpecId,
  269. AccCardsNum: s.AccCardsNum,
  270. AccCardType: s.AccCardType,
  271. CpuCores: s.CpuCores,
  272. MemGiB: s.MemGiB,
  273. GPUMemGiB: s.GPUMemGiB,
  274. ShareMemGiB: s.ShareMemGiB,
  275. ComputeResource: s.ComputeResource,
  276. UnitPrice: s.UnitPrice,
  277. QueueId: s.QueueId,
  278. QueueCode: s.QueueCode,
  279. Cluster: s.Cluster,
  280. AiCenterCode: s.AiCenterCode,
  281. AiCenterName: s.AiCenterName,
  282. IsExclusive: s.IsExclusive,
  283. ExclusiveOrg: s.ExclusiveOrg,
  284. }
  285. _, err := models.InsertCloudbrainSpec(c)
  286. if err != nil {
  287. log.Error("InsertCloudbrainSpec error.CloudbrainSpec=%v. err=%v", c, err)
  288. return err
  289. }
  290. return nil
  291. }
  292. func GetCloudbrainSpec(cloudbrainId int64) (*models.Specification, error) {
  293. c, err := models.GetCloudbrainSpecByID(cloudbrainId)
  294. if err != nil {
  295. return nil, err
  296. }
  297. if c == nil {
  298. return nil, nil
  299. }
  300. return c.ConvertToSpecification(), nil
  301. }
  302. func RefreshHistorySpec(scopeAll bool, ids []int64) (int64, int64, error) {
  303. var success int64
  304. var total int64
  305. if !scopeAll {
  306. if ids == nil || len(ids) == 0 {
  307. return 0, 0, nil
  308. }
  309. total = int64(len(ids))
  310. tasks, err := models.GetCloudbrainWithDeletedByIDs(ids)
  311. if err != nil {
  312. return total, 0, err
  313. }
  314. for _, task := range tasks {
  315. err = RefreshOneHistorySpec(task)
  316. if err != nil {
  317. log.Error("RefreshOneHistorySpec error.%v", err)
  318. continue
  319. }
  320. success++
  321. }
  322. } else {
  323. page := 1
  324. pageSize := 100
  325. n, err := models.CountNoSpecHistoricTask()
  326. if err != nil {
  327. log.Error("FindNoSpecHistoricTask CountNoSpecHistoricTask error. e=%v", err)
  328. return 0, 0, err
  329. }
  330. total = n
  331. for i := 0; i < 500; i++ {
  332. list, err := models.FindCloudbrainTask(page, pageSize)
  333. page++
  334. if err != nil {
  335. log.Error("FindCloudbrainTask error.page=%d pageSize=%d e=%v", page, pageSize, err)
  336. return total, success, err
  337. }
  338. if len(list) == 0 {
  339. log.Info("RefreshHistorySpec. list is empty")
  340. break
  341. }
  342. for _, task := range list {
  343. s, err := GetCloudbrainSpec(task.ID)
  344. if err != nil {
  345. log.Error("RefreshHistorySpec GetCloudbrainSpec error.%v", err)
  346. continue
  347. }
  348. if s != nil {
  349. continue
  350. }
  351. err = RefreshOneHistorySpec(task)
  352. if err != nil {
  353. log.Error("RefreshOneHistorySpec error.%v", err)
  354. continue
  355. }
  356. success++
  357. }
  358. if len(list) < pageSize {
  359. log.Info("RefreshHistorySpec. list < pageSize")
  360. break
  361. }
  362. }
  363. }
  364. return total, success, nil
  365. }
  366. func RefreshOneHistorySpec(task *models.Cloudbrain) error {
  367. var spec *models.Specification
  368. var err error
  369. switch task.Type {
  370. case models.TypeCloudBrainOne:
  371. spec, err = getCloudbrainOneSpec(task)
  372. case models.TypeCloudBrainTwo:
  373. spec, err = getCloudbrainTwoSpec(task)
  374. case models.TypeC2Net:
  375. spec, err = getGrampusSpec(task)
  376. }
  377. if err != nil {
  378. log.Error("find spec error,task.ID=%d err=%v", task.ID, err)
  379. return err
  380. }
  381. if spec == nil {
  382. log.Error("find spec failed,task.ID=%d", task.ID)
  383. return errors.New("find spec failed")
  384. }
  385. return InsertCloudbrainSpec(task.ID, spec)
  386. }
  387. func getCloudbrainOneSpec(task *models.Cloudbrain) (*models.Specification, error) {
  388. if task.GpuQueue == "" {
  389. log.Info("gpu queue is empty.task.ID = %d", task.ID)
  390. return nil, nil
  391. }
  392. //find from config
  393. spec, err := findCloudbrainOneSpecFromConfig(task)
  394. if err != nil {
  395. log.Error("getCloudbrainOneSpec findCloudbrainOneSpecFromConfig error.%v", err)
  396. return nil, err
  397. }
  398. if spec != nil {
  399. return spec, nil
  400. }
  401. //find from remote
  402. return findCloudbrainOneSpecFromRemote(task)
  403. }
  404. func findCloudbrainOneSpecFromRemote(task *models.Cloudbrain) (*models.Specification, error) {
  405. time.Sleep(200 * time.Millisecond)
  406. log.Info("start findCloudbrainOneSpecFromRemote")
  407. result, err := cloudbrain.GetJob(task.JobID)
  408. if err != nil {
  409. log.Error("getCloudbrainOneSpec error. %v", err)
  410. return nil, err
  411. }
  412. if result == nil {
  413. log.Info("findCloudbrainOneSpecFromRemote failed,result is empty.task.ID=%d", task.ID)
  414. return nil, nil
  415. }
  416. jobRes, _ := models.ConvertToJobResultPayload(result.Payload)
  417. memSize, _ := models.ParseMemSizeFromGrampus(jobRes.Resource.Memory)
  418. if task.ComputeResource == "CPU/GPU" {
  419. task.ComputeResource = models.GPU
  420. }
  421. var shmMB float32
  422. if jobRes.Config.TaskRoles != nil && len(jobRes.Config.TaskRoles) > 0 {
  423. shmMB = float32(jobRes.Config.TaskRoles[0].ShmMB) / 1024
  424. if jobRes.Config.TaskRoles[0].ShmMB == 103600 {
  425. shmMB = 100
  426. } else if jobRes.Config.TaskRoles[0].ShmMB == 51800 {
  427. shmMB = 50
  428. }
  429. }
  430. opt := models.FindSpecsOptions{
  431. ComputeResource: task.ComputeResource,
  432. Cluster: models.OpenICluster,
  433. AiCenterCode: models.AICenterOfCloudBrainOne,
  434. QueueCode: task.GpuQueue,
  435. AccCardsNum: jobRes.Resource.NvidiaComGpu,
  436. UseAccCardsNum: true,
  437. CpuCores: jobRes.Resource.CPU,
  438. UseCpuCores: true,
  439. MemGiB: memSize,
  440. UseMemGiB: memSize > 0,
  441. ShareMemGiB: shmMB,
  442. UseShareMemGiB: shmMB > 0,
  443. RequestAll: true,
  444. }
  445. specs, err := models.FindSpecs(opt)
  446. if err != nil {
  447. log.Error("getCloudbrainOneSpec from remote error,%v", err)
  448. return nil, err
  449. }
  450. if len(specs) == 1 {
  451. return specs[0], nil
  452. }
  453. if len(specs) == 0 {
  454. s, err := InitQueueAndSpec(opt, "云脑一", "处理历史云脑任务时自动添加")
  455. if err != nil {
  456. log.Error("getCloudbrainOneSpec InitQueueAndSpec error.err=%v", err)
  457. return nil, nil
  458. }
  459. return s, nil
  460. }
  461. log.Error("Too many results matched.size=%d opt=%+v", len(specs), opt)
  462. return nil, nil
  463. }
  464. func findCloudbrainOneSpecFromConfig(task *models.Cloudbrain) (*models.Specification, error) {
  465. //find from config
  466. var specConfig *models.ResourceSpec
  467. hasSpec := false
  468. if task.JobType == string(models.JobTypeTrain) {
  469. if cloudbrain.TrainResourceSpecs == nil {
  470. json.Unmarshal([]byte(setting.TrainResourceSpecs), &cloudbrain.TrainResourceSpecs)
  471. }
  472. for _, tmp := range cloudbrain.TrainResourceSpecs.ResourceSpec {
  473. if tmp.Id == task.ResourceSpecId {
  474. hasSpec = true
  475. specConfig = tmp
  476. break
  477. }
  478. }
  479. } else if task.JobType == string(models.JobTypeInference) {
  480. if cloudbrain.InferenceResourceSpecs == nil {
  481. json.Unmarshal([]byte(setting.InferenceResourceSpecs), &cloudbrain.InferenceResourceSpecs)
  482. }
  483. for _, tmp := range cloudbrain.InferenceResourceSpecs.ResourceSpec {
  484. if tmp.Id == task.ResourceSpecId {
  485. hasSpec = true
  486. specConfig = tmp
  487. break
  488. }
  489. }
  490. } else {
  491. if cloudbrain.ResourceSpecs == nil {
  492. json.Unmarshal([]byte(setting.ResourceSpecs), &cloudbrain.ResourceSpecs)
  493. }
  494. for _, tmp := range cloudbrain.ResourceSpecs.ResourceSpec {
  495. if tmp.Id == task.ResourceSpecId {
  496. hasSpec = true
  497. specConfig = tmp
  498. break
  499. }
  500. }
  501. }
  502. if !hasSpec && cloudbrain.SpecialPools != nil {
  503. for _, specialPool := range cloudbrain.SpecialPools.Pools {
  504. if specialPool.ResourceSpec != nil {
  505. for _, spec := range specialPool.ResourceSpec {
  506. if task.ResourceSpecId == spec.Id {
  507. hasSpec = true
  508. specConfig = spec
  509. break
  510. }
  511. }
  512. }
  513. }
  514. }
  515. if specConfig == nil {
  516. log.Error("getCloudbrainOneSpec from config failed,task.ResourceSpecId=%d", task.ResourceSpecId)
  517. return nil, nil
  518. }
  519. if task.ComputeResource == "CPU/GPU" {
  520. task.ComputeResource = models.GPU
  521. }
  522. shareMemMiB := float32(specConfig.ShareMemMiB) / 1024
  523. if specConfig.ShareMemMiB == 103600 {
  524. shareMemMiB = 100
  525. } else if specConfig.ShareMemMiB == 51800 {
  526. shareMemMiB = 50
  527. }
  528. opt := models.FindSpecsOptions{
  529. JobType: models.JobType(task.JobType),
  530. ComputeResource: task.ComputeResource,
  531. Cluster: models.OpenICluster,
  532. AiCenterCode: models.AICenterOfCloudBrainOne,
  533. QueueCode: task.GpuQueue,
  534. AccCardsNum: specConfig.GpuNum,
  535. UseAccCardsNum: true,
  536. CpuCores: specConfig.CpuNum,
  537. UseCpuCores: true,
  538. MemGiB: float32(specConfig.MemMiB) / 1024,
  539. UseMemGiB: true,
  540. ShareMemGiB: shareMemMiB,
  541. UseShareMemGiB: true,
  542. RequestAll: true,
  543. }
  544. specs, err := models.FindSpecs(opt)
  545. if err != nil {
  546. log.Error("getCloudbrainOneSpec from config error,%v", err)
  547. return nil, err
  548. }
  549. if len(specs) > 1 {
  550. log.Error("Too many results matched.size=%d opt=%+v", len(specs), opt)
  551. return nil, nil
  552. }
  553. if len(specs) == 0 {
  554. s, err := InitQueueAndSpec(opt, "云脑一", "处理历史云脑任务时自动添加")
  555. if err != nil {
  556. log.Error("getCloudbrainOneSpec InitQueueAndSpec error.err=%v", err)
  557. return nil, nil
  558. }
  559. return s, nil
  560. }
  561. return specs[0], nil
  562. }
  563. func getCloudbrainTwoSpec(task *models.Cloudbrain) (*models.Specification, error) {
  564. specMap, err := models.GetCloudbrainTwoSpecs()
  565. if err != nil {
  566. log.Error("InitCloudbrainTwoSpecs err.%v", err)
  567. return nil, err
  568. }
  569. if task.FlavorCode != "" {
  570. return specMap[task.FlavorCode], nil
  571. }
  572. time.Sleep(200 * time.Millisecond)
  573. log.Info("start getCloudbrainTwoSpec FromRemote")
  574. if task.JobType == string(models.JobTypeDebug) {
  575. result, err := modelarts.GetNotebook2(task.JobID)
  576. if err != nil {
  577. log.Error("getCloudbrainTwoSpec GetNotebook2 error.%v", err)
  578. return nil, err
  579. }
  580. if result != nil {
  581. return specMap[result.Flavor], nil
  582. }
  583. } else if task.JobType == string(models.JobTypeTrain) || task.JobType == string(models.JobTypeInference) {
  584. result, err := modelarts.GetTrainJob(task.JobID, strconv.FormatInt(task.VersionID, 10))
  585. if err != nil {
  586. log.Error("getCloudbrainTwoSpec GetTrainJob error:%v", task.JobName, err)
  587. return nil, err
  588. }
  589. if result != nil {
  590. return specMap[result.Flavor.Code], nil
  591. }
  592. }
  593. return nil, nil
  594. }
  595. func getGrampusSpec(task *models.Cloudbrain) (*models.Specification, error) {
  596. specMap, err := models.GetGrampusSpecs()
  597. if err != nil {
  598. log.Error("GetGrampusSpecs err.%v", err)
  599. return nil, err
  600. }
  601. if task.AiCenter != "" {
  602. c := strings.Split(task.AiCenter, "+")
  603. spec := specMap[task.FlavorCode+"_"+c[0]]
  604. if spec != nil {
  605. return spec, nil
  606. }
  607. }
  608. return specMap[task.FlavorCode], nil
  609. }
  610. func InitQueueAndSpec(opt models.FindSpecsOptions, aiCenterName string, remark string) (*models.Specification, error) {
  611. return models.InitQueueAndSpec(models.ResourceQueue{
  612. QueueCode: opt.QueueCode,
  613. Cluster: opt.Cluster,
  614. AiCenterCode: opt.AiCenterCode,
  615. AiCenterName: aiCenterName,
  616. ComputeResource: opt.ComputeResource,
  617. AccCardType: models.GetCloudbrainOneAccCardType(opt.QueueCode),
  618. Remark: remark,
  619. }, models.ResourceSpecification{
  620. AccCardsNum: opt.AccCardsNum,
  621. CpuCores: opt.CpuCores,
  622. MemGiB: opt.MemGiB,
  623. GPUMemGiB: opt.GPUMemGiB,
  624. ShareMemGiB: opt.ShareMemGiB,
  625. Status: models.SpecOffShelf,
  626. IsAvailable: true,
  627. })
  628. }