SDK를 사용하여 컴파일된 모델 배포하기 SageMaker - 아마존 SageMaker

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

SDK를 사용하여 컴파일된 모델 배포하기 SageMaker

AWS SDK for Python (Boto3) AWS CLI, 또는 Amazon SageMaker 콘솔을 사용하여 모델을 컴파일한 경우 사전 요구 사항 섹션을 충족해야 합니다. 다음 사용 사례 중 하나를 따라 모델을 컴파일한 방식에 따라 SageMaker Neo로 컴파일된 모델을 배포하십시오.

SDK를 사용하여 모델을 컴파일한 SageMaker 경우

컴파일된 모델에 대한 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 모델 API에서 deploy () API를 사용하여 배포합니다. MXNet의 경우 MX이고 용은 NetModel MX입니다. PyTorch PyTorchModel SageMaker 모델을 만들고 배포할 때는 MMS_DEFAULT_RESPONSE_TIMEOUT 환경 변수를 로 설정하고 매개 변수를 추론 스크립트 () 로 500 지정하고 entry_point 매개 변수를 추론 스크립트의 디렉터리 위치 (inference.pycode) 로 지정해야 합니다. source_dir 추론 스크립트(inference.py)를 준비하려면 사전 조건 단계를 따르십시오.

다음 예제는 이러한 함수를 사용하여 Python용 SageMaker SDK를 사용하여 컴파일된 모델을 배포하는 방법을 보여줍니다.

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)
참고

AmazonSageMakerFullAccessAmazonS3ReadOnlyAccess 정책은 AmazonSageMaker-ExecutionRole IAM 역할에 연결되어야 합니다.

Boto3, SageMaker 콘솔 또는 CLI를 사용하여 모델을 컴파일한 경우 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객체를 구성하는 방법에 대한 자세한 내용은 SDK를 참조하십시오. SageMaker

참고

모델을 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 페이지를 참조하세요.