本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 TorchServe 部署模型
TorchServe 是 PyTorch 推薦的模型伺服器,已預先安裝在 AWS PyTorch Deep Learning Container (DLC)。此功能強大的工具為客戶提供一致且易於使用的體驗,無論模型大小或分佈為何,都能在各種 AWS 執行個體之間部署多個 PyTorch 模型時提供高效能,包括 CPU、GPU、Neuron 和 Graviton。
TorchServe 支援多種進階功能,包括動態批處理、微批處理、模型 A/B 測試、串流傳輸、Torch XLA、TensorRT、ONNX 及 IPEX。此外,它還流暢整合了 PyTorch 的大型模型解決方案 PiPPy,能夠有效處理大型模型。此外,TorchServe 還擴展了對 DeepSpeed、Accelerate、Fast Transformers 等熱門開放原始碼程式庫的支援,進一步擴展了其功能。使用 TorchServe, AWS 使用者可以放心地部署和提供 PyTorch 模型,利用其在各種硬體組態和模型類型中的多樣性和最佳化效能。有關詳細資訊,請參閱 PyTorch 文件
下表列出 AWS TorchServe 支援的 PyTorch TorchServe DLCs。
執行個體類型 | SageMaker AI PyTorch DLC 連結 |
---|---|
CPU 和 GPU |
|
Neuron |
|
Graviton |
下列各節說明在 Amazon SageMaker AI 上建置和測試 PyTorch DLCs 的設定。
開始使用
首先,請務必確認您已進行下列事前準備:
-
確保您可存取 AWS 帳戶。設定您的環境,讓 AWS CLI 可以透過 IAM AWS 使用者或 IAM 角色存取您的帳戶。我們建議使用 IAM 角色。為了在個人帳戶進行測試,您可以將以下受管權限政策附加到 IAM 角色:
-
在本機設定相依性,如以下範例所示:
from datetime import datetime import os import json import logging import time # External Dependencies: import boto3 from botocore.exceptions import ClientError import sagemaker sess = boto3.Session() sm = sess.client("sagemaker") region = sess.region_name account = boto3.client("sts").get_caller_identity().get("Account") smsess = sagemaker.Session(boto_session=sess) role = sagemaker.get_execution_role() # Configuration: bucket_name = smsess.default_bucket() prefix = "torchserve" output_path = f"s3://{bucket_name}/{prefix}/models" print(f"account={account}, region={region}, role={role}")
-
擷取 PyTorch DLC 映像檔,如下列範例所示。
SageMaker AI PyTorch DLC 映像適用於所有 AWS 區域。如需詳細資訊,請參閱 DLC 容器映像檔清單
。 baseimage = sagemaker.image_uris.retrieve( framework="pytorch", region="
<region>
", py_version="py310", image_scope="inference", version="2.0.1", instance_type="ml.g4dn.16xlarge", ) -
建立本機工作區。
mkdir -p workspace/
新增套件
下列各節描述如何為 PyTorch DLC 映像檔新增及預先安裝套件。
BYOC 使用案例
下列步驟概述如何將套件新增至 PyTorch DLC 映像檔。如需自訂容器的詳細資訊,請參閱建置 AWS 深度學習容器自訂映像
-
假設您想要將套件新增至 PyTorch DLC Docker 映像檔。在
docker
目錄建立 Dockerfile,如下列範例所示:mkdir -p workspace/docker cat workspace/docker/Dockerfile ARG BASE_IMAGE FROM $BASE_IMAGE #Install any additional libraries RUN pip install transformers==4.28.1
-
使用下列 build_and_push.sh
指令碼建立並發布自訂的 Docker 映像檔。 # Download script build_and_push.sh to workspace/docker ls workspace/docker build_and_push.sh Dockerfile # Build and publish your docker image reponame = "torchserve" versiontag = "demo-0.1" ./build_and_push.sh {reponame} {versiontag} {baseimage} {region} {account}
SageMaker AI 預先安裝使用案例
下列範例向您顯示如何預先安裝套件至 PyTorch DLC 容器。您必須在本機目錄 workspace/code
建立 requirements.txt
檔案。
mkdir -p workspace/code cat workspace/code/requirements.txt transformers==4.28.1
建立 TorchServe 模型成品
在下面的範例中,我們使用預先訓練的 MNIST 模型workspace/mnist
目錄,依照 TorchServe 自訂服務指示torch-model-archiver
工具建立模型成品並上傳到 Amazon S3。
-
在
model-config.yaml
設定模型參數。ls -al workspace/mnist-dev mnist.py mnist_handler.py mnist_cnn.pt model-config.yaml # config the model cat workspace/mnist-dev/model-config.yaml minWorkers: 1 maxWorkers: 1 batchSize: 4 maxBatchDelay: 200 responseTimeout: 300
-
使用 torch-model-archiver
建立模型成品。 torch-model-archiver --model-name mnist --version 1.0 --model-file workspace/mnist-dev/mnist.py --serialized-file workspace/mnist-dev/mnist_cnn.pt --handler workspace/mnist-dev/mnist_handler.py --config-file workspace/mnist-dev/model-config.yaml --archive-format tgz
如果要預先安裝套件,則必須在
tar.gz
檔案包含code
目錄。cd workspace torch-model-archiver --model-name mnist --version 1.0 --model-file mnist-dev/mnist.py --serialized-file mnist-dev/mnist_cnn.pt --handler mnist-dev/mnist_handler.py --config-file mnist-dev/model-config.yaml --archive-format no-archive cd mnist mv ../code . tar cvzf mnist.tar.gz .
-
將
mnist.tar.gz
上傳到 Amazon S3。# upload mnist.tar.gz to S3 output_path = f"s3://{bucket_name}/{prefix}/models" aws s3 cp mnist.tar.gz {output_path}/mnist.tar.gz
使用單一模型端點透過 TorchServe 進行部署
下列範例說明如何建立單一模型即時推斷端點、將模型部署到端點,以及如何使用 Amazon SageMaker Python SDK
from sagemaker.model import Model from sagemaker.predictor import Predictor # create the single model endpoint and deploy it on SageMaker AI model = Model(model_data = f'{output_path}/mnist.tar.gz', image_uri = baseimage, role = role, predictor_cls = Predictor, name = "mnist", sagemaker_session = smsess) endpoint_name = 'torchserve-endpoint-' + time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) predictor = model.deploy(instance_type='ml.g4dn.xlarge', initial_instance_count=1, endpoint_name = endpoint_name, serializer=JSONSerializer(), deserializer=JSONDeserializer()) # test the endpoint import random import numpy as np dummy_data = {"inputs": np.random.rand(16, 1, 28, 28).tolist()} res = predictor.predict(dummy_data)
使用多模型端點透過 TorchServe 進行部署
多模型端點提供可擴展且經濟實惠的解決方案,可在一個端點後面託管大量模型。它們透過共用相同的資源機群及服務容器來託管所有模型,進而提高端點使用率。它們也會降低部署開銷,因為 SageMaker AI 會動態管理載入和卸載模型,並根據流量模式擴展資源。對於需要加速運算能力的深度學習及生成式 AI 模型,多模型端點特別有用。
透過在 SageMaker AI 多模型端點上使用 TorchServe,您可以使用您熟悉的服務堆疊來加速開發,同時利用 SageMaker AI 多模型端點提供的資源共用和簡化模型管理。
以下範例說明如何建立多模型端點、將模型部署到端點,以及如何使用 Amazon SageMaker Python SDK
from sagemaker.multidatamodel import MultiDataModel from sagemaker.model import Model from sagemaker.predictor import Predictor # create the single model endpoint and deploy it on SageMaker AI model = Model(model_data = f'{output_path}/mnist.tar.gz', image_uri = baseimage, role = role, sagemaker_session = smsess) endpoint_name = 'torchserve-endpoint-' + time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) mme = MultiDataModel( name = endpoint_name, model_data_prefix = output_path, model = model, sagemaker_session = smsess) mme.deploy( initial_instance_count = 1, instance_type = "ml.g4dn.xlarge", serializer=sagemaker.serializers.JSONSerializer(), deserializer=sagemaker.deserializers.JSONDeserializer()) # list models list(mme.list_models()) # create mnist v2 model artifacts cp mnist.tar.gz mnistv2.tar.gz # add mnistv2 mme.add_model(mnistv2.tar.gz) # list models list(mme.list_models()) predictor = Predictor(endpoint_name=mme.endpoint_name, sagemaker_session=smsess) # test the endpoint import random import numpy as np dummy_data = {"inputs": np.random.rand(16, 1, 28, 28).tolist()} res = predictor.predict(date=dummy_data, target_model="mnist.tar.gz")
指標
TorchServe 支援系統層級與模型層級指標。您可以透過環境變數 TS_METRICS_MODE
,在日誌格式模式或 Prometheus 模式啟用指標。您可以使用 TorchServe 中央指標設定檔 metrics.yaml
來指定要追蹤的指標類型,例如請求計數、延遲、記憶體使用量、GPU 使用率等。透過參考此檔案,您可以深入瞭解已部署模型的效能及健康狀況,並即時有效監控 TorchServe 伺服器的行為。如需詳細資訊,請參閱 TorchServe 指標文件
您可以透過 Amazon CloudWatch 日誌篩選條件存取類似於 StatsD 格式的指標日誌。下列為 TorchServe 指標日誌的範例:
CPUUtilization.Percent:0.0|#Level:Host|#hostname:my_machine_name,timestamp:1682098185 DiskAvailable.Gigabytes:318.0416717529297|#Level:Host|#hostname:my_machine_name,timestamp:1682098185