

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

# 提供不同剖析选项的默认系统监控和自定义框架剖析
<a name="debugger-configure-framework-profiling-options"></a>

本节将介绍支持的剖析配置类以及配置示例。您可以使用以下分析配置类来管理框架分析选项：
+ [DetailedProfilingConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.DetailedProfilingConfig)— 指定目标步骤或时间范围，使用原生框架分析器（探查器和探查器）来TensorFlow 分析框架操作。 PyTorch 例如，如果使用 TensorFlow，则调试器挂钩使 TensorFlow 探查器能够收集 TensorFlow特定于框架的指标。通过详细的分析，您可以在训练作业执行步骤之前（在第一步之前）、在步骤内以及在步骤之间对所有框架运算符进行分析。
**注意**  
详细分析可能会显著增加 GPU 内存消耗。我们建议不要为多个步骤启用详细分析。
+ [DataloaderProfilingConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.DataloaderProfilingConfig)— 指定目标步骤或时间范围来分析深度学习框架数据加载器进程。Debugger 收集框架的每个数据加载器事件。
**注意**  
数据加载器分析在从数据加载器收集信息时，可能会降低训练性能。我们建议不要为多个步骤启用数据加载器分析。  
Debugger 已预配置为仅对 AWS Deep Learning Containers 数据加载器进程进行标注。Debugger 无法从任何其他自定义或外部训练容器中分析数据加载器进程。
+ [PythonProfilingConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.PythonProfilingConfig)— 指定目标步骤或时间范围来分析 Python 函数。您也可以在 cProfile 和 Pyinstrument 这两个 Python 探查器之间进行选择。
  + *cProfile* – 标准 Python 探查器。cProfile 会为训练期间调用的每个 Python 运算符收集信息。借助 cProfile，Debugger 可以节省每个函数调用的累积时间和注释，提供 Python 函数的完整详细信息。例如，在深度学习中，最常被调用的函数可能是卷积筛选条件和向后传递运算符，cProfile 会对其中各项进行分析。对于 cProfile 选项，您可以进一步选择计时器选项：总时间、CPU 时间和 CPU 外时间。虽然您可以在 CPU 时间内分析处理器（包括 CPU 和 GPU）上执行的每个函数调用，但也可以使用 “非CPU 时间” 选项来识别 I/O 或网络瓶颈。默认值为总时间，Debugger 会分析 CPU 和 CPU 外时间。借助 cProfile，您可以在对分析数据进行分析时，向下钻取到每个函数。
  + *Pyinstrument* – Pyinstrument 是一个基于采样进行处理的低开销 Python 探查器。使用 Pyinstrument 选项，Debugger 对性能分析事件进行每毫秒采样。由于 Pyinstrument 测量的是所用时钟时间而不是 CPU 时间，因此对于减少分析噪音（筛选掉会快速累积的不相关函数调用）和捕获会带来密集运算的运算符（累积较慢）而言，Pyinstrument 选项可以是比 cProfile 选项更好的选择。使用 Pyinstrument，您可以看到函数调用树，并更好地了解结构以及造成速度缓慢的根本原因。
**注意**  
启用 Python 分析可能会减慢整体训练速度。cProfile 会在每次调用时分析最常调用的 Python 运算符，因此分析的处理时间会随调用数量而增加。对于 Pyinstrument，由于其采样机制，累积分析时间会随着时间的推移而增加。

以下示例配置显示了使用具有指定值的不同分析选项时的完整结构。

```
import time
from sagemaker.debugger import (ProfilerConfig, 
                                FrameworkProfile, 
                                DetailedProfilingConfig, 
                                DataloaderProfilingConfig, 
                                PythonProfilingConfig,
                                PythonProfiler, cProfileTimer)

profiler_config=ProfilerConfig(
    system_monitor_interval_millis={{500}},
    framework_profile_params=FrameworkProfile(
        detailed_profiling_config=DetailedProfilingConfig(
            start_step={{5}}, 
            num_steps={{1}}
        ),
        dataloader_profiling_config=DataloaderProfilingConfig(
            start_step={{7}}, 
            num_steps={{1}}
        ),
        python_profiling_config=PythonProfilingConfig(
            start_step={{9}}, 
            num_steps={{1}}, 
            python_profiler=PythonProfiler.{{CPROFILE}}, 
            cprofile_timer={{cProfileTimer.TOTAL_TIME}}
        )
    )
)
```

有关可用分析选项的更多信息，请参阅 [Amaz SageMaker on Python 软件开发工具包[PythonProfilingConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.PythonProfilingConfig)](https://sagemaker.readthedocs.io/en/stable)中的[DetailedProfilingConfig[DataloaderProfilingConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.DataloaderProfilingConfig)](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.DetailedProfilingConfig)、和。