

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

# 定義擴展政策
<a name="endpoint-auto-scaling-add-code-define"></a>

將擴展政策新增至模型之前，請先以文字檔案中的 JSON 區塊儲存政策組態。叫用 AWS Command Line Interface (AWS CLI) 或 Application Auto Scaling API 時，您可以使用該文字檔案。您可以選擇適合的 CloudWatch 指標來最佳化擴展。不過，在生產環境中使用自訂指標之前，您必須以自訂指標測試自動擴展。

**Topics**
+ [指定預先定義的指標 (CloudWatch 指標：InvocationsPerInstance)](#endpoint-auto-scaling-add-code-predefined)
+ [指定高解析度預先定義的指標 (CloudWatch 指標：ConcurrentRequestsPerModel 和 ConcurrentRequestsPerCopy)](#endpoint-auto-scaling-add-code-high-res)
+ [定義自訂指標 (CloudWatch 指標：CPUUtilization)](#endpoint-auto-scaling-add-code-custom)
+ [定義自訂指標 (CloudWatch 指標：ExplanationsPerInstance)](#endpoint-auto-scaling-online-explainability)
+ [指定冷卻時間](#endpoint-auto-scaling-add-code-cooldown)

本節說明目標追蹤擴展政策的政策組態範例。

## 指定預先定義的指標 (CloudWatch 指標：InvocationsPerInstance)
<a name="endpoint-auto-scaling-add-code-predefined"></a>

**Example**  
以下是變體的目標追蹤政策組態範例，該組態會將每個執行個體的平均調用保持在 70。將此組態儲存至名為 `config.json` 的檔案。  

```
{
    "TargetValue": 70.0,
    "PredefinedMetricSpecification":
    {
        "PredefinedMetricType": "SageMakerVariantInvocationsPerInstance"
    }
}
```
如需詳細資訊，請參閱 *Application Auto Scaling API 參考*中的 [TargetTrackingScalingPolicyConfiguration](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_TargetTrackingScalingPolicyConfiguration.html)。

## 指定高解析度預先定義的指標 (CloudWatch 指標：ConcurrentRequestsPerModel 和 ConcurrentRequestsPerCopy)
<a name="endpoint-auto-scaling-add-code-high-res"></a>

透過下列高解析度 CloudWatch 指標，您可以為模型所接收的並行請求量設定擴展政策：

**ConcurrentRequestsPerModel**  
模型容器所接收的並行請求數量。

**ConcurrentRequestsPerCopy**  
推論元件所接收的並行請求數量。

這些指標會追蹤模型容器所處理的並行請求數量，包括排入容器佇列的請求。針對以記號串流形式傳送推論回應的模型，這些指標會追蹤每個請求，直到模型傳送請求的最後一個字符為止。

高解析度指標比標準 CloudWatch 指標更頻繁發出資料。例如 `InvocationsPerInstance` 指標等標準指標每分鐘發出一次資料。而高解析度指標每 10 秒即發出一次資料。因此，隨著模型的並行流量增加，您的政策會比標準指標更快橫向擴充以進行回應。不過，隨著模型流量減少，您的政策會以與標準指標相同的速度縮減。

以下是目標追蹤政策組態範例，如果每個模型的並行請求數量超過 5，則會新增執行個體。將此組態儲存至名為 `config.json` 的檔案。

```
{
    "TargetValue": 5.0,
    "PredefinedMetricSpecification":
    {
        "PredefinedMetricType": "SageMakerVariantConcurrentRequestsPerModelHighResolution"
    }
}
```

如果您使用推論元件將多個模型部署到相同端點，您可以建立一個類似的政策。在此情況下，請將 `PredefinedMetricType` 設為 `SageMakerInferenceComponentConcurrentRequestsPerCopyHighResolution`。

如需詳細資訊，請參閱 *Application Auto Scaling API 參考*中的 [TargetTrackingScalingPolicyConfiguration](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_TargetTrackingScalingPolicyConfiguration.html)。

## 定義自訂指標 (CloudWatch 指標：CPUUtilization)
<a name="endpoint-auto-scaling-add-code-custom"></a>

若要使用自訂指標建立目標追蹤擴展政策，請指定指標名稱、命名空間、單位、統計資料，以及零或多個維度。維度由維度名稱和維度值組成。您可以使用隨容量比例變更的任何生產變體指標。

**Example**  
下列的組態範例是有自訂指標的目標追蹤擴展政策。此政策會根據所有執行個體的平均 CPU 使用率 50% 來擴展變體。將此組態儲存至名為 `config.json` 的檔案。  

```
{
    "TargetValue": 50.0,
    "CustomizedMetricSpecification":
    {
        "MetricName": "CPUUtilization",
        "Namespace": "/aws/sagemaker/Endpoints",
        "Dimensions": [
            {"Name": "EndpointName", "Value": "my-endpoint" },
            {"Name": "VariantName","Value": "my-variant"}
        ],
        "Statistic": "Average",
        "Unit": "Percent"
    }
}
```
如需詳細資訊，請參閱《*Application Auto Scaling API 參考*》中的 [CustomizedMetricSpecification](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_CustomizedMetricSpecification.html)。

## 定義自訂指標 (CloudWatch 指標：ExplanationsPerInstance)
<a name="endpoint-auto-scaling-online-explainability"></a>

當端點啟用線上可解釋性時，會發出一個 `ExplanationsPerInstance` 指標，輸出每個執行個體的一個變體每分鐘可解釋性的平均記錄數。可解釋記錄的資源使用率，可以和預測的記錄大為不同。我們強烈建議啟用線上可解釋性，將此指標用於端點的目標追蹤擴展。

您可以為可擴展目標建立多個目標追蹤政策。請考慮從 [指定預先定義的指標 (CloudWatch 指標：InvocationsPerInstance)](#endpoint-auto-scaling-add-code-predefined) 區段 (除了 `ExplanationsPerInstance` 政策) 加入 `InvocationsPerInstance` 政策。如果大多數調用因為 `EnableExplanations` 參數中設定的閾值而未傳回解釋，則端點可以選擇 `InvocationsPerInstance` 政策。如果有大量的解釋，端點可以使用該 `ExplanationsPerInstance` 策略。

**Example**  
下列的組態範例是有自訂指標的目標追蹤擴展政策。該政策擴展會調整變體執行個體的數量，讓每個執行個體的 `ExplanationsPerInstance` 指標為 20。將此組態儲存至名為 `config.json` 的檔案。  

```
{
    "TargetValue": 20.0,
    "CustomizedMetricSpecification":
    {
        "MetricName": "ExplanationsPerInstance",
        "Namespace": "AWS/SageMaker",
        "Dimensions": [
            {"Name": "EndpointName", "Value": "my-endpoint" },
            {"Name": "VariantName","Value": "my-variant"}
        ],
        "Statistic": "Sum"
    }
}
```

如需詳細資訊，請參閱《*Application Auto Scaling API 參考*》中的 [CustomizedMetricSpecification](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_CustomizedMetricSpecification.html)。

## 指定冷卻時間
<a name="endpoint-auto-scaling-add-code-cooldown"></a>

您可以選擇指定 `ScaleOutCooldown` 和 `ScaleInCooldown` 參數，在目標追蹤擴展政策中定義冷卻時間。

**Example**  
以下是變體的目標追蹤政策組態範例，該組態會將每個執行個體的平均調用保持在 70。這個政策組態分別提供了 10 分鐘 (600 秒) 的縮減冷卻時間和 5 分鐘 (300 秒) 的橫向擴充冷卻時間。將此組態儲存至名為 `config.json` 的檔案。  

```
{
    "TargetValue": 70.0,
    "PredefinedMetricSpecification":
    {
        "PredefinedMetricType": "SageMakerVariantInvocationsPerInstance"
    },
    "ScaleInCooldown": 600,
    "ScaleOutCooldown": 300
}
```
如需詳細資訊，請參閱 *Application Auto Scaling API 參考*中的 [TargetTrackingScalingPolicyConfiguration](https://docs.aws.amazon.com/autoscaling/application/APIReference/API_TargetTrackingScalingPolicyConfiguration.html)。