本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
将 EI 在 Amazon SageMaker 托管终端节点上
要在 Amazon SageMaker 中将 Elastic Inference (EI) 用于托管终端节点进行实时推理,请在您创建托管在终端节点上的可部署模型时,请指定 EI 加速器。您可以通过下列方式之一来执行该操作:
-
使用Amazon SageMaker Python 开发工具包
TensorFlow、MXNet 或 PyTorch 的版本,以及适用于 TensorFlow、MXNet 和 PyTorch 的 SageMaker 预构建容器 -
构建您自己的容器,然后使用低级别 SageMaker API (Boo 3)。您需要从提供的 Amazon S3 位置,将 TensorFlow、MXNet 或 PyTorch 的启用 EI 的版本导入容器中,并使用这些版本之一来编写训练脚本。
-
使用 图像分类算法 或 对象检测算法 内置算法,并使用 Amazon SDK for Python (Boto3) 来运行训练作业和创建您的可部署模型及托管终端节点。
主题
将 EI 与 SageMaker TensorFlow 容器结合使用
要在 SageMaker 中将 TensorFlow 与 EI 结合使用,你需要调用deploy任何一种方法估计程序
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 输入参数指定加速器类型。有关在中使用 MXNet 的信息Amazon SageMaker Python 开发工具包
为方便起见,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 输入参数指定加速器类型。有关在中使用 PyTorch 的信息Amazon SageMaker Python 开发工具包
为方便起见,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 存储桶中下载启用了 EI 的 TensorFlow 二进制文件,网址为:console e.aws.amazon.com/s3/buckets/amazonei-tensorflow
使用创建 EI 终端节点Amazon适用于 Python 的 SDK (Boo 3)
要使用适用于 Python 的 Amazon 开发工具包 (Boto 3) 创建终端节点,首先需要创建终端节点配置。终端节点配置指定您要托管在终端节点上的一个或多个模型(称为生产变体)。要将 EI 附加到托管在终端节点上的一个或多个生产变体,您可以指定 EI 实例类型之一作为该 ProductionVariant 的 AcceleratorType 字段。然后,创建终端节点时,传递该终端节点配置。
创建终端节点配置
要使用 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 方法调用它,就像对待任何其他终端节点一样。