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

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

创建终端节点并部署模型

使用部署模型有多种选择 SageMaker 托管服务。您可以使用以编程方式部署模型Amazon开发工具包(例如,SDK for Python (Boto3)), SageMaker Python 开发工具包,Amazon CLI,或者你可以使用 SageMaker 控制台。这些区域有:AmazonSDK 是一个低级 API,支持 Java、C++、Go、JavaScript、Node.js、PHP、Ruby 和 Python 而 SageMaker Python 开发工具包是一个高级 Python API。以下文档演示了如何使用Amazon SDK for Python (Boto3)和 SageMaker Python 开发工具包。

使用部署模型 SageMaker 如果使用托管服务有三个步骤,Amazon SDK for Python (Boto3),Amazon CLI,或者 SageMaker 控制台:

  1. 创建 SageMaker SageMaker 中的模型。

  2. 为 HTTPS 终端节点创建终端节点配置。

  3. 创建 HTTPS 终端节点。

使用 SageMaker Python SDK 不要求您创建终端节点配置。因此,这包括两个步骤的过程:

  1. 通过创建模型对象Model可以部署到 HTTPS 终端节点的类。

  2. 使用模型对象的预构建创建 HTTPS 终端节点deploy()方法。

要使用继续执行的代码片段,请替换斜体占位符文本在示例代码中包含您自己的信息。

注意
  • SageMaker 支持运行 (a) 多个模型,(b) 一个模型的多个变体,或 (c) 在同一端点上运行模型和变体的组合。模型变体可以引用同一模型推理容器(即运行相同的算法),但使用不同的模型构件(例如,基于其他超参数配置的不同模型权重值)。相比之下,两种不同的模型可能使用相同的算法,但专注于不同的业务问题或基本目标,并可能在不同的数据集上运行。

  • 当你创建终端节点时, SageMaker 将 Amazon EBS 存储卷附加到托管该终端节点的每个 ML 计算实例。存储卷的大小取决于实例类型。有关实例类型列表 SageMaker 托管服务支持,请参阅Amazon服务限制. 有关存储卷的大小列表 SageMaker 附加到每个实例,请参阅主机实例存储卷.

  • 终端节点的作用域限于单个 Amazon 账户,并且终端节点不是公有的。该 URL 不包含账户 ID,但 SageMaker 通过调用者提供的身份验证令牌确定账户 ID。

    有关如何使用 Amazon API Gateway 的示例和Amazon Lambda要设置和部署可以从不在帐户范围内的客户端应用程序调用的 Web 服务,请参阅调用 SageMaker 使用 Amazon API Gateway 模型终端节点和Amazon Lambda中的AmazonMachine Learning 博客.

开始前的准备工作

在创建和部署 SageMaker 模型,找到并记下:

  • AmazonAmazon S3 存储桶所在的区域

  • 存储模型工件的 Amazon S3 URI 路径

  • SageMaker 的 IAM 角色

  • 包含推理代码的客户映像的 Docker Amazon ECR URI 注册表路径,或者支持和Amazon.

列表Amazon每个都可用的服务Amazon地区,请参阅区域地图和边缘网络. 请参阅创建 IAM 角色有关如何创建 IAM 角色的信息。

重要

存储模型构件的 Amazon S3 存储桶必须与您正在创建的模型位于同一区域中。

以下内容演示了如何以编程方式定义上述参数。

Amazon SDK for Python (Boto3)

导入 Boto3。定义您的Amazon区域。接下来,初始化低级 SageMaker 服务客户端对象使用Client类。这将使发送和接收请求变得容易Amazon服务。此外,定义Amazon赋予的角色 SageMaker 访问权限Amazon代表您提供的服务:

import boto3 # Specify your AWS Region aws_region='<aws_region>' # Create a low-level SageMaker service client. sagemaker_client = boto3.client('sagemaker', region_name=aws_region) # Role to give SageMaker permission to access Amazon services. sagemaker_role= "arn:aws:iam::<region>:<account>:role/*"

前几行定义:

  • sagemaker_client: 低级别 SageMaker 客户端对象,可以轻松发送和接收请求Amazon服务。

  • sagemaker_role: 一个字符串变量,带 SageMaker IAM 角色 Amazon 资源名称 (ARN)。

  • aws_region: 具有你的名字的字符串变量Amazon区域。

导入image_uris中的模块 SageMaker Python 开发工具包。使用retrieve()函数来检索与给定参数匹配的 Docker 映像的 Amazon ECR URI。同时提供框架名称或算法的名称 (framework字段)以及框架或算法的版本(version字段)。有关由提供的内置 Docker 映像列表Amazon,请参阅可用的 Deep Learning Containers 映像. 有关如何自带图像的更多信息,请参阅。使用您自己的推理代码.

from sagemaker import image_uris # Name of the framework or algorithm framework='<framework>' #framework='xgboost' # Example # Version of the framework or algorithm version = '<version-number>' #version = '0.90-1' # Example # Specify an AWS container image. container = image_uris.retrieve(region=aws_region, framework=framework, version=version)

接下来,提供预训练模型的 Amazon S3 URI。此模型将用于创建 SageMaker Python 开发工具包Model.model将在下一步中执行对象。在此示例中,完整的 Amazon S3 URI 存储在字符串变量中model_url

# Create a variable w/ the model S3 URI # First, provide the name of your S3 bucket s3_bucket = '<your-bucket-name>' # Specify what directory within your S3 bucket your model is stored in bucket_prefix = '<directory-name>' # Replace with the name of your model artifact model_filename = '<model-name>.tar.gz' # Relative S3 path model_s3_key = f'{bucket_prefix}/'+model_filename # Combine bucket name, model file name, and relate S3 path to create S3 model URI model_url = f's3://{s3_bucket}/{model_s3_key}'
SageMaker Python SDK

定义您的Amazon区域。此外,定义Amazon赋予的角色 SageMaker 访问权限Amazon代表您提供的服务:

# Specify your AWS Region aws_region='<aws_region>' # Role to give SageMaker permission to access Amazon services. sagemaker_role= "arn:aws:iam::<region>:<account>:role/*"

前几行定义:

  • sagemaker_role: 一个字符串变量,带 SageMaker IAM 角色 Amazon 资源名称 (ARN)。

  • aws_region: 具有你的名字的字符串变量Amazon区域。

导入image_uris中的模块 SageMaker Python 开发工具包。使用retrieve()函数来检索与给定参数匹配的 Docker 映像的 Amazon ECR URI。同时提供框架名称或算法的名称 (framework字段)以及框架或算法的版本(version字段)。有关由提供的内置 Docker 映像列表Amazon,请参阅可用的 Deep Learning Containers 映像. 有关如何自带图像的更多信息,请参阅。使用您自己的推理代码.

from sagemaker import image_uris # Name of the framework or algorithm framework='<framework>' #framework='xgboost' # Example # Version of the framework or algorithm version = '<version-number>' #version = '0.90-1' # Example # Specify an AWS container image. container = image_uris.retrieve(region=aws_region, framework=framework, version=version)

接下来,提供预训练模型的 Amazon S3 URI。此模型将用于创建 SageMaker Python 开发工具包Model.model将在下一步中执行对象。在此示例中,完整的 Amazon S3 URI 存储在字符串变量中model_url

# Create a variable w/ the model S3 URI # First, provide the name of your S3 bucket s3_bucket = '<your-bucket-name>' # Specify what directory within your S3 bucket your model is stored in bucket_prefix = '<directory-name>' # Replace with the name of your model artifact model_filename = '<model-name>.tar.gz' # Relative S3 path model_s3_key = f'{bucket_prefix}/'+model_filename # Combine bucket name, model file name, and relate S3 path to create S3 model URI model_url = f's3://{s3_bucket}/{model_s3_key}'

创建模型

通过创建模型,你告诉 SageMaker 在哪里可以找到模型组件。

Amazon SDK for Python (Boto3)

在 中创建模型 SageMaker 和CreateModel. 指定以下内容:

  • ModelName:模型的名称(在本例中,它存储为一个字符串变量,名为model_name)。

  • ExecutionRoleArn:IAM 角色的 Amazon 资源名称 (ARN),Amazon SageMaker 可代入此角色以访问模型构件和 Docker 映像以在 ML 计算实例上进行部署或批量转换作业。

  • PrimaryContainer:主要 Docker 映像的位置,该映像包含推理代码、关联构件和自定义环境贴图(在部署模型以进行预测时,推理代码将使用此贴图)。

对于主容器,指定包含推理代码的 Docker 映像、构件(来自先前的培训)和推理代码在您部署预测模型时使用的自定义环境贴图。

model_name = '<The_name_of_the_model>' #Create model create_model_response = sagemaker_client.create_model( ModelName = model_name, ExecutionRoleArn = sagemaker_role, PrimaryContainer = { 'Image': container, 'ModelDataUrl': model_url, })

请参阅CreateModel中的描述 SageMaker API 参考指南了解 API 参数的完整列表。

SageMaker Python SDK

通过创建模型对象 SageMaker Python 开发工具包Model类。在后面的步骤中,您将使用此对象将模型部署到终端节点。

from sagemaker.model import Model model = Model(image_uri=container, model_data=model_url, role=sagemaker_role)

创建终端节点配置

为 HTTPS 终端节点创建终端节点配置。指定生产中的一个或多个模型的名称(变体)和您想要的 ML 计算实例。 SageMaker 启动以托管每个生产变体。

注意

如果您专门使用 SageMaker Python 开发工具包。

在生产环境中托管模型时,您可以将终端节点配置为弹性扩展部署的 ML 计算实例。对于每个生产变体,您指定所要部署的 ML 计算实例数量。当您指定两个或更多实例时, SageMaker 在多个可用区中启动它们。这可确保连续可用性。SageMaker 管理实例的部署。

Amazon SDK for Python (Boto3)

使用创建端点配置CreateEndpointConfig. 亚马逊 SageMaker 托管服务使用此配置来部署模型。在配置中,您可以标识使用与创建的一个或多个模型。CreateModel,部署您希望 Amazon 的资源 SageMaker 预置。

以下示例演示了如何使用创建终端节点配置。Amazon SDK for Python (Boto3):

import datetime from time import gmtime, strftime # Create an endpoint config name. Here we create one based on the date # so it we can search endpoints based on creation time. endpoint_config_name = '<endpoint-config-name>' # The name of the model that you want to host. This is the name that you specified when creating the model. model_name='<The_name_of_your_model>' instance_type = '<instance-type>' # instance_type='ml.m5.xlarge' # Example endpoint_config_response = sagemaker_client.create_endpoint_config( EndpointConfigName=endpoint_config_name, # You will specify this name in a CreateEndpoint request. # List of ProductionVariant objects, one for each model that you want to host at this endpoint. ProductionVariants=[ { "VariantName": "variant1", # The name of the production variant. "ModelName": model_name, "InstanceType": instance_type, # Specify the compute instance type. "InitialInstanceCount": 1 # Number of instances to launch initially. } ] ) print(f"Created EndpointConfig: {endpoint_config_response['EndpointConfigArn']}")

在上述示例中,您可以指定以下密钥以下内容以下内容ProductionVariants字段:

SageMaker Python SDK

如果使用 SageMaker Python 开发工具包Model类。

部署模型

部署模型并创建 HTTPS 终端节点。

Amazon SDK for Python (Boto3)

向 SageMaker 提供端点配置。该服务会启动 ML 计算实例,并按照配置中的规定部署一个或多个模型。

拥有模型和终端节点配置后,请使用CreateEndpoint用于创建终端节点的 API。终端节点名称在Amazon您的区域Amazonaccount.

以下内容使用在请求中指定的终端节点配置创建终端节点。亚马逊 SageMaker 使用终端节点来配置资源和部署模型。

# The name of the endpoint. The name must be unique within an AWS Region in your AWS account. endpoint_name = '<endpoint-name>' # The name of the endpoint configuration associated with this endpoint. endpoint_config_name='<endpoint-config-name>' create_endpoint_response = sagemaker_client.create_endpoint( EndpointName=endpoint_name, EndpointConfigName=endpoint_config_name)

有关更多信息,请参阅 CreateEndpoint API。

SageMaker Python SDK

为终端节点定义名称。此为可选步骤。如果你不提供一个, SageMaker 将为你创建一个唯一的名字:

from datetime import datetime endpoint_name = f"DEMO-{datetime.utcnow():%Y-%m-%d-%H%M}" print("EndpointName =", endpoint_name)

使用模型对象的内置将模型部署到实时的 HTTPS 终端节点deploy()方法。在instance_type字段以及要在其上运行终端节点的实例的初始数量initial_instance_count字段:

initial_instance_count=<integer> # initial_instance_count=1 # Example instance_type='<instance-type>' # instance_type='ml.m4.xlarge' # Example model.deploy( initial_instance_count=initial_instance_count, instance_type=instance_type, endpoint_name=endpoint_name )