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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. 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 []*imageInference.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 := imageInference.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. for i := len(clusters) - 1; i >= 0; i-- {
  86. if clusters[i].Replicas == 0 {
  87. clusters = append(clusters[:i], clusters[i+1:]...)
  88. }
  89. }
  90. adapterName, err := l.svcCtx.Scheduler.AiStorages.GetAdapterNameById(opt.AdapterId)
  91. if err != nil {
  92. return nil, err
  93. }
  94. imageInfer, err := imageInference.New(imageInference.NewImageClassification(), ts, clusters, opt, l.svcCtx.Scheduler.AiStorages, l.svcCtx.Scheduler.AiService.InferenceAdapterMap, adapterName)
  95. if err != nil {
  96. return nil, err
  97. }
  98. in := inference.Inference{
  99. In: imageInfer,
  100. }
  101. id, err := in.In.CreateTask()
  102. if err != nil {
  103. return nil, err
  104. }
  105. go func() {
  106. err := in.In.InferTask(id)
  107. if err != nil {
  108. logx.Errorf(err.Error())
  109. return
  110. }
  111. }()
  112. return resp, nil
  113. }

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.