

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

# 設定重試政策
<a name="pipelines-configuring-retry-policy"></a>

雖然 SageMaker Pipelines 提供強大且自動化的方式來協調機器學習工作流程，但您可能會在執行它們時遇到失敗。若要正常處理這類案例並改善管道的可靠性，您可以設定重試政策，定義遇到例外狀況後自動重試特定步驟的方式和時間。重試政策可讓您指定要重試的例外狀況類型、重試嘗試次數上限、重試之間的間隔，以及用於增加重試間隔的退避率。下節提供如何在 JSON 和使用 SageMaker Python SDK 為管道中的訓練步驟設定重試政策的範例。

以下是具有重試政策的訓練步驟範例。

```
{
    "Steps": [
        {
            "Name": "{{MyTrainingStep}}",
            "Type": "Training",
            "RetryPolicies": [
                {
                    "ExceptionType": [
                        "{{SageMaker.JOB_INTERNAL_ERROR}}",
                        "{{SageMaker.CAPACITY_ERROR}}"
                    ],
                    "IntervalSeconds": {{1}},
                    "BackoffRate": {{2}},
                    "MaxAttempts": {{5}}
                }
            ]
        }
    ]
}
```



以下範例示範了如何使用重試政策在適用於 Python 的 SDK (Boto3) 中建置 `TrainingStep`。

```
from sagemaker.workflow.retry import (
    StepRetryPolicy, 
    StepExceptionTypeEnum,
    SageMakerJobExceptionTypeEnum,
    SageMakerJobStepRetryPolicy
)

step_train = TrainingStep(
    name="{{MyTrainingStep}}",
    xxx,
    retry_policies=[
        // override the default 
        StepRetryPolicy(
            exception_types=[
                {{StepExceptionTypeEnum.SERVICE_FAULT}}, 
                {{StepExceptionTypeEnum.THROTTLING}}
            ],
            expire_after_mins={{5}},
            interval_seconds={{10}},
            backoff_rate={{2.0}} 
        ),
        // retry when resource limit quota gets exceeded
        SageMakerJobStepRetryPolicy(
            exception_types=[{{SageMakerJobExceptionTypeEnum.RESOURCE_LIMIT}}],
            expire_after_mins={{120}},
            interval_seconds={{60}},
            backoff_rate={{2.0}}
        ),
        // retry when job failed due to transient error or EC2 ICE.
        SageMakerJobStepRetryPolicy(
            failure_reason_types=[
                {{SageMakerJobExceptionTypeEnum.INTERNAL_ERROR}},
                {{SageMakerJobExceptionTypeEnum.CAPACITY_ERROR}},
            ],
            max_attempts={{10}},
            interval_seconds={{30}},
            backoff_rate={{2.0}}
        )
    ]
)
```

如需為特定步驟類型設定重試行為的詳細資訊，請參閱 Amazon SageMaker Python SDK 文件中的 [Amazon SageMaker Pipelines - 重試政策](https://sagemaker.readthedocs.io/en/stable/amazon_sagemaker_model_building_pipeline.html#retry-policy)。**