

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立多模型端點
<a name="create-multi-model-endpoint"></a>

您可以使用 SageMaker AI 主控台或 適用於 Python (Boto) 的 AWS SDK 來建立多模型端點。若要透過主控台建立 CPU 或 GPU 支援的端點，請參閱以下各節的主控台程序。如果您想要使用 建立多模型端點 適用於 Python (Boto) 的 AWS SDK，請使用下列各節中的 CPU 或 GPU 程序。CPU 與 GPU 工作流程類似，但有幾項差異，例如容器需求。

**Topics**
+ [建立多模型端點 (主控台)](#create-multi-model-endpoint-console)
+ [使用 CPUs搭配 建立多模型端點 適用於 Python (Boto3) 的 AWS SDK](#create-multi-model-endpoint-sdk-cpu)
+ [使用 GPUs 搭配 建立多模型端點 適用於 Python (Boto3) 的 AWS SDK](#create-multi-model-endpoint-sdk-gpu)

## 建立多模型端點 (主控台)
<a name="create-multi-model-endpoint-console"></a>

您可利用主控台建立 CPU 與 GPU 支援的多模型端點。利用下列程序，透過 SageMaker AI 主控台建立多模型端點。

**建立多模型端點 (主控台)**

1. 開啟 Amazon SageMaker AI 主控台，網址為 [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/)。

1. 選擇 **Model** (模型)，然後從 **Inference** (推斷) 群組中選擇 **Create model** (建立模型)。

1. 在 **Model name** (模型名稱) 中，輸入名稱。

1. 針對 **IAM role**，選擇或建立已連接 `AmazonSageMakerFullAccess` IAM 政策的 IAM 角色。

1.  針對 **Container definition** (容器定義) 區段的 **Provide model artifacts and inference image options** (提供模型成品與推論映像選項)，選擇 **Use multiple models** (使用多個模型)。  
![\[[建立模型] 頁面區段，在此您可選擇使用多個模型。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/mme-create-model-ux-2.PNG)

1. 針對 **Inference container image** (推論容器映像)，輸入所需容器映像的 Amazon ECR 路徑。

   針對 GPU 模型，您必須使用 NVIDIA Triton 推論伺服器支援的容器。如需可搭配 GPU 支援端點使用的容器映像清單，請參閱 [NVIDIA Triton 推論容器 (僅限 SM 支援)](https://github.com/aws/deep-learning-containers/blob/master/available_images.md#nvidia-triton-inference-containers-sm-support-only)。如需 NVIDIA Triton 推論伺服器的詳細資訊，請參閱[搭配 SageMaker AI 使用 Triton 推論伺服器](https://docs.aws.amazon.com/sagemaker/latest/dg/triton.html)。

1. 選擇**建立模型**。

1. 部署多模型端點，做法就像部署單一模型端點一樣。如需說明，請參閱[將模型部署至 SageMaker AI 託管服務](ex1-model-deployment.md#ex1-deploy-model)。

## 使用 CPUs搭配 建立多模型端點 適用於 Python (Boto3) 的 AWS SDK
<a name="create-multi-model-endpoint-sdk-cpu"></a>

請參閱下一節來建立由 CPU 執行個體支援的多模型端點。您可利用 Amazon SageMaker AI [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_model](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_model)、[https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_endpoint_config](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_endpoint_config) 與 [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_endpoint](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_endpoint) API 來建立多模型端點，就像建立單一模型端點一樣，但有兩個變更。當定模型義容器時，您必須傳遞新的 `Mode` 參數值 `MultiModel`。您也需要傳遞指定模型成品所在之 Amazon S3 的字首 `ModelDataUrl` 欄位，而不是單一模型成品的路徑，就像部署單一模型時的做法一樣。

如需使用 SageMaker AI 部署多個 XGBoost 模型至單一端點的範例筆記本，請參閱[多模型端點 XGBoost 範例筆記本](https://sagemaker-examples.readthedocs.io/en/latest/advanced_functionality/multi_model_xgboost_home_value/xgboost_multi_model_endpoint_home_value.html)。

下列程序概述該範例採用的重要步驟，以便建立 CPU 支援的多模型端點。

**部署模型 (AWS SDK for Python (Boto 3))**

1. 取得容器，其需包含支援部署多模型端點的映像。如需支援多模型端點的內建演算法與架構容器清單，請參閱[支援的多模型端點適用演算法、架構與執行個體](multi-model-support.md)。我們在此範例採用 [K 近鄰 (k-NN) 演算法](k-nearest-neighbors.md) 內建演算法。我們呼叫 [SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable/v2.html) 公用程式函式 `image_uris.retrieve()` 來取得 k 近鄰內建演算法映像位址。

   ```
   import sagemaker
   region = sagemaker_session.boto_region_name
   image = sagemaker.image_uris.retrieve("knn",region=region)
   container = { 
                 'Image':        image,
                 'ModelDataUrl': 's3://<BUCKET_NAME>/<PATH_TO_ARTIFACTS>',
                 'Mode':         'MultiModel'
               }
   ```

1. 取得 an 適用於 Python (Boto3) 的 AWS SDK SageMaker AI 用戶端並建立使用此容器的模型。

   ```
   import boto3
   sagemaker_client = boto3.client('sagemaker')
   response = sagemaker_client.create_model(
                 ModelName        = '<MODEL_NAME>',
                 ExecutionRoleArn = role,
                 Containers       = [container])
   ```

1. (選用) 如果您使用的是序列推論管道，請在管道納入額外容器，並將其包含在 `CreateModel` 的 `Containers` 引數中：

   ```
   preprocessor_container = { 
                  'Image': '<ACCOUNT_ID>.dkr.ecr.<REGION_NAME>.amazonaws.com/<PREPROCESSOR_IMAGE>:<TAG>'
               }
   
   multi_model_container = { 
                 'Image': '<ACCOUNT_ID>.dkr.ecr.<REGION_NAME>.amazonaws.com/<IMAGE>:<TAG>',
                 'ModelDataUrl': 's3://<BUCKET_NAME>/<PATH_TO_ARTIFACTS>',
                 'Mode':         'MultiModel'
               }
   
   response = sagemaker_client.create_model(
                 ModelName        = '<MODEL_NAME>',
                 ExecutionRoleArn = role,
                 Containers       = [preprocessor_container, multi_model_container]
               )
   ```
**注意**  
您只能在序列推論管道中使用一個啟用多模型的端點。

1. (選用) 如您的使用案例無法從模型快取受益，請將 `MultiModelConfig` 參數 `ModelCacheSetting` 欄位的值設為 `Disabled`，並將其包含在呼叫 `create_model` 的 `Container` 引數。`ModelCacheSetting` 欄位的預設值為 `Enabled`。

   ```
   container = { 
                   'Image': image, 
                   'ModelDataUrl': 's3://<BUCKET_NAME>/<PATH_TO_ARTIFACTS>',
                   'Mode': 'MultiModel' 
                   'MultiModelConfig': {
                           // Default value is 'Enabled'
                           'ModelCacheSetting': 'Disabled'
                   }
              }
   
   response = sagemaker_client.create_model(
                 ModelName        = '<MODEL_NAME>',
                 ExecutionRoleArn = role,
                 Containers       = [container]
               )
   ```

1. 為該模型設定多模型端點。建議您至少使用兩個執行個體來設定端點。這可讓 SageMaker AI 在模型的多個可用區域中提供一組高可用性的預測結果。

   ```
   response = sagemaker_client.create_endpoint_config(
                   EndpointConfigName = '<ENDPOINT_CONFIG_NAME>',
                   ProductionVariants=[
                        {
                           'InstanceType':        'ml.m4.xlarge',
                           'InitialInstanceCount': 2,
                           'InitialVariantWeight': 1,
                           'ModelName':            '<MODEL_NAME>',
                           'VariantName':          'AllTraffic'
                         }
                   ]
              )
   ```
**注意**  
您只能在序列推論管道中使用一個啟用多模型的端點。

1. 使用 `EndpointName` 和 `EndpointConfigName` 參數建立多模型端點。

   ```
   response = sagemaker_client.create_endpoint(
                 EndpointName       = '<ENDPOINT_NAME>',
                 EndpointConfigName = '<ENDPOINT_CONFIG_NAME>')
   ```

## 使用 GPUs 搭配 建立多模型端點 適用於 Python (Boto3) 的 AWS SDK
<a name="create-multi-model-endpoint-sdk-gpu"></a>

請參閱下列區段建立 GPU 支援的多模型端點。您可利用 Amazon SageMaker AI [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_model](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_model)、[https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_endpoint_config](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_endpoint_config) 與 [https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_endpoint](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_endpoint) API 來建立多模型端點 ，這類似建立單一模型端點，但有幾項變更。當定模型義容器時，您必須傳遞新的 `Mode` 參數值 `MultiModel`。您也需要傳遞指定模型成品所在之 Amazon S3 的字首 `ModelDataUrl` 欄位，而不是單一模型成品的路徑，就像部署單一模型時的做法一樣。對於 GPU 支援的多模型端點，您也必須使用搭配 NVIDIA Triton 推論伺服器的容器，該伺服器已針對 GPU 執行個體進行最佳化。如需可搭配 GPU 支援端點使用的容器映像清單，請參閱 [NVIDIA Triton 推論容器 (僅限 SM 支援)](https://github.com/aws/deep-learning-containers/blob/master/available_images.md#nvidia-triton-inference-containers-sm-support-only)。

如需範例筆記本示範如何建立由 GPU 支援的多模型端點，請參閱[運用 Amazon SageMaker AI 多模型端點 (MME) 在 GPU 執行多個深度學習模型](https://github.com/aws/amazon-sagemaker-examples/blob/main/multi-model-endpoints/mme-on-gpu/cv/resnet50_mme_with_gpu.ipynb)。

下列程序針對建立由 GPU 支援的多模型端點概述重要步驟。

**部署模型 (AWS SDK for Python (Boto 3))**

1. 定義容器映像。若要建立具有 GPU 支援 ResNet 模型的多模型端點，請定義要使用 [NVIDIA Triton 伺服器映像](https://docs.aws.amazon.com/sagemaker/latest/dg/triton.html)的容器。此容器支援多模型端點，並已最佳化以便用於 GPU 執行個體。我們呼叫 [SageMaker AI Python SDK](https://sagemaker.readthedocs.io/en/stable/v2.html) 公用程式函式 `image_uris.retrieve()` 來取得映像位址。例如：

   ```
   import sagemaker
   region = sagemaker_session.boto_region_name
   
   // Find the sagemaker-tritonserver image at 
   // https://github.com/aws/amazon-sagemaker-examples/blob/main/sagemaker-triton/resnet50/triton_resnet50.ipynb
   // Find available tags at https://github.com/aws/deep-learning-containers/blob/master/available_images.md#nvidia-triton-inference-containers-sm-support-only
   
   image = "<ACCOUNT_ID>.dkr.ecr.<REGION_NAME>.amazonaws.com/sagemaker-tritonserver:<TAG>".format(
       account_id=account_id_map[region], region=region
   )
   
   container = { 
                 'Image':        image,
                 'ModelDataUrl': 's3://<BUCKET_NAME>/<PATH_TO_ARTIFACTS>',
                 'Mode':         'MultiModel',
                 "Environment": {"SAGEMAKER_TRITON_DEFAULT_MODEL_NAME": "resnet"},
               }
   ```

1. 取得 an 適用於 Python (Boto3) 的 AWS SDK SageMaker AI 用戶端並建立使用此容器的模型。

   ```
   import boto3
   sagemaker_client = boto3.client('sagemaker')
   response = sagemaker_client.create_model(
                 ModelName        = '<MODEL_NAME>',
                 ExecutionRoleArn = role,
                 Containers       = [container])
   ```

1. (選用) 如果您使用的是序列推論管道，請在管道納入額外容器，並將其包含在 `CreateModel` 的 `Containers` 引數中：

   ```
   preprocessor_container = { 
                  'Image': '<ACCOUNT_ID>.dkr.ecr.<REGION_NAME>.amazonaws.com/<PREPROCESSOR_IMAGE>:<TAG>'
               }
   
   multi_model_container = { 
                 'Image': '<ACCOUNT_ID>.dkr.ecr.<REGION_NAME>.amazonaws.com/<IMAGE>:<TAG>',
                 'ModelDataUrl': 's3://<BUCKET_NAME>/<PATH_TO_ARTIFACTS>',
                 'Mode':         'MultiModel'
               }
   
   response = sagemaker_client.create_model(
                 ModelName        = '<MODEL_NAME>',
                 ExecutionRoleArn = role,
                 Containers       = [preprocessor_container, multi_model_container]
               )
   ```
**注意**  
您只能在序列推論管道中使用一個啟用多模型的端點。

1. (選用) 如您的使用案例無法從模型快取受益，請將 `MultiModelConfig` 參數 `ModelCacheSetting` 欄位的值設為 `Disabled`，並將其包含在呼叫 `create_model` 的 `Container` 引數。`ModelCacheSetting` 欄位的預設值為 `Enabled`。

   ```
   container = { 
                   'Image': image, 
                   'ModelDataUrl': 's3://<BUCKET_NAME>/<PATH_TO_ARTIFACTS>',
                   'Mode': 'MultiModel' 
                   'MultiModelConfig': {
                           // Default value is 'Enabled'
                           'ModelCacheSetting': 'Disabled'
                   }
              }
   
   response = sagemaker_client.create_model(
                 ModelName        = '<MODEL_NAME>',
                 ExecutionRoleArn = role,
                 Containers       = [container]
               )
   ```

1. 為模型設定具 GPU 支援執行個體的多模型端點。建議您將端點設為多個執行個體，以便提供高可用性與更高快取命中。

   ```
   response = sagemaker_client.create_endpoint_config(
                   EndpointConfigName = '<ENDPOINT_CONFIG_NAME>',
                   ProductionVariants=[
                        {
                           'InstanceType':        'ml.g4dn.4xlarge',
                           'InitialInstanceCount': 2,
                           'InitialVariantWeight': 1,
                           'ModelName':            '<MODEL_NAME>',
                           'VariantName':          'AllTraffic'
                         }
                   ]
              )
   ```

1. 使用 `EndpointName` 和 `EndpointConfigName` 參數建立多模型端點。

   ```
   response = sagemaker_client.create_endpoint(
                 EndpointName       = '<ENDPOINT_NAME>',
                 EndpointConfigName = '<ENDPOINT_CONFIG_NAME>')
   ```