先决条件 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

先决条件

要使用 Amazon SageMaker Inference Recommender,请首先确保您满足下面列出的先决条件。例如,我们展示了如何将 PyTorch (v1.7.1) ResNet-18 预训练模型用于这两种类型的 Amazon SageMaker Inference Recommender 推荐作业。

注意
  • 以下代码示例使用 Python。如果在终端或 Amazon CLI 中运行以下任意代码示例,请删除 ! 前缀字符。

  • 此示例使用 Amazon SageMaker 笔记本实例中的 conda_pytorch_p36_latest 内核。该内核由 SageMaker 提供,它使用的是 Python 3.6 和 PyTorch 1.7.1。有关 SageMaker 笔记本的更多信息,请参阅 Amazon SageMaker 笔记本实例

  1. 为 Amazon SageMaker 创建 IAM 角色。

    为已附加 AmazonSageMakerFullAccess IAM 托管策略的 Amazon SageMaker 创建 IAM 角色。

  2. (可选)查看已由 Inference Recommender 进行基准测试的现有模型。

    来自常用模型库的 Inference Recommender 基准模型。Inference Recommender 为您的模型提供支持,即使它尚未进行基准测试也是如此。

    使用 ListModelMetaData 获取一个响应对象,其中列出了常见模型库中包含的机器学习模型的域、框架、任务和模型名称。

    您可以在后续步骤中使用域、框架、框架版本、任务和模型名称来推理 Docker 映像并将模型注册到 SageMaker 模型注册表中。下面演示了如何使用适用于 Python 的 SDK (Boto3) 列出模型元数据:

    import boto3 aws_region="<INSERT>" sagemaker_client = boto3.client("sagemaker", aws_region) list_model_metadata_response=sagemaker_client.list_model_metadata()

    输出包括模型摘要 (ModelMetadataSummaries) 和响应元数据 (ResponseMetadata),类似于以下内容:

    { 'ModelMetadataSummaries': [{ 'Domain': 'NATURAL_LANGUAGE_PROCESSING', 'Framework': 'PYTORCH:1.6.0', 'Model': 'bert-base-cased', 'Task': 'FILL_MASK' }, { 'Domain': 'NATURAL_LANGUAGE_PROCESSING', 'Framework': 'PYTORCH:1.6.0', 'Model': 'bert-base-uncased', 'Task': 'FILL_MASK' }, { 'Domain': 'COMPUTER_VISION', 'Framework': 'MXNET:1.8.0', 'Model': 'resnet18v2-gluon', 'Task': 'IMAGE_CLASSIFICATION' }, { 'Domain': 'COMPUTER_VISION', 'Framework': 'PYTORCH:1.6.0', 'Model': 'resnet152', 'Task': 'IMAGE_CLASSIFICATION' }], 'ResponseMetadata': { 'HTTPHeaders': { 'content-length': '2345', 'content-type': 'application/x-amz-json-1.1', 'date': 'Tue, 19 Oct 2021 20:52:03 GMT', 'x-amzn-requestid': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }, 'HTTPStatusCode': 200, 'RequestId': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'RetryAttempts': 0 } }

    在此演示中,我们使用 PyTorch (v1.7.1) ResNet-18 模型来执行图像分类。以下 Python 代码示例将框架、框架版本、域和任务存储到变量中以便以后使用:

    # ML framework details framework = 'PYTORCH' framework_version = '1.7.1' # ML model details ml_domain = 'COMPUTER_VISION' ml_task = 'IMAGE_CLASSIFICATION'
  3. 将机器学习模型上传到 Amazon S3。

    如果您没有预先训练的机器学习模型,请使用此 PyTorch (v1.7.1) ResNet-18 模型:

    # Optional: Download a sample PyTorch model import torch from torchvision import models, transforms, datasets # Create an example input for tracing image = torch.zeros([1, 3, 256, 256], dtype=torch.float32) # Load a pretrained resnet18 model from TorchHub model = models.resnet18(pretrained=True) # Tell the model we are using it for evaluation (not training). Note this is required for Inferentia compilation. model.eval() model_trace = torch.jit.trace(model, image) # Save your traced model model_trace.save('model.pth')

    下载示例推理脚本 inference.py。创建 code 目录并将推理脚本移至 code 目录。

    # Download the inference script !wget https://aws-ml-blog-artifacts.s3.us-east-2.amazonaws.com/inference.py # move it into a code/ directory !mkdir code !mv inference.py code/

    Amazon SageMaker 要求将预训练的机器学习模型打包为压缩的 TAR 文件 (*.tar.gz)。压缩模型以满足此要求:

    !tar -czf test.tar.gz model.pth resnet18.py

    预置端点时,会将存档中的文件提取到端点上的 /opt/ml/model/

    将模型和模型构件压缩为 .tar.gz 文件后,请将其上传到 Amazon S3 存储桶。下面演示了如何使用 Amazon CLI 将模型上传到 Amazon S3:

    !aws s3 cp test.tar.gz s3://{your-bucket}/models/
  4. 选择预构建的 Docker 推理映像或创建自己的推理 Docker 映像。

    SageMaker 为其内置算法提供容器,并为一些最常见的机器学习框架(如 Apache MXNet、TensorFlow、PyTorch 和 Chainer)提供预构建的 Docker 映像。有关可用 SageMaker 映像的完整列表,请参阅可用的深度学习容器映像

    如果现有的 SageMaker 容器都不能满足您的需求,并且您没有自己的现有容器,请创建一个新的 Docker 映像。请参阅使用您自己的推理代码以了解有关如何创建 Docker 映像的信息。

    下面演示了如何使用 SageMaker Python SDK 检索 PyTorch 1.7.1 版推理映像:

    from sagemaker import image_uris aws_region="<aws_region>" ## Uncomment and replace with your own values if you did not define ## these variables a previous step. #framework = 'PYTORCH' #framework_version = '1.7.1' # Note: you can use any CPU-based instance here, # this is just to set the arch as CPU for the Docker image instance_type = 'ml.m5.2xlarge' image_uri = image_uris.retrieve(framework, region=aws_region, version=framework_version, py_version='py3', instance_type=instance_type, image_scope='inference')

    有关可用的 SageMaker 实例的列表,请参阅 Amazon SageMaker 定价

  5. 创建示例负载存档。

    创建一个存档,其中包含负载测试工具可发送到 SageMaker 端点的各个文件。您的推理代码必须能够从示例负载中读取文件格式。

    下面将下载一张 .jpg 图像,此示例在稍后的步骤中将其用于 ResNet-18 模型。

    !wget https://cdn.pixabay.com/photo/2020/12/18/05/56/flowers-5841251_1280.jpg

    将示例有效载荷作为 tarball 压缩:

    !tar -cvzf payload.tar.gz flowers-5841251_1280.jpg

    将示例负载上传到 Amazon S3 并记下 Amazon S3 URI:

    !aws s3 cp payload.tar.gz s3://{bucket}/models/

    您需要在后面的步骤中使用 Amazon S3 URI,因此,请将它存储在变量中:

    bucket_prefix='models' bucket = '<your-bucket-name>' # Provide the name of your S3 bucket payload_s3_key = f"{bucket_prefix}/payload.tar.gz" sample_payload_url= f"s3://{bucket}/{payload_s3_key}"
  6. 为推荐作业准备模型输入

    对于最后一个先决条件,为您提供了两个选项来准备模型输入。您可以将模型注册到 SageMaker 模型注册表中,并使用该注册表对模型进行编目以用于生产;也可以创建一个 SageMaker 模型,并在创建推荐作业时在 ContainerConfig 字段中指定该模型。如果您想利用模型注册表提供的功能,例如管理模型版本和自动部署模型,那么最好是选择第一个选项。如果您想快速开始操作,那么第二个选项是理想之选。对于第一个选项,请转到步骤 7。对于第二个选项,请跳过步骤 7 并转到步骤 8。

  7. 选项 1:将模型注册到模型注册表中

    使用 SageMaker 模型注册表,您可以对模型进行编目以用于生产、管理模型版本、将元数据(例如训练指标)与模型关联、管理模型的审批状态、将模型部署到生产环境以及使用 CI/CD 自动实施模型部署。

    在使用 SageMaker 模型注册表来跟踪和管理模型时,它们将在模型包组中显示为版本控制模型包。未受版本控制的模型包不是模型组的一部分。模型包组包含模型的多个版本或迭代。尽管不需要为注册表中的每个模型创建它们,但它们有助于组织各种具有相同用途的模型并提供自动版本控制。

    要使用 Amazon SageMaker Inference Recommender,您必须拥有版本控制的模型包。您可以使用 Amazon SDK for Python (Boto3) 或 Amazon SageMaker Studio 以编程方式创建版本控制模型包。要以编程方式创建版本控制模型包,请首先使用 CreateModelPackageGroup API 创建模型包组。接下来,使用 CreateModelPackage API 创建模型包。调用此方法将生成版本控制模型包。

    请参阅创建模型组注册模型版本,查看有关如何以编程方式和交互方式创建模型包组,以及如何分别使用 Amazon SDK for Python (Boto3) 和 Amazon SageMaker Studio 创建版本控制模型包的详细说明。

    以下代码示例演示了如何使用 Amazon SDK for Python (Boto3) 创建版本控制模型包。

    注意

    您无需批准模型包即可创建 Inference Recommender 作业。

    1. 创建模型包组

      使用 CreateModelPackageGroup API 创建模型包组。为 ModelPackageGroupName 的模型包组提供名称,并可以选择在 ModelPackageGroupDescription 字段中提供模型包的描述。

      model_package_group_name = '<INSERT>' model_package_group_description = '<INSERT>' model_package_group_input_dict = { "ModelPackageGroupName" : model_package_group_name, "ModelPackageGroupDescription" : model_package_group_description, } model_package_group_response = sagemaker_client.create_model_package_group(**model_package_group_input_dict)

      请参阅 Amazon SageMaker API 参考指南,查看有关可传递给 CreateModelPackageGroup 的可选和必需参数的完整列表。

      通过指定运行推理代码和模型构件的 Amazon S3 位置的 Docker 映像来创建模型包,并提供 InferenceSpecification 的值。InferenceSpecification 应包含有关可以与基于此模型包的模型一起运行的推理作业的信息,包括以下内容:

      • 运行您的推理代码的映像的 Amazon ECR 路径。

      • (可选)模型包支持的实例类型,适用于用来推理的转换作业和实时端点。

      • 模型包支持的用于推断的输入和输出内容格式。

      此外,在创建模型包时,必须指定以下参数:

      • :模型包及其组件的机器学习域。常见的机器学习域包括计算机视觉和自然语言处理。

      • 任务:模型包完成的机器学习任务。常见的机器学习任务包括对象检测和图像分类。如果 API 参考指南中列出的任务均无法满足您的使用案例,请指定“其他”。有关支持的机器学习任务的列表,请参阅任务 API 字段描述。

      • SamplePayloadUrl:用于存储示例负载的 Amazon Simple Storage Service (Amazon S3) 路径。此路径必须指向单个 gzip 压缩 tar 存档文件(.tar.gz 后缀)。

      • 框架:模型包容器映像的机器学习框架。

      • FrameworkVersion:模型包容器映像的框架版本。

      如果您提供将用于实时生成 SupportedRealtimeInferenceInstanceTypes 的推理的实例类型的允许列表,则 Inference Recommender 将在 Default 作业期间限制实例类型的搜索空间。如果您有预算限制,或知道有一组特定的实例类型可以支持您的模型和容器映像,请使用此参数。

      在上一个步骤中,我们下载了预先训练的 ResNet18 模型,并将它存储在一个名为 models 的目录中的 Amazon S3 存储桶中。我们检索了 PyTorch (v1.7.1) 深度学习容器推理映像并将 URI 存储在一个名为 image_uri 的变量中。我们在下面的代码示例中使用这些变量,其中我们定义了用作 CreateModelPackage API 的输入的字典。

      # Provide the Amazon S3 URI of your compressed tarfile # so that Model Registry knows where to find your model artifacts bucket_prefix='models' bucket = '<your-bucket-name>' # Provide the name of your S3 bucket model_s3_key = f"{bucket_prefix}/test.tar.gz" model_url= f"s3://{bucket}/{model_s3_key}" # Similar open source model to the packaged model # The name of the ML model as standardized by common model zoos nearest_model_name = 'resnet18' # The supported MIME types for input and output data. In this example, # we are using images as input. input_content_type='image/jpeg' # Optional - provide a description of your model. model_package_description = '<INSERT>' ## Uncomment if you did not store the domain and task in an earlier ## step #ml_domain = 'COMPUTER_VISION' #ml_task = 'IMAGE_CLASSIFICATION' ## Uncomment if you did not store the framework and framework version ## in a previous step. #framework = 'PYTORCH' #framework_version = '1.7.1' # Optional: Used for optimizing your model using SageMaker Neo # PyTorch uses NCHW format for images data_input_configuration = "[[1,3,256,256]]" # Create a dictionary to use as input for creating a model pacakge group model_package_input_dict = { "ModelPackageGroupName" : model_package_group_name, "ModelPackageDescription" : model_package_description, "Domain": ml_domain, "Task": ml_task, "SamplePayloadUrl": sample_payload_url, "InferenceSpecification": { "Containers": [ { "Image": image_uri, "ModelDataUrl": model_url, "Framework": framework.upper(), "FrameworkVersion": framework_version, "NearestModelName": nearest_model_name, "ModelInput": {"DataInputConfig": data_input_configuration} } ], "SupportedContentTypes": [input_content_type] } }
    2. 创建模型包

      使用 CreateModelPackage API 创建模型包。传递上一步中定义的输入字典:

      model_package_response = sagemaker_client.create_model_package(**model_package_input_dict)

      您需要模型包 ARN 才能使用 Amazon SageMaker Inference Recommender。记下模型包的 ARN 或将它存储在变量中:

      model_package_arn = model_package_response["ModelPackageArn"] print('ModelPackage Version ARN : {}'.format(model_package_arn))
  8. 选项 2:创建模型并配置 ContainerConfig 字段

    如果您需要开始推理推荐作业,并且不需要在模型注册表中注册模型,请使用此选项。在以下步骤中,您将在 SageMaker 中创建模型,并将 ContainerConfig 字段配置为推荐作业的输入。

    1. 创建模型

      使用 CreateModel API 创建模型。有关在将模型部署到 SageMaker 主机时调用此方法的示例,请参阅创建模型 (Amazon SDK for Python (Boto3))

      在上一个步骤中,我们下载了预先训练的 ResNet18 模型,并将它存储在一个名为 models 的目录中的 Amazon S3 存储桶中。我们检索了 PyTorch (v1.7.1) 深度学习容器推理映像并将 URI 存储在一个名为 image_uri 的变量中。我们在下面的代码示例中使用这些变量,其中我们定义了用作 CreateModel API 的输入的字典。

      model_name = '<name_of_the_model>' # Role to give SageMaker permission to access AWS services. sagemaker_role= "arn:aws:iam::<region>:<account>:role/*" # Provide the Amazon S3 URI of your compressed tarfile # so that Model Registry knows where to find your model artifacts bucket_prefix='models' bucket = '<your-bucket-name>' # Provide the name of your S3 bucket model_s3_key = f"{bucket_prefix}/test.tar.gz" model_url= f"s3://{bucket}/{model_s3_key}" #Create model create_model_response = sagemaker_client.create_model( ModelName = model_name, ExecutionRoleArn = sagemaker_role, PrimaryContainer = { 'Image': image_uri, 'ModelDataUrl': model_url, })
    2. 配置 ContainerConfig 字段

      接下来,您必须使用刚刚创建的模型配置 ContainerConfig 字段,并在其中指定以下参数:

      • Domain:模型及其组件的机器学习域,例如计算机视觉或自然语言处理。

      • Task:模型完成的机器学习任务,例如图像分类或对象检测。

      • PayloadConfig:推荐作业的负载的配置。有关子字段的更多信息,请参阅 RecommendationJobPayloadConfig

      • Framework:容器映像的机器学习框架,例如 PyTorch。

      • FrameworkVersion:容器映像的框架版本。

      • (可选)SupportedInstanceTypes:用于实时生成推理的实例类型的列表。

      如果您使用 SupportedInstanceTypes 参数,则 Inference Recommender 会限制 Default 作业期间实例类型的搜索空间。如果您有预算限制,或知道有一组特定的实例类型可以支持您的模型和容器映像,请使用此参数。

      在下面的代码示例中,我们使用之前定义的参数以及 NearestModelName 来定义用作 CreateInferenceRecommendationsJob API 的输入的字典。

      ## Uncomment if you did not store the domain and task in a previous step #ml_domain = 'COMPUTER_VISION' #ml_task = 'IMAGE_CLASSIFICATION' ## Uncomment if you did not store the framework and framework version in a previous step #framework = 'PYTORCH' #framework_version = '1.7.1' # The name of the ML model as standardized by common model zoos nearest_model_name = 'resnet18' # The supported MIME types for input and output data. In this example, # we are using images as input input_content_type='image/jpeg' # Optional: Used for optimizing your model using SageMaker Neo # PyTorch uses NCHW format for images data_input_configuration = "[[1,3,256,256]]" # Create a dictionary to use as input for creating an inference recommendation job container_config = { "Domain": ml_domain, "Framework": framework.upper(), "FrameworkVersion": framework_version, "NearestModelName": nearest_model_name, "PayloadConfig": { "SamplePayloadUrl": sample_payload_url, "SupportedContentTypes": [ input_content_type ] }, "DataInputConfig": data_input_configuration "Task": ml_task, }