|
- /* ******************************************************************************
- * 2019 - present Contributed by Apulis Technology (Shenzhen) Co. LTD
- *
- * This program and the accompanying materials are made available under the
- * terms of the MIT License, which is available at
- * https://www.opensource.org/licenses/MIT
- *
- * See the NOTICE file distributed with this work for additional
- * information regarding copyright ownership.
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- * SPDX-License-Identifier: MIT
- ******************************************************************************/
- package controllers
-
- import (
- "encoding/json"
- "fmt"
- "path"
- "strconv"
- "strings"
-
- "github.com/apulis/app/ai-arts-backend/pkg/exports"
-
- "github.com/apulis/app/ai-arts-backend/internal/configs"
- "github.com/apulis/app/ai-arts-backend/internal/constant"
- "github.com/apulis/app/ai-arts-backend/internal/dto"
- "github.com/apulis/app/ai-arts-backend/internal/services"
- "github.com/gin-gonic/gin"
- )
-
- type PresetLab struct {
- UserInfo
- HeaderInfo
- ControllerBase
- }
-
- func (ctrl PresetLab) TaskTemplate(c *gin.Context) {
- var req dto.GetModelVersionDetailReq
- if bindAndValidate(c, &req) {
- ctrl.InitUserInfo(c)
- ctrl.InitHeader(c)
-
- getProjectResp, err := services.GetGlobalProjectService().Get(&dto.GetProjectReq{Id: req.ProjectId})
- if err != nil {
- fail(c, err)
- return
- }
-
- resp, commResp, err := services.GetGlobalApWorkShopService().GetModelVersionDetail(&req, ctrl.HeaderInfo.header)
- if err != nil {
- fail(c, err)
- return
- } else if commResp.Code != 0 {
- failWithData(c, commResp.Code, commResp.Msg, commResp.Data)
- return
- }
-
- bffresp := dto.GetModelVersionDetailBffResp{}
- bffresp.Init(resp)
- bffresp.VersionId, err = strconv.Atoi(req.VersionId)
- bffresp.LabId = getProjectResp.LabId
- if err != nil {
- fail(c, err)
- return
- }
-
- /***
- quotaReq := &dto.GetResourceQuotaReq{}
- quotaItems, commResp, err := services.GetGlobalAomService().GetResourceQuotas(quotaReq, ctrl.HeaderInfo.header)
- if err != nil {
- fail(c, err)
- return
- } else if commResp.Code != 0 {
- failWithData(c, commResp.Code, commResp.Msg, commResp.Data)
- return
- }***/
-
- respWithData(c, bffresp)
- }
- }
-
- func (ctrl PresetLab) GetModelVersionDetail(c *gin.Context, id string, versionId string) *dto.GetModelVersionDetailResp {
- var getModelDetailReq dto.GetModelVersionDetailReq
- getModelDetailReq.Id = fmt.Sprintf("%v", id)
- getModelDetailReq.VersionId = fmt.Sprintf("%v", versionId)
- resp, commResp, err := services.GetGlobalApWorkShopService().GetModelVersionDetail(&getModelDetailReq, ctrl.HeaderInfo.header)
- if err != nil {
- fail(c, err)
- return nil
- } else if commResp.Code != 0 {
- failWithData(c, commResp.Code, commResp.Msg, commResp.Data)
- return nil
- }
- return resp
- }
-
- func (ctrl PresetLab) CreateAiLabRunReq(c *gin.Context, runType string, modelVersionDetail *dto.GetModelVersionDetailResp, quota *dto.Quota, labId string, req *dto.CreateTrainAndEvalBaseReq) *dto.CreateAiLabRunReq {
- runReq := &dto.CreateAiLabRunReq{}
- runReq.Name = "test"
- runReq.Creator = ctrl.userName
- runReq.Quota = *quota
- runReq.Arch = quota.Arch
- //runReq.Quota.Request = *quota
- //runReq.Quota.Node = runReq.Quota.Request.Node
- //runReq.Quota.Request.Node = 0
-
- runReq.Resource = map[string]dto.ResourceType{}
- labId1, err := strconv.ParseInt(labId, 10, 64)
- if err != nil {
- fail(c, err)
- return nil
- }
- runReq.LabId = labId1
- runReq.UserId = ctrl.userId
- runReq.Engine = modelVersionDetail.ModelVersion.Engine
-
- runReq.Cmd = append(runReq.Cmd, "/start/aiarts_launcher")
-
- runReq.Tags = map[string]string{}
- runReq.Tags["framework"] = modelVersionDetail.ModelVersion.Framework
- runReq.Tags["backbone"] = modelVersionDetail.ModelVersion.BackBone
- runReq.Tags["engine"] = modelVersionDetail.ModelVersion.Engine
- runReq.Tags["datasetId"] = fmt.Sprintf("%v", req.DatasetId)
- runReq.Tags["datasetName"] = req.DatasetName
- runReq.Tags["datasetVersion"] = fmt.Sprintf("%v", req.DatasetVersion)
- runReq.Tags["modelId"] = fmt.Sprintf("%v", req.ModelId)
- runReq.Tags["modelName"] = modelVersionDetail.ModelVersion.ModelName
- runReq.Tags["modelVersion"] = fmt.Sprintf("%v", req.ModelVersion)
- runReq.Tags["modelVersionName"] = modelVersionDetail.ModelVersion.Name
- runReq.Tags["field"] = modelVersionDetail.ModelVersion.Field
- runReq.Tags["task"] = modelVersionDetail.ModelVersion.Task
- if modelVersionDetail.ModelVersion.Train.Visualization != "" {
- runReq.Tags["visualization"] = modelVersionDetail.ModelVersion.Train.Visualization
- }
- launcherConfig := dto.LaunchConfig{}
- if runType == "train" {
- launcherConfig.Entry = modelVersionDetail.ModelVersion.Train.Entry
- } else if runType == "eval" {
- launcherConfig.Entry = modelVersionDetail.ModelVersion.Eval.Entry
- }
-
- var userParams []dto.ModelUserParamType
-
- if runType == "train" {
- userParams = modelVersionDetail.ModelVersion.Train.UserParams
- } else if runType == "eval" {
- userParams = modelVersionDetail.ModelVersion.Eval.UserParams
- }
-
- for _, v := range userParams {
- p := dto.UserParamsType{Name: v.Name, Value: v.Default, Default: v.Default, Required: v.Required, Desc: v.Desc, Editable: v.Editable}
- //user params
- for _, userParms := range req.UserParams {
- if userParms.Name == v.Name {
- if userParms.Value == nil {
- p.Value = ""
- } else {
- p.Value = fmt.Sprintf("%v", userParms.Value)
- }
- }
- }
- launcherConfig.UserParams = append(launcherConfig.UserParams, p)
- }
- runReq.Resource = map[string]dto.ResourceType{}
-
- var sysParams []dto.ModelSysParamType
-
- if runType == "train" {
- sysParams = modelVersionDetail.ModelVersion.Train.SysParams
- } else if runType == "eval" {
- sysParams = modelVersionDetail.ModelVersion.Eval.SysParams
- }
-
- for _, v := range sysParams {
- p := dto.SysParamsType{Name: v.Name, Arg: v.Arg}
- resource := dto.ResourceType{}
- switch p.Name {
- case constant.SYS_PARAM_DATA_PATH:
- p.Type = constant.AILAB_RESOURCE_TYPE_DATASET
- resource.Type = constant.AILAB_RESOURCE_TYPE_DATASET
- resource.ID = fmt.Sprintf("%v", req.DatasetId)
- resource.Version = fmt.Sprintf("%v", req.DatasetVersion)
- case constant.SYS_PARAM_PRETRAINED_MODEL:
- p.Type = constant.AILAB_RESOURCE_TYPE_MLRUN
- resource.Type = constant.AILAB_RESOURCE_TYPE_MLRUN
- continue
- case constant.SYS_PARAM_OUTPUT_PATH:
- p.Type = constant.AILAB_RESOURCE_TYPE_OUTPUT
- resource.Type = constant.AILAB_RESOURCE_TYPE_OUTPUT
- case constant.SYS_PARAM_CHECKPOINT_PATH:
- p.Type = "#train"
- resource.Type = "#train"
- case constant.SYS_PARAM_LABELS:
- continue
- case constant.SYS_PARAM_DEVICE_NUM:
- p.Type = ""
- p.Value = req.Quota.Device.DeviceNum
- default:
- fail(c, fmt.Errorf("SysParams name: %s error", v.Name))
- return nil
- }
-
- launcherConfig.SysParams = append(launcherConfig.SysParams, p)
- if p.Type != "" {
- runReq.Resource[p.Name] = resource
- }
- }
- runReq.Resource["model"] = dto.ResourceType{Type: constant.AILAB_RESOURCE_TYPE_APWORKSHOP,
- ID: fmt.Sprintf("%v", req.ModelId),
- Version: fmt.Sprintf("%v", req.ModelVersion)}
-
- runReq.Config = map[string]dto.LaunchConfig{}
- runReq.Config[runType] = launcherConfig
- return runReq
- }
-
- func (ctrl PresetLab) CreateEvalLab(c *gin.Context) {
- var req dto.CreateEvalReq
- if bindAndValidate(c, &req) {
- ctrl.InitUserInfo(c)
- ctrl.InitHeader(c)
- fmt.Println(req.LabId, req.RunId)
- modelVersionDetail := ctrl.GetModelVersionDetail(c, fmt.Sprintf("%v", req.ModelId), fmt.Sprintf("%v", req.ModelVersion))
- if modelVersionDetail == nil {
- return
- }
- if req.Quota.QuotaId == 0 {
- fail(c, exports.ParameterError("start train job invalid quota id !"))
- return
- }
-
- quota, err1 := services.ValidateUserGroupQuota(c.GetHeader("Authorization"), ctrl.groupId,
- req.Quota.QuotaId, req.Quota.Num,
- req.Quota.Node, modelVersionDetail, c.Query("checkUsed") == "1")
- if err1 != nil {
- fail(c, err1)
- return
- }
- fmt.Println(quota, quota)
-
- runReq := ctrl.CreateAiLabRunReq(c, "eval", modelVersionDetail, quota, req.LabId, &req.CreateTrainAndEvalBaseReq)
- if runReq == nil {
- return
- }
- runReq.Parent = req.RunId
- startTrainLabResp, commResp, err := services.GetGlobalAiLabService().StartEvalLab(runReq, ctrl.header)
- if err != nil {
- fail(c, err)
- return
- } else if commResp.Code != 0 {
- failWithData(c, commResp.Code, commResp.Msg, commResp.Data)
- return
- }
- respWithData(c, startTrainLabResp)
-
- }
- }
-
- func (ctrl PresetLab) CreateTrainLab(c *gin.Context) {
- var req dto.CreateTrainReq
- if bindAndValidate(c, &req) {
- ctrl.InitUserInfo(c)
- ctrl.InitHeader(c)
-
- reqData, _ := json.Marshal(req)
- fmt.Println(string(reqData))
- modelVersionDetail := ctrl.GetModelVersionDetail(c, fmt.Sprintf("%v", req.ModelId), fmt.Sprintf("%v", req.ModelVersion))
- if modelVersionDetail == nil {
- return
- }
- if req.Quota.QuotaId == 0 {
- fail(c, exports.ParameterError("start train job invalid quota id !"))
- return
- }
-
- quota, err1 := services.ValidateUserGroupQuota(c.GetHeader("Authorization"), ctrl.groupId,
- req.Quota.QuotaId, req.Quota.Num,
- req.Quota.Node, modelVersionDetail, c.Query("checkUsed") == "1")
- if err1 != nil {
- fail(c, err1)
- return
- }
- fmt.Println(quota, quota)
- runReq := ctrl.CreateAiLabRunReq(c, "train", modelVersionDetail, quota, req.LabId, &req.CreateTrainAndEvalBaseReq)
- if runReq == nil {
- return
- }
- startTrainLabResp, commResp, err := services.GetGlobalAiLabService().StartTrainLab(runReq, ctrl.header)
- if err != nil {
- fail(c, err)
- return
- } else if commResp.Code != 0 {
- failWithData(c, commResp.Code, commResp.Msg, commResp.Data)
- return
- }
- respWithData(c, startTrainLabResp)
-
- //err = services.GetGlobalPresetLabService().Create(&req)
- //if err != nil {
- // fail(c, err)
- // return
- //}
- }
-
- }
-
- func (ctrl PresetLab) ListTrainLab(c *gin.Context) {
- labId := c.Param("labId")
- ctrl.ForwardWithQuery(c, configs.GetAppConfig().Resources.Ailab+fmt.Sprintf("/labs/%s/runs", labId),
- fmt.Sprintf("&jobType=train"))
- }
-
- func (ctrl PresetLab) ListEvalLab(c *gin.Context) {
- labId := c.Param("labId")
- runId := c.Param("runId")
- ctrl.ForwardWithQuery(c, configs.GetAppConfig().Resources.Ailab+fmt.Sprintf("/labs/%s/runs", labId),
- fmt.Sprintf("&parent=%s&jobType=eval", runId))
- }
-
- func (ctrl PresetLab) DeleteRunLab(c *gin.Context) {
- labId := c.Param("labId")
- runId := c.Param("runId")
- ctrl.Forward(c, configs.GetAppConfig().Resources.Ailab+fmt.Sprintf("/labs/%s/runs/%s", labId, runId))
- }
-
- func (ctrl PresetLab) GetRunLabLogs(c *gin.Context) {
- labId := c.Param("labId")
- runId := c.Param("runId")
- ctrl.Forward(c, configs.GetAppConfig().Resources.Ailab+fmt.Sprintf("/labs/%s/runs/%s/logs", labId, runId))
- }
-
- func (ctrl PresetLab) GetRunLabFiles(c *gin.Context) {
- labId := c.Param("labId")
- runId := c.Param("runId")
- ctrl.Forward(c, configs.GetAppConfig().Resources.Ailab+fmt.Sprintf("/labs/%s/runs/%s/files", labId, runId))
- }
-
- func (ctrl PresetLab) GetRunLabViews(c *gin.Context) {
- labId := c.Param("labId")
- runId := c.Param("runId")
- ctrl.Forward(c, configs.GetAppConfig().Resources.Ailab+fmt.Sprintf("/labs/%s/runs/%s/view", labId, runId))
- }
-
- func (ctrl PresetLab) FetchRunLabLogs(c *gin.Context) {
- labId := c.Param("labId")
- runId := c.Param("runId")
- ctrl.Forward(c, configs.GetAppConfig().Resources.Ailab+fmt.Sprintf("/labs/%s/runs/%s/fetch-logs", labId, runId))
- }
-
- func (ctrl PresetLab) GetRunLab(c *gin.Context) {
- labId := c.Param("labId")
- runId := c.Param("runId")
- ctrl.Forward(c, configs.GetAppConfig().Resources.Ailab+fmt.Sprintf("/labs/%s/runs/%s", labId, runId))
- }
-
- func (ctrl *PresetLab) VisualOpt(c *gin.Context) {
- var req dto.VisualOptReq
- if bindAndValidate(c, &req) {
- ctrl.InitUserInfo(c)
- ctrl.InitHeader(c)
-
- if req.Action != "open_visual" {
- ctrl.Forward(c, configs.GetAppConfig().Resources.Ailab+fmt.Sprintf("/labs/%s/runs/%s", req.LabId, req.RunId))
- return
- }
- //@modify: job system only deal with lower case
- visualization := strings.ToLower(req.Visualization)
-
- imageInfo, ok := configs.GetAppConfig().SystemImages[visualization]
- if !ok {
- fail(c, fmt.Errorf("Visualization %v error", req.Visualization))
- return
- }
-
- fmt.Println(req)
- runReq := dto.AiLabRunOptReq{}
- fmt.Println("action", req.Action)
- runReq.Action = req.Action
- runReq.LabId, _ = strconv.ParseInt(req.LabId, 10, 64)
- runReq.Name = req.Visualization
- runReq.Engine = imageInfo.Engine
- runReq.Description = ""
- runReq.Creator = ctrl.userName
- runReq.UserId = ctrl.userId
- runReq.Cmd = imageInfo.Cmd
- runReq.EndPoints = append(runReq.EndPoints, dto.EndPointType{Name: visualization, Port: 45110})
- runReq.RunId = req.RunId
- runReq.Quota.Request.CPU = imageInfo.Cpu
- runReq.Quota.Request.Memory = imageInfo.Mem
- runReq.Quota.Limit = runReq.Quota.Request
-
- aiLabService := services.GetGlobalAiLabService()
- data, commResp, err := aiLabService.AiLabRunOpt(&runReq, c.Request.Header)
-
- if err != nil {
- fail(c, err)
- return
- } else if commResp.Code != 0 && commResp.Code != 280000023{
- failWithData(c, commResp.Code, commResp.Msg, commResp.Data)
- return
- }
- respWithData(c, data)
- }
- }
-
- func (ctrl PresetLab) RegisterModel(c *gin.Context) {
- var req dto.RegisterModelReq
- if bindAndValidate(c, &req) {
- ctrl.InitHeader(c)
- ctrl.InitUserInfo(c)
-
- var getModelReq dto.GetModelVersionDetailReq
- getModelReq.Id = req.ModelId
- getModelReq.VersionId = req.ModelVersion
- _, commResp, err := services.GetGlobalApWorkShopService().GetModelVersionDetail(&getModelReq, ctrl.HeaderInfo.header)
- if err != nil {
- fail(c, fmt.Errorf("req ApWorkShop error %s", err))
- return
- } else if commResp.Code != 0 {
- failWithData(c, commResp.Code, commResp.Msg, commResp.Data)
- return
- }
-
- imageInfo, ok := configs.GetAppConfig().SystemImages["register"]
- if !ok {
- fail(c, fmt.Errorf("register error,imageInfo config not exist"))
- return
- }
- if req.ModelId == "" {
- fail(c, fmt.Errorf("modelId is empty!"))
- return
- }
- if req.ModelVersion == "" {
- fail(c, fmt.Errorf("modelVersion is empty!"))
- return
- }
-
- runReq := dto.AiLabRunOptReq{}
- fmt.Println("action", req.Action)
- runReq.Action = req.Action
- runReq.LabId, _ = strconv.ParseInt(req.LabId, 10, 64)
- runReq.Name = fmt.Sprintf("register")
- runReq.Engine = imageInfo.Engine
- runReq.Description = ""
- runReq.Creator = ctrl.userName
- runReq.UserId = ctrl.userId
- runReq.Cmd = append(runReq.Cmd, "/code/start/aiarts_launcher")
- runReq.RunId = req.RunId
- runReq.Quota.Request.CPU = imageInfo.Cpu
- runReq.Quota.Request.Memory = imageInfo.Mem
- runReq.Quota.Limit = runReq.Quota.Request
-
- runReq.Resource = map[string]dto.ResourceType{}
- runReq.Resource["#train"] = dto.ResourceType{}
- runReq.Resource["#eval"] = dto.ResourceType{}
-
- runReq.Resource["save"] = dto.ResourceType{Type: constant.AILAB_RESOURCE_TYPE_APWORKSHOP,
- Scope: "ai-arts",
- Name: req.Name,
- ID: "*",
- Access: 1}
-
- if req.Logo != "" {
- runReq.Resource["file-server"] = dto.ResourceType{Type: constant.AILAB_RESOURCE_TYPE_STORE,
- Path: req.Logo[0:strings.LastIndex(req.Logo, "/")],
- Rpath: "/home/AppData/file-server/"}
- }
-
- runReq.Resource["model"] = dto.ResourceType{Type: constant.AILAB_RESOURCE_TYPE_APWORKSHOP,
- ID: fmt.Sprintf("%v", req.ModelId),
- Version: fmt.Sprintf("%v", req.ModelVersion)}
-
- runReq.Tags = map[string]string{}
-
- registerConfig := &dto.LaunchConfig{}
- registerConfig.UserParams = []dto.UserParamsType{}
- registerConfig.UserParams = append(registerConfig.UserParams, dto.UserParamsType{Name: "description", Value: req.Description})
- if req.Logo != "" {
- registerConfig.UserParams = append(registerConfig.UserParams, dto.UserParamsType{Name: "logo", Value: path.Join("/home/AppData/file-server/", req.Logo[strings.LastIndex(req.Logo, "/")+1:])})
- }
- runReq.Config = map[string]dto.LaunchConfig{}
- runReq.Config["register"] = *registerConfig
- aiLabService := services.GetGlobalAiLabService()
- _, commResp, err = aiLabService.AiLabRunOpt(&runReq, c.Request.Header)
-
- if err != nil {
- fail(c, err)
- return
- } else if commResp.Code != 0 && commResp.Code != 280000023 {
- failWithData(c, commResp.Code, commResp.Msg, commResp.Data)
- return
- }
- respWithData(c, commResp)
- }
- }
-
- func (ctrl *PresetLab) UpdateRunInfo(c *gin.Context) {
- ctrl.Forward(c, configs.GetAppConfig().Resources.Ailab+fmt.Sprintf("/labs/%s/runs/%s", c.Param("labId") ,c.Param("runId")))
- }
|