

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將推論工作負載從 x86 遷移至 AWS Graviton
<a name="realtime-endpoints-graviton"></a>

 [AWS Graviton](https://aws.amazon.com/ec2/graviton/) 是由 設計的一系列 ARM 型處理器 AWS。它們比基於 x86 的處理器更具能源效率，並且具有令人信服的性價比。Amazon SageMaker AI 提供以 Graviton 為基礎的執行個體，因此您可以利用這些進階處理器來滿足您的推論需求。

 您可以使用 ARM 相容容器映像或多架構容器映像，將現有的推論工作負載從 x86 型執行個體遷移到 Graviton 型的執行個體。本指南假設您使用的是 [AWS 深度學習容器映像檔](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 Elastic Container Registry (Amazon ECR) AWS 。

1. 建立 SageMaker AI 模型。

1. 建立端點組態。

1. 建立端點。

 本指南的以下各節提供有關上述步驟的更多詳細資訊。以您自己的資訊取代程式碼範例中的{{使用者預留位置文字}}。

**Topics**
+ [將容器映像推送到 Amazon ECR](#realtime-endpoints-graviton-ecr)
+ [建立 SageMaker AI 模型](#realtime-endpoints-graviton-model)
+ [建立一個端點組態](#realtime-endpoints-graviton-epc)
+ [建立端點](#realtime-endpoints-graviton-ep)

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

 您可以使用 將容器映像推送至 Amazon ECR AWS CLI。使用 ARM 相容映像時，請確認其支援 ARM 架構：

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

 回應 `"Architecture": "arm64"` 表示影像支援 ARM 架構。您可以使用 `docker push` 命令將它推送到 Amazon ECR。如需詳細資訊，請查看[推送 Docker 映像檔](https://docs.aws.amazon.com/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://docs.aws.amazon.com/AmazonECR/latest/userguide/docker-push-multi-architecture-image.html)送資訊清單的詳細資訊，請參閱適用於 Amazon ECR 的多架構映像](https://aws.amazon.com/blogs/containers/introducing-multi-architecture-container-images-for-amazon-ecr/)簡介。

## 建立 SageMaker AI 模型
<a name="realtime-endpoints-graviton-model"></a>

 透過呼叫 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html) API 建立 SageMaker 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.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpointConfig.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpointConfig.html) API 來建立端點組態。如需以 Graviton 為基礎的執行個體清單，請勾選[運算最佳化執行個體](https://docs.aws.amazon.com/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.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpoint.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpoint.html) API 建立端點。

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