翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
非同期推論エンドポイントを作成する
ホスティングサービスを使用してエンドポイントを作成するのと同じ方法で非同期エンドポイントを作成します。 SageMaker
SageMaker でモデルを作成します。
CreateModel
CreateEndpointConfig
を使用してエンドポイント設定を作成する。CreateEndpoint
を使用して HTTPS エンドポイントを作成する。
エンドポイントを作成するには、まず、CreateModel
を使用してモデルアーティファクトと Docker レジストリパス (イメージ) をポイントする、モデルを作成します。次に、デプロイする CreateModel
API CreateEndpointConfig
を使用して作成した 1 つ以上のモデルと、 SageMaker プロビジョニングするリソースを指定して設定を作成します。リクエストで指定したエンドポイント設定を使用して CreateEndpoint
でエンドポイントを作成します。非同期エンドポイントは、UpdateEndpoint
API を使用して更新できます。InvokeEndpointAsync
を使って、エンドポイント ホストされているモデルとの間で推論リクエストを送受信します。エンドポイントは DeleteEndpoint
API を使用して削除できます。
SageMaker 利用可能なイメージの全リストについては、「利用可能なDeep Learning Containers イメージ
モデルを作成する
次の例は、 AWS SDK for Python (Boto3)を使用してモデルを作成する方法を示しています。最初の数行では、以下を定義します。
sagemaker_client
: SageMaker AWS サービスへのリクエストの送受信を容易にする低レベルのクライアントオブジェクト。sagemaker_role
: SageMaker IAM ロールの Amazon リソースネーム (ARN) を含む文字列変数。aws_region
: AWS リージョンの名前を含む文字列変数。
import boto3 # Specify your AWS Region aws_region=
'<aws_region>'
# Create a low-level SageMaker service client. sagemaker_client = boto3.client('sagemaker', region_name=aws_region) # Role to give SageMaker permission to access AWS services. sagemaker_role= "arn:aws:iam::<account>:role/*
"
次に、Amazon S3 に保存されているトレーニング済みモデルの場所を指定します。この例では、demo-xgboost-model.tar.gz
という名前のトレーニング済み XGBoost モデルを使用します。完全な Amazon S3 URI は文字列変数 model_url
に保存されています。
#Create a variable w/ the model S3 URI s3_bucket =
'<your-bucket-name>'
# Provide the name of your S3 bucket bucket_prefix='saved_models' model_s3_key = f"{bucket_prefix}/demo-xgboost-model.tar.gz" #Specify S3 bucket w/ model model_url = f"s3://{s3_bucket}/{model_s3_key}"
プライマリコンテナを指定します。プライマリコンテナについては、推論コードを含む Docker イメージ、アーティファクト (以前のトレーニングから)、予測のためにモデルをデプロイするときに推論コードが使うカスタム環境マップを指定します。
この例では、XGBoost 組み込みアルゴリズムコンテナイメージを指定します。
from sagemaker import image_uris # Specify an AWS container image. container = image_uris.retrieve(region=aws_region, framework='xgboost', version='0.90-1')
を使用して Amazon SageMaker でモデルを作成しますCreateModel
。次を指定します:
ModelName
: モデルの名前 (この例では、model_name
という文字列変数として保存されています)。ExecutionRoleArn
: ML コンピュートインスタンスへのデプロイやバッチ変換ジョブのために Amazon がモデルアーティファクトや Docker SageMaker イメージにアクセスするために引き受けられる IAM ロールの Amazon リソースネーム(ARN)。PrimaryContainer
: 推論コード、関連アーティファクト、予測のためにモデルがデプロイされるときに推論コードが使用するカスタム環境マップを含むプライマリ Docker イメージの場所。
model_name =
'<The_name_of_the_model>'
#Create model create_model_response = sagemaker_client.create_model( ModelName = model_name, ExecutionRoleArn = sagemaker_role, PrimaryContainer = { 'Image': container, 'ModelDataUrl': model_url, })
API パラメータの全リストについては、『 SageMaker API リファレンスガイド』CreateModel
の説明を参照してください。
SageMaker 付属のコンテナを使用している場合は、このステップで環境変数を設定することで、モデルサーバーのタイムアウトとペイロードサイズをデフォルト値からフレームワークがサポートする最大値まで増やすことができます。これらの変数を明示的に設定しないと、非同期推論がサポートする最大タイムアウトサイズとペイロードサイズを利用できない場合があります。次の例は、に基づいて推論コンテナの環境変数を設定する方法を示しています。 PyTorch TorchServe
model_name =
'<The_name_of_the_model>'
#Create model create_model_response = sagemaker_client.create_model( ModelName = model_name, ExecutionRoleArn = sagemaker_role, PrimaryContainer = { 'Image': container, 'ModelDataUrl': model_url, 'Environment': { 'TS_MAX_REQUEST_SIZE': '100000000', 'TS_MAX_RESPONSE_SIZE': '100000000', 'TS_DEFAULT_RESPONSE_TIMEOUT': '1000' }, })
エンドポイントの作成が完了したら、inference.py
スクリプトから環境変数を出力して、環境変数が正しく設定されているかどうかをテストする必要があります。次の表は、デフォルト値を変更するために設定できる複数のフレームワークの環境変数の一覧です。
フレームワーク | 環境変数 |
---|---|
PyTorch 1.8 (ベース) TorchServe |
'TS_MAX_REQUEST_SIZE': '100000000' 'TS_MAX_RESPONSE_SIZE': '100000000' 'TS_DEFAULT_RESPONSE_TIMEOUT': '1000' |
PyTorch 1.4 (MMS に基づく) |
'MMS_MAX_REQUEST_SIZE': '1000000000' 'MMS_MAX_RESPONSE_SIZE': '1000000000' 'MMS_DEFAULT_RESPONSE_TIMEOUT': '900' |
HuggingFace 推論コンテナ (MMS ベース) |
'MMS_MAX_REQUEST_SIZE': '2000000000' 'MMS_MAX_RESPONSE_SIZE': '2000000000' 'MMS_DEFAULT_RESPONSE_TIMEOUT': '900' |
エンドポイント設定を作成する
モデルを作成したら、CreateEndpointConfig
を使用してエンドポイント設定を作成します。Amazon SageMaker ホスティングサービスはこの設定を使用してモデルをデプロイします。この構成では、with を使用して作成した 1 つ以上のモデルを指定してCreateModel
、Amazon SageMaker にプロビジョニングさせたいリソースをデプロイします。AsyncInferenceConfig
オブジェクトを指定し、OutputConfig
に対する出力 Amazon S3 ロケーションを指定します。オプションで、予測結果に関する通知を送信する Amazon SNS トピックを指定できます。Amazon SNS トピックの詳細については、「Amazon SNS を設定する」を参照してください。
次の例では、 AWS SDK for Python (Boto3)を使用してエンドポイント設定を作成する方法を示します。
import datetime from time import gmtime, strftime # Create an endpoint config name. Here we create one based on the date # so it we can search endpoints based on creation time. endpoint_config_name = f"XGBoostEndpointConfig-{strftime('%Y-%m-%d-%H-%M-%S', gmtime())}" # The name of the model that you want to host. This is the name that you specified when creating the model. model_name=
'<The_name_of_your_model>'
create_endpoint_config_response = sagemaker_client.create_endpoint_config( EndpointConfigName=endpoint_config_name, # You will specify this name in a CreateEndpoint request. # List of ProductionVariant objects, one for each model that you want to host at this endpoint. ProductionVariants=[ { "VariantName":"variant1"
, # The name of the production variant. "ModelName": model_name, "InstanceType":"ml.m5.xlarge"
, # Specify the compute instance type. "InitialInstanceCount":1
# Number of instances to launch initially. } ], AsyncInferenceConfig={ "OutputConfig": { # Location to upload response outputs when no location is provided in the request. "S3OutputPath": f"s3://{s3_bucket}/{bucket_prefix}/output" # (Optional) specify Amazon SNS topics "NotificationConfig": { "SuccessTopic": "arn:aws:sns:aws-region:account-id:topic-name
", "ErrorTopic": "arn:aws:sns:aws-region:account-id:topic-name
", } }, "ClientConfig": { # (Optional) Specify the max number of inflight invocations per instance # If no value is provided, Amazon SageMaker will choose an optimal value for you "MaxConcurrentInvocationsPerInstance": 4 } } ) print(f"Created EndpointConfig: {create_endpoint_config_response['EndpointConfigArn']}")
前述の例では、次のキーを AsyncInferenceConfig
フィールドの OutputConfig
に指定します。
S3OutputPath
: リクエストで場所が指定されていない場合に、レスポンスの出力をアップロードする場所。NotificationConfig
: (オプション) 推論リクエストが正常に終了した場合 (SuccessTopic
) または失敗した場合 (ErrorTopic
) に通知を投稿する SNS トピック。
AsyncInferenceConfig
フィールドの ClientConfig
に次のオプションの引数を指定することもできます。
MaxConcurrentInvocationsPerInstance
: (オプション) SageMaker クライアントがモデルコンテナに送信する同時リクエストの最大数。
エンドポイントの作成
モデルとエンドポイントの設定が完了したら、CreateEndpoint
API を使用してエンドポイントを作成します。 AWS AWS エンドポイント名はアカウントのリージョン内で一意である必要があります。
次が、リクエストで指定されたエンドポイント設定を使用してエンドポイントを作成します。Amazon SageMaker はエンドポイントを使用してリソースをプロビジョニングし、モデルをデプロイします。
# The name of the endpoint.The name must be unique within an AWS Region in your AWS account. endpoint_name =
'<endpoint-name>'
# The name of the endpoint configuration associated with this endpoint. endpoint_config_name='<endpoint-config-name>'
create_endpoint_response = sagemaker_client.create_endpoint( EndpointName=endpoint_name, EndpointConfigName=endpoint_config_name)
CreateEndpoint
API を呼び出すと、Amazon SageMaker 非同期推論は Amazon SNS トピックが設定されていることを確認するためのテスト通知を送信します。Amazon SageMaker 非同期推論は、およびへの呼び出し後にテスト通知も送信します。UpdateEndpoint
UpdateEndpointWeightsAndCapacities
これにより、 SageMaker 必要な権限があるかどうかを確認できます。通知は無視できます。テスト通知は次の形式です。
{ "eventVersion":"1.0", "eventSource":"aws:sagemaker", "eventName":"TestNotification" }