本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
請參閱以下各節了解如何針對多模型端點使用自有容器及相依性。
在 CPU 支援的執行個體為多模型端點提供自有相依性
如預先建置的容器映像都無法滿足您的需求,您可建置自有容器來搭配 CPU 支援的多模型端點使用。
部署在 Amazon SageMaker AI 中的自訂 Amazon Elastic Container Registry (Amazon ECR) 映像,預期會遵守 中所述的基本合約使用託管服務的自訂推論程式碼,該合約會規範 SageMaker AI 如何與執行您自己的推論程式碼的 Docker 容器互動。若是能夠並行載入並為多個模型提供服務的容器,則會有必須遵從的額外 API 和行為。這份額外的合約包含了載入、列出、取得和取消載入模型的 API,以及另一個調用模型的 API。也有 API 必須遵守的不同錯誤情境行為。若要表示容器符合額外的要求,您可以將下列命令新增到 Docker 檔案:
LABEL com.amazonaws.sagemaker.capabilities.multi-models=true
SageMaker AI 也會將環境變數注入容器
SAGEMAKER_MULTI_MODEL=true
如果您要為序列推論管道建立多模型端點,則 Docker 檔案必須具有多模型和序列推論管道所需的標籤。如需序列資訊管道的詳細資訊,請參閱使用推論管道執行即時預測。
為協助您實作自訂容器的這些要求,提供下列兩個程式庫:
-
多模型伺服器
是一種可為機器學習模型提供服務的開放原始碼架構,可安裝於容器中以提供符合新多模型端點容器 API 要求的前端。它可提供多模型端點所需的 HTTP 前端和模型管理功能,以將多個模型託管於單一容器內、動態地將模型載入到容器中及從中取消載入模型,以及在指定的載入模型上執行推斷。它還提供了隨插即用的後端,支援隨插即用的自訂後端處理常式,可讓您實作自己的演算法。 -
SageMaker AI Inference Toolkit
是一個程式庫,可引導具有組態和設定的多模型伺服器,使其與 SageMaker AI 多模型端點相容。也可讓您根據不同的情境需求調校重要效能參數,例如每個模型的工作者數量。
在 GPU 支援的執行個體為多模型端點提供自有相依性
Multi Model Server 和 SageMaker AI Inference Toolkit 程式庫目前不支援在具有 GPU 支援執行個體的多模型端點上使用您自己的容器 (BYOC) 功能。
若要使用 GPU 支援的執行個體建立多模型端點,您可以使用 SageMaker AI 支援的 NVIDIA Triton 推論伺服器。 搭配 NVIDIA Triton 推論容器
FROM 301217895009.dkr.ecr.us-west-2.amazonaws.com/sagemaker-tritonserver:22.07-py3
重要
若要用於 GPU 支援的多模型端點,具 Triton 推論伺服器的容器是唯一支援的容器。
使用 SageMaker AI 推論工具組
注意
SageMaker AI 推論工具組僅支援 CPU 支援的多模型端點。SageMaker AI 推論工具組目前不支援 GPU 支援的多模型端點。
多模型端點支援的演算法、架構和執行個體 列出支援多模型端點的預先建置容器。如果你想要使用其他任何架構或演算法,則需要建置容器。最簡單的方法是使用 SageMaker AI 推論工具組
注意
SageMaker AI 推論工具組僅支援 Python 模型處理常式。如果您想要以其他任何語言來實作處理常式,則必須建置您自己的容器,以實作其他多模型端點 API。如需相關資訊,請參閱 多模型端點的自訂容器合約。
使用 SageMaker AI 推論工具組擴展容器
-
建立模型處理常式。MMS 需要模型處理常式,這是一個 Python 檔案,其中實作函式來預處理、從模型取得預測,以及在模型處理常式中處理輸出。如需模型處理常式的範例,請參閱範例筆記本中的 model_handler.py
。 -
匯入推論工具組,並使用其
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()
-
在
Dockerfile
中,複製第一個步驟中的模型處理常式,並將上一個步驟中的 Python 檔案指定為Dockerfile
中的進入點。下列幾行來自範例筆記本中使用的 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"]
-
建置並註冊容器。範例筆記本中有下列殼層指令碼,可建置容器,並上傳到您 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 中部署多模型端點。