AWS 肉毒桿菌 - Amazon SageMaker

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

AWS 肉毒桿菌

您可以使用 AWS Boto3 SageMaker 用戶端的create_training_job()功能,為訓練任務設定 Amazon SageMaker 偵錯器內建規則。您需要在 RuleEvaluatorImage 參數中指定正確的映像 URI,下列範例會逐步引導您如何設定適用於 create_training_job() 功能的請求內文。

下列程式碼顯示如何設定要create_training_job()求主體的偵錯工具,並在中啟動訓練工作的完整範例us-west-2,假設訓練指令碼entry_point/train.py已使用 TensorFlow。若要尋找範 end-to-end 例筆記本,請參閱使用 Amazon SageMaker 除錯器 (Boto3) 剖析 TensorFlow 多 GPU 多節點訓練任務。

注意

請確定您使用正確的 Docker 容器映像。若要尋找可用的 AWS Deep Learning Containers 映像,請參閱可用的深度學習容器映像。要查找用於使用調試器規則的可用 Docker 映像的完整列表,請參閱為內建或自訂規則使用偵錯器 Docker 映像

import sagemaker, boto3 import datetime, tarfile # Start setting up a SageMaker session and a Boto3 SageMaker client session = sagemaker.Session() region = session.boto_region_name bucket = session.default_bucket() # Upload a training script to a default Amazon S3 bucket of the current SageMaker session source = 'source.tar.gz' project = 'debugger-boto3-test' tar = tarfile.open(source, 'w:gz') tar.add ('entry_point/train.py') # Specify the directory and name of your training script tar.close() s3 = boto3.client('s3') s3.upload_file(source, bucket, project+'/'+source) # Set up a Boto3 session client for SageMaker sm = boto3.Session(region_name=region).client("sagemaker") # Start a training job sm.create_training_job( TrainingJobName='debugger-boto3-'+datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S'), HyperParameters={ 'sagemaker_submit_directory': 's3://'+bucket+'/'+project+'/'+source, 'sagemaker_program': '/entry_point/train.py' # training scrip file location and name under the sagemaker_submit_directory }, AlgorithmSpecification={ # Specify a training Docker container image URI (Deep Learning Container or your own training container) to TrainingImage. 'TrainingImage': '763104351884.dkr.ecr.us-west-2.amazonaws.com/tensorflow-training:2.4.1-gpu-py37-cu110-ubuntu18.04', 'TrainingInputMode': 'File', 'EnableSageMakerMetricsTimeSeries': False }, RoleArn='arn:aws:iam::111122223333:role/service-role/AmazonSageMaker-ExecutionRole-20201014T161125', OutputDataConfig={'S3OutputPath': 's3://'+bucket+'/'+project+'/output'}, ResourceConfig={ 'InstanceType': 'ml.p3.8xlarge', 'InstanceCount': 1, 'VolumeSizeInGB': 30 }, StoppingCondition={ 'MaxRuntimeInSeconds': 86400 }, DebugHookConfig={ 'S3OutputPath': 's3://'+bucket+'/'+project+'/debug-output', 'CollectionConfigurations': [ { 'CollectionName': 'losses', 'CollectionParameters' : { 'train.save_interval': '500', 'eval.save_interval': '50' } } ] }, DebugRuleConfigurations=[ { 'RuleConfigurationName': 'LossNotDecreasing', 'RuleEvaluatorImage': '895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest', 'RuleParameters': {'rule_to_invoke': 'LossNotDecreasing'} } ], ProfilerConfig={ 'S3OutputPath': 's3://'+bucket+'/'+project+'/profiler-output', 'ProfilingIntervalInMilliseconds': 500, 'ProfilingParameters': { 'DataloaderProfilingConfig': '{"StartStep": 5, "NumSteps": 3, "MetricsRegex": ".*", }', 'DetailedProfilingConfig': '{"StartStep": 5, "NumSteps": 3, }', 'PythonProfilingConfig': '{"StartStep": 5, "NumSteps": 3, "ProfilerName": "cprofile", "cProfileTimer": "total_time"}', 'LocalPath': '/opt/ml/output/profiler/' # Optional. Local path for profiling outputs } }, ProfilerRuleConfigurations=[ { 'RuleConfigurationName': 'ProfilerReport', 'RuleEvaluatorImage': '895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest', 'RuleParameters': {'rule_to_invoke': 'ProfilerReport'} } ] )

若要設定偵錯模型參數的 Debugger 規則

下列程式碼範例顯示如何使用此 SageMaker API 設定內建VanishingGradient規則。

若要啟用 Debugger 收集輸出張量

請指定 Debugger 勾點組態,如下所示:

DebugHookConfig={ 'S3OutputPath': 's3://<default-bucket>/<training-job-name>/debug-output', 'CollectionConfigurations': [ { 'CollectionName': 'gradients', 'CollectionParameters' : { 'train.save_interval': '500', 'eval.save_interval': '50' } } ] }

這將讓訓練任務儲存一個張量集合(gradients、每 500 個步驟就 save_interval 一次)。若要找到可用的 CollectionName 值,請參閱 SMDebug 用戶端程式庫文件中的 Debugger 內建集合。若要尋找可用的CollectionParameters參數鍵和值,請參閱 SageMaker Python SDK 文件中的sagemaker.debugger.CollectionConfig類別。

若要啟用適用於偵錯輸出張量的 Debugger 規則

下列 DebugRuleConfigurations API 範例會示範如何在已儲存的 gradients 集合上執行內建 VanishingGradient 規則。

DebugRuleConfigurations=[ { 'RuleConfigurationName': 'VanishingGradient', 'RuleEvaluatorImage': '895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest', 'RuleParameters': { 'rule_to_invoke': 'VanishingGradient', 'threshold': '20.0' } } ]

就此範例中的組態來說,Debugger 會使用 gradients 張量集合上的 VanishingGradient 規則,對訓練任務啟動規則評估任務。要查找用於使用調試器規則的可用 Docker 映像的完整列表,請參閱為內建或自訂規則使用偵錯器 Docker 映像。若要查找RuleParameters,請參閱偵錯工具內建規則清單

若要設定適用於分析系統和架構指標的 Debugger 內建規則

下列範例程式碼示範如何指定 ProfilerConfig API 作業,以便收集系統和架構指標。

若要啟用 Debugger 分析收集系統和架構指標

Target Step
ProfilerConfig={ 'S3OutputPath': 's3://<default-bucket>/<training-job-name>/profiler-output', # Optional. Path to an S3 bucket to save profiling outputs # Available values for ProfilingIntervalInMilliseconds: 100, 200, 500, 1000 (1 second), 5000 (5 seconds), and 60000 (1 minute) milliseconds. 'ProfilingIntervalInMilliseconds': 500, 'ProfilingParameters': { 'DataloaderProfilingConfig': '{ "StartStep": 5, "NumSteps": 3, "MetricsRegex": ".*" }', 'DetailedProfilingConfig': '{ "StartStep": 5, "NumSteps": 3 }', 'PythonProfilingConfig': '{ "StartStep": 5, "NumSteps": 3, "ProfilerName": "cprofile", # Available options: cprofile, pyinstrument "cProfileTimer": "total_time" # Include only when using cprofile. Available options: cpu, off_cpu, total_time }', 'LocalPath': '/opt/ml/output/profiler/' # Optional. Local path for profiling outputs } }
Target Time Duration
ProfilerConfig={ 'S3OutputPath': 's3://<default-bucket>/<training-job-name>/profiler-output', # Optional. Path to an S3 bucket to save profiling outputs # Available values for ProfilingIntervalInMilliseconds: 100, 200, 500, 1000 (1 second), 5000 (5 seconds), and 60000 (1 minute) milliseconds. 'ProfilingIntervalInMilliseconds': 500, 'ProfilingParameters': { 'DataloaderProfilingConfig': '{ "StartTimeInSecSinceEpoch": 12345567789, "DurationInSeconds": 10, "MetricsRegex": ".*" }', 'DetailedProfilingConfig': '{ "StartTimeInSecSinceEpoch": 12345567789, "DurationInSeconds": 10 }', 'PythonProfilingConfig': '{ "StartTimeInSecSinceEpoch": 12345567789, "DurationInSeconds": 10, "ProfilerName": "cprofile", # Available options: cprofile, pyinstrument "cProfileTimer": "total_time" # Include only when using cprofile. Available options: cpu, off_cpu, total_time }', 'LocalPath': '/opt/ml/output/profiler/' # Optional. Local path for profiling outputs } }

若要啟用 Debugger 規則分析指標

下列範例程式碼示範如何設定 ProfilerReport 規則。

ProfilerRuleConfigurations=[ { 'RuleConfigurationName': 'ProfilerReport', 'RuleEvaluatorImage': '895741380848.dkr.ecr.us-west-2.amazonaws.com/sagemaker-debugger-rules:latest', 'RuleParameters': { 'rule_to_invoke': 'ProfilerReport', 'CPUBottleneck_cpu_threshold': '90', 'IOBottleneck_threshold': '90' } } ]

要查找用於使用調試器規則的可用 Docker 映像的完整列表,請參閱為內建或自訂規則使用偵錯器 Docker 映像。若要查找RuleParameters,請參閱偵錯工具內建規則清單

使用 UpdateTrainingJob API 操作更新 Debugger 分析組態

您可以在訓練工作執行時,使用 AWS Boto3 SageMaker 用戶端的update_training_job()功能來更新除錯程式分析組態。配置新ProfilerRuleConfiguration物件ProfilerConfig和物件,並為TrainingJobName參數指定訓練工作名稱。

ProfilerConfig={ 'DisableProfiler': boolean, 'ProfilingIntervalInMilliseconds': number, 'ProfilingParameters': { 'string' : 'string' } }, ProfilerRuleConfigurations=[ { 'RuleConfigurationName': 'string', 'RuleEvaluatorImage': 'string', 'RuleParameters': { 'string' : 'string' } } ], TrainingJobName='your-training-job-name-YYYY-MM-DD-HH-MM-SS-SSS'

將偵錯工具自訂規則組態新增至 CreateTrainingJob API 作業

您可以使用 DebugHookConfig DebugRuleConfiguration物件,使用 AWS Boto3 用 SageMaker 戶端的函數為訓練工作配置自訂規則。create_training_job()下列程式碼範例顯示如何設定使用此 SageMaker API 作業以 smdebug 程式庫撰寫的自訂ImproperActivation規則。此範例假設您已在 custom_rules.py 檔案中撰寫自訂規則,並上傳到 Amazon S3 儲存貯體。下列範例提供預先建置的 Docker 映像,可用來執行您的自訂規則。這些都列在 Amazon SageMaker 調試器註冊表自URLs定義規則評估器。您需要在 RuleEvaluatorImage 參數中指定預先建置的 Docker 影像的 URL 登錄位址。

DebugHookConfig={ 'S3OutputPath': 's3://<default-bucket>/<training-job-name>/debug-output', 'CollectionConfigurations': [ { 'CollectionName': 'relu_activations', 'CollectionParameters': { 'include_regex': 'relu', 'save_interval': '500', 'end_step': '5000' } } ] }, DebugRulesConfigurations=[ { 'RuleConfigurationName': 'improper_activation_job', 'RuleEvaluatorImage': '552407032007.dkr.ecr.ap-south-1.amazonaws.com/sagemaker-debugger-rule-evaluator:latest', 'InstanceType': 'ml.c4.xlarge', 'VolumeSizeInGB': 400, 'RuleParameters': { 'source_s3_uri': 's3://bucket/custom_rules.py', 'rule_to_invoke': 'ImproperActivation', 'collection_names': 'relu_activations' } } ]

要查找用於使用調試器規則的可用 Docker 映像的完整列表,請參閱為內建或自訂規則使用偵錯器 Docker 映像。若要查找RuleParameters,請參閱偵錯工具內建規則清單