使用亚马逊配置调试器SageMakerPython 开发工具包 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用亚马逊配置调试器SageMakerPython 开发工具包

要配置调试器,请使用亚马逊SageMakerPython 开发工具包并在构造时指定调试器特定的参数SageMaker估算程序。您需要配置三个参数:profiler_configdebugger_hook_config, 和rules.

注意

默认情况下,调试器会监视和调试SageMaker在中配置了没有任何调试器特定参数的训练作业SageMaker估算程序。调试器每 500 毫秒收集系统指标,每 500 个步骤收集基本输出张量(标量输出,如损耗和准确度)。它还运行ProfilerReport规则来分析系统指标并聚合 Studio Debug 见解仪表板和性能分析报告。调试器将输出数据保存在安全的 S3 存储桶中。

重要

要使用新的调试器功能,你需要升级SageMakerPython 开发工具包和SMDebug客户端库。在你的 iPython 内核中,Jupyter 笔记本,或者JupyterLab环境中,运行以下代码以安装库的最新版本并重新启动内核。

import sys import IPython !{sys.executable} -m pip install -U sagemaker smdebug IPython.Application.instance().kernel.do_shutdown(True)

构造SageMaker带调试程序的估算程序

以下示例代码说明了如何构建SageMaker估计器具有调试者特定参数,具体取决于你选择的框架。在以下主题的整个文档中,您可以找到有关如何配置可以根据需要混合和匹配的调试器特定参数的更多信息。

注意

以下示例代码不能直接执行。你需要继续下一节并配置调试器特定的参数。

TensorFlow

访问深度分析功能TensorFlow,目前您需要指定最新的Amazon使用 CUDA 11 进行深度学习容器映像。例如,您必须指定具体映像 URI,如以下示例代码所示:

# An example of constructing a SageMaker TensorFlow estimator import boto3 import sagemaker from sagemaker.tensorflow import TensorFlow from sagemaker.debugger import ProfilerConfig, DebuggerHookConfig, Rule, ProfilerRule, rule_configs session=boto3.session.Session() region=session.region_name profiler_config=ProfilerConfig(...) debugger_hook_config=DebuggerHookConfig(...) rules=[ Rule.sagemaker(rule_configs.built_in_rule()), ProfilerRule.sagemaker(rule_configs.BuiltInRule()) ] estimator=TensorFlow( entry_point="directory/to/your_training_script.py", role=sagemaker.get_execution_role(), base_job_name="debugger-demo", instance_count=1, instance_type="ml.p3.2xlarge", image_uri=f"763104351884.dkr.ecr.{region}.amazonaws.com/tensorflow-training:2.3.1-gpu-py37-cu110-ubuntu18.04" # Debugger-specific parameters profiler_config=profiler_config, debugger_hook_config=debugger_hook_config, rules=rules ) estimator.fit(wait=False)
PyTorch

访问深度分析功能PyTorch,目前您需要指定最新的Amazon使用 CUDA 11 进行深度学习容器映像。例如,您必须指定具体映像 URI,如以下示例代码所示:

# An example of constructing a SageMaker PyTorch estimator import boto3 import sagemaker from sagemaker.pytorch import PyTorch from sagemaker.debugger import ProfilerConfig, DebuggerHookConfig, Rule, ProfilerRule, rule_configs session=boto3.session.Session() region=session.region_name profiler_config=ProfilerConfig(...) debugger_hook_config=DebuggerHookConfig(...) rules=[ Rule.sagemaker(rule_configs.built_in_rule()), ProfilerRule.sagemaker(rule_configs.BuiltInRule()) ] estimator=PyTorch( entry_point="directory/to/your_training_script.py", role=sagemaker.get_execution_role(), base_job_name="debugger-demo", instance_count=1, instance_type="ml.p3.2xlarge", image_uri=f"763104351884.dkr.ecr.{region}.amazonaws.com/pytorch-training:1.6.0-gpu-py36-cu110-ubuntu18.04", # Debugger-specific parameters profiler_config=profiler_config, debugger_hook_config=debugger_hook_config, rules=rules ) estimator.fit(wait=False)
MXNet
# An example of constructing a SageMaker MXNet estimator import sagemaker from sagemaker.mxnet import MXNet from sagemaker.debugger import ProfilerConfig, DebuggerHookConfig, Rule, ProfilerRule, rule_configs profiler_config=ProfilerConfig(...) debugger_hook_config=DebuggerHookConfig(...) rules=[ Rule.sagemaker(rule_configs.built_in_rule()), ProfilerRule.sagemaker(rule_configs.BuiltInRule()) ] estimator=MXNet( entry_point="directory/to/your_training_script.py", role=sagemaker.get_execution_role(), base_job_name="debugger-demo", instance_count=1, instance_type="ml.p3.2xlarge", framework_version="1.7.0", py_version="py37", # Debugger-specific parameters profiler_config=profiler_config, debugger_hook_config=debugger_hook_config, rules=rules ) estimator.fit(wait=False)
注意

对于 MXNet,在配置profiler_config参数,您只能配置系统监控。MxNet 不支持分析框架指标。

XGBoost
# An example of constructing a SageMaker XGBoost estimator import sagemaker from sagemaker.xgboost.estimator import XGBoost from sagemaker.debugger import ProfilerConfig, DebuggerHookConfig, Rule, ProfilerRule, rule_configs profiler_config=ProfilerConfig(...) debugger_hook_config=DebuggerHookConfig(...) rules=[ Rule.sagemaker(rule_configs.built_in_rule()), ProfilerRule.sagemaker(rule_configs.BuiltInRule()) ] estimator=XGBoost( entry_point="directory/to/your_training_script.py", role=sagemaker.get_execution_role(), base_job_name="debugger-demo", instance_count=1, instance_type="ml.p3.2xlarge", framework_version="1.2-1", # Debugger-specific parameters profiler_config=profiler_config, debugger_hook_config=debugger_hook_config, rules=rules ) estimator.fit(wait=False)
注意

对于 xgBoost,在配置profiler_config参数,您只能配置系统监控。xgBoost 不支持分析框架指标。

Generic estimator
# An example of constructing a SageMaker generic estimator using the XGBoost algorithm base image import boto3 import sagemaker from sagemaker.estimator import Estimator from sagemaker import image_uris from sagemaker.debugger import ProfilerConfig, DebuggerHookConfig, Rule, ProfilerRule, rule_configs profiler_config=ProfilerConfig(...) debugger_hook_config=DebuggerHookConfig(...) rules=[ Rule.sagemaker(rule_configs.built_in_rule()), ProfilerRule.sagemaker(rule_configs.BuiltInRule()) ] region=boto3.Session().region_name xgboost_container=sagemaker.image_uris.retrieve("xgboost", region, "1.2-1") estimator=Estimator( role=sagemaker.get_execution_role() image_uri=xgboost_container, base_job_name="debugger-demo", instance_count=1, instance_type="ml.m5.2xlarge", # Debugger-specific parameters profiler_config=profiler_config, debugger_hook_config=debugger_hook_config, rules=rules ) estimator.fit(wait=False)

您可以在其中配置以下参数:

  • profiler_config参数 — 配置调试器以从训练作业中收集系统指标和框架指标,然后保存到安全的 S3 存储桶 URI 或本地计算机中。要了解如何配置profiler_config参数,请参阅配置调试器监视硬件系统资源利用率配置调试器框架分析.

  • debugger_hook_config参数 — 配置调试器以从训练作业中收集输出张量并保存到安全的 S3 存储桶 URI 或本地计算机中。要了解如何配置debugger_hook_config参数,请参阅配置调试程序挂钩以保存张量.

  • rules参数 — 配置此参数以启用要并行运行的调试器内置规则。这些规则会自动分析你的训练工作并找到培训问题。这些区域有:ProfilerReport规则将调试器性能分析报告保存在安全的 S3 存储桶 URI 中。要了解如何配置rules参数,请参阅配置调试程序内置规则.

注意

调试器将输出数据安全地保存在默认 S3 存储桶的子文件夹中。例如,默认 S3 存储桶 URI 的格式为s3://sagemaker-<region>-<12digit_account_id>/<base-job-name>/<debugger-subfolders>/. 调试器创建了三个子文件夹:debug-outputprofiler-output, 和rule-output. 您还可以使用检索默认的 S3 存储桶 URI。SageMaker估算程序类方法.

请参阅以下主题,了解如何详细配置调试器特定参数。