您必須先將模型部署至端點,才能使用模型進行線上預測。部署模型時,系統會將實體資源與模型建立關聯,讓模型以低延遲的方式提供線上預測結果。
本頁說明如何使用線上預測,將模型部署至端點。
事前準備
將模型部署至端點前,請先匯出用於預測工作的模型構件,並確保符合該頁面的所有必要條件。
建立資源集區
ResourcePool自訂資源可讓您精細控管模型的行為。您可以定義下列設定:
- 自動調度資源設定。
- 機器類型,定義 CPU 和記憶體需求。
- 加速器選項,例如 GPU 資源。
您傳送至建立預測叢集的節點集區規格要求,必須包含機器類型。
對於已部署模型的資源集區,加速器數量和類型會決定 GPU 用量。機器類型只會決定要求的 CPU 和記憶體資源。因此,在 ResourcePool 規格中加入 GPU 加速器時,machineType 欄位會控管模型的 CPU 和記憶體需求,而 acceleratorType 欄位則會控管 GPU。此外,acceleratorCount 欄位會控管 GPU 區塊數量。
請按照下列步驟建立ResourcePool自訂資源:
建立定義
ResourcePool自訂資源的 YAML 檔案。下列範例包含資源集區的 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 檔案,並回報成功或失敗。當您將模型部署至端點時,預測運算子會從資源集區佈建及保留資源。
將模型部署至端點
如果您有資源集區,可以將多個模型部署至端點,也可以將模型部署至多個端點。部署以支援的容器為目標的預測模型。視端點是否已存在,選擇下列兩種方法之一:
將模型部署至新端點
請按照下列步驟將預測模型部署至新端點:
建立定義
DeployedModel自訂資源的 YAML 檔案: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-fqn/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-fqn/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-18:latest # For GPU-based models: gcr.io/aiml/prediction/containers/tf2-gpu.2-18:latest imageURI: gcr.io/aiml/prediction/containers/tf2-gpu.2-18: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 prefix that # includes the model artifacts for the model version. # For example, s3://my-prediction-bucket-fqn/pytorch-model/1 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-1:latest # For GPU-based models: gcr.io/aiml/prediction/containers/pytorch-gpu.2-1:latest imageURI: gcr.io/aiml/prediction/containers/pytorch-cpu.2-1: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自訂資源,並在預測叢集中提供該資源。建立定義
Endpoint自訂資源的 YAML 檔案。以下 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物件,請將其新增至destinations欄位中的 YAML 檔案,並將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:預測專案命名空間的名稱。
將模型部署至現有端點
只有在端點剛建立時,您曾將其他模型部署至該端點,才能將模型部署至現有端點。系統需要完成上一個步驟,才能建立端點。
如要將預測模型部署至現有端點,請按照下列步驟操作:
建立定義
DeployedModel自訂資源的 YAML 檔案。以下 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-fqn/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-fqn/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物件,藉此更新自訂資源定義的 YAML 檔案。Endpoint在新物件中,根據新建立的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物件,請將其新增至destinations欄位的 YAML 檔案,並將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:預測專案命名空間的名稱。