在 Amazon SageMaker 托管端点上使用 EI - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

在 Amazon SageMaker 托管端点上使用 EI

要在 Amazon SageMaker 中将 Elastic Inference (EI) 用于托管端点进行实时推理,在您创建托管在端点上的可部署模型时,请指定 EI 加速器。您可以通过下列方式之一来执行该操作:

  • 使用 TensorFlow、MXNet 或 PyTorch 的 Amazon SageMaker Python SDK 版本,以及适用于 TensorFlow、MXNet 和 PyTorch 的 SageMaker 预构建容器

  • 构建您自己的容器,然后使用低级 SageMaker API (Boto 3)。您需要从提供的 Amazon S3 位置,将 TensorFlow、MXNet 或 PyTorch 的启用 EI 的版本导入容器中,并使用这些版本之一来编写训练脚本。

  • 使用 图像分类 – MXNet对象检测 – MXNet 内置算法,并使用 Amazon SDK for Python (Boto3) 来运行训练作业和创建您的可部署模型及托管端点。

将 EI 与 SageMaker TensorFlow 容器结合使用

要在 SageMaker 中将 TensorFlow 与 EI 结合使用,您需要调用估算器模型对象的 deploy 方法。然后,您可以使用 accelerator_type 输入参数指定加速器类型。有关在 SageMaker Python SDK 中使用 TensorFlow 的信息,请参阅:https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/index.html

SageMaker 提供了默认模型训练和推理代码以便您使用。对于自定义文件格式,您可能需要实施自定义模型训练和推理代码。

使用评估程序对象

要将评估程序对象与 EI 结合使用,请在使用部署方法时包含 accelerator_type 输入参数。评估程序返回我们调用其部署方法的预测器对象,如示例代码中所示。

# Deploy an estimator using EI (using the accelerator_type input argument) predictor = estimator.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge', accelerator_type='ml.eia2.medium')

使用模型对象

要将模型对象与 EI 结合使用,请在使用部署方法时包含 accelerator_type 输入参数。评估程序返回我们调用其部署方法的预测器对象,如示例代码中所示。

# Deploy a model using EI (using the accelerator_type input argument) predictor = model.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge', accelerator_type='ml.eia2.medium')

将 EI 与 SageMaker MXNet 容器结合使用

要在 SageMaker 中将 MXNet 与 EI 结合使用,您需要调用估算器模型对象的 deploy 方法。然后,您可以使用 accelerator_type 输入参数指定加速器类型。有关在 Amazon SageMaker Python SDK 中使用 MXNet 的信息,请参阅 https://sagemaker.readthedocs.io/en/stable/frameworks/mxnet/index.html

为方便您使用,SageMaker 提供了默认模型训练和推理代码。对于自定义文件格式,您可能需要编写自定义模型训练和推理代码。

使用评估程序对象

要将评估程序对象与 EI 结合使用,请在使用部署方法时包含 accelerator_type 输入参数。评估程序返回我们调用其部署方法的预测器对象,如示例代码中所示。

# Deploy an estimator using EI (using the accelerator_type input argument) predictor = estimator.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge', accelerator_type='ml.eia2.medium')

使用模型对象

要将模型对象与 EI 结合使用,请在使用部署方法时包含 accelerator_type 输入参数。评估程序返回我们调用其部署方法的预测器对象,如示例代码中所示。

# Deploy a model using EI (using the accelerator_type input argument) predictor = model.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge', accelerator_type='ml.eia2.medium')

有关在 SageMaker 中将 EI 与 MXNet 结合使用的完整示例,请参阅位于 https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/mxnet_mnist/mxnet_mnist_elastic_inference.ipynb 的示例笔记本

将 EI 与 SageMaker PyTorch 容器结合使用

要在 SageMaker 中将 PyTorch 与 EI 结合使用,您需要调用估算器模型对象的 deploy 方法。然后,您可以使用 accelerator_type 输入参数指定加速器类型。有关在 Amazon SageMaker Python SDK 中使用 PyTorch 的信息,请参阅 SageMaker PyTorch 估算器和模型

为方便您使用,SageMaker 提供了默认模型训练和推理代码。对于自定义文件格式,您可能需要编写自定义模型训练和推理代码。

使用评估程序对象

要将评估程序对象与 EI 结合使用,请在使用部署方法时包含 accelerator_type 输入参数。评估程序返回我们调用其部署方法的预测器对象,如此示例代码中所示。

# Deploy an estimator using EI (using the accelerator_type input argument) predictor = estimator.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge', accelerator_type='ml.eia2.medium')

使用模型对象

要将模型对象与 EI 结合使用,请在使用部署方法时包含 accelerator_type 输入参数。此模型返回我们调用其部署方法的预测器对象,如示例代码中所示:

# Deploy a model using EI (using the accelerator_type input argument) predictor = model.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge', accelerator_type='ml.eia2.medium')

将 EI 用于自己的容器

要将 EI 用于您构建的自定义容器中的一个模型,请使用适用于 Python 的低级 Amazon 开发工具包 (Boto 3)。下载并导入 Amazon 启用了 EI 的 TensorFlow、Apache MXNet 或 PyTorch 机器学习框架版本,并使用这些框架编写训练脚本。

将 TensorFlow、MXNet 或 PyTorch 的 EI 版本导入您的 Docker 容器

要将 EI 用于您自己的容器,您需要将 Amazon EI TensorFlow Serving 库、Amazon EI Apache MXNet 库或启用 Elastic Inference 的 PyTorch 库导入到容器中。TensorFlow 和 MXNet 启用 EI 的版本当前作为存储在 Amazon S3 位置中的二进制文件提供。您可以从 Amazon S3 存储桶下载 TensorFlow 的启用 EI 的二进制文件,网址为 console.aws.amazon.com/s3/buckets/amazonei-tensorflow。有关构建使用启用了 EI 的 TensorFlow 版本的容器的信息,请参阅 https://github.com/aws/sagemaker-tensorflow-container#building-the-sagemaker-elastic-inference-tensorflow-serving-container。您可以从公共 Amazon S3 存储桶下载 Apache MXNet 的启用 EI 的二进制文件,网址为 console.aws.amazon.com/s3/buckets/amazonei-apachemxnet。有关构建使用启用了 EI 的 MXNet 版本的容器的信息,请参阅 https://github.com/aws/sagemaker-mxnet-container#building-the-sagemaker-elastic-inference-mxnet-container。您可以下载适用于 PyTorch 的启用 Elastic Inference 的二进制文件。有关构建容器(该容器使用启用了 Elastic Inference 的 PyTorch 版本)的信息,请参阅构建映像

使用 Amazon SDK for Python (Boto 3) 创建 EI 端点

要使用适用于 Python 的 Amazon 开发工具包 (Boto 3) 创建端点,首先需要创建端点配置。端点配置指定您要托管在端点上的一个或多个模型(称为生产变体)。要将 EI 附加到托管在端点上的一个或多个生产变体,您可以指定 EI 实例类型之一作为该 ProductionVariantAcceleratorType 字段。然后,创建端点时,传递该端点配置。

创建端点配置

要使用 EI,您需要在端点配置中指定加速器类型。

# Create Endpoint Configuration from time import gmtime, strftime endpoint_config_name = 'ImageClassificationEndpointConfig-' + strftime("%Y-%m-%d-%H-%M-%S", gmtime()) print(endpoint_config_name) create_endpoint_config_response = sagemaker.create_endpoint_config( EndpointConfigName = endpoint_config_name, ProductionVariants=[{ 'InstanceType':'ml.m4.xlarge', 'InitialInstanceCount':1, 'ModelName':model_name, 'VariantName':'AllTraffic', 'AcceleratorType':'ml.eia2.medium'}]) print("Endpoint Config Arn: " + create_endpoint_config_response['EndpointConfigArn'])

创建端点

创建具有加速器类型的端点配置之后,您可以创建端点。

endpoint_name = 'ImageClassificationEndpoint-' + strftime("%Y-%m-%d-%H-%M-%S", gmtime()) endpoint_response = sagemaker.create_endpoint( EndpointName=endpoint_name, EndpointConfigName=endpoint_config_name)

创建端点后,您可以使用 Boto3 运行时对象中的 invoke_endpoint 方法调用它,就像对待任何其他端点一样。