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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "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. if len(req.Instances) == 0 {
  33. return nil, errors.New("instances are empty")
  34. }
  35. opt := &option.InferOption{
  36. TaskName: req.TaskName,
  37. TaskDesc: req.TaskDesc,
  38. //AdapterId: req.AdapterId,
  39. //AiClusterIds: req.AiClusterIds,
  40. //ModelName: req.ModelName,
  41. ModelType: req.ModelType,
  42. Strategy: req.Strategy,
  43. StaticWeightMap: req.StaticWeightMap,
  44. }
  45. var ts []*imageInference.ImageFile
  46. uploadedFiles := r.MultipartForm.File
  47. if len(uploadedFiles) == 0 {
  48. return nil, errors.New("Images does not exist")
  49. }
  50. if len(uploadedFiles["images"]) == 0 {
  51. return nil, errors.New("Images does not exist")
  52. }
  53. for _, header := range uploadedFiles["images"] {
  54. file, err := header.Open()
  55. if err != nil {
  56. return nil, err
  57. }
  58. defer file.Close()
  59. var ir types.ImageResult
  60. ir.ImageName = header.Filename
  61. t := imageInference.ImageFile{
  62. ImageResult: &ir,
  63. File: file,
  64. }
  65. ts = append(ts, &t)
  66. }
  67. //_, ok := l.svcCtx.Scheduler.AiService.AiCollectorAdapterMap[opt.AdapterId]
  68. //if !ok {
  69. // return nil, errors.New("AdapterId does not exist")
  70. //}
  71. //
  72. adapterName, err := l.svcCtx.Scheduler.AiStorages.GetAdapterNameById(opt.AdapterId)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if opt.Strategy != "" {
  77. return nil, errors.New("strategy is empty")
  78. }
  79. var strat strategy.Strategy
  80. switch opt.Strategy {
  81. case strategy.STATIC_WEIGHT:
  82. strat = strategy.NewStaticWeightStrategy(opt.StaticWeightMap, int32(len(ts)))
  83. if err != nil {
  84. return nil, err
  85. }
  86. default:
  87. return nil, errors.New("no strategy has been chosen")
  88. }
  89. clusters, err := strat.Schedule()
  90. if err != nil {
  91. return nil, err
  92. }
  93. if clusters == nil || len(clusters) == 0 {
  94. return nil, errors.New("clusters is nil")
  95. }
  96. for i := len(clusters) - 1; i >= 0; i-- {
  97. if clusters[i].Replicas == 0 {
  98. clusters = append(clusters[:i], clusters[i+1:]...)
  99. }
  100. }
  101. imageInfer, err := imageInference.New(imageInference.NewImageClassification(), ts, clusters, req.Instances, opt, l.svcCtx.Scheduler.AiStorages, l.svcCtx.Scheduler.AiService.InferenceAdapterMap, adapterName)
  102. if err != nil {
  103. return nil, err
  104. }
  105. in := inference.Inference{
  106. In: imageInfer,
  107. }
  108. id, err := in.In.CreateTask()
  109. if err != nil {
  110. return nil, err
  111. }
  112. go func() {
  113. err := in.In.InferTask(id)
  114. if err != nil {
  115. logx.Errorf(err.Error())
  116. return
  117. }
  118. }()
  119. return resp, nil
  120. }

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.