From 271e3d69e4e0b5ddb7af17245a63fd0e56abc8cb Mon Sep 17 00:00:00 2001 From: tzwang Date: Tue, 27 Aug 2024 11:18:41 +0800 Subject: [PATCH 1/7] updated inference apis Former-commit-id: 8645e4ce15e7b96fbb4bff7f07a8d08c8a6bfdaf --- desc/inference/inference.api | 46 +++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/desc/inference/inference.api b/desc/inference/inference.api index a868a6da..272f318c 100644 --- a/desc/inference/inference.api +++ b/desc/inference/inference.api @@ -1,6 +1,20 @@ syntax = "v1" type ( + /******************image inference*************************/ + DeployInstance { + InstanceId string `json:"instanceId"` + InstanceName string `json:"instanceName"` + AdapterId string `json:"adapterId"` + AdapterName string `json:"adapterName"` + ClusterId string `json:"clusterId"` + ClusterName string `json:"clusterName"` + ModelName string `json:"modelName"` + ModelType string `json:"modelType"` + InferCard string `json:"inferCard"` + Status string `json:"status"` + } + /******************image inference*************************/ ModelTypesResp { ModelTypes []string `json:"types"` @@ -16,20 +30,13 @@ type ( /******************image inference*************************/ ImageInferenceReq { - TaskName string `form:"taskName"` - TaskDesc string `form:"taskDesc"` - ModelName string `form:"modelName"` - ModelType string `form:"modelType"` - AdapterIds []string `form:"adapterIds"` - AiClusterIds []string `form:"aiClusterIds,optional"` - ResourceType string `form:"resourceType,optional"` - ComputeCard string `form:"card,optional"` - Strategy string `form:"strategy"` - StaticWeightMap map[string]int32 `form:"staticWeightMap,optional"` - Params []string `form:"params,optional"` - Envs []string `form:"envs,optional"` - Cmd string `form:"cmd,optional"` - Replica int32 `form:"replicas,optional"` + 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"` } ImageInferenceResp { @@ -159,10 +166,17 @@ type ( GetRunningInstanceReq { AdapterIds []string `form:"adapterIds"` - ModelType string `path:"modelType"` - ModelName string `path:"modelName"` + ModelType string `form:"modelType"` } GetRunningInstanceResp { List interface{} `json:"list,omitempty"` } + + GetDeployTasksByTypeReq { + ModelType string `form:"modelType"` + } + + GetDeployTasksByTypeResp { + List interface{} `json:"list,omitempty"` + } ) From 5ec517690ae7a8ec79cdfc1ce5bf9184e5b8dd11 Mon Sep 17 00:00:00 2001 From: tzwang Date: Tue, 27 Aug 2024 15:09:58 +0800 Subject: [PATCH 2/7] updated inference apis Former-commit-id: d907a498a213be552236b306f48903942d63078d --- desc/inference/inference.api | 14 +--- desc/pcm.api | 8 +-- ...dler.go => getdeploytasksbytypehandler.go} | 8 +-- ....go => getrunninginstancebytypehandler.go} | 15 ++-- internal/handler/routes.go | 8 +-- ...slogic.go => getdeploytasksbytypelogic.go} | 8 +-- .../getrunninginstancebymodellogic.go | 30 -------- .../getrunninginstancebytypelogic.go | 30 ++++++++ internal/types/types.go | 68 ++++++++++++------- 9 files changed, 101 insertions(+), 88 deletions(-) rename internal/handler/inference/{getdeploytaskshandler.go => getdeploytasksbytypehandler.go} (70%) rename internal/handler/inference/{getrunninginstancebymodelhandler.go => getrunninginstancebytypehandler.go} (55%) rename internal/logic/inference/{getdeploytaskslogic.go => getdeploytasksbytypelogic.go} (54%) delete mode 100644 internal/logic/inference/getrunninginstancebymodellogic.go create mode 100644 internal/logic/inference/getrunninginstancebytypelogic.go diff --git a/desc/inference/inference.api b/desc/inference/inference.api index 272f318c..63a5e21d 100644 --- a/desc/inference/inference.api +++ b/desc/inference/inference.api @@ -92,7 +92,7 @@ type ( AiClusterIds []string `form:"aiClusterIds"` } TextToImageInferenceResp{ - Result []byte + Result []byte `json:"result"` } /******************Deploy instance*************************/ @@ -156,20 +156,12 @@ type ( } - GetDeployTasksReq { - PageInfo - } - - GetDeployTasksResp { - PageResult - } - GetRunningInstanceReq { AdapterIds []string `form:"adapterIds"` ModelType string `form:"modelType"` } GetRunningInstanceResp { - List interface{} `json:"list,omitempty"` + List interface{} `json:"list"` } GetDeployTasksByTypeReq { @@ -177,6 +169,6 @@ type ( } GetDeployTasksByTypeResp { - List interface{} `json:"list,omitempty"` + List interface{} `json:"list"` } ) diff --git a/desc/pcm.api b/desc/pcm.api index 6eacc710..421604fc 100644 --- a/desc/pcm.api +++ b/desc/pcm.api @@ -966,11 +966,11 @@ service pcm { @handler StopAllByDeployTaskId post /inference/stopAll (StopAllByDeployTaskIdReq) returns (StopAllByDeployTaskIdResp) - @handler GetDeployTasks - get /inference/getDeployTasks (GetDeployTasksReq) returns (GetDeployTasksResp) + @handler GetRunningInstanceByType + get /inference/getInstanceByType (GetRunningInstanceReq) returns (GetRunningInstanceResp) - @handler GetRunningInstanceByModel - get /inference/getInstanceByModel (GetRunningInstanceReq) returns (GetRunningInstanceResp) + @handler GetDeployTasksByType + get /inference/getDeployTasksByType (GetDeployTasksByTypeReq) returns (GetDeployTasksByTypeResp) } @server( diff --git a/internal/handler/inference/getdeploytaskshandler.go b/internal/handler/inference/getdeploytasksbytypehandler.go similarity index 70% rename from internal/handler/inference/getdeploytaskshandler.go rename to internal/handler/inference/getdeploytasksbytypehandler.go index ef6aeeea..1d40c870 100644 --- a/internal/handler/inference/getdeploytaskshandler.go +++ b/internal/handler/inference/getdeploytasksbytypehandler.go @@ -9,16 +9,16 @@ import ( "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" ) -func GetDeployTasksHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { +func GetDeployTasksByTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.GetDeployTasksReq + var req types.GetDeployTasksByTypeReq if err := httpx.Parse(r, &req); err != nil { httpx.ErrorCtx(r.Context(), w, err) return } - l := inference.NewGetDeployTasksLogic(r.Context(), svcCtx) - resp, err := l.GetDeployTasks(&req) + l := inference.NewGetDeployTasksByTypeLogic(r.Context(), svcCtx) + resp, err := l.GetDeployTasksByType(&req) if err != nil { httpx.ErrorCtx(r.Context(), w, err) } else { diff --git a/internal/handler/inference/getrunninginstancebymodelhandler.go b/internal/handler/inference/getrunninginstancebytypehandler.go similarity index 55% rename from internal/handler/inference/getrunninginstancebymodelhandler.go rename to internal/handler/inference/getrunninginstancebytypehandler.go index 8075a73e..cf342f0f 100644 --- a/internal/handler/inference/getrunninginstancebymodelhandler.go +++ b/internal/handler/inference/getrunninginstancebytypehandler.go @@ -1,7 +1,6 @@ package inference import ( - "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" "net/http" "github.com/zeromicro/go-zero/rest/httpx" @@ -10,16 +9,20 @@ import ( "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" ) -func GetRunningInstanceByModelHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { +func GetRunningInstanceByTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.GetRunningInstanceReq if err := httpx.Parse(r, &req); err != nil { - result.ParamErrorResult(r, w, err) + httpx.ErrorCtx(r.Context(), w, err) return } - l := inference.NewGetRunningInstanceByModelLogic(r.Context(), svcCtx) - resp, err := l.GetRunningInstanceByModel(&req) - result.HttpResult(r, w, resp, err) + l := inference.NewGetRunningInstanceByTypeLogic(r.Context(), svcCtx) + resp, err := l.GetRunningInstanceByType(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } } } diff --git a/internal/handler/routes.go b/internal/handler/routes.go index c317a876..c9524f9d 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -1225,13 +1225,13 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodGet, - Path: "/inference/getDeployTasks", - Handler: inference.GetDeployTasksHandler(serverCtx), + Path: "/inference/getInstanceByType", + Handler: inference.GetRunningInstanceByTypeHandler(serverCtx), }, { Method: http.MethodGet, - Path: "/inference/getInstanceByModel", - Handler: inference.GetRunningInstanceByModelHandler(serverCtx), + Path: "/inference/getDeployTasksByType", + Handler: inference.GetDeployTasksByTypeHandler(serverCtx), }, }, rest.WithPrefix("/pcm/v1"), diff --git a/internal/logic/inference/getdeploytaskslogic.go b/internal/logic/inference/getdeploytasksbytypelogic.go similarity index 54% rename from internal/logic/inference/getdeploytaskslogic.go rename to internal/logic/inference/getdeploytasksbytypelogic.go index 1f545613..d81a92e2 100644 --- a/internal/logic/inference/getdeploytaskslogic.go +++ b/internal/logic/inference/getdeploytasksbytypelogic.go @@ -9,21 +9,21 @@ import ( "github.com/zeromicro/go-zero/core/logx" ) -type GetDeployTasksLogic struct { +type GetDeployTasksByTypeLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } -func NewGetDeployTasksLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDeployTasksLogic { - return &GetDeployTasksLogic{ +func NewGetDeployTasksByTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDeployTasksByTypeLogic { + return &GetDeployTasksByTypeLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } -func (l *GetDeployTasksLogic) GetDeployTasks(req *types.GetDeployTasksReq) (resp *types.GetDeployTasksResp, err error) { +func (l *GetDeployTasksByTypeLogic) GetDeployTasksByType(req *types.GetDeployTasksByTypeReq) (resp *types.GetDeployTasksByTypeResp, err error) { // todo: add your logic here and delete this line return diff --git a/internal/logic/inference/getrunninginstancebymodellogic.go b/internal/logic/inference/getrunninginstancebymodellogic.go deleted file mode 100644 index 1e598dad..00000000 --- a/internal/logic/inference/getrunninginstancebymodellogic.go +++ /dev/null @@ -1,30 +0,0 @@ -package inference - -import ( - "context" - - "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" - "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type GetRunningInstanceByModelLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewGetRunningInstanceByModelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRunningInstanceByModelLogic { - return &GetRunningInstanceByModelLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -func (l *GetRunningInstanceByModelLogic) GetRunningInstanceByModel(req *types.GetRunningInstanceReq) (resp *types.GetRunningInstanceResp, err error) { - resp = &types.GetRunningInstanceResp{} - - return -} diff --git a/internal/logic/inference/getrunninginstancebytypelogic.go b/internal/logic/inference/getrunninginstancebytypelogic.go new file mode 100644 index 00000000..fc2c1c63 --- /dev/null +++ b/internal/logic/inference/getrunninginstancebytypelogic.go @@ -0,0 +1,30 @@ +package inference + +import ( + "context" + + "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetRunningInstanceByTypeLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetRunningInstanceByTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRunningInstanceByTypeLogic { + return &GetRunningInstanceByTypeLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetRunningInstanceByTypeLogic) GetRunningInstanceByType(req *types.GetRunningInstanceReq) (resp *types.GetRunningInstanceResp, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/internal/types/types.go b/internal/types/types.go index d0124b66..30bcf2ef 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -5904,6 +5904,19 @@ type Category struct { Name string `json:"name"` } +type DeployInstance struct { + InstanceId string `json:"instanceId"` + InstanceName string `json:"instanceName"` + AdapterId string `json:"adapterId"` + AdapterName string `json:"adapterName"` + ClusterId string `json:"clusterId"` + ClusterName string `json:"clusterName"` + ModelName string `json:"modelName"` + ModelType string `json:"modelType"` + InferCard string `json:"inferCard"` + Status string `json:"status"` +} + type ModelTypesResp struct { ModelTypes []string `json:"types"` } @@ -5917,20 +5930,13 @@ type ModelNamesResp struct { } type ImageInferenceReq struct { - TaskName string `form:"taskName"` - TaskDesc string `form:"taskDesc"` - ModelName string `form:"modelName"` - ModelType string `form:"modelType"` - AdapterId string `form:"adapterId"` - AiClusterIds []string `form:"aiClusterIds,optional"` - ResourceType string `form:"resourceType,optional"` - ComputeCard string `form:"card,optional"` - Strategy string `form:"strategy"` - StaticWeightMap map[string]int32 `form:"staticWeightMap,optional"` - Params []string `form:"params,optional"` - Envs []string `form:"envs,optional"` - Cmd string `form:"cmd,optional"` - Replica int32 `form:"replicas,optional"` + 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"` } type ImageInferenceResp struct { @@ -5976,6 +5982,18 @@ type TextToTextInferenceReq struct { type TextToTextInferenceResp struct { } +type TextToImageInferenceReq struct { + TaskName string `form:"taskName"` + TaskDesc string `form:"taskDesc"` + ModelName string `form:"modelName"` + ModelType string `form:"modelType"` + AiClusterIds []string `form:"aiClusterIds"` +} + +type TextToImageInferenceResp struct { + Result []byte `json:"result"` +} + type DeployInstanceListReq struct { PageInfo } @@ -6034,19 +6052,19 @@ type StopAllByDeployTaskIdReq struct { type StopAllByDeployTaskIdResp struct { } -type GetDeployTasksReq struct { - PageInfo -} - -type GetDeployTasksResp struct { - PageResult -} - type GetRunningInstanceReq struct { - ModelType string `path:"modelType"` - ModelName string `path:"modelName"` + AdapterIds []string `form:"adapterIds"` + ModelType string `form:"modelType"` } type GetRunningInstanceResp struct { - List interface{} `json:"list,omitempty"` + List interface{} `json:"list"` +} + +type GetDeployTasksByTypeReq struct { + ModelType string `form:"modelType"` +} + +type GetDeployTasksByTypeResp struct { + List interface{} `json:"list"` } From 1103e589f4c1df26e33494e98488f73c75c45075 Mon Sep 17 00:00:00 2001 From: tzwang Date: Tue, 27 Aug 2024 15:30:26 +0800 Subject: [PATCH 3/7] updated inference api logics Former-commit-id: 64f183b77efd974618fc6306aa78db6dd2d1f893 --- .../inference/getdeploytasksbytypehandler.go | 9 +++------ .../inference/getrunninginstancebytypehandler.go | 10 ++++------ .../logic/inference/getdeploytasksbytypelogic.go | 15 +++++++++++++-- .../inference/getrunninginstancebytypelogic.go | 2 +- internal/logic/inference/imageinferencelogic.go | 10 +++++----- internal/scheduler/database/aiStorage.go | 10 ++++++++++ 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/internal/handler/inference/getdeploytasksbytypehandler.go b/internal/handler/inference/getdeploytasksbytypehandler.go index 1d40c870..31465365 100644 --- a/internal/handler/inference/getdeploytasksbytypehandler.go +++ b/internal/handler/inference/getdeploytasksbytypehandler.go @@ -1,6 +1,7 @@ package inference import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" "net/http" "github.com/zeromicro/go-zero/rest/httpx" @@ -13,16 +14,12 @@ func GetDeployTasksByTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.GetDeployTasksByTypeReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := inference.NewGetDeployTasksByTypeLogic(r.Context(), svcCtx) resp, err := l.GetDeployTasksByType(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/internal/handler/inference/getrunninginstancebytypehandler.go b/internal/handler/inference/getrunninginstancebytypehandler.go index cf342f0f..628ca46a 100644 --- a/internal/handler/inference/getrunninginstancebytypehandler.go +++ b/internal/handler/inference/getrunninginstancebytypehandler.go @@ -1,6 +1,7 @@ package inference import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" "net/http" "github.com/zeromicro/go-zero/rest/httpx" @@ -13,16 +14,13 @@ func GetRunningInstanceByTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFun return func(w http.ResponseWriter, r *http.Request) { var req types.GetRunningInstanceReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := inference.NewGetRunningInstanceByTypeLogic(r.Context(), svcCtx) resp, err := l.GetRunningInstanceByType(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) + } } diff --git a/internal/logic/inference/getdeploytasksbytypelogic.go b/internal/logic/inference/getdeploytasksbytypelogic.go index d81a92e2..90c96b5c 100644 --- a/internal/logic/inference/getdeploytasksbytypelogic.go +++ b/internal/logic/inference/getdeploytasksbytypelogic.go @@ -2,6 +2,7 @@ package inference import ( "context" + "github.com/pkg/errors" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" @@ -24,7 +25,17 @@ func NewGetDeployTasksByTypeLogic(ctx context.Context, svcCtx *svc.ServiceContex } func (l *GetDeployTasksByTypeLogic) GetDeployTasksByType(req *types.GetDeployTasksByTypeReq) (resp *types.GetDeployTasksByTypeResp, err error) { - // todo: add your logic here and delete this line + resp = &types.GetDeployTasksByTypeResp{} - return + list, err := l.svcCtx.Scheduler.AiStorages.GetDeployTaskListByType(req.ModelType) + if err != nil { + return nil, err + } + + if len(list) == 0 { + return nil, errors.New("实列不存在") + } + + resp.List = list + return resp, nil } diff --git a/internal/logic/inference/getrunninginstancebytypelogic.go b/internal/logic/inference/getrunninginstancebytypelogic.go index fc2c1c63..e6e15ced 100644 --- a/internal/logic/inference/getrunninginstancebytypelogic.go +++ b/internal/logic/inference/getrunninginstancebytypelogic.go @@ -24,7 +24,7 @@ func NewGetRunningInstanceByTypeLogic(ctx context.Context, svcCtx *svc.ServiceCo } func (l *GetRunningInstanceByTypeLogic) GetRunningInstanceByType(req *types.GetRunningInstanceReq) (resp *types.GetRunningInstanceResp, err error) { - // todo: add your logic here and delete this line + resp = &types.GetRunningInstanceResp{} return } diff --git a/internal/logic/inference/imageinferencelogic.go b/internal/logic/inference/imageinferencelogic.go index 0ad1c127..9cc749e7 100644 --- a/internal/logic/inference/imageinferencelogic.go +++ b/internal/logic/inference/imageinferencelogic.go @@ -35,11 +35,11 @@ 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{} opt := &option.InferOption{ - TaskName: req.TaskName, - TaskDesc: req.TaskDesc, - AdapterId: req.AdapterId, - AiClusterIds: req.AiClusterIds, - ModelName: req.ModelName, + TaskName: req.TaskName, + TaskDesc: req.TaskDesc, + //AdapterId: req.AdapterId, + //AiClusterIds: req.AiClusterIds, + //ModelName: req.ModelName, ModelType: req.ModelType, Strategy: req.Strategy, StaticWeightMap: req.StaticWeightMap, diff --git a/internal/scheduler/database/aiStorage.go b/internal/scheduler/database/aiStorage.go index 58fbc554..5735c3ac 100644 --- a/internal/scheduler/database/aiStorage.go +++ b/internal/scheduler/database/aiStorage.go @@ -431,6 +431,16 @@ func (s *AiStorage) GetDeployTaskById(id int64) (*models.AiDeployInstanceTask, e return &task, nil } +func (s *AiStorage) GetDeployTaskListByType(modelType string) ([]*models.AiDeployInstanceTask, error) { + var tasks []*models.AiDeployInstanceTask + tx := s.DbEngin.Raw("select * from ai_deploy_instance_task where `model_type` = ?", modelType).Scan(&tasks) + if tx.Error != nil { + logx.Errorf(tx.Error.Error()) + return nil, tx.Error + } + return tasks, nil +} + func (s *AiStorage) GetAllDeployTasks() ([]*models.AiDeployInstanceTask, error) { var tasks []*models.AiDeployInstanceTask tx := s.DbEngin.Raw("select * from ai_deploy_instance_task").Scan(&tasks) From 8ab67fe8685892b543f778bd7184955699ce998e Mon Sep 17 00:00:00 2001 From: tzwang Date: Tue, 27 Aug 2024 15:46:20 +0800 Subject: [PATCH 4/7] updated GetRunningInstance apis Former-commit-id: 580ae562208ee4c896ca7065364b874bed62927e --- desc/inference/inference.api | 3 +-- internal/types/types.go | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/desc/inference/inference.api b/desc/inference/inference.api index 63a5e21d..abbd56ae 100644 --- a/desc/inference/inference.api +++ b/desc/inference/inference.api @@ -157,8 +157,7 @@ type ( } GetRunningInstanceReq { - AdapterIds []string `form:"adapterIds"` - ModelType string `form:"modelType"` + Id string `json:"deployTaskId"` } GetRunningInstanceResp { List interface{} `json:"list"` diff --git a/internal/types/types.go b/internal/types/types.go index 30bcf2ef..ca3a2125 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -6053,8 +6053,7 @@ type StopAllByDeployTaskIdResp struct { } type GetRunningInstanceReq struct { - AdapterIds []string `form:"adapterIds"` - ModelType string `form:"modelType"` + Id string `json:"deployTaskId"` } type GetRunningInstanceResp struct { From 9abbf03b33f8eaa9bd9d23b343f982231ce5d121 Mon Sep 17 00:00:00 2001 From: tzwang Date: Tue, 27 Aug 2024 15:55:18 +0800 Subject: [PATCH 5/7] updated GetRunningInstance apis Former-commit-id: 8f2ac755f354e759d4a3b6a1c981f027045b8da1 --- desc/inference/inference.api | 2 +- desc/pcm.api | 4 +-- ...er.go => getrunninginstancebyidhandler.go} | 16 +++++----- internal/handler/routes.go | 4 +-- .../inference/getrunninginstancebyidlogic.go | 30 +++++++++++++++++++ .../getrunninginstancebytypelogic.go | 30 ------------------- internal/types/types.go | 2 +- 7 files changed, 45 insertions(+), 43 deletions(-) rename internal/handler/inference/{getrunninginstancebytypehandler.go => getrunninginstancebyidhandler.go} (55%) create mode 100644 internal/logic/inference/getrunninginstancebyidlogic.go delete mode 100644 internal/logic/inference/getrunninginstancebytypelogic.go diff --git a/desc/inference/inference.api b/desc/inference/inference.api index abbd56ae..cbcd0030 100644 --- a/desc/inference/inference.api +++ b/desc/inference/inference.api @@ -157,7 +157,7 @@ type ( } GetRunningInstanceReq { - Id string `json:"deployTaskId"` + Id string `form:"deployTaskId"` } GetRunningInstanceResp { List interface{} `json:"list"` diff --git a/desc/pcm.api b/desc/pcm.api index 421604fc..9f068aa7 100644 --- a/desc/pcm.api +++ b/desc/pcm.api @@ -966,8 +966,8 @@ service pcm { @handler StopAllByDeployTaskId post /inference/stopAll (StopAllByDeployTaskIdReq) returns (StopAllByDeployTaskIdResp) - @handler GetRunningInstanceByType - get /inference/getInstanceByType (GetRunningInstanceReq) returns (GetRunningInstanceResp) + @handler GetRunningInstanceById + get /inference/getRunningInstanceById (GetRunningInstanceReq) returns (GetRunningInstanceResp) @handler GetDeployTasksByType get /inference/getDeployTasksByType (GetDeployTasksByTypeReq) returns (GetDeployTasksByTypeResp) diff --git a/internal/handler/inference/getrunninginstancebytypehandler.go b/internal/handler/inference/getrunninginstancebyidhandler.go similarity index 55% rename from internal/handler/inference/getrunninginstancebytypehandler.go rename to internal/handler/inference/getrunninginstancebyidhandler.go index 628ca46a..5861991d 100644 --- a/internal/handler/inference/getrunninginstancebytypehandler.go +++ b/internal/handler/inference/getrunninginstancebyidhandler.go @@ -1,7 +1,6 @@ package inference import ( - "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" "net/http" "github.com/zeromicro/go-zero/rest/httpx" @@ -10,17 +9,20 @@ import ( "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" ) -func GetRunningInstanceByTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { +func GetRunningInstanceByIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.GetRunningInstanceReq if err := httpx.Parse(r, &req); err != nil { - result.ParamErrorResult(r, w, err) + httpx.ErrorCtx(r.Context(), w, err) return } - l := inference.NewGetRunningInstanceByTypeLogic(r.Context(), svcCtx) - resp, err := l.GetRunningInstanceByType(&req) - result.HttpResult(r, w, resp, err) - + l := inference.NewGetRunningInstanceByIdLogic(r.Context(), svcCtx) + resp, err := l.GetRunningInstanceById(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } } } diff --git a/internal/handler/routes.go b/internal/handler/routes.go index c9524f9d..0e7f6b77 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -1225,8 +1225,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodGet, - Path: "/inference/getInstanceByType", - Handler: inference.GetRunningInstanceByTypeHandler(serverCtx), + Path: "/inference/getRunningInstanceById", + Handler: inference.GetRunningInstanceByIdHandler(serverCtx), }, { Method: http.MethodGet, diff --git a/internal/logic/inference/getrunninginstancebyidlogic.go b/internal/logic/inference/getrunninginstancebyidlogic.go new file mode 100644 index 00000000..ab4d75c0 --- /dev/null +++ b/internal/logic/inference/getrunninginstancebyidlogic.go @@ -0,0 +1,30 @@ +package inference + +import ( + "context" + + "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetRunningInstanceByIdLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetRunningInstanceByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRunningInstanceByIdLogic { + return &GetRunningInstanceByIdLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetRunningInstanceByIdLogic) GetRunningInstanceById(req *types.GetRunningInstanceReq) (resp *types.GetRunningInstanceResp, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/internal/logic/inference/getrunninginstancebytypelogic.go b/internal/logic/inference/getrunninginstancebytypelogic.go deleted file mode 100644 index e6e15ced..00000000 --- a/internal/logic/inference/getrunninginstancebytypelogic.go +++ /dev/null @@ -1,30 +0,0 @@ -package inference - -import ( - "context" - - "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" - "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type GetRunningInstanceByTypeLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewGetRunningInstanceByTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRunningInstanceByTypeLogic { - return &GetRunningInstanceByTypeLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -func (l *GetRunningInstanceByTypeLogic) GetRunningInstanceByType(req *types.GetRunningInstanceReq) (resp *types.GetRunningInstanceResp, err error) { - resp = &types.GetRunningInstanceResp{} - - return -} diff --git a/internal/types/types.go b/internal/types/types.go index ca3a2125..9ba67844 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -6053,7 +6053,7 @@ type StopAllByDeployTaskIdResp struct { } type GetRunningInstanceReq struct { - Id string `json:"deployTaskId"` + Id string `form:"deployTaskId"` } type GetRunningInstanceResp struct { From b016f42a0f1aaa65bc5c6d02cd4ba7e0c63e42fb Mon Sep 17 00:00:00 2001 From: tzwang Date: Tue, 27 Aug 2024 16:22:08 +0800 Subject: [PATCH 6/7] updated GetRunningInstance apis Former-commit-id: e3b83f65357add17098b0a277f51a1e13bfbd87f --- desc/inference/inference.api | 1 + internal/types/types.go | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/desc/inference/inference.api b/desc/inference/inference.api index cbcd0030..c58b1dc1 100644 --- a/desc/inference/inference.api +++ b/desc/inference/inference.api @@ -158,6 +158,7 @@ type ( GetRunningInstanceReq { Id string `form:"deployTaskId"` + AdapterId string `form:"adapterId"` } GetRunningInstanceResp { List interface{} `json:"list"` diff --git a/internal/types/types.go b/internal/types/types.go index 9ba67844..a5fe2f86 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -6053,7 +6053,8 @@ type StopAllByDeployTaskIdResp struct { } type GetRunningInstanceReq struct { - Id string `form:"deployTaskId"` + Id string `form:"deployTaskId"` + AdapterId string `form:"adapterId"` } type GetRunningInstanceResp struct { From a60e97134d6659006849d2d70ec8bd06d1f05d55 Mon Sep 17 00:00:00 2001 From: tzwang Date: Tue, 27 Aug 2024 17:47:20 +0800 Subject: [PATCH 7/7] updated inference api logics Former-commit-id: 7c4b471eb2de5b6f6bbe41b4d5b62c940ac4ec4b --- .../getrunninginstancebyidhandler.go | 10 ++- .../inference/getrunninginstancebyidlogic.go | 10 ++- .../logic/inference/imageinferencelogic.go | 70 ++++++++++++------- internal/scheduler/database/aiStorage.go | 4 +- .../imageInference/imageInference.go | 29 +++++++- .../scheduler/service/inference/inference.go | 1 + 6 files changed, 87 insertions(+), 37 deletions(-) diff --git a/internal/handler/inference/getrunninginstancebyidhandler.go b/internal/handler/inference/getrunninginstancebyidhandler.go index 5861991d..ec8e3605 100644 --- a/internal/handler/inference/getrunninginstancebyidhandler.go +++ b/internal/handler/inference/getrunninginstancebyidhandler.go @@ -1,6 +1,7 @@ package inference import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" "net/http" "github.com/zeromicro/go-zero/rest/httpx" @@ -13,16 +14,13 @@ func GetRunningInstanceByIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc return func(w http.ResponseWriter, r *http.Request) { var req types.GetRunningInstanceReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := inference.NewGetRunningInstanceByIdLogic(r.Context(), svcCtx) resp, err := l.GetRunningInstanceById(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) + } } diff --git a/internal/logic/inference/getrunninginstancebyidlogic.go b/internal/logic/inference/getrunninginstancebyidlogic.go index ab4d75c0..80c25976 100644 --- a/internal/logic/inference/getrunninginstancebyidlogic.go +++ b/internal/logic/inference/getrunninginstancebyidlogic.go @@ -2,6 +2,7 @@ package inference import ( "context" + "strconv" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" @@ -24,7 +25,14 @@ func NewGetRunningInstanceByIdLogic(ctx context.Context, svcCtx *svc.ServiceCont } func (l *GetRunningInstanceByIdLogic) GetRunningInstanceById(req *types.GetRunningInstanceReq) (resp *types.GetRunningInstanceResp, err error) { - // todo: add your logic here and delete this line + resp = &types.GetRunningInstanceResp{} + id, err := strconv.ParseInt(req.Id, 10, 64) + if err != nil { + return nil, err + } + list, err := l.svcCtx.Scheduler.AiStorages.GetRunningDeployInstanceById(id, req.AdapterId) + + resp.List = list return } diff --git a/internal/logic/inference/imageinferencelogic.go b/internal/logic/inference/imageinferencelogic.go index 9cc749e7..88f93ded 100644 --- a/internal/logic/inference/imageinferencelogic.go +++ b/internal/logic/inference/imageinferencelogic.go @@ -34,6 +34,10 @@ 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 { + return nil, errors.New("instances are empty") + } + opt := &option.InferOption{ TaskName: req.TaskName, TaskDesc: req.TaskDesc, @@ -72,42 +76,54 @@ 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") - } + //_, ok := l.svcCtx.Scheduler.AiService.AiCollectorAdapterMap[opt.AdapterId] + //if !ok { + // return nil, errors.New("AdapterId does not exist") + //} + // - var strat strategy.Strategy - switch opt.Strategy { - case strategy.STATIC_WEIGHT: - strat = strategy.NewStaticWeightStrategy(opt.StaticWeightMap, int32(len(ts))) + 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 } - 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:]...) + 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 { + // + // } + //} - imageInfer, err := imageInference.New(imageInference.NewImageClassification(), ts, clusters, opt, l.svcCtx.Scheduler.AiStorages, l.svcCtx.Scheduler.AiService.InferenceAdapterMap, adapterName) + imageInfer, err := imageInference.New(imageInference.NewImageClassification(), ts, cs, req.Instances, opt, l.svcCtx.Scheduler.AiStorages, l.svcCtx.Scheduler.AiService.InferenceAdapterMap, adapterName) if err != nil { return nil, err } diff --git a/internal/scheduler/database/aiStorage.go b/internal/scheduler/database/aiStorage.go index 5735c3ac..6f9c4285 100644 --- a/internal/scheduler/database/aiStorage.go +++ b/internal/scheduler/database/aiStorage.go @@ -584,9 +584,9 @@ func (s *AiStorage) SaveInferDeployTask(taskName string, modelName string, model return taskModel.Id, nil } -func (s *AiStorage) GetRunningDeployInstanceByModelNameAndAdapterId(modelType string, modelName string, adapterId string) ([]*models.AiInferDeployInstance, error) { +func (s *AiStorage) GetRunningDeployInstanceById(id int64, adapterId string) ([]*models.AiInferDeployInstance, error) { var list []*models.AiInferDeployInstance - tx := s.DbEngin.Raw("select * from ai_infer_deploy_instance where `model_type` = ? and `model_name` = ? and `adapter_id` = ? and `status` = 'Running'", modelType, modelName, adapterId).Scan(&list) + tx := s.DbEngin.Raw("select * from ai_infer_deploy_instance where `deploy_instance_task_id` = ? and `adapter_id` = ? and `status` = 'Running'", id, adapterId).Scan(&list) if tx.Error != nil { logx.Errorf(tx.Error.Error()) return nil, tx.Error diff --git a/internal/scheduler/service/inference/imageInference/imageInference.go b/internal/scheduler/service/inference/imageInference/imageInference.go index ea569056..8c45c5a1 100644 --- a/internal/scheduler/service/inference/imageInference/imageInference.go +++ b/internal/scheduler/service/inference/imageInference/imageInference.go @@ -46,6 +46,7 @@ type ImageInference struct { inference IImageInference files []*ImageFile clusters []*strategy.AssignedCluster + instances []types.DeployInstance opt *option.InferOption storage *database.AiStorage inferAdapter map[string]map[string]inference.ICluster @@ -57,6 +58,7 @@ func New( inference IImageInference, files []*ImageFile, clusters []*strategy.AssignedCluster, + instances []types.DeployInstance, opt *option.InferOption, storage *database.AiStorage, inferAdapter map[string]map[string]inference.ICluster, @@ -66,6 +68,7 @@ func New( inference: inference, files: files, clusters: clusters, + instances: instances, opt: opt, storage: storage, inferAdapter: inferAdapter, @@ -145,7 +148,7 @@ func (i *ImageInference) saveAiTask(id int64) error { return nil } -func (i *ImageInference) filterClusters() ([]*FilteredCluster, error) { +func (i *ImageInference) filterClustersTemp() ([]*FilteredCluster, error) { var wg sync.WaitGroup var ch = make(chan *FilteredCluster, len(i.clusters)) var cs []*FilteredCluster @@ -190,6 +193,30 @@ func (i *ImageInference) filterClusters() ([]*FilteredCluster, error) { return cs, nil } +func (i *ImageInference) filterClusters() ([]*FilteredCluster, error) { + var cs []*FilteredCluster + for _, cluster := range i.clusters { + var inferurls []*inference.InferUrl + 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 + } + var url inference.InferUrl + url.Url = deployInstance.InferUrl + url.Card = deployInstance.InferCard + inferurls = append(inferurls, &url) + } + } + var f FilteredCluster + f.urls = inferurls + cs = append(cs, &f) + } + return cs, nil +} + func (i *ImageInference) inferImages(cs []*FilteredCluster) ([]*types.ImageResult, error) { var wg sync.WaitGroup var ch = make(chan *types.ImageResult, len(i.files)) diff --git a/internal/scheduler/service/inference/inference.go b/internal/scheduler/service/inference/inference.go index 1e8442bc..4695ec07 100644 --- a/internal/scheduler/service/inference/inference.go +++ b/internal/scheduler/service/inference/inference.go @@ -46,6 +46,7 @@ type DeployInstance struct { ModelName string ModelType string InferCard string + InferUrl string ClusterName string ClusterType string Status string