

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

# 使用 Amazon SageMaker 推理推荐器的先决条件
先决条件

在使用 Amazon SageMaker 推理推荐器之前，您必须完成先决条件步骤。例如，我们展示了如何使用 PyTorch (v1.7.1) ResNet -18 预训练模型来处理这两种类型的 Amazon SageMaker Inference 推荐器推荐任务。显示的示例使用 适用于 Python (Boto3) 的 AWS SDK。

**注意**  
以下代码示例使用 Python。如果在终端或 AWS CLI中运行以下任意代码示例，请删除 `!` 前缀字符。
你可以在亚马逊 SageMaker Studio 笔记本中使用 Python 3（TensorFlow 2.6 Python 3.8 CPU 优化）内核运行以下示例。有关 Studio 的更多信息，请参阅 [亚马逊 SageMaker Studio](studio-updated.md)。

1. **为 Amazon A SageMaker I 创建 IAM 角色。**

   为附加 IAM 托管策略的 SageMaker Amazon AI 创建`AmazonSageMakerFullAccess`一个 IAM 角色。

1. **设置环境。**

   为您的 AWS 区域、您的 A SageMaker I IAM 角色（从步骤 1 开始）和 A SageMaker I 客户端导入依赖关系并创建变量。

   ```
   !pip install --upgrade pip awscli botocore boto3  --quiet
   from sagemaker import get_execution_role, Session, image_uris
   import boto3
   
   region = boto3.Session().region_name
   role = get_execution_role()
   sagemaker_client = boto3.client("sagemaker", region_name=region)
   sagemaker_session = Session()
   ```

1. **（可选）查看已由 Inference Recommender 进行基准测试的现有模型。**

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

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

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

   ```
   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'
   ```

1. **将机器学习模型上传到 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 AI 要求将经过预训练的机器学习模型打包为压缩的 TAR 文件 (`*.tar.gz`)。压缩模型和推理脚本以满足此要求：

   ```
   !tar -czf test.tar.gz model.pth code/inference.py
   ```

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

   将模型和模型构件压缩为 `.tar.gz` 文件后，请将其上传到 Amazon S3 存储桶。以下示例演示了如何使用 AWS CLI将您的模型上传到 Amazon S3：

   ```
   !aws s3 cp test.tar.gz s3://{your-bucket}/models/
   ```

1. **选择预构建的 Docker 推理映像或创建自己的推理 Docker 映像。**

   SageMaker AI 为其内置算法提供容器，为一些最常见的机器学习框架（例如 Apache MXNet、、和 Chainer）提供预构建的 Docker 镜像。 TensorFlow PyTorch有关可用 SageMaker AI 镜像的完整列表，请参阅[可用的 Deep Learning Containers 镜像](https://github.com/aws/deep-learning-containers/blob/master/available_images.md)。

   如果现有的 SageMaker AI 容器都不能满足您的需求，并且您没有自己的现有容器，请创建新的 Docker 镜像。请参阅[具有自定义推理代码的容器](your-algorithms-inference-main.md)以了解有关如何创建 Docker 映像的信息。

   以下内容演示如何使用 Pyth SageMaker on 软件开发工具包检索 1.7.1 PyTorch 版本的推理图像：

   ```
   from sagemaker import image_uris
   
   ## 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, 
                                   version=framework_version, 
                                   py_version='py3', 
                                   instance_type=instance_type, 
                                   image_scope='inference')
   ```

   有关可用 SageMaker AI 实例的列表，请参阅 A [mazon A SageMaker I 定价](https://aws.amazon.com/sagemaker/pricing/)。

1. **创建示例负载存档。**

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

   以下内容下载了一张.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}"
   ```

1. **为推荐作业准备模型输入**

   对于最后一个先决条件，为您提供了两个选项来准备模型输入。您可以在 Model Registry 中注册 SageMaker 模型，使用该注册表对模型进行编目以供生产，也可以创建 A SageMaker I 模型并在创建推荐作业时在`ContainerConfig`字段中进行指定。如果您想利用[模型注册表](https://docs.aws.amazon.com/sagemaker/latest/dg/model-registry.html)提供的功能，例如管理模型版本和自动部署模型，那么最好是选择第一个选项。如果您想快速开始操作，那么第二个选项是理想之选。对于第一个选项，请转到步骤 7。对于第二个选项，请跳过步骤 7 并转到步骤 8。

1. **选项 1：将模型注册到模型注册表中**

   借助 SageMaker Model Registry，您可以对生产模型进行编目、管理模型版本、将元数据（例如训练指标）与模型关联起来、管理模型的批准状态、将模型部署到生产环境以及使用 CI/CD 自动部署模型。

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

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

   有关如何以编程方式[创建模型组](model-registry-model-group.md)和[注册模型版本](model-registry-version.md)交互方式创建模型包组以及如何使用和 适用于 Python (Boto3) 的 AWS SDK Ama SageMaker zon Studio Classic 分别创建版本控制模型包组的详细说明，请参阅和。

   以下代码示例演示了如何使用 适用于 Python (Boto3) 的 AWS SDK创建版本控制模型包。
**注意**  
您无需批准模型包即可创建 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 参考指南[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelPackageGroup.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelPackageGroup.html)](https://docs.aws.amazon.com/sagemaker/latest/APIReference/Welcome.html)。

      通过指定运行推理代码和模型构件的 Amazon S3 位置的 Docker 映像来创建模型包，并提供 `InferenceSpecification`。`InferenceSpecification` 应包含有关可以与基于此模型包的模型一起运行的推理作业的信息，包括以下内容：
      + 运行您的推理代码的映像的 Amazon ECR 路径。
      + （可选）模型包支持用于转换作业和推理的实时端点的实例类型。
      + 模型包支持的用于推理的输入和输出内容格式。

      此外，在创建模型包时，必须指定以下参数：
      + [域](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelPackage.html#sagemaker-CreateModelPackage-request-Domain)：模型包及其组件的机器学习域。常见的机器学习域包括计算机视觉和自然语言处理。
      + [任务](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelPackage.html#sagemaker-CreateModelPackage-request-Task)：模型包完成的机器学习任务。常见的机器学习任务包括对象检测和图像分类。如果 [API 参考指南](https://docs.aws.amazon.com/sagemaker/latest/APIReference/Welcome.html)中列出的任务均无法满足您的使用案例，请指定“其他”。有关支持的机器学习任务的列表，请参阅[任务](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelPackage.html#sagemaker-CreateModelPackage-request-Task) API 字段描述。
      + [SamplePayloadUrl](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelPackage.html#sagemaker-CreateModelPackage-request-SamplePayloadUrl)：存储示例负载的亚马逊简单存储服务 (Amazon S3) Service 路径。此路径必须指向单个 GZIP 压缩 TAR 归档文件（.tar.gz 后缀）。
      + [框架](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ModelPackageContainerDefinition.html#sagemaker-Type-ModelPackageContainerDefinition-Framework)：模型包容器映像的机器学习框架。
      + [FrameworkVersion](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ModelPackageContainerDefinition.html#sagemaker-Type-ModelPackageContainerDefinition-FrameworkVersion)：模型包容器镜像的框架版本。

      如果您提供实例类型的允许列表以用于实时生成推断 [SupportedRealtimeInferenceInstanceTypes](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_InferenceSpecification.html#sagemaker-Type-InferenceSpecification-SupportedRealtimeInferenceInstanceTypes)，则推断推荐器会在作业期间限制实例类型的搜索空间。`Default`如果您有预算限制，或知道有一组特定的实例类型可以支持您的模型和容器映像，请使用此参数。

      在前面的步骤中，我们下载了一个预训练的 ResNet 18 模型，并将其存储在 Amazon S3 存储桶中的一个名`models`为的目录中。我们检索了一张 PyTorch (v1.7.1) 深度学习容器推理图像，并将 URI 存储在一个名为的变量中。`image_uri`在以下代码示例中使用这些变量定义了用作输入的字典 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelPackage.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModelPackage.html) 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]
              }
          }
      ```

   1. **创建模型包**

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

      ```
      model_package_response = sagemaker_client.create_model_package(**model_package_input_dict)
      ```

      您需要模型包 ARN 才能使用 Amazon SageMaker 推理推荐器。记下模型包的 ARN 或将它存储在变量中：

      ```
      model_package_arn = model_package_response["ModelPackageArn"]
      
      print('ModelPackage Version ARN : {}'.format(model_package_arn))
      ```

1. **选项 2：创建模型并配置 `ContainerConfig` 字段**

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

   1. **创建模型**

      使用 `CreateModel` API 创建模型。有关在将模型部署到 SageMaker AI Hosting 时调用此方法的示例，请参阅[创建模型 (适用于 Python (Boto3) 的 AWS SDK)](https://docs.aws.amazon.com/sagemaker/latest/dg/realtime-endpoints-deployment.html#realtime-endpoints-deployment-create-model)。

      在前面的步骤中，我们下载了一个预训练的 ResNet 18 模型，并将其存储在 Amazon S3 存储桶中的一个名`models`为的目录中。我们检索了一张 PyTorch (v1.7.1) 深度学习容器推理图像，并将 URI 存储在一个名为的变量中。`image_uri`我们在下面的代码示例中使用这些变量，其中我们定义了用作 `[CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html#sagemaker-CreateModel-request-ModelName)` 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,
          })
      ```

   1. **配置 `ContainerConfig` 字段**

      接下来，必须使用刚才创建的模型配置该[ContainerConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_RecommendationJobInputConfig.html#sagemaker-Type-RecommendationJobInputConfig-ContainerConfig)字段，并在其中指定以下参数：
      + `Domain`：模型及其组件的机器学习域，例如计算机视觉或自然语言处理。
      + `Task`：模型完成的机器学习任务，例如图像分类或对象检测。
      + `PayloadConfig`：推荐作业的负载的配置。有关子字段的更多信息，请参阅 `[RecommendationJobPayloadConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_RecommendationJobPayloadConfig.html#sagemaker-Type-RecommendationJobPayloadConfig-SamplePayloadUrl)`。
      + `Framework`: 容器镜像的机器学习框架，例如 PyTorch。
      + `FrameworkVersion`：容器映像的框架版本。
      + （可选）`SupportedInstanceTypes`：用于实时生成推理的实例类型的列表。

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

      在下面的代码示例中，我们使用之前定义的参数以及 `NearestModelName` 来定义用作 `[CreateInferenceRecommendationsJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateInferenceRecommendationsJob.html)` 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,
              }
      ```