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.

imageinferencelogic.go 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package inference
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers/option"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/service/inference"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/strategy"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc"
  10. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
  11. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  12. "net/http"
  13. )
  14. type ImageInferenceLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewImageInferenceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageInferenceLogic {
  20. return &ImageInferenceLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. }
  25. }
  26. func (l *ImageInferenceLogic) ImageInference(req *types.ImageInferenceReq) (resp *types.ImageInferenceResp, err error) {
  27. return nil, nil
  28. }
  29. func (l *ImageInferenceLogic) ImageInfer(r *http.Request, req *types.ImageInferenceReq) (resp *types.ImageInferenceResp, err error) {
  30. resp = &types.ImageInferenceResp{}
  31. opt := &option.InferOption{
  32. TaskName: req.TaskName,
  33. TaskDesc: req.TaskDesc,
  34. AdapterId: req.AdapterId,
  35. AiClusterIds: req.AiClusterIds,
  36. ModelName: req.ModelName,
  37. ModelType: req.ModelType,
  38. Strategy: req.Strategy,
  39. StaticWeightMap: req.StaticWeightMap,
  40. }
  41. var ts []*inference.ImageFile
  42. uploadedFiles := r.MultipartForm.File
  43. if len(uploadedFiles) == 0 {
  44. return nil, errors.New("Images does not exist")
  45. }
  46. if len(uploadedFiles["images"]) == 0 {
  47. return nil, errors.New("Images does not exist")
  48. }
  49. for _, header := range uploadedFiles["images"] {
  50. file, err := header.Open()
  51. if err != nil {
  52. return nil, err
  53. }
  54. defer file.Close()
  55. var ir types.ImageResult
  56. ir.ImageName = header.Filename
  57. t := inference.ImageFile{
  58. ImageResult: &ir,
  59. File: file,
  60. }
  61. ts = append(ts, &t)
  62. }
  63. _, ok := l.svcCtx.Scheduler.AiService.AiCollectorAdapterMap[opt.AdapterId]
  64. if !ok {
  65. return nil, errors.New("AdapterId does not exist")
  66. }
  67. var strat strategy.Strategy
  68. switch opt.Strategy {
  69. case strategy.STATIC_WEIGHT:
  70. strat = strategy.NewStaticWeightStrategy(opt.StaticWeightMap, int32(len(ts)))
  71. if err != nil {
  72. return nil, err
  73. }
  74. default:
  75. return nil, errors.New("no strategy has been chosen")
  76. }
  77. clusters, err := strat.Schedule()
  78. if err != nil {
  79. return nil, err
  80. }
  81. if clusters == nil || len(clusters) == 0 {
  82. return nil, errors.New("clusters is nil")
  83. }
  84. //save task
  85. var synergystatus int64
  86. if len(clusters) > 1 {
  87. synergystatus = 1
  88. }
  89. strategyCode, err := l.svcCtx.Scheduler.AiStorages.GetStrategyCode(opt.Strategy)
  90. if err != nil {
  91. return nil, err
  92. }
  93. adapterName, err := l.svcCtx.Scheduler.AiStorages.GetAdapterNameById(opt.AdapterId)
  94. if err != nil {
  95. return nil, err
  96. }
  97. id, err := l.svcCtx.Scheduler.AiStorages.SaveTask(opt.TaskName, strategyCode, synergystatus, "11")
  98. if err != nil {
  99. return nil, err
  100. }
  101. l.svcCtx.Scheduler.AiStorages.AddNoticeInfo(opt.AdapterId, adapterName, "", "", opt.TaskName, "create", "任务创建中")
  102. for i := len(clusters) - 1; i >= 0; i-- {
  103. if clusters[i].Replicas == 0 {
  104. clusters = append(clusters[:i], clusters[i+1:]...)
  105. }
  106. }
  107. //save taskai
  108. for _, c := range clusters {
  109. clusterName, _ := l.svcCtx.Scheduler.AiStorages.GetClusterNameById(c.ClusterId)
  110. opt.Replica = c.Replicas
  111. err := l.svcCtx.Scheduler.AiStorages.SaveAiTask(id, opt, adapterName, c.ClusterId, clusterName, "", constants.Saved, "")
  112. if err != nil {
  113. return nil, err
  114. }
  115. }
  116. go l.svcCtx.Scheduler.AiService.ImageInfer(opt, id, adapterName, clusters, ts)
  117. return resp, nil
  118. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.