SageMaker SDK を使用してコンパイル済みモデルをデプロイする - Amazon SageMaker

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

SageMaker SDK を使用してコンパイル済みモデルをデプロイする

モデルが AWS SDK for Python (Boto3)、、または Amazon SageMaker コンソールを使用してコンパイルされている場合は AWS CLI、前提条件セクションを満たす必要があります。次のいずれかのユースケースに従って、モデルのコンパイル方法に基づいて SageMaker Neo でコンパイルされたモデルをデプロイします。

SageMaker SDK を使用してモデルをコンパイルした場合

コンパイル済みモデルの sagemaker.Model オブジェクトハンドルは、推論リクエストを処理するエンドポイントの作成を可能にする deploy() 関数を提供します。この関数を使用すると、エンドポイントに使用されるインスタンスの数と種類を設定できます。モデルをコンパイルしたインスタンスを選択する必要があります。例えば、モデルをコンパイル (Amazon SageMaker SDK) セクションでコンパイルされたジョブでは、これは ですml_c5

predictor = compiled_model.deploy(initial_instance_count = 1, instance_type = 'ml.c5.4xlarge') # Print the name of newly created endpoint print(predictor.endpoint_name)

MXNet または を使用してモデルをコンパイルした場合 PyTorch

SageMaker モデルを作成し、フレームワーク固有の Model API の deploy() APIs。MXNet の場合、MXNetModel で、 の場合 PyTorch、 です PyTorchModel。 SageMaker モデルを作成してデプロイするときは、 MMS_DEFAULT_RESPONSE_TIMEOUT 環境変数を に設定500し、 entry_pointパラメータを推論スクリプト (inference.py) として指定し、 source_dirパラメータを推論スクリプトのディレクトリの場所 (code) として指定する必要があります。推論スクリプト (inference.py) を準備するには、「前提条件」の手順に従います。

次の例は、これらの関数を使用して SageMaker SDK for Python を使用してコンパイル済みモデルをデプロイする方法を示しています。

MXNet
from sagemaker.mxnet import MXNetModel # Create SageMaker model and deploy an endpoint sm_mxnet_compiled_model = MXNetModel( model_data='insert S3 path of compiled MXNet model archive', role='AmazonSageMaker-ExecutionRole', entry_point='inference.py', source_dir='code', framework_version='1.8.0', py_version='py3', image_uri='insert appropriate ECR Image URI for MXNet', env={'MMS_DEFAULT_RESPONSE_TIMEOUT': '500'}, ) # Replace the example instance_type below to your preferred instance_type predictor = sm_mxnet_compiled_model.deploy(initial_instance_count = 1, instance_type = 'ml.p3.2xlarge') # Print the name of newly created endpoint print(predictor.endpoint_name)
PyTorch 1.4 and Older
from sagemaker.pytorch import PyTorchModel # Create SageMaker model and deploy an endpoint sm_pytorch_compiled_model = PyTorchModel( model_data='insert S3 path of compiled PyTorch model archive', role='AmazonSageMaker-ExecutionRole', entry_point='inference.py', source_dir='code', framework_version='1.4.0', py_version='py3', image_uri='insert appropriate ECR Image URI for PyTorch', env={'MMS_DEFAULT_RESPONSE_TIMEOUT': '500'}, ) # Replace the example instance_type below to your preferred instance_type predictor = sm_pytorch_compiled_model.deploy(initial_instance_count = 1, instance_type = 'ml.p3.2xlarge') # Print the name of newly created endpoint print(predictor.endpoint_name)
PyTorch 1.5 and Newer
from sagemaker.pytorch import PyTorchModel # Create SageMaker model and deploy an endpoint sm_pytorch_compiled_model = PyTorchModel( model_data='insert S3 path of compiled PyTorch model archive', role='AmazonSageMaker-ExecutionRole', entry_point='inference.py', source_dir='code', framework_version='1.5', py_version='py3', image_uri='insert appropriate ECR Image URI for PyTorch', ) # Replace the example instance_type below to your preferred instance_type predictor = sm_pytorch_compiled_model.deploy(initial_instance_count = 1, instance_type = 'ml.p3.2xlarge') # Print the name of newly created endpoint print(predictor.endpoint_name)
注記

AmazonSageMaker-ExecutionRole IAM ロールに AmazonSageMakerFullAccess ポリシーと AmazonS3ReadOnlyAccess ポリシーをアタッチする必要があります。

Boto3、 SageMaker コンソール、または CLI for を使用してモデルをコンパイルした場合 TensorFlow

TensorFlowModel オブジェクトを構築し、deploy を呼び出します。

role='AmazonSageMaker-ExecutionRole' model_path='S3 path for model file' framework_image='inference container arn' tf_model = TensorFlowModel(model_data=model_path, framework_version='1.15.3', role=role, image_uri=framework_image) instance_type='ml.c5.xlarge' predictor = tf_model.deploy(instance_type=instance_type, initial_instance_count=1)

詳細については、「モデルアーティファクトから直接デプロイする」を参照してください。

こちらのリストから、ニーズを満たす Docker イメージの Amazon ECR URI を選択できます。

TensorFlowModel オブジェクトの作成方法の詳細については、「 SageMaker SDK」を参照してください。

注記

モデルを GPU にデプロイした場合、最初の推論リクエストではレイテンシーが高くなる可能性があります。これは、最初の推論リクエストで最適化されたコンピューティングカーネルが作成されるためです。TFX にモデルファイル送る前に、推論リクエストのウォームアップファイルを作成し、モデルファイルと一緒に保存しておくことを推奨します。これをモデルの「ウォームアップ」と呼びます。

次のコードスニペットは、前提条件セクションにあるイメージ分類の例のためにウォームアップファイルを作成する方法を示しています。

import tensorflow as tf from tensorflow_serving.apis import classification_pb2 from tensorflow_serving.apis import inference_pb2 from tensorflow_serving.apis import model_pb2 from tensorflow_serving.apis import predict_pb2 from tensorflow_serving.apis import prediction_log_pb2 from tensorflow_serving.apis import regression_pb2 import numpy as np with tf.python_io.TFRecordWriter("tf_serving_warmup_requests") as writer: img = np.random.uniform(0, 1, size=[224, 224, 3]).astype(np.float32) img = np.expand_dims(img, axis=0) test_data = np.repeat(img, 1, axis=0) request = predict_pb2.PredictRequest() request.model_spec.name = 'compiled_models' request.model_spec.signature_name = 'serving_default' request.inputs['Placeholder:0'].CopyFrom(tf.compat.v1.make_tensor_proto(test_data, shape=test_data.shape, dtype=tf.float32)) log = prediction_log_pb2.PredictionLog( predict_log=prediction_log_pb2.PredictLog(request=request)) writer.write(log.SerializeToString())

モデルを「ウォームアップ」する方法の詳細については、TensorFlow 「 TFX」ページを参照してください。