From 3c3d45fe6200239b9da46f6e66e745783a1d9cee Mon Sep 17 00:00:00 2001 From: tzwang Date: Wed, 28 Aug 2024 15:22:41 +0800 Subject: [PATCH 1/5] updated imageinference logics Former-commit-id: 1bb4a47ac943ee12a0d92ce37820af58b155641e --- .../logic/inference/imageinferencelogic.go | 71 +++++++++---------- .../imageInference/imageInference.go | 16 ++++- .../inference/textInference/textToImage.go | 2 +- .../inference/textInference/textToText.go | 2 + internal/storeLink/octopus.go | 5 ++ 5 files changed, 54 insertions(+), 42 deletions(-) diff --git a/internal/logic/inference/imageinferencelogic.go b/internal/logic/inference/imageinferencelogic.go index 88f93ded..94b1ddad 100644 --- a/internal/logic/inference/imageinferencelogic.go +++ b/internal/logic/inference/imageinferencelogic.go @@ -82,48 +82,41 @@ func (l *ImageInferenceLogic) ImageInfer(r *http.Request, req *types.ImageInfere //} // - var cs []*strategy.AssignedCluster - var adapterName string - if opt.Strategy != "" { - var strat strategy.Strategy - switch opt.Strategy { - case strategy.STATIC_WEIGHT: - strat = strategy.NewStaticWeightStrategy(opt.StaticWeightMap, int32(len(ts))) - if err != nil { - return nil, err - } - default: - return nil, errors.New("no strategy has been chosen") - } - clusters, err := strat.Schedule() - if err != nil { - return nil, err - } - - if clusters == nil || len(clusters) == 0 { - return nil, errors.New("clusters is nil") - } - - for i := len(clusters) - 1; i >= 0; i-- { - if clusters[i].Replicas == 0 { - clusters = append(clusters[:i], clusters[i+1:]...) - } - } - - name, err := l.svcCtx.Scheduler.AiStorages.GetAdapterNameById(opt.AdapterId) - if err != nil { - return nil, err - } - adapterName = name + adapterName, err := l.svcCtx.Scheduler.AiStorages.GetAdapterNameById(opt.AdapterId) + if err != nil { + return nil, err } - //else { - // for i, instance := range req.Instances { - // - // } - //} + if opt.Strategy != "" { + return nil, errors.New("strategy is empty") + } - imageInfer, err := imageInference.New(imageInference.NewImageClassification(), ts, cs, req.Instances, opt, l.svcCtx.Scheduler.AiStorages, l.svcCtx.Scheduler.AiService.InferenceAdapterMap, adapterName) + var strat strategy.Strategy + switch opt.Strategy { + case strategy.STATIC_WEIGHT: + strat = strategy.NewStaticWeightStrategy(opt.StaticWeightMap, int32(len(ts))) + if err != nil { + return nil, err + } + default: + return nil, errors.New("no strategy has been chosen") + } + clusters, err := strat.Schedule() + if err != nil { + return nil, err + } + + if clusters == nil || len(clusters) == 0 { + return nil, errors.New("clusters is nil") + } + + for i := len(clusters) - 1; i >= 0; i-- { + if clusters[i].Replicas == 0 { + clusters = append(clusters[:i], clusters[i+1:]...) + } + } + + imageInfer, err := imageInference.New(imageInference.NewImageClassification(), ts, clusters, req.Instances, opt, l.svcCtx.Scheduler.AiStorages, l.svcCtx.Scheduler.AiService.InferenceAdapterMap, adapterName) if err != nil { return nil, err } diff --git a/internal/scheduler/service/inference/imageInference/imageInference.go b/internal/scheduler/service/inference/imageInference/imageInference.go index 8c45c5a1..a11b80f9 100644 --- a/internal/scheduler/service/inference/imageInference/imageInference.go +++ b/internal/scheduler/service/inference/imageInference/imageInference.go @@ -131,7 +131,7 @@ func (i *ImageInference) saveTask() (int64, error) { return 0, err } - i.storage.AddNoticeInfo(i.opt.AdapterId, i.adapterName, "", "", i.opt.TaskName, "create", "任务创建中") + i.storage.AddNoticeInfo("", "", "", "", i.opt.TaskName, "create", "任务创建中") return id, nil } @@ -197,21 +197,33 @@ func (i *ImageInference) filterClusters() ([]*FilteredCluster, error) { var cs []*FilteredCluster for _, cluster := range i.clusters { var inferurls []*inference.InferUrl + var clustertype string for _, instance := range i.instances { if cluster.ClusterId == instance.ClusterId { r := http.Request{} deployInstance, err := i.inferAdapter[instance.AdapterId][instance.ClusterId].GetInferDeployInstance(r.Context(), instance.InstanceId) if err != nil { - return nil, err + continue } var url inference.InferUrl url.Url = deployInstance.InferUrl url.Card = deployInstance.InferCard inferurls = append(inferurls, &url) + + clustertype = deployInstance.ClusterType } } + if len(inferurls) == 0 { + continue + } + + i.inference.AppendRoute(inferurls) + var f FilteredCluster f.urls = inferurls + f.clusterName = cluster.ClusterName + f.clusterType = clustertype + f.imageNum = cluster.Replicas cs = append(cs, &f) } return cs, nil diff --git a/internal/scheduler/service/inference/textInference/textToImage.go b/internal/scheduler/service/inference/textInference/textToImage.go index 7fe2496c..bc803444 100644 --- a/internal/scheduler/service/inference/textInference/textToImage.go +++ b/internal/scheduler/service/inference/textInference/textToImage.go @@ -9,7 +9,7 @@ import ( ) const ( - TEXTTOIMAGE = "text-to-image" + TEXTTOIMAGE = "generate_image" TEXTTOIMAGE_AiTYPE = "14" ) diff --git a/internal/scheduler/service/inference/textInference/textToText.go b/internal/scheduler/service/inference/textInference/textToText.go index b795e766..3851a35e 100644 --- a/internal/scheduler/service/inference/textInference/textToText.go +++ b/internal/scheduler/service/inference/textInference/textToText.go @@ -79,9 +79,11 @@ func filterClusters(opt *option.InferOption, storage *database.AiStorage, inferA wg.Done() return } + for i, _ := range clusterInferUrl.InferUrls { clusterInferUrl.InferUrls[i].Url = clusterInferUrl.InferUrls[i].Url + inference.FORWARD_SLASH + CHAT } + clusterName, _ := storage.GetClusterNameById(cId) var f FilteredCluster diff --git a/internal/storeLink/octopus.go b/internal/storeLink/octopus.go index f2c4a3aa..0b2fc3fd 100644 --- a/internal/storeLink/octopus.go +++ b/internal/storeLink/octopus.go @@ -1154,11 +1154,16 @@ func (o *OctopusLink) GetInferDeployInstance(ctx context.Context, id string) (*i if resp.Payload == nil { return nil, errors.New("instance does not exist") } + + url := strings.Replace(resp.Payload.Notebook.Tasks[0].Url, FORWARD_SLASH, "", -1) + inferUrl := DOMAIN + url + ins.InstanceName = resp.Payload.Notebook.Name ins.InstanceId = resp.Payload.Notebook.Id ins.ClusterName = o.platform ins.Status = resp.Payload.Notebook.Status ins.ClusterType = TYPE_OCTOPUS + ins.InferUrl = inferUrl return ins, nil } From e650da868a0c9b48803b30fb0ff9041be37cf729 Mon Sep 17 00:00:00 2001 From: tzwang Date: Wed, 28 Aug 2024 15:27:07 +0800 Subject: [PATCH 2/5] updated inference.api Former-commit-id: f4093deb9a6e9e6513e3316da30ed105f727c52c --- desc/inference/inference.api | 14 +++++++------- internal/types/types.go | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/desc/inference/inference.api b/desc/inference/inference.api index c58b1dc1..c9308f6c 100644 --- a/desc/inference/inference.api +++ b/desc/inference/inference.api @@ -30,13 +30,13 @@ type ( /******************image inference*************************/ ImageInferenceReq { - TaskName string `json:"taskName"` - TaskDesc string `json:"taskDesc"` - ModelType string `json:"modelType"` - Instances []DeployInstance `json:"instances"` - Strategy string `json:"strategy,,optional"` - StaticWeightMap map[string]int32 `json:"staticWeightMap,optional"` - Replica int32 `json:"replicas,optional"` + TaskName string `form:"taskName"` + TaskDesc string `form:"taskDesc"` + ModelType string `form:"modelType"` + Instances []DeployInstance `form:"instances"` + Strategy string `form:"strategy,,optional"` + StaticWeightMap map[string]int32 `form:"staticWeightMap,optional"` + Replica int32 `form:"replicas,optional"` } ImageInferenceResp { diff --git a/internal/types/types.go b/internal/types/types.go index a5fe2f86..ff56124d 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -5930,13 +5930,13 @@ type ModelNamesResp struct { } type ImageInferenceReq struct { - TaskName string `json:"taskName"` - TaskDesc string `json:"taskDesc"` - ModelType string `json:"modelType"` - Instances []DeployInstance `json:"instances"` - Strategy string `json:"strategy,,optional"` - StaticWeightMap map[string]int32 `json:"staticWeightMap,optional"` - Replica int32 `json:"replicas,optional"` + TaskName string `form:"taskName"` + TaskDesc string `form:"taskDesc"` + ModelType string `form:"modelType"` + Instances []DeployInstance `form:"instances"` + Strategy string `form:"strategy,,optional"` + StaticWeightMap map[string]int32 `form:"staticWeightMap,optional"` + Replica int32 `form:"replicas,optional"` } type ImageInferenceResp struct { From d7ebdebfea4e3ab84f833c88b76ad1e916ce3352 Mon Sep 17 00:00:00 2001 From: tzwang Date: Wed, 28 Aug 2024 15:42:36 +0800 Subject: [PATCH 3/5] updated inference.api Former-commit-id: 796f5afe078150063b00a51c61cafa266578dc68 --- desc/inference/inference.api | 7 ++++++- internal/types/types.go | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/desc/inference/inference.api b/desc/inference/inference.api index c9308f6c..392a39e4 100644 --- a/desc/inference/inference.api +++ b/desc/inference/inference.api @@ -3,6 +3,8 @@ syntax = "v1" type ( /******************image inference*************************/ DeployInstance { + Id string `json:"id"` + DeployTaskId string `json:"deployTaskId"` InstanceId string `json:"instanceId"` InstanceName string `json:"instanceName"` AdapterId string `json:"adapterId"` @@ -13,6 +15,9 @@ type ( ModelType string `json:"modelType"` InferCard string `json:"inferCard"` Status string `json:"status"` + CreateTime string `json:"createTime"` + UpdateTime string `json:"updateTime"` + ClusterType string `json:"clusterType"` } /******************image inference*************************/ @@ -33,7 +38,7 @@ type ( TaskName string `form:"taskName"` TaskDesc string `form:"taskDesc"` ModelType string `form:"modelType"` - Instances []DeployInstance `form:"instances"` + Instances []*DeployInstance `form:"instances"` Strategy string `form:"strategy,,optional"` StaticWeightMap map[string]int32 `form:"staticWeightMap,optional"` Replica int32 `form:"replicas,optional"` diff --git a/internal/types/types.go b/internal/types/types.go index ff56124d..6d7ef5c6 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -5905,6 +5905,8 @@ type Category struct { } type DeployInstance struct { + Id string `json:"id"` + DeployTaskId string `json:"deployTaskId"` InstanceId string `json:"instanceId"` InstanceName string `json:"instanceName"` AdapterId string `json:"adapterId"` @@ -5915,6 +5917,9 @@ type DeployInstance struct { ModelType string `json:"modelType"` InferCard string `json:"inferCard"` Status string `json:"status"` + CreateTime string `json:"createTime"` + UpdateTime string `json:"updateTime"` + ClusterType string `json:"clusterType"` } type ModelTypesResp struct { @@ -5930,13 +5935,13 @@ type ModelNamesResp struct { } type ImageInferenceReq struct { - TaskName string `form:"taskName"` - TaskDesc string `form:"taskDesc"` - ModelType string `form:"modelType"` - Instances []DeployInstance `form:"instances"` - Strategy string `form:"strategy,,optional"` - StaticWeightMap map[string]int32 `form:"staticWeightMap,optional"` - Replica int32 `form:"replicas,optional"` + TaskName string `form:"taskName"` + TaskDesc string `form:"taskDesc"` + ModelType string `form:"modelType"` + Instances []*DeployInstance `form:"instances"` + Strategy string `form:"strategy,,optional"` + StaticWeightMap map[string]int32 `form:"staticWeightMap,optional"` + Replica int32 `form:"replicas,optional"` } type ImageInferenceResp struct { From 6f689ce78864ee211741af8e27e6879ba9c39b36 Mon Sep 17 00:00:00 2001 From: tzwang Date: Wed, 28 Aug 2024 16:45:11 +0800 Subject: [PATCH 4/5] updated inference.api Former-commit-id: 84df5c7572c89e46f7eac5e947a4c21e69c0261f --- desc/inference/inference.api | 4 ++-- internal/types/types.go | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/desc/inference/inference.api b/desc/inference/inference.api index 392a39e4..b80d31aa 100644 --- a/desc/inference/inference.api +++ b/desc/inference/inference.api @@ -38,9 +38,9 @@ type ( TaskName string `form:"taskName"` TaskDesc string `form:"taskDesc"` ModelType string `form:"modelType"` - Instances []*DeployInstance `form:"instances"` + InstanceIds []int64 `form:"instanceIds"` Strategy string `form:"strategy,,optional"` - StaticWeightMap map[string]int32 `form:"staticWeightMap,optional"` + StaticWeightMap map[string]map[string]int32 `form:"staticWeightMap,optional"` Replica int32 `form:"replicas,optional"` } diff --git a/internal/types/types.go b/internal/types/types.go index 6d7ef5c6..2e72d660 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -5935,13 +5935,13 @@ type ModelNamesResp struct { } type ImageInferenceReq struct { - TaskName string `form:"taskName"` - TaskDesc string `form:"taskDesc"` - ModelType string `form:"modelType"` - Instances []*DeployInstance `form:"instances"` - Strategy string `form:"strategy,,optional"` - StaticWeightMap map[string]int32 `form:"staticWeightMap,optional"` - Replica int32 `form:"replicas,optional"` + TaskName string `form:"taskName"` + TaskDesc string `form:"taskDesc"` + ModelType string `form:"modelType"` + InstanceIds []int64 `form:"instanceIds"` + Strategy string `form:"strategy,,optional"` + StaticWeightMap map[string]map[string]int32 `form:"staticWeightMap,optional"` + Replica int32 `form:"replicas,optional"` } type ImageInferenceResp struct { From 739948d184cb499a4548e7000660068f88827ed1 Mon Sep 17 00:00:00 2001 From: tzwang Date: Wed, 28 Aug 2024 17:24:39 +0800 Subject: [PATCH 5/5] updated imageinference logics Former-commit-id: 3dde5aa691fa23f3ebae3772620640d96ab70570 --- .../logic/inference/imageinferencelogic.go | 56 +++++++++++++------ .../imageInference/imageInference.go | 11 ++-- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/internal/logic/inference/imageinferencelogic.go b/internal/logic/inference/imageinferencelogic.go index 94b1ddad..2b26e8b9 100644 --- a/internal/logic/inference/imageinferencelogic.go +++ b/internal/logic/inference/imageinferencelogic.go @@ -10,7 +10,9 @@ import ( "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/strategy" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" "net/http" + "strconv" ) type ImageInferenceLogic struct { @@ -34,21 +36,24 @@ func NewImageInferenceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Im func (l *ImageInferenceLogic) ImageInfer(r *http.Request, req *types.ImageInferenceReq) (resp *types.ImageInferenceResp, err error) { resp = &types.ImageInferenceResp{} - if len(req.Instances) == 0 { + if len(req.InstanceIds) == 0 { return nil, errors.New("instances are empty") } - opt := &option.InferOption{ - TaskName: req.TaskName, - TaskDesc: req.TaskDesc, - //AdapterId: req.AdapterId, - //AiClusterIds: req.AiClusterIds, - //ModelName: req.ModelName, - ModelType: req.ModelType, - Strategy: req.Strategy, - StaticWeightMap: req.StaticWeightMap, + var instanceList []*models.AiInferDeployInstance + for _, id := range req.InstanceIds { + instance, err := l.svcCtx.Scheduler.AiStorages.GetInferDeployInstanceById(id) + if err != nil { + return nil, err + } + instanceList = append(instanceList, instance) } + if len(instanceList) == 0 { + return nil, errors.New("instances are empty") + } + + // process uploaded images var ts []*imageInference.ImageFile uploadedFiles := r.MultipartForm.File @@ -76,17 +81,35 @@ func (l *ImageInferenceLogic) ImageInfer(r *http.Request, req *types.ImageInfere ts = append(ts, &t) } - //_, ok := l.svcCtx.Scheduler.AiService.AiCollectorAdapterMap[opt.AdapterId] - //if !ok { - // return nil, errors.New("AdapterId does not exist") - //} - // + //single adapter logic + if len(req.StaticWeightMap) != 1 { + return nil, errors.New("staticWeightMap != 1") + } + + adapterId := strconv.FormatInt(instanceList[0].AdapterId, 10) + staticWeightMap, ok := req.StaticWeightMap[adapterId] + if !ok { + return nil, errors.New("set staticWeightMap failed") + } + + // create InferOption + opt := &option.InferOption{ + TaskName: req.TaskName, + TaskDesc: req.TaskDesc, + AdapterId: adapterId, + //AiClusterIds: req.AiClusterIds, + //ModelName: req.ModelName, + ModelType: req.ModelType, + Strategy: req.Strategy, + StaticWeightMap: staticWeightMap, + } adapterName, err := l.svcCtx.Scheduler.AiStorages.GetAdapterNameById(opt.AdapterId) if err != nil { return nil, err } + // set strategy if opt.Strategy != "" { return nil, errors.New("strategy is empty") } @@ -116,7 +139,8 @@ func (l *ImageInferenceLogic) ImageInfer(r *http.Request, req *types.ImageInfere } } - imageInfer, err := imageInference.New(imageInference.NewImageClassification(), ts, clusters, req.Instances, opt, l.svcCtx.Scheduler.AiStorages, l.svcCtx.Scheduler.AiService.InferenceAdapterMap, adapterName) + // create inference struct + imageInfer, err := imageInference.New(imageInference.NewImageClassification(), ts, clusters, instanceList, opt, l.svcCtx.Scheduler.AiStorages, l.svcCtx.Scheduler.AiService.InferenceAdapterMap, adapterName) if err != nil { return nil, err } diff --git a/internal/scheduler/service/inference/imageInference/imageInference.go b/internal/scheduler/service/inference/imageInference/imageInference.go index a11b80f9..101ba45f 100644 --- a/internal/scheduler/service/inference/imageInference/imageInference.go +++ b/internal/scheduler/service/inference/imageInference/imageInference.go @@ -46,7 +46,7 @@ type ImageInference struct { inference IImageInference files []*ImageFile clusters []*strategy.AssignedCluster - instances []types.DeployInstance + instances []*models.AiInferDeployInstance opt *option.InferOption storage *database.AiStorage inferAdapter map[string]map[string]inference.ICluster @@ -58,7 +58,7 @@ func New( inference IImageInference, files []*ImageFile, clusters []*strategy.AssignedCluster, - instances []types.DeployInstance, + instances []*models.AiInferDeployInstance, opt *option.InferOption, storage *database.AiStorage, inferAdapter map[string]map[string]inference.ICluster, @@ -199,9 +199,12 @@ func (i *ImageInference) filterClusters() ([]*FilteredCluster, error) { var inferurls []*inference.InferUrl var clustertype string for _, instance := range i.instances { - if cluster.ClusterId == instance.ClusterId { + clusterId := strconv.FormatInt(instance.ClusterId, 10) + adapterId := strconv.FormatInt(instance.AdapterId, 10) + + if cluster.ClusterId == clusterId { r := http.Request{} - deployInstance, err := i.inferAdapter[instance.AdapterId][instance.ClusterId].GetInferDeployInstance(r.Context(), instance.InstanceId) + deployInstance, err := i.inferAdapter[adapterId][clusterId].GetInferDeployInstance(r.Context(), instance.InstanceId) if err != nil { continue }