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 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package inference
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/schedulers/option"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/inference"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/inference/imageInference"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/strategy"
  10. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  11. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  12. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  13. "net/http"
  14. "strconv"
  15. )
  16. type ImageInferenceLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewImageInferenceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ImageInferenceLogic {
  22. return &ImageInferenceLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. }
  27. }
  28. //
  29. //func (l *ImageInferenceLogic) ImageInference(req *types.ImageInferenceReq) (resp *types.ImageInferenceResp, err error) {
  30. // return nil, nil
  31. //}
  32. func (l *ImageInferenceLogic) ImageInfer(r *http.Request, req *types.ImageInferenceReq) (resp *types.ImageInferenceResp, err error) {
  33. resp = &types.ImageInferenceResp{}
  34. if len(req.InstanceIds) == 0 {
  35. return nil, errors.New("instances are empty")
  36. }
  37. var instanceList []*models.AiInferDeployInstance
  38. for _, id := range req.InstanceIds {
  39. instance, err := l.svcCtx.Scheduler.AiStorages.GetInferDeployInstanceById(id)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if instance == nil {
  44. return nil, errors.New("instance is empty ")
  45. }
  46. instanceList = append(instanceList, instance)
  47. }
  48. if len(instanceList) == 0 {
  49. return nil, errors.New("instanceList are empty")
  50. }
  51. // process uploaded images
  52. var ts []*imageInference.ImageFile
  53. uploadedFiles := r.MultipartForm.File
  54. if len(uploadedFiles) == 0 {
  55. return nil, errors.New("Images does not exist")
  56. }
  57. if len(uploadedFiles["images"]) == 0 {
  58. return nil, errors.New("Images does not exist")
  59. }
  60. for _, header := range uploadedFiles["images"] {
  61. file, err := header.Open()
  62. if err != nil {
  63. return nil, err
  64. }
  65. defer file.Close()
  66. var ir types.ImageResult
  67. ir.ImageName = header.Filename
  68. t := imageInference.ImageFile{
  69. ImageResult: &ir,
  70. File: file,
  71. }
  72. ts = append(ts, &t)
  73. }
  74. //single adapter logic
  75. if len(req.StaticWeightMap) != 1 {
  76. return nil, errors.New("staticWeightMap != 1")
  77. }
  78. adapterId := strconv.FormatInt(instanceList[0].AdapterId, 10)
  79. staticWeightMap, ok := req.StaticWeightMap[adapterId]
  80. if !ok {
  81. return nil, errors.New("set staticWeightMap failed")
  82. }
  83. // create InferOption
  84. opt := &option.InferOption{
  85. TaskName: req.TaskName,
  86. TaskDesc: req.TaskDesc,
  87. AdapterId: adapterId,
  88. //AiClusterIds: req.AiClusterIds,
  89. //ModelName: req.ModelName,
  90. ModelType: req.ModelType,
  91. Strategy: req.Strategy,
  92. StaticWeightMap: staticWeightMap,
  93. }
  94. adapterName, err := l.svcCtx.Scheduler.AiStorages.GetAdapterNameById(opt.AdapterId)
  95. if err != nil {
  96. return nil, err
  97. }
  98. // set strategy
  99. if opt.Strategy == "" {
  100. return nil, errors.New("strategy is empty")
  101. }
  102. var strat strategy.Strategy
  103. switch opt.Strategy {
  104. case strategy.STATIC_WEIGHT:
  105. strat = strategy.NewStaticWeightStrategy(opt.StaticWeightMap, int32(len(ts)))
  106. if err != nil {
  107. return nil, err
  108. }
  109. default:
  110. return nil, errors.New("no strategy has been chosen")
  111. }
  112. clusters, err := strat.Schedule()
  113. if err != nil {
  114. return nil, err
  115. }
  116. if clusters == nil || len(clusters) == 0 {
  117. return nil, errors.New("clusters is nil")
  118. }
  119. //remove empty replica
  120. for i := len(clusters) - 1; i >= 0; i-- {
  121. if clusters[i].Replicas == 0 {
  122. clusters = append(clusters[:i], clusters[i+1:]...)
  123. }
  124. }
  125. // create inference struct
  126. imageInfer, err := imageInference.New(imageInference.NewImageClassification(), ts, clusters, instanceList, opt, l.svcCtx.Scheduler.AiStorages, l.svcCtx.Scheduler.AiService.InferenceAdapterMap, adapterName)
  127. if err != nil {
  128. return nil, err
  129. }
  130. in := inference.Inference{
  131. In: imageInfer,
  132. }
  133. id, err := in.In.CreateTask()
  134. if err != nil {
  135. return nil, err
  136. }
  137. go func() {
  138. err := in.In.InferTask(id)
  139. if err != nil {
  140. logx.Errorf(err.Error())
  141. return
  142. }
  143. }()
  144. return resp, nil
  145. }

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.