使用 SageMaker SDK 部署编译的模型 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 SageMaker SDK 部署编译的模型

如果您是使用 Amazon SDK for Python (Boto3)、Amazon CLI 或 Amazon SageMaker 控制台编译模型,则必须满足先决条件部分中的要求。按照以下使用案例之一,根据您编译模型的方式部署使用 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 编译模型

在框架特定模型 API 下使用 deploy() API 创建并部署 SageMaker 模型。对应 MXNet 的是 MXNetModel,对应 PyTorch 的是 PyTorchModel。在创建和部署 SageMaker 模型时,必须将 MMS_DEFAULT_RESPONSE_TIMEOUT 环境变量设置为 500,并指定 entry_point 参数作为推理脚本 (inference.py),指定 source_dir 参数作为推理脚本的目录位置 (code)。按照先决条件中的步骤准备推理脚本 (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 对象,然后调用部署:

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 页面