

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

# 預設系統監控和使用不同分析選項的自訂架構分析
<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 已為註釋資料預先設定，僅適用於 AWS 深度學習容器的資料載入器程序。偵錯工具無法剖析來自任何其他自訂或外部訓練容器的資料載入器程序。
+ [PythonProfilingConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.PythonProfilingConfig) — 指定目標步驟或時間範圍以分析 Python 函式。您還可以在兩個 Python 剖析工具之間進行選擇：cProfile 和 Pyinstrument。
  + *cProfile* — 標準的 Python 剖析工具。cProfile 會收集訓練期間呼叫的每個 Python 運算子的資訊。使用 cProfile，偵錯工具儲存每個函式呼叫的累積時間和註釋，提供有關 Python 函式的完整細節。例如，在深度學習中，最常被呼叫的函式可能是卷積網路篩選器和向後傳遞運算子，而 cProfile 會剖析其中的每一個。針對 cProfile 選項，您可以進一步選擇計時器選項：總時間、CPU 時間和關閉 CPU 時間。雖然您可以分析 CPU 時間內在處理器 (CPU 和 GPU) 上執行的每個函式呼叫，但您也可以使用關閉 CPU 時間選項來識別 I/O 或網路瓶頸。預設值為總時間，偵錯工具會同時分析 CPU時間和關閉 CPU 時間。使用 CPenfile，您可以在分析設定檔資料時向下切入至每一個函式。
  + *Pyinstrument*— Pyinstrument 是一種基於取樣的低額外負載 Python 剖析工具。使用 Pyinstrument 選項，偵錯工具每毫秒對分析事件進行一次取樣。由於 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}}
        )
    )
)
```

如需可用設定檔選項的更多相關資訊，請參閱 [Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable) 內的 [DetailedProfilingConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.DetailedProfilingConfig)、[DataloaderProfilingConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.DataloaderProfilingConfig) 和 [PythonProfilingConfig](https://sagemaker.readthedocs.io/en/stable/api/training/debugger.html#sagemaker.debugger.PythonProfilingConfig)。