將模型部署至端點

您必須先將模型部署至端點,才能使用模型進行線上預測。部署模型時,系統會將實體資源與模型建立關聯,讓模型以低延遲的方式提供線上預測結果。

本頁說明如何使用線上預測,將模型部署至端點。

事前準備

將模型部署至端點前,請先匯出用於預測工作的模型構件,並確保符合該頁面的所有必要條件。

建立資源集區

ResourcePool自訂資源可讓您精細控管模型的行為。您可以定義下列設定:

  • 自動調度資源設定。
  • 機器類型,定義 CPU 和記憶體需求。
  • 加速器選項,例如 GPU 資源。

您傳送至建立預測叢集的節點集區規格要求,必須包含機器類型。

對於已部署模型的資源集區,加速器數量和類型會決定 GPU 用量。機器類型只會決定要求的 CPU 和記憶體資源。因此,在 ResourcePool 規格中加入 GPU 加速器時,machineType 欄位會控管模型的 CPU 和記憶體需求,而 acceleratorType 欄位則會控管 GPU。此外,acceleratorCount 欄位會控管 GPU 區塊數量。

請按照下列步驟建立ResourcePool自訂資源:

  1. 建立定義 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 欄位的值。

  2. ResourcePool 定義檔案套用至預測叢集:

    kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG apply -f RESOURCE_POOL_NAME.yaml
    

    更改下列內容:

    • PREDICTION_CLUSTER_KUBECONFIG:預測叢集中的 kubeconfig 檔案路徑。
    • RESOURCE_POOL_NAMEResourcePool 定義檔案的名稱。

建立 ResourcePool 自訂資源時,Kubernetes API 和 Webhook 服務會驗證 YAML 檔案,並回報成功或失敗。當您將模型部署至端點時,預測運算子會從資源集區佈建及保留資源。

將模型部署至端點

如果您有資源集區,可以將多個模型部署至端點,也可以將模型部署至多個端點。部署以支援的容器為目標的預測模型。視端點是否已存在,選擇下列其中一種方法:

將模型部署至新端點

請按照下列步驟將預測模型部署至新端點:

  1. 建立定義 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/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 定義檔案指定的名稱。

    根據預測模型修改其餘欄位的值。

  2. DeployedModel 定義檔案套用至預測叢集:

    kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG apply -f DEPLOYED_MODEL_NAME.yaml
    

    更改下列內容:

    • PREDICTION_CLUSTER_KUBECONFIG:預測叢集中的 kubeconfig 檔案路徑。
    • DEPLOYED_MODEL_NAMEDeployedModel 定義檔案的名稱。

    建立 DeployedModel 自訂資源時,Kubernetes API 和 Webhook 服務會驗證 YAML 檔案,並回報成功或失敗。預測運算子會調解 DeployedModel 自訂資源,並在預測叢集中提供該資源。

  3. 建立定義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 欄位。根據端點設定,修改其餘欄位的值。

  4. 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:預測專案命名空間的名稱。

將模型部署至現有端點

只有在端點剛建立時,您曾將其他模型部署至該端點,才能將模型部署至現有端點。系統需要完成上一個步驟,才能建立端點。

如要將預測模型部署至現有端點,請按照下列步驟操作:

  1. 建立定義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/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 定義檔案指定的名稱。

    根據預測模型修改其餘欄位的值。

  2. DeployedModel 定義檔案套用至預測叢集:

    kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG apply -f DEPLOYED_MODEL_NAME.yaml
    

    更改下列內容:

    • PREDICTION_CLUSTER_KUBECONFIG:預測叢集中的 kubeconfig 檔案路徑。
    • DEPLOYED_MODEL_NAMEDeployedModel 定義檔案的名稱。

    建立 DeployedModel 自訂資源時,Kubernetes API 和 Webhook 服務會驗證 YAML 檔案,並回報成功或失敗。預測運算子會調解 DeployedModel 自訂資源,並在預測叢集中提供該資源。

  3. 顯示現有 Endpoint 自訂資源的詳細資料:

    kubectl --kubeconfig PREDICTION_CLUSTER_KUBECONFIG describe -f ENDPOINT_NAME.yaml
    

    請將 ENDPOINT_NAME 替換為 Endpoint 定義檔的名稱。

  4. 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 物件,請將其新增至 destinations 欄位的 YAML 檔案,並將 DEPLOYED_MODEL_NAME_3 替換為您為建立的第三個 DeployedModel 定義檔案指定的名稱。視您部署的模型數量而定,視需要新增或移除 serviceRef 物件。

    根據您要在這個端點的模型之間分配流量的方式,設定 trafficPercentage 欄位。根據端點設定,修改其餘欄位的值。

  5. 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:預測專案命名空間的名稱。