如何解決使用 Amazon SageMaker Python SDK 時發生的限流錯誤, 例如 “botocore.exceptions.ClientError: An error occurred (ThrottlingException)” (botocore.exceptions.ClientError: 發生錯誤 (限流異常))?
簡短說明
對任何 AWS 服務的 API 呼叫不得超過每個帳戶和每個 AWS 區域允許的最大 API 請求率。這些 API 呼叫可能來自應用程式、AWS Command Line Interface (AWS CLI) 或 AWS 管理主控台。如果 API 請求超過比率上限,您會收到「超出請求率」錯誤,且 API 呼叫會受到限流。
由於 Boto3 中的預設重試組態,因此在呼叫 SageMaker API 時可能會收到此錯誤。您可以覆寫此組態以增加重試次數,以及連線和讀取回應的逾時時間。
您可以將具有自訂重試組態的 SageMaker boto3 用戶端新增至 SageMaker Python SDK 用戶端,以解決此錯誤。
解決方法
1. 建立具有自訂重試組態的 SageMaker boto3 用戶端。範例:
import boto3
from botocore.config import Config
sm_boto = boto3.client('sagemaker', config=Config(connect_timeout=5, read_timeout=60, retries={'max_attempts': 20}))
print(sm_boto.meta.config.retries)
2. 使用上一步建立的 boto3 用戶端,建立 SageMaker Python SDK 用戶端。範例:
import sagemaker
sagemaker_session = sagemaker.Session(sagemaker_client = sm_boto)
region = sagemaker_session.boto_session.region_name
print(sagemaker_session.sagemaker_client.meta.config.retries)
3. 使用來自 SageMaker Python SDK 的多個請求測試 SageMaker API。範例:
import multiprocessing
def worker(TrainingJobName):
print(sagemaker_session.sagemaker_client
.describe_training_job(TrainingJobName=TrainingJobName)
['TrainingJobName'])
return
if __name__ == '__main__':
jobs = []
TrainingJobName = 'your-job-name'
for i in range(10):
p = multiprocessing.Process(target=worker, args=(TrainingJobName,))
jobs.append(p)
p.start()
- 使用 sagemaker_session 參數建立 sagemaker.estimator.Estimator 類別的執行個體。範例:
estimator = sagemaker.estimator.Estimator(container,
role,
train_instance_count=1,
train_instance_type='ml.c4.4xlarge',
train_volume_size = 30,
train_max_run = 360000,
input_mode= 'File',
output_path=s3_output_location,
sagemaker_session=sagemaker_session )
5. 若要確認重試組態能解決限流異常狀況,請從您在上一步建立的估量器啟動訓練任務:
estimator.fit()
相關資訊
Boto3 文件