

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

# 取得現有端點的推論建議
<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 Inference Recommender，根據使用適用於 Python 的 AWS SDK (Boto3) 和 的模型類型，為現有端點建立推論建議 AWS CLI。

**注意**  
在建立 Inference Recommender 建議任務之前，請確定您已符合 [使用 Amazon SageMaker Inference Recommender 的先決條件](inference-recommender-prerequisites.md)。

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

如果您還沒有 SageMaker AI 推論端點，則可以在沒有端點的情況下[取得推論建議](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。為您的推論建議指定任務名稱、現有 SageMaker AI 推論端點的名稱、IAM AWS 角色 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 建議任務提供名稱。Inference Recommender 任務名稱在 AWS 區域和您的帳戶 AWS 內必須是唯一的。
+ IAM 角色的 Amazon Resource Name (ARN)，可讓 Inference Recommender 代表您執行任務。為 `RoleArn` 欄位定義此項目。
+ 您在模型註冊表中註冊模型時所建立的版本化模型套件之 ARN。在 `InputConfig` 欄位中為 `ModelPackageVersionArn` 定義此項目。
+ 在 `InputConfig` 欄位中提供您要為 `Endpoints` 在 Inference Recommender 中進行基準測試的現有 SageMaker AI 推論端點的名稱。

匯入 適用於 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/Welcome.html)，以取得可傳遞給 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateInferenceRecommendationsJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateInferenceRecommendationsJob.html) 的選用和必要引數之完整清單。

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

使用 `create-inference-recommendations-job` API 取得執行個體端點建議。將執行個體端點建議任務的 `job-type` 欄位設為 `'Default'`。此外，請提供下列項目：
+ 為 `job-name` 欄位的 Inference Recommender 建議任務提供名稱。Inference Recommender 任務名稱在 AWS 區域和您的帳戶 AWS 內必須是唯一的。
+ IAM 角色的 Amazon Resource Name (ARN)，可讓 Amazon SageMaker Inference Recommender 代表您執行任務。為 `role-arn` 欄位定義此項目。
+ 您在模型註冊表中註冊模型時所建立的版本化模型套件之 ARN。在 `input-config` 欄位中為 `ModelPackageVersionArn` 定義此項目。
+ 在 `input-config` 欄位中提供您要為 `Endpoints` 在 Inference Recommender 中進行基準測試的現有 SageMaker AI 推論端點的名稱。

```
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`)，這是模型回應 SageMaker AI 所花費的時間間隔 (以毫秒為單位) 等資訊。模型延遲包含傳送請求和從模型容器擷取回應的本機通訊時間，以及在容器中完成推論的時間。

`EndpointPerformances` 巢狀字典包含執行建議任務的現有端點名稱 (`EndpointName`) 以及端點的效能指標 (`MaxInvocations` 和 `ModelLatency`)。