

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# SageMaker SDK를 사용하여 컴파일된 모델 배포
<a name="neo-deployment-hosting-services-sdk"></a>

모델이 AWS SDK for Python (Boto3) AWS CLI또는 Amazon SageMaker AI 콘솔을 사용하여 컴파일된 경우 [ 사전 조건](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 콘솔 또는 TensorFlow용 CLI를 사용하여 모델을 컴파일한 경우](#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`으로 설정하고, `entry_point` 파라미터를 추론 스크립트(`inference.py`)로 지정하고, `source_dir` 파라미터를 추론 스크립트의 디렉터리 위치(`code`)로 지정해야 합니다. 추론 스크립트(`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)
```

------

**참고**  
`AmazonSageMakerFullAccess` 및 `AmazonS3ReadOnlyAccess` 정책은 `AmazonSageMaker-ExecutionRole` IAM 역할에 연결되어야 합니다.

## Boto3, SageMaker 콘솔 또는 TensorFlow용 CLI를 사용하여 모델을 컴파일한 경우
<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)에서 요구 사항에 맞는 도커 이미지 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)를 참조하세요.