

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

# SageMaker SDK を使ってコンパイル済みモデルをデプロイする
<a name="neo-deployment-hosting-services-sdk"></a>

モデルが または Amazon SageMaker AI コンソールを使用してコンパイルされている場合は AWS SDK for Python (Boto3) AWS CLI、[前提条件](https://docs.aws.amazon.com//sagemaker/latest/dg/neo-deployment-hosting-services-prerequisites)セクションを満たす必要があります。次のいずれかのユースケースに従い、モデルをコンパイルした方法に応じて、SageMaker Neo でコンパイル済みのモデルをデプロイします。

**Topics**
+ [SageMaker SDK を使ってモデルをコンパイルした場合](#neo-deployment-hosting-services-sdk-deploy-sm-sdk)
+ [MXNet または PyTorch を使ってモデルをコンパイルした場合](#neo-deployment-hosting-services-sdk-deploy-sm-boto3)
+ [Boto3、SageMaker コンソール、または CLI を使って TensorFlow モデルをコンパイルした場合](#neo-deployment-hosting-services-sdk-deploy-sm-boto3-tensorflow)

## SageMaker SDK を使ってモデルをコンパイルした場合
<a name="neo-deployment-hosting-services-sdk-deploy-sm-sdk"></a>

コンパイル済みモデルの [sagemaker.Model](https://sagemaker.readthedocs.io/en/stable/api/inference/model.html?highlight=sagemaker.Model) オブジェクトハンドルは、推論リクエストを処理するエンドポイントの作成を可能にする [deploy()](https://sagemaker.readthedocs.io/en/stable/api/inference/model.html?highlight=sagemaker.Model#sagemaker.model.Model.deploy) 関数を提供します。この関数を使用すると、エンドポイントに使用されるインスタンスの数と種類を設定できます。モデルをコンパイルしたインスタンスを選択する必要があります。例えば、「[モデルをコンパイルする (Amazon SageMaker SDK)](https://docs.aws.amazon.com/sagemaker/latest/dg/neo-job-compilation-sagemaker-sdk.html)」セクションでコンパイルされたジョブでは、これは `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 を使ってモデルをコンパイルした場合
<a name="neo-deployment-hosting-services-sdk-deploy-sm-boto3"></a>

SageMaker AI モデルを作成し、フレームワーク固有のモデル API の deploy() API を使ってデプロイします。MXNet の場合は [MXNetModel](https://sagemaker.readthedocs.io/en/stable/frameworks/mxnet/sagemaker.mxnet.html?highlight=MXNetModel#mxnet-model)、PyTorch の場合は [PyTorchModel](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/sagemaker.pytorch.html?highlight=PyTorchModel#sagemaker.pytorch.model.PyTorchModel) です。SageMaker AI モデルを作成してデプロイするときには、`MMS_DEFAULT_RESPONSE_TIMEOUT` 環境変数に `500` を設定し、推論スクリプト (`inference.py`) としての `entry_point` パラメータと、推論スクリプトのディレクトリの場所 (`code`) としての `source_dir` パラメータを指定する必要があります。推論スクリプト (`inference.py`) を準備するには、「前提条件」の手順に従います。

次の例は、SageMaker AI 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 を使って TensorFlow モデルをコンパイルした場合
<a name="neo-deployment-hosting-services-sdk-deploy-sm-boto3-tensorflow"></a>

`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)
```

詳細については、「[モデルアーティファクトから直接デプロイする](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/deploying_tensorflow_serving.html#deploying-directly-from-model-artifacts)」を参照してください。

[こちらのリスト](https://docs.aws.amazon.com//sagemaker/latest/dg/neo-deployment-hosting-services-container-images.html)から、ニーズを満たす Docker イメージの Amazon ECR URI を選択できます。

`TensorFlowModel` オブジェクトを構築する方法については、[SageMaker SDK](https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/sagemaker.tensorflow.html#tensorflow-serving-model) を参照してください。

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

次のコードスニペットは、[前提条件](https://docs.aws.amazon.com//sagemaker/latest/dg/neo-deployment-hosting-services-prerequisites)セクションにあるイメージ分類の例のためにウォームアップファイルを作成する方法を示しています。

```
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 のページ](https://www.tensorflow.org/tfx/serving/saved_model_warmup)を参照してください。