

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

# 将推理工作负载从 x86 迁移到 Graviton Amazon
<a name="realtime-endpoints-graviton"></a>

 [Amazon Graviton](https://www.amazonaws.cn/ec2/graviton/) 是一系列基于 ARM 的处理器，由. Amazon它们比基于 x86 的处理器更节能，并且提供极具吸引力的性价比。Amazon SageMaker AI 提供基于 Graviton 的实例，因此您可以利用这些高级处理器来满足您的推理需求。

 您可以使用兼容 ARM 的容器映像或多架构容器映像，将现有的推理工作负载从基于 x86 的实例迁移到基于 Graviton 的实例。本指南假设您使用 [Amazon 深度学习容器映像](https://github.com/aws/deep-learning-containers/blob/master/available_images.md)，或者使用您自己的兼容 ARM 的容器映像。有关构建您自己的映像的更多信息，请查看[构建您的映像](https://github.com/aws/deep-learning-containers#building-your-image)。

 概括来说，将推理工作负载从基于 x86 的实例迁移到基于 Graviton 的实例需要以下四个步骤：

1. 将容器映像推送到亚马逊弹性容器注册表 (Amazon ECR) Container Registry（ Amazon 一个托管容器注册表）。

1. 创建 A SageMaker I 模型。

1. 创建端点配置。

1. 创建端点。

 本指南的以下部分提供了有关上述步骤的更多详细信息。将代码示例{{user placeholder text}}中的替换为您自己的信息。

**Topics**
+ [将容器映像推送到 Amazon ECR](#realtime-endpoints-graviton-ecr)
+ [创建 A SageMaker I 模型](#realtime-endpoints-graviton-model)
+ [创建端点配置](#realtime-endpoints-graviton-epc)
+ [创建 端点](#realtime-endpoints-graviton-ep)

## 将容器映像推送到 Amazon ECR
<a name="realtime-endpoints-graviton-ecr"></a>

 您可以使用将容器映像推送到 Amazon ECR。 Amazon CLI使用兼容 ARM 的映像时，请验证该映像是否支持 ARM 架构：

```
docker inspect {{deep-learning-container-uri}}
```

 如果响应 `"Architecture": "arm64"`，则表明该映像支持 ARM 架构。您可以使用 `docker push` 命令将其推送至 Amazon ECR。有关更多信息，请查看[推送 Docker 映像](https://docs.amazonaws.cn/AmazonECR/latest/userguide/docker-push-ecr-image.html)。

 从本质上讲，多架构容器映像是一组支持不同架构或操作系统的容器映像，您可以用通用的清单名称来引用这些映像。如果您使用的是多架构容器映像，那么除了将映像推送到 Amazon ECR 之外，您还必须将清单列表推送到 Amazon ECR。清单列表允许嵌套包含其他映像清单，其中包含的每个映像均由架构、操作系统和其他平台属性指定。以下示例创建了一个清单列表，并将其推送到 Amazon ECR。

1. 创建清单列表。

   ```
   docker manifest create {{aws-account-id}}.dkr.ecr.{{aws-region}}.amazonaws.com/{{my-repository}} \
     {{aws-account-id}}.dkr.ecr.{{aws-account-id}}.amazonaws.com/{{my-repository:amd64}} \
   	{{aws-account-id}}.dkr.ecr.{{aws-account-id}}.amazonaws.com/{{my-repository:arm64}} \
   ```

1.  为清单列表添加注释，使其正确识别哪个映像适用于哪个架构。

   ```
   docker manifest annotate --arch arm64 {{aws-account-id}}.dkr.ecr.{{aws-region}}.amazonaws.com/{{my-repository}} \
     {{aws-account-id}}.dkr.ecr.{{aws-region}}.amazonaws.com/{{my-repository:arm64}}
   ```

1. 推送此清单。

   ```
   docker manifest push {{aws-account-id}}.dkr.ecr.{{aws-region}}.amazonaws.com/{{my-repository}}
   ```

 有关创建清单列表并将其推送到 Amazon ECR 的更多信息，请参阅[适用于 Amazon ECR 的多架构容器映像简介](https://www.amazonaws.cn/blogs/containers/introducing-multi-architecture-container-images-for-amazon-ecr/)和[推送多架构映像](https://docs.amazonaws.cn/AmazonECR/latest/userguide/docker-push-multi-architecture-image.html)。

## 创建 A SageMaker I 模型
<a name="realtime-endpoints-graviton-model"></a>

 通过调用 [https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateModel.html](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateModel.html)AP SageMaker I 创建 AI 模型。

```
import boto3
from sagemaker import get_execution_role


aws_region = "{{aws-region}}"
sagemaker_client = boto3.client("sagemaker", region_name=aws_region)

role = get_execution_role()

sagemaker_client.create_model(
    ModelName = "{{model-name}}",
    PrimaryContainer = {
        "Image": "{{deep-learning-container-uri}}",
        "ModelDataUrl": "{{model-s3-location}}",
        "Environment": {
            "SAGEMAKER_PROGRAM": "{{inference.py}}",
            "SAGEMAKER_SUBMIT_DIRECTORY": "{{inference-script-s3-location}}",
            "SAGEMAKER_CONTAINER_LOG_LEVEL": "20",
            "SAGEMAKER_REGION": aws_region,
        }
    },
    ExecutionRoleArn = role
)
```

## 创建端点配置
<a name="realtime-endpoints-graviton-epc"></a>

 通过调用 [https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateEndpointConfig.html](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateEndpointConfig.html) API 创建端点配置。有关基于 Graviton 的实例的列表，请查看[计算优化型实例](https://docs.amazonaws.cn/AWSEC2/latest/UserGuide/compute-optimized-instances.html)。

```
sagemaker_client.create_endpoint_config(
    EndpointConfigName = "{{endpoint-config-name}}",
    ProductionVariants = [
        {
            "VariantName": "{{variant-name}}",
            "ModelName": "{{model-name}}",
            "InitialInstanceCount": {{1}},
            "InstanceType": "{{ml.c7g.xlarge}}", # Graviton-based instance
       }
    ]
)
```

## 创建 端点
<a name="realtime-endpoints-graviton-ep"></a>

 通过调用 [https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateEndpoint.html](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateEndpoint.html) API 创建端点。

```
sagemaker_client.create_endpoint(
    EndpointName = "{{endpoint-name}}",
    EndpointConfigName = "{{endpoint-config-name}}"
)
```