

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

# 為 SageMaker AI 多模型端點建置自有容器
<a name="build-multi-model-build-container"></a>

請參閱以下各節了解如何針對多模型端點使用自有容器及相依性。

**Topics**
+ [在 CPU 支援的執行個體為多模型端點提供自有相依性](#build-multi-model-container-cpu)
+ [在 GPU 支援的執行個體為多模型端點提供自有相依性](#build-multi-model-container-gpu)
+ [運用 SageMaker AI 推論工具組](#multi-model-inference-toolkit)
+ [多模型端點的自訂容器合約](mms-container-apis.md)

## 在 CPU 支援的執行個體為多模型端點提供自有相依性
<a name="build-multi-model-container-cpu"></a>

如預先建置的容器映像都無法滿足您的需求，您可建置自有容器來搭配 CPU 支援的多模型端點使用。

Amazon SageMaker AI 部署的自訂 Amazon Elastic Container Registry (Amazon ECR) 映像應遵守[具託管服務的自訂推論程式碼](your-algorithms-inference-code.md)所述的基本合約，該合約會規範 SageMaker AI 如何與執行您自有推斷程式碼的 Docker 容器進行互動。若是能夠並行載入並為多個模型提供服務的容器，則會有必須遵從的額外 API 和行為。這份額外的合約包含了載入、列出、取得和取消載入模型的 API，以及另一個調用模型的 API。也有 API 必須遵守的不同錯誤情境行為。若要表示容器符合額外的要求，您可以將下列命令新增到 Docker 檔案：

```
LABEL com.amazonaws.sagemaker.capabilities.multi-models=true
```

SageMaker AI 也會將環境變數插入到容器中

```
SAGEMAKER_MULTI_MODEL=true
```

如果您要為序列推論管道建立多模型端點，則 Docker 檔案必須具有多模型和序列推論管道所需的標籤。如需序列資訊管道的詳細資訊，請參閱[使用推論管道執行即時預測](inference-pipeline-real-time.md)。

為協助您實作自訂容器的這些要求，提供下列兩個程式庫：
+ [多模型伺服器](https://github.com/awslabs/multi-model-server)是一種可為機器學習模型提供服務的開放原始碼架構，可安裝於容器中以提供符合新多模型端點容器 API 要求的前端。它可提供多模型端點所需的 HTTP 前端和模型管理功能，以將多個模型託管於單一容器內、動態地將模型載入到容器中及從中取消載入模型，以及在指定的載入模型上執行推斷。它還提供了隨插即用的後端，支援隨插即用的自訂後端處理常式，可讓您實作自己的演算法。
+ [SageMaker AI 推論工具組](https://github.com/aws/sagemaker-inference-toolkit)是使用組態和設定來引導多模型伺服器的程式庫，以使其與 SageMaker AI 多模型端點相容。也可讓您根據不同的情境需求調校重要效能參數，例如每個模型的工作者數量。

## 在 GPU 支援的執行個體為多模型端點提供自有相依性
<a name="build-multi-model-container-gpu"></a>

針對採用由 GPU 支援執行個體的多模型端點，多模型伺服器與 SageMaker AI 推論工具組程式庫目前不支援其運用自有容器 (BYOC) 功能。

若要使用由 GPU 支援的執行個體建立多模型端點，您可利用 SageMaker AI 支援的 [NVIDIA Triton 推論伺服器](https://docs.aws.amazon.com/sagemaker/latest/dg/triton.html)，並搭配 [NVIDIA Triton 推論容器](https://github.com/aws/deep-learning-containers/blob/master/available_images.md#nvidia-triton-inference-containers-sm-support-only)。若要採用自有相依性，您可採用 SageMaker AI 支援的 [NVIDIA Triton 推論伺服器](https://docs.aws.amazon.com/sagemaker/latest/dg/triton.html)來建置自有容器，做為 Docker 檔案的基礎映像：

```
FROM 301217895009.dkr.ecr.us-west-2.amazonaws.com/sagemaker-tritonserver:22.07-py3
```

**重要**  
若要用於 GPU 支援的多模型端點，具 Triton 推論伺服器的容器是唯一支援的容器。

## 運用 SageMaker AI 推論工具組
<a name="multi-model-inference-toolkit"></a>

**注意**  
SageMaker AI 推論工具組僅支援 CPU 支援的多模型端點。SageMaker AI 推論工具組目前不支援 GPU 支援的多模型端點。

[支援的多模型端點適用演算法、架構與執行個體](multi-model-support.md) 列出支援多模型端點的預先建置容器。如果你想要使用其他任何架構或演算法，則需要建置容器。最簡單的做法是運用 [SageMaker AI 推論工具組](https://github.com/aws/sagemaker-inference-toolkit)來擴展現有預先建置的容器。SageMaker AI 推論工具組是多模型伺服器 (MMS) 的實作，可讓您建立可在 SageMaker AI 部署的端點。如需示範如何設定及部署可在 SageMaker AI 支援多模型端點之自訂容器的範例筆記本，請參閱[多模型端點 BYOC 範例筆記本](https://github.com/awslabs/amazon-sagemaker-examples/tree/master/advanced_functionality/multi_model_bring_your_own)。

**注意**  
SageMaker AI 推論工具組僅支援 Python 模型處理常式。如果您想要以其他任何語言來實作處理常式，則必須建置您自己的容器，以實作其他多模型端點 API。如需相關資訊，請參閱[多模型端點的自訂容器合約](mms-container-apis.md)。

**使用 SageMaker AI 推論工具組來擴展容器**

1. 建立模型處理常式。MMS 需要模型處理常式，這是一個 Python 檔案，其中實作函式來預處理、從模型取得預測，以及在模型處理常式中處理輸出。如需模型處理常式的範例，請參閱範例筆記本中的 [model\_handler.py](https://github.com/awslabs/amazon-sagemaker-examples/blob/master/advanced_functionality/multi_model_bring_your_own/container/model_handler.py)。

1. 匯入推論工具組，並使用其 `model_server.start_model_server` 函式來啟動 MMS。下列範例來自範例筆記本中的 `dockerd-entrypoint.py` 檔案。請注意，呼叫 `model_server.start_model_server` 會傳遞上一個步驟中描述的模型處理常式：

   ```
   import subprocess
   import sys
   import shlex
   import os
   from retrying import retry
   from subprocess import CalledProcessError
   from sagemaker_inference import model_server
   
   def _retry_if_error(exception):
       return isinstance(exception, CalledProcessError or OSError)
   
   @retry(stop_max_delay=1000 * 50,
          retry_on_exception=_retry_if_error)
   def _start_mms():
       # by default the number of workers per model is 1, but we can configure it through the
       # environment variable below if desired.
       # os.environ['SAGEMAKER_MODEL_SERVER_WORKERS'] = '2'
       model_server.start_model_server(handler_service='/home/model-server/model_handler.py:handle')
   
   def main():
       if sys.argv[1] == 'serve':
           _start_mms()
       else:
           subprocess.check_call(shlex.split(' '.join(sys.argv[1:])))
   
       # prevent docker exit
       subprocess.call(['tail', '-f', '/dev/null'])
       
   main()
   ```

1. 在 `Dockerfile` 中，複製第一個步驟中的模型處理常式，並將上一個步驟中的 Python 檔案指定為 `Dockerfile` 中的進入點。下列幾行來自範例筆記本中使用的 [Dockerfile](https://github.com/awslabs/amazon-sagemaker-examples/blob/master/advanced_functionality/multi_model_bring_your_own/container/Dockerfile)：

   ```
   # Copy the default custom service file to handle incoming data and inference requests
   COPY model_handler.py /home/model-server/model_handler.py
   
   # Define an entrypoint script for the docker image
   ENTRYPOINT ["python", "/usr/local/bin/dockerd-entrypoint.py"]
   ```

1. 建置並註冊容器。範例筆記本中有下列殼層指令碼，可建置容器，並上傳到您 AWS 帳戶的 Amazon Elastic Container Registry 儲存庫：

   ```
   %%sh
   
   # The name of our algorithm
   algorithm_name=demo-sagemaker-multimodel
   
   cd container
   
   account=$(aws sts get-caller-identity --query Account --output text)
   
   # Get the region defined in the current configuration (default to us-west-2 if none defined)
   region=$(aws configure get region)
   region=${region:-us-west-2}
   
   fullname="${account}.dkr.ecr.${region}.amazonaws.com/${algorithm_name}:latest"
   
   # If the repository doesn't exist in ECR, create it.
   aws ecr describe-repositories --repository-names "${algorithm_name}" > /dev/null 2>&1
   
   if [ $? -ne 0 ]
   then
       aws ecr create-repository --repository-name "${algorithm_name}" > /dev/null
   fi
   
   # Get the login command from ECR and execute it directly
   $(aws ecr get-login --region ${region} --no-include-email)
   
   # Build the docker image locally with the image name and then push it to ECR
   # with the full name.
   
   docker build -q -t ${algorithm_name} .
   docker tag ${algorithm_name} ${fullname}
   
   docker push ${fullname}
   ```

現在，您可以使用此容器在 SageMaker AI 部署多模型端點。

**Topics**
+ [在 CPU 支援的執行個體為多模型端點提供自有相依性](#build-multi-model-container-cpu)
+ [在 GPU 支援的執行個體為多模型端點提供自有相依性](#build-multi-model-container-gpu)
+ [運用 SageMaker AI 推論工具組](#multi-model-inference-toolkit)
+ [多模型端點的自訂容器合約](mms-container-apis.md)