

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

# 取得自動擴展政策的建議
<a name="inference-recommender-autoscaling"></a>

使用 Amazon SageMaker Inference Recommender，您可以根據預期的流量模式取得 SageMaker AI 端點自動擴展政策的建議。如果您已經完成推論建議任務，則可以提供任務的詳細資料，以取得可套用至端點之自動擴展政策的建議。

Inference Recommender 會針對每個指標進行不同的基準測試，以決定適用於端點的理想自動擴展組態。自動擴展建議會針對在推論建議任務中定義的每個指標，傳回建議的自動擴展政策。您可以儲存政策，並使用 [PutScalingPolicy](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PutScalingPolicy.html) API 將其套用至您的端點。

若要開始使用，請檢閱下列先決條件。

## 先決條件
<a name="inference-recommender-autoscaling-prereqs"></a>

在開始之前，您必須已經完成成功的推論建議任務。在下一節中，您可以提供在推論建議任務期間進行基準測試的推論建議 ID 或 SageMaker AI 端點的名稱。

若要擷取建議任務 ID 或端點名稱，您可以在 SageMaker AI 主控台中檢視推論建議任務的詳細資料，也可以使用 `RecommendationId` 或 [DescribeInferenceRecommendationsJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeInferenceRecommendationsJob.html) API 傳回的 `EndpointName` 欄位。

## 建立自動擴展設定建議
<a name="inference-recommender-autoscaling-create"></a>

若要建立自動擴展建議政策，您可以使用 適用於 Python (Boto3) 的 AWS SDK。

以下範例顯示 [GetScalingConfigurationRecommendation](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_GetScalingConfigurationRecommendation.html) API 的欄位。當您呼叫 API 時，請使用下列欄位：
+ `InferenceRecommendationsJobName` — 輸入推論建議任務的名稱。
+ `RecommendationId` — 輸入建議任務中推論建議的 ID。如果您已指定 `EndpointName` 欄位，這將為選用項目。
+ `EndpointName` — 輸入在推論建議任務期間進行基準測試的端點名稱。如果您已指定 `RecommendationId` 欄位，這將為選用項目。
+ `TargetCpuUtilizationPerCore` — (選用) 輸入您希望端點上執行個體在自動擴展之前使用多少使用率的百分比值。如未指定此欄位，預設值則為 50%。
+ `ScalingPolicyObjective` — (選用) 您可以在其中指定預期流量模式的物件。
  + `MinInvocationsPerMinute` — (選用) 每分鐘對端點的預期請求數量下限。
  + `MaxInvocationsPerMinute` — (選用) 每分鐘對端點的預期請求數目上限。

```
{
    "InferenceRecommendationsJobName": "string", // Required
    "RecommendationId": "string", // Optional, provide one of RecommendationId or EndpointName
    "EndpointName": "string", // Optional, provide one of RecommendationId or EndpointName
    "TargetCpuUtilizationPerCore": number, // Optional
    "ScalingPolicyObjective": { // Optional
        "MinInvocationsPerMinute": number,
        "MaxInvocationsPerMinute": number
    }
}
```

提交請求後，您將收到回應，其中包含針對每個指標定義的自動擴展政策。如需解讀回應的相關資訊，請參閱下列一節。

## 檢閱自動擴展設定建議結果
<a name="inference-recommender-autoscaling-review"></a>

以下範例顯示 [GetScalingConfigurationRecommendation](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_GetScalingConfigurationRecommendation.html) API 的回應：

```
{
    "InferenceRecommendationsJobName": "string", 
    "RecommendationId": "string", // One of RecommendationId or EndpointName is shown
    "EndpointName": "string", 
    "TargetUtilizationPercentage": Integer,
    "ScalingPolicyObjective": { 
        "MinInvocationsPerMinute": Integer, 
        "MaxInvocationsPerMinute": Integer
    },
    "Metric": {
        "ModelLatency": Integer,
        "InvocationsPerInstance": Integer
    },
    "DynamicScalingConfiguration": {
        "MinCapacity": number,
        "MaxCapacity": number, 
        "ScaleInCooldown": number,
        "ScaleOutCooldown": number,
        "ScalingPolicies": [
            {
                "TargetTracking": {
                    "MetricSpecification": {
                        "Predefined" {
                            "PredefinedMetricType": "string"
                         },
                        "Customized": {
                            "MetricName": "string",
                            "Namespace": "string",
                            "Statistic": "string"
                         }
                    },
                    "TargetValue": Double
                } 
            }
        ]
    }
}
```

系統會從您的初始請求複製 `InferenceRecommendationsJobName`、`RecommendationID` 或 `EndpointName`、`TargetCpuUtilizationPerCore` 和 `ScalingPolicyObjective` 物件欄位。

`Metric` 物件會列出推論建議任務中基準測試的指標，以及執行個體使用率與 `TargetCpuUtilizationPerCore` 值相同時，每個指標值的計算方式。這對於在使用建議的自動擴展政策進行擴展和縮小時預測端點上的效能指標非常實用。例如，假設您的執行個體使用率在推論建議任務中為 50%，而您原本的 `InvocationsPerInstance` 值為 `4`。如果您在自動擴展建議請求中將 `TargetCpuUtilizationPerCore` 值指定為 100%，則回應中傳回的 `InvocationsPerInstance` 指標值是 `2`，因為您預期配置的執行個體使用率是您預期配置兩倍的執行個體使用率。

當您呼叫 [PutScalingPolicy](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PutScalingPolicy.html) API 時，`DynamicScalingConfiguration` 物件會傳回您應該為 [TargetTrackingScalingPolicyConfiguration](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PutScalingPolicy.html#autoscaling-PutScalingPolicy-request-TargetTrackingScalingPolicyConfiguration) 指定的值。這包括建議的最小和最大容量值、建議的擴展和縮小冷卻時間，以及 `ScalingPolicies` 物件，其中包含您應為每個指標指定的建議 `TargetValue`。