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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. //
  27. //func (l *ImageInferenceLogic) ImageInference(req *types.ImageInferenceReq) (resp *types.ImageInferenceResp, err error) {
  28. // return nil, nil
  29. //}
  30. func (l *ImageInferenceLogic) ImageInfer(r *http.Request, req *types.ImageInferenceReq) (resp *types.ImageInferenceResp, err error) {
  31. resp = &types.ImageInferenceResp{}
  32. opt := &option.InferOption{
  33. TaskName: req.TaskName,
  34. TaskDesc: req.TaskDesc,
  35. AdapterId: req.AdapterId,
  36. AiClusterIds: req.AiClusterIds,
  37. ModelName: req.ModelName,
  38. ModelType: req.ModelType,
  39. Strategy: req.Strategy,
  40. StaticWeightMap: req.StaticWeightMap,
  41. }
  42. var ts []*inference.ImageFile
  43. uploadedFiles := r.MultipartForm.File
  44. if len(uploadedFiles) == 0 {
  45. return nil, errors.New("Images does not exist")
  46. }
  47. if len(uploadedFiles["images"]) == 0 {
  48. return nil, errors.New("Images does not exist")
  49. }
  50. for _, header := range uploadedFiles["images"] {
  51. file, err := header.Open()
  52. if err != nil {
  53. return nil, err
  54. }
  55. defer file.Close()
  56. var ir types.ImageResult
  57. ir.ImageName = header.Filename
  58. t := inference.ImageFile{
  59. ImageResult: &ir,
  60. File: file,
  61. }
  62. ts = append(ts, &t)
  63. }
  64. _, ok := l.svcCtx.Scheduler.AiService.AiCollectorAdapterMap[opt.AdapterId]
  65. if !ok {
  66. return nil, errors.New("AdapterId does not exist")
  67. }
  68. var strat strategy.Strategy
  69. switch opt.Strategy {
  70. case strategy.STATIC_WEIGHT:
  71. strat = strategy.NewStaticWeightStrategy(opt.StaticWeightMap, int32(len(ts)))
  72. if err != nil {
  73. return nil, err
  74. }
  75. default:
  76. return nil, errors.New("no strategy has been chosen")
  77. }
  78. clusters, err := strat.Schedule()
  79. if err != nil {
  80. return nil, err
  81. }
  82. if clusters == nil || len(clusters) == 0 {
  83. return nil, errors.New("clusters is nil")
  84. }
  85. //save task
  86. var synergystatus int64
  87. if len(clusters) > 1 {
  88. synergystatus = 1
  89. }
  90. strategyCode, err := l.svcCtx.Scheduler.AiStorages.GetStrategyCode(opt.Strategy)
  91. if err != nil {
  92. return nil, err
  93. }
  94. adapterName, err := l.svcCtx.Scheduler.AiStorages.GetAdapterNameById(opt.AdapterId)
  95. if err != nil {
  96. return nil, err
  97. }
  98. id, err := l.svcCtx.Scheduler.AiStorages.SaveTask(opt.TaskName, strategyCode, synergystatus, "11")
  99. if err != nil {
  100. return nil, err
  101. }
  102. l.svcCtx.Scheduler.AiStorages.AddNoticeInfo(opt.AdapterId, adapterName, "", "", opt.TaskName, "create", "任务创建中")
  103. for i := len(clusters) - 1; i >= 0; i-- {
  104. if clusters[i].Replicas == 0 {
  105. clusters = append(clusters[:i], clusters[i+1:]...)
  106. }
  107. }
  108. //save taskai
  109. for _, c := range clusters {
  110. clusterName, _ := l.svcCtx.Scheduler.AiStorages.GetClusterNameById(c.ClusterId)
  111. opt.Replica = c.Replicas
  112. err := l.svcCtx.Scheduler.AiStorages.SaveAiTask(id, opt, adapterName, c.ClusterId, clusterName, "", constants.Saved, "")
  113. if err != nil {
  114. return nil, err
  115. }
  116. }
  117. go l.svcCtx.Scheduler.AiService.ImageInfer(opt, id, adapterName, clusters, ts)
  118. return resp, nil
  119. }

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.