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

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在亚马逊 SageMaker 托管的终端节点上使用 EI

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

  • 使用 Amaz SageMaker on Python 软件开发工具包版本的 TensorFlow、mxNet 或, PyTorch以及 SageMaker 预先构建的容器 TensorFlow mxNet 和 PyTorch

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

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

将 EI 与 SageMaker TensorFlow容器一起使用

要在 EI 中 TensorFlow 使用 SageMaker,你需要调用 E stimator 或 M odel 对象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 容器配合使用

要在 EI 中使用带有 EI 的 MxNet SageMaker,你需要调用估算器或模型对象deploy的方法。然后,您可以使用 accelerator_type 输入参数指定加速器类型。有关在亚马逊 Pyth SageMaker on 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')

将 EI 与 SageMaker PyTorch 容器一起使用

要在 EI 中 PyTorch 使用 SageMaker,你需要调用 E stimator 或 M odel 对象deploy的方法。然后,您可以使用 accelerator_type 输入参数指定加速器类型。有关 PyTorch 在 Amaz SageMaker on Python 软件开发工具包中使用的信息,请参阅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 SDK (Boto 3)。下载并导入 Amazon 支持 EI 的版本、 TensorFlow Apache MxNet 或 PyTorch 机器学习框架,然后使用这些框架编写训练脚本。

将 EI 版本的 mxNet 或导 PyTorch 入你的 Docker 容器中 TensorFlow

要将 EI 与自己的容器一起使用,您需要将 Amazon EI TensorFlow 服务库、Amazon EI Apache MXnet 库或 PyTorch 支持 Elastic Inference 的库导入到您的容器中。支持 EI 的版本和 TensorFlow MXnet 目前以二进制文件形式提供,存储在 Amazon S3 位置。你可以通过 console.aws.amazon.com/s3/buckets/amazonei-tensorflow TensorFlow 从亚马逊 S3 存储桶下载支持 EI 的二进制文件。有关构建使用支持 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。您可以下载启用 Elastic Inference 的二进制文件。 PyTorch有关使用支持 Elastic Inference 的版本构建容器的信息 PyTorch,请参阅构建您的映像

使用适用于 Python 的 S Amazon DK 创建 EI 端点 (Boto 3)

要使用 Amazon 适用于 Python 的 SDK(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 方法调用它,就像对待任何其他端点一样。