您必须先将模型部署到端点,然后才能使用该模型执行在线预测。部署模型会将物理资源相关联,以便以低延迟方式执行在线预测。
本页面介绍了必须遵循的步骤,以便使用在线预测将模型部署到端点。
准备工作
在将模型部署到端点之前,请导出模型工件以进行预测,并确保您满足该页面中的所有前提条件。
创建资源池
借助ResourcePool
自定义资源,您可以精细控制模型的行为。您可以定义以下设置:
- 自动扩缩配置。
- 机器类型,用于定义 CPU 和内存要求。
- 加速器选项,例如 GPU 资源。
机器类型对于您发送的用于创建预测集群的节点池规范请求至关重要。
对于已部署模型的资源池,加速器数量和类型决定了 GPU 使用情况。机器类型仅决定所请求的 CPU 和内存资源。因此,在 ResourcePool
规范中包含 GPU 加速器时,machineType
字段用于控制模型的 CPU 和内存要求,而 acceleratorType
字段用于控制 GPU。此外,acceleratorCount
字段用于控制 GPU 切片的数量。
如需创建 ResourcePool
自定义资源,请按以下步骤操作:
创建一个 YAML 文件,用于定义
ResourcePool
自定义资源。以下示例包含用于以下资源池的 YAML 文件:具有 GPU 加速器(基于 GPU 的模型)和不具有 GPU 加速器(基于 CPU 的模型):基于 GPU 的模型
apiVersion: prediction.aiplatform.gdc.goog/v1 kind: ResourcePool metadata: name: RESOURCE_POOL_NAME namespace: PROJECT_NAMESPACE spec: resourcePoolID: RESOURCE_POOL_NAME enableContainerLogging: false dedicatedResources: machineSpec: # The system adds computing overhead to the nodes for mandatory components. # Choose a machineType value that allocates fewer CPU and memory resources # than those used by the nodes in the prediction cluster. machineType: a2-highgpu-1g-gdc acceleratorType: nvidia-a100-80gb # The accelerator count is a slice of the requested virtualized GPUs. # The value corresponds to one-seventh of 80 GB of GPUs for each count. acceleratorCount: 2 autoscaling: minReplica: 2 maxReplica: 10
基于 CPU 的模型
apiVersion: prediction.aiplatform.gdc.goog/v1 kind: ResourcePool metadata: name: RESOURCE_POOL_NAME namespace: PROJECT_NAMESPACE spec: resourcePoolID: RESOURCE_POOL_NAME enableContainerLogging: false dedicatedResources: machineSpec: # The system adds computing overhead to the nodes for mandatory components. # Choose a machineType value that allocates fewer CPU and memory resources # than those used by the nodes in the prediction cluster. machineType: n2-highcpu-8-gdc autoscaling: minReplica: 2 maxReplica: 10
替换以下内容:
RESOURCE_POOL_NAME
:您要为ResourcePool
定义文件指定的名称。PROJECT_NAMESPACE
:与预测集群关联的项目命名空间的名称。
根据资源需求和预测集群中的可用资源,修改
dedicatedResources
字段中的值。将
ResourcePool
定义文件应用于预测集群:kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG apply -f RESOURCE_POOL_NAME.yaml
替换以下内容:
PREDICTION_CLUSTER_KUBECONFIG
:预测集群中 kubeconfig 文件的路径。RESOURCE_POOL_NAME
:ResourcePool
定义文件的名称。
创建 ResourcePool
自定义资源时,Kubernetes API 和 webhook 服务会验证 YAML 文件,并报告成功或失败。当您将模型部署到端点时,预测运算符会从资源池中预配并预留资源。
将模型部署到端点
如果您有资源池,可以将多个模型部署到一个端点,也可以将一个模型部署到多个端点。部署以受支持的容器为目标平台的预测模型。 根据端点是否已存在,选择以下两种方法之一:
将模型部署到新端点
请按照以下步骤将预测模型部署到新端点:
创建一个 YAML 文件,用于定义
DeployedModel
自定义资源:TensorFlow
以下 YAML 文件展示了 TensorFlow 模型的示例配置:
apiVersion: prediction.aiplatform.gdc.goog/v1 kind: DeployedModel metadata: name: DEPLOYED_MODEL_NAME namespace: PROJECT_NAMESPACE spec: # The endpoint path structure is endpoints/<endpoint-id> endpointPath: endpoints/PREDICTION_ENDPOINT modelSpec: # The artifactLocation field must be the s3 path to the folder that # contains the various model versions. # For example, s3://my-prediction-bucket/tensorflow artifactLocation: s3://PATH_TO_MODEL # The value in the id field must be unique to each model. id: img-detection-model modelDisplayName: my_img_detection_model # The model resource name structure is models/<model-id>/<model-version-id> modelResourceName: models/img-detection-model/1 # The model version ID must match the name of the first folder in # the artifactLocation bucket, inside the 'tensorflow' folder. # For example, if the bucket path is # s3://my-prediction-bucket/tensorflow/1/, # then the value for the model version ID is "1". modelVersionID: "1" modelContainerSpec: args: - --model_config_file=/models/models.config - --rest_api_port=8080 - --port=8500 - --file_system_poll_wait_seconds=30 - --model_config_file_poll_wait_seconds=30 command: - /bin/tensorflow_model_server # The image URI field must contain one of the following values: # For CPU-based models: gcr.io/aiml/prediction/containers/tf2-cpu.2-14:latest # For GPU-based models: gcr.io/aiml/prediction/containers/tf2-gpu.2-14:latest imageURI: gcr.io/aiml/prediction/containers/tf2-gpu.2-14:latest ports: - 8080 grpcPorts: - 8500 resourcePoolRef: kind: ResourcePool name: RESOURCE_POOL_NAME namespace: PROJECT_NAMESPACE
替换以下内容:
DEPLOYED_MODEL_NAME
:您要为DeployedModel
定义文件指定的名称。PROJECT_NAMESPACE
:与预测集群关联的项目命名空间的名称。PREDICTION_ENDPOINT
:您要为新端点提供的名称,例如my-img-prediction-endpoint
。PATH_TO_MODEL
:存储桶中模型的路径。RESOURCE_POOL_NAME
:创建资源池以托管模型时,您为ResourcePool
定义文件指定的名称。
根据您的预测模型修改其余字段中的值。
PyTorch
以下 YAML 文件展示了 PyTorch 模型的示例配置:
apiVersion: prediction.aiplatform.gdc.goog/v1 kind: DeployedModel metadata: name: DEPLOYED_MODEL_NAME namespace: PROJECT_NAMESPACE spec: endpointPath: PREDICTION_ENDPOINT endpointInfo: id: PREDICTION_ENDPOINT modelSpec: # The artifactLocation field must be the s3 path to the folder that # contains the various model versions. # For example, s3://my-prediction-bucket/pytorch artifactLocation: s3://PATH_TO_MODEL # The value in the id field must be unique to each model. id: "pytorch" modelDisplayName: my-pytorch-model # The model resource name structure is models/<model-id>/<model-version-id> modelResourceName: models/pytorch/1 modelVersionID: "1" modelContainerSpec: # The image URI field must contain one of the following values: # For CPU-based models: gcr.io/aiml/prediction/containers/pytorch-cpu.2-4:latest # For GPU-based models: gcr.io/aiml/prediction/containers/pytorch-gpu.2-4:latest imageURI: gcr.io/aiml/prediction/containers/pytorch-cpu.2-4:latest ports: - 8080 grpcPorts: - 7070 sharesResourcePool: false resourcePoolRef: kind: ResourcePool name: RESOURCE_POOL_NAME namespace: PROJECT_NAMESPACE
替换以下内容:
DEPLOYED_MODEL_NAME
:您要为DeployedModel
定义文件指定的名称。PROJECT_NAMESPACE
:与预测集群关联的项目命名空间的名称。PREDICTION_ENDPOINT
:您要为新端点提供的名称,例如my-img-prediction-endpoint
。PATH_TO_MODEL
:存储桶中模型的路径。RESOURCE_POOL_NAME
:创建资源池以托管模型时,您为ResourcePool
定义文件指定的名称。
根据您的预测模型修改其余字段中的值。
将
DeployedModel
定义文件应用于预测集群:kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG apply -f DEPLOYED_MODEL_NAME.yaml
替换以下内容:
PREDICTION_CLUSTER_KUBECONFIG
:预测集群中 kubeconfig 文件的路径。DEPLOYED_MODEL_NAME
:DeployedModel
定义文件的名称。
创建
DeployedModel
自定义资源时,Kubernetes API 和 webhook 服务会验证 YAML 文件,并报告成功或失败。 预测运算符会协调DeployedModel
自定义资源,并在预测集群中提供该资源。创建一个 YAML 文件,用于定义
Endpoint
自定义资源。以下 YAML 文件显示了一个示例配置:
apiVersion: aiplatform.gdc.goog/v1 kind: Endpoint metadata: name: ENDPOINT_NAME namespace: PROJECT_NAMESPACE spec: createDns: true id: PREDICTION_ENDPOINT destinations: - serviceRef: kind: DeployedModel name: DEPLOYED_MODEL_NAME namespace: PROJECT_NAMESPACE trafficPercentage: 50 grpcPort: 8501 httpPort: 8081 - serviceRef: kind: DeployedModel name: DEPLOYED_MODEL_NAME_2 namespace: PROJECT_NAMESPACE trafficPercentage: 50 grpcPort: 8501 httpPort: 8081
替换以下内容:
ENDPOINT_NAME
:您要为Endpoint
定义文件指定的名称。PROJECT_NAMESPACE
:与预测集群关联的项目命名空间的名称。PREDICTION_ENDPOINT
:新端点的名称。您在DeployedModel
定义文件中定义了此名称。DEPLOYED_MODEL_NAME
:您为DeployedModel
定义文件指定的名称。
您可以有一个或多个
serviceRef
目的地。如果您有第二个serviceRef
对象,请将其添加到 YAML 文件中的destinations
字段,并将DEPLOYED_MODEL_NAME_2
替换为您为创建的第二个DeployedModel
定义文件指定的名称。根据您部署的模型数量,根据需要继续添加或移除serviceRef
对象。根据您希望如何在此端点上的模型之间分配流量,设置
trafficPercentage
字段。根据您的端点配置修改其余字段中的值。将
Endpoint
定义文件应用于预测集群:kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG apply -f ENDPOINT_NAME.yaml
将
ENDPOINT_NAME
替换为Endpoint
定义文件的名称。
如需获取预测模型的端点网址路径,请运行以下命令:
kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG get endpoint PREDICTION_ENDPOINT -n PROJECT_NAMESPACE -o jsonpath='{.status.endpointFQDN}'
替换以下内容:
PREDICTION_CLUSTER_KUBECONFIG
:预测集群中 kubeconfig 文件的路径。PREDICTION_ENDPOINT
:新端点的名称。PROJECT_NAMESPACE
:预测项目命名空间的名称。
将模型部署到现有端点
只有在之前曾将其他模型部署到该端点(当时该端点是新端点)的情况下,您才能将模型部署到现有端点。系统需要执行上一步才能创建端点。
请按照以下步骤将预测模型部署到现有端点:
创建一个 YAML 文件,用于定义
DeployedModel
自定义资源。以下 YAML 文件显示了一个示例配置:
apiVersion: prediction.aiplatform.gdc.goog/v1 kind: DeployedModel metadata: name: DEPLOYED_MODEL_NAME namespace: PROJECT_NAMESPACE spec: # The endpoint path structure is endpoints/<endpoint-id> endpointPath: endpoints/PREDICTION_ENDPOINT modelSpec: # The artifactLocation field must be the s3 path to the folder that # contains the various model versions. # For example, s3://my-prediction-bucket/tensorflow artifactLocation: s3://PATH_TO_MODEL # The value in the id field must be unique to each model. id: img-detection-model-v2 modelDisplayName: my_img_detection_model # The model resource name structure is models/<model-id>/<model-version-id> modelResourceName: models/img-detection-model/2 # The model version ID must match the name of the first folder in # the artifactLocation bucket, # inside the 'tensorflow' folder. # For example, if the bucket path is # s3://my-prediction-bucket/tensorflow/2/, # then the value for the model version ID is "2". modelVersionID: "2" modelContainerSpec: args: - --model_config_file=/models/models.config - --rest_api_port=8080 - --port=8500 - --file_system_poll_wait_seconds=30 - --model_config_file_poll_wait_seconds=30 command: - /bin/tensorflow_model_server # The image URI field must contain one of the following values: # For CPU-based models: gcr.io/aiml/prediction/containers/tf2-cpu.2-6:latest # For GPU-based models: gcr.io/aiml/prediction/containers/tf2-gpu.2-6:latest imageURI: gcr.io/aiml/prediction/containers/tf2-gpu.2-6:latest ports: - 8080 grpcPorts: - 8500 resourcePoolRef: kind: ResourcePool name: RESOURCE_POOL_NAME namespace: PROJECT_NAMESPACE
替换以下内容:
DEPLOYED_MODEL_NAME
:您要为DeployedModel
定义文件指定的名称。PROJECT_NAMESPACE
:与预测集群关联的项目命名空间的名称。PREDICTION_ENDPOINT
:现有端点的名称,例如my-img-prediction-endpoint
。PATH_TO_MODEL
:存储桶中模型的路径。RESOURCE_POOL_NAME
:创建资源池以托管模型时,您为ResourcePool
定义文件指定的名称。
根据您的预测模型修改其余字段中的值。
将
DeployedModel
定义文件应用于预测集群:kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG apply -f DEPLOYED_MODEL_NAME.yaml
替换以下内容:
PREDICTION_CLUSTER_KUBECONFIG
:预测集群中 kubeconfig 文件的路径。DEPLOYED_MODEL_NAME
:DeployedModel
定义文件的名称。
创建
DeployedModel
自定义资源时,Kubernetes API 和 webhook 服务会验证 YAML 文件,并报告成功或失败。 预测运算符会协调DeployedModel
自定义资源,并在预测集群中提供该资源。显示现有
Endpoint
自定义资源的详细信息:kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG describe -f ENDPOINT_NAME.yaml
将
ENDPOINT_NAME
替换为Endpoint
定义文件的名称。通过在
destinations
字段中添加新的serviceRef
对象,更新Endpoint
自定义资源定义的 YAML 文件。在新对象中,根据您新创建的DeployedModel
自定义资源添加相应的服务名称。以下 YAML 文件显示了一个示例配置:
apiVersion: aiplatform.gdc.goog/v1 kind: Endpoint metadata: name: ENDPOINT_NAME namespace: PROJECT_NAMESPACE spec: createDns: true id: PREDICTION_ENDPOINT destinations: - serviceRef: kind: DeployedModel name: DEPLOYED_MODEL_NAME namespace: PROJECT_NAMESPACE trafficPercentage: 40 grpcPort: 8501 httpPort: 8081 - serviceRef: kind: DeployedModel name: DEPLOYED_MODEL_NAME_2 namespace: PROJECT_NAMESPACE trafficPercentage: 50 grpcPort: 8501 httpPort: 8081 - serviceRef: kind: DeployedModel name: DEPLOYED_MODEL_NAME_3 namespace: PROJECT_NAMESPACE trafficPercentage: 10 grpcPort: 8501 httpPort: 8081
替换以下内容:
ENDPOINT_NAME
:现有Endpoint
定义文件的名称。PROJECT_NAMESPACE
:与预测集群关联的项目命名空间的名称。PREDICTION_ENDPOINT
:现有端点的名称。您在DeployedModel
定义文件中引用了此名称。DEPLOYED_MODEL_NAME
:之前创建的DeployedModel
定义文件的名称。DEPLOYED_MODEL_NAME_2
:您为新创建的DeployedModel
定义文件指定的名称。
您可以有一个或多个
serviceRef
目的地。如果您有第三个serviceRef
对象,请将其添加到 YAML 文件中的destinations
字段,并将DEPLOYED_MODEL_NAME_3
替换为您为创建的第三个DeployedModel
定义文件指定的名称。根据您部署的模型数量,根据需要继续添加或移除serviceRef
对象。根据您希望如何在此端点的模型之间分配流量,设置
trafficPercentage
字段。根据您的端点配置修改其余字段中的值。将
Endpoint
定义文件应用于预测集群:kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG apply -f ENDPOINT_NAME.yaml
将
ENDPOINT_NAME
替换为Endpoint
定义文件的名称。
如需获取预测模型的端点网址路径,请运行以下命令:
kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG get endpoint PREDICTION_ENDPOINT -n PROJECT_NAMESPACE -o jsonpath='{.status.endpointFQDN}'
替换以下内容:
PREDICTION_CLUSTER_KUBECONFIG
:预测集群中 kubeconfig 文件的路径。PREDICTION_ENDPOINT
:端点的名称。PROJECT_NAMESPACE
:预测项目命名空间的名称。