

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

# 获取现有端点的推理推荐
<a name="inference-recommender-existing-endpoint"></a>

推理推荐作业对推荐的实例类型或现有端点运行一组负载测试。推理推荐作业使用的性能指标基于使用模型版本注册期间提供的示例数据执行的负载测试。

您可以对现有 SageMaker AI 推理端点进行基准测试并获取推理建议，以帮助您提高终端节点的性能。获取现有 SageMaker AI 推理端点的推荐的过程与[获取不带端点的推理建议](https://docs.aws.amazon.com/sagemaker/latest/dg/inference-recommender-instance-recommendation.html)的过程类似。在对现有端点进行基准测试时，要记下几个功能排除项：
+ 每个 Inference Recommender 作业只能使用一个现有端点。
+ 端点上只能有一个变体。
+ 您无法使用支持自动扩缩的端点。
+ 仅[实时推理](https://docs.aws.amazon.com/sagemaker/latest/dg/realtime-endpoints.html)支持此功能。
+ 此功能不支持[实时多模型端点](https://docs.aws.amazon.com/sagemaker/latest/dg/multi-model-endpoints.html)。

**警告**  
我们强烈建议您不要在处理实时流量的生产端点上运行 Inference Recommender 作业。基准测试期间的合成负载可能会影响您的生产端点并导致限制，或提供不准确的基准测试结果。我们建议您使用非生产端点或开发人员端点进行比较。

以下各节演示如何使用 Amazon SageMaker 推理推荐器，使用适用于 Python 的 AWS 软件开发工具包 (Boto3) 和，根据您的模型类型为现有终端节点创建推理建议。 AWS CLI

**注意**  
在创建 Inference Recommender 推荐作业之前，请先确保您满足[使用 Amazon SageMaker 推理推荐器的先决条件](inference-recommender-prerequisites.md)。

## 先决条件
<a name="inference-recommender-existing-endpoint-prerequisites"></a>

如果您还没有 A SageMaker I 推理终端节点，则可以在没有终端节点的情况下[获取推理建议](https://docs.aws.amazon.com/sagemaker/latest/dg/inference-recommender-instance-recommendation.html)，也可以按照创建终端节点[并部署模型中的说明创建实时推理端点](https://docs.aws.amazon.com/sagemaker/latest/dg/realtime-endpoints-deployment.html)。

## 为现有端点创建推理推荐作业
<a name="inference-recommender-existing-endpoint-create"></a>

使用 适用于 Python (Boto3) 的 AWS SDK或以编程方式创建推理建议。 AWS CLI指定推理建议的任务名称、现有 A SageMaker I 推理终端节点的名称、 AWS IAM 角色 ARN、输入配置以及您在模型注册表中注册模型时的模型包 ARN。

------
#### [ 适用于 Python (Boto3) 的 AWS SDK ]

使用 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateInferenceRecommendationsJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateInferenceRecommendationsJob.html) API 获取推理推荐。将推理推荐作业的 `JobType` 字段设置为 `'Default'`。此外，请提供以下各项：
+ 为 `JobName` 字段的 Inference Recommender 推理作业提供名称。推理推荐人任务名称在 AWS 区域内和您的 AWS 账户中必须是唯一的。
+ IAM 角色的 Amazon 资源名称 (ARN)，此角色可让 Inference Recommender 代表您执行任务。为 `RoleArn` 字段定义此项。
+ 在模型注册表中注册模型时创建的版本控制模型包的 ARN。在 `InputConfig` 字段中为 `ModelPackageVersionArn` 定义此项。
+ 在推理推荐器中提供要在现场进行基准测试的现有 SageMaker AI 推理端点`Endpoints`的名称。`InputConfig`

导入 适用于 Python (Boto3) 的 AWS SDK 软件包并使用客户端类创建 SageMaker AI 客户端对象。如果您已执行**先决条件**部分中的步骤，则模型包组 ARN 已存储在名为 `model_package_arn` 的变量中。

```
# Create a low-level SageMaker service client.
import boto3
aws_region = '<region>'
sagemaker_client = boto3.client('sagemaker', region_name=aws_region) 

# Provide your model package ARN that was created when you registered your 
# model with Model Registry 
model_package_arn = '<model-package-arn>'

# Provide a unique job name for SageMaker Inference Recommender job
job_name = '<job-name>'

# Inference Recommender job type. Set to Default to get an initial recommendation
job_type = 'Default'

# Provide an IAM Role that gives SageMaker Inference Recommender permission to 
# access AWS services
role_arn = '<arn:aws:iam::<account>:role/*>'
                                    
# Provide endpoint name for your endpoint that want to benchmark in Inference Recommender
endpoint_name = '<existing-endpoint-name>'

sagemaker_client.create_inference_recommendations_job(
    JobName = job_name,
    JobType = job_type,
    RoleArn = role_arn,
    InputConfig = {
        'ModelPackageVersionArn': model_package_arn,
        'Endpoints': [{'EndpointName': endpoint_name}]
    }
)
```

有关您可以传递的可选参数和必填参数的完整列表，请参阅 [Amazon SageMaker API 参考指南[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateInferenceRecommendationsJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateInferenceRecommendationsJob.html)](https://docs.aws.amazon.com/sagemaker/latest/APIReference/Welcome.html)。

------
#### [ AWS CLI ]

使用 `create-inference-recommendations-job` API 以获取实例端点推荐。为实例端点推荐作业将 `job-type` 字段设置为 `'Default'`。此外，请提供以下各项：
+ 为 `job-name` 字段的 Inference Recommender 推理作业提供名称。推理推荐人任务名称在 AWS 区域内和您的 AWS 账户中必须是唯一的。
+ 允许亚马逊 SageMaker 推理推荐人代表您执行任务的 IAM 角色的亚马逊资源名称 (ARN)。为 `role-arn` 字段定义此项。
+ 在模型注册表中注册模型时创建的版本控制模型包的 ARN。在 `input-config` 字段中为 `ModelPackageVersionArn` 定义此项。
+ 在推理推荐器中提供要在现场进行基准测试的现有 SageMaker AI 推理端点`Endpoints`的名称。`input-config`

```
aws sagemaker create-inference-recommendations-job 
    --region <region>\
    --job-name <job_name>\
    --job-type Default\
    --role-arn arn:aws:iam::<account:role/*>\
    --input-config "{
        \"ModelPackageVersionArn\": \"arn:aws:sagemaker:<region:account:role/*>\",
        \"Endpoints\": [{\"EndpointName\": <endpoint_name>}]
        }"
```

------

## 获取推理推荐作业结果
<a name="inference-recommender-existing-endpoint-results"></a>

您可以使用与标准推理推荐作业相同的过程以编程方式收集您的推理推荐作业的结果。有关更多信息，请参阅 [获取推理推荐作业结果](instance-recommendation-results.md)。

当您获得现有端点的推理推荐作业结果时，您将收到类似于以下内容的 JSON 响应：

```
{
    "JobName": "job-name",
    "JobType": "Default",
    "JobArn": "arn:aws:sagemaker:region:account-id:inference-recommendations-job/resource-id",
    "RoleArn": "iam-role-arn",
    "Status": "COMPLETED",
    "CreationTime": 1664922919.2,
    "LastModifiedTime": 1664924208.291,
    "InputConfig": {
        "ModelPackageVersionArn": "arn:aws:sagemaker:region:account-id:model-package/resource-id",
        "Endpoints": [
            {
                "EndpointName": "endpoint-name"
            }
        ]
    },
    "InferenceRecommendations": [
        {
            "Metrics": {
                "CostPerHour": 0.7360000014305115,
                "CostPerInference": 7.456940238625975e-06,
                "MaxInvocations": 1645,
                "ModelLatency": 171
            },
            "EndpointConfiguration": {
                "EndpointName": "sm-endpoint-name",
                "VariantName": "variant-name",
                "InstanceType": "ml.g4dn.xlarge",
                "InitialInstanceCount": 1
            },
            "ModelConfiguration": {
                "EnvironmentParameters": [
                    {
                        "Key": "TS_DEFAULT_WORKERS_PER_MODEL",
                        "ValueType": "string",
                        "Value": "4"
                    }
                ]
            }
        }
    ],
    "EndpointPerformances": [
        {
            "Metrics": {
                "MaxInvocations": 184,
                "ModelLatency": 1312
            },
            "EndpointConfiguration": {
                "EndpointName": "endpoint-name"
            }
        }
    ]
}
```

前几行提供了有关推理推荐作业本身的信息。这包括作业名称、角色 ARN 以及创建时间和最新修改时间。

`InferenceRecommendations` 字典包含 Inference Recommender 推理推荐的列表。

`EndpointConfiguration`嵌套字典包含实例类型 (`InstanceType`) 建议以及推荐作业期间使用的端点和变体名称（已部署的 AWS 机器学习模型）。

`Metrics`嵌套字典包含有关实时终端节点每小时的估计成本 (`CostPerHour`)、实时终端节点的每次推理的估计成本 (`CostPerInference`)（以美元计）、发送到终端节点的每分钟预期最大`InvokeEndpoint`请求数 (`MaxInvocations`) 以及模型延迟 (`ModelLatency`)（即模型响应 AI 所花费的时间间隔（以毫秒为单位）的信息。 SageMaker 模型延迟包括发送请求以及从模型容器提取响应所花费的本地通信时间，以及在容器中完成推理所用的时间。

`EndpointPerformances` 嵌套字典包含运行推荐作业的现有端点的名称 (`EndpointName`) 和您的端点的性能指标（`MaxInvocations` 和 `ModelLatency`）。