

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

# 存取 Python 分析統計資料
<a name="debugger-access-data-python-profiling"></a>

Python 設定檔會在訓練指令碼和 SageMaker AI 深度學習架構中，提供與 Python 函式和運算子相關的架構指標。

<a name="debugger-access-data-python-profiling-modes"></a>**訓練模式和階段 Python 分析**

若要在訓練期間分析特定的間隔，以分割每個間隔的統計資料，偵錯工具會提供設定模式和階段的工具。

對於訓練模型，必須使用以下 `PythonProfileModes` 類別：

```
from smdebug.profiler.python_profile_utils import PythonProfileModes
```

此類別提供下列選項：
+ `PythonProfileModes.TRAIN`—如果您想要分析訓練階段中的目標步驟，請使用此選項。此模式選項僅適用於 TensorFlow。
+ `PythonProfileModes.EVAL`—如果您要分析評估階段中的目標步驟，請使用此選項。此模式選項僅適用於 TensorFlow。
+ `PythonProfileModes.PREDICT`—如果您要分析預測階段中的目標步驟，請使用此選項。此模式選項僅適用於 TensorFlow。
+ `PythonProfileModes.GLOBAL`—如果您想分析全面階段 (包含先前三個階段) 中的目標步驟，請使用此選項。此模式選項僅適用於 PyTorch。
+ `PythonProfileModes.PRE_STEP_ZERO`—如果您要分析在第一個週期的第一個訓練步驟開始之前的初始化階段中的目標步驟，請使用此選項。此階段包含初始工作提交、將訓練指令碼上傳至 EC2 執行個體、準備 EC2 執行個體，以及下載輸入資料。此模式選項適用於 TensorFlow 和 PyTorch。
+ `PythonProfileModes.POST_HOOK_CLOSE`—如果您想要分析在訓練工作完成且偵錯工具勾點關閉後的完成階段中的目標步驟，請使用此選項。此階段包含結束和完成訓練工作時的分析資料。此模式選項適用於 TensorFlow 和 PyTorch。

<a name="debugger-access-data-python-profiling-phases"></a>對於訓練階段，請使用下列 `StepPhase` 類別：

```
from smdebug.profiler.analysis.utils.python_profile_analysis_utils import StepPhase
```

此類別提供下列選項：
+ `StepPhase.START`—用於指定初始化階段的起點。
+ `StepPhase.STEP_START`—用於指定訓練階段的開始步驟。
+ `StepPhase.FORWARD_PASS_END`—用於指定向前傳遞結束的步驟。這個選項只適用於 PyTorch。
+ `StepPhase.STEP_END`—用於指定訓練階段中的結束步驟。這個選項只適用於 TensorFlow。
+ `StepPhase.END`—用於指定完成 (post-hook-close) 階段的終點。如果回呼勾點未關閉，則不會產生完成階段的分析。

**Python 效能分析的分析工具**

偵錯工具有兩個分析工具支援 Python 效能分析：
+ CPileFile—標準的 Python 效能分析工具。啟用分析時，cProfile 收集呼叫的每個函式的 CPU 時間的架構指標。
+ Pyinstrument—這是一個低額外負荷的 Python 效能分析工具，每毫秒取樣分析事件。

若要進一步了解 Python 效能分析選項以及所收集的內容，請參閱[預設系統監控和使用不同分析選項的自訂架構分析](debugger-configure-framework-profiling-options.md)。

以下的 `PythonProfileAnalysis`、`cProfileAnalysis`、`PyinstrumentAnalysis` 類別方法，供擷取和分析 Python 效能分析資料。每個函式都會從預設的 S3 URI 載入最新資料。

```
from smdebug.profiler.analysis.python_profile_analysis import PythonProfileAnalysis, cProfileAnalysis, PyinstrumentAnalysis
```

要設定 Python 效能分析物件以進行分析，使用 cProfileAnalysis 或 PyinstrumentAnalysis 類別，如下面的範例程式碼所示。它顯示如何設置 `cProfileAnalysis` 物件，如果你想使用 `PyinstrumentAnalysis`，請取代類別名稱。

```
python_analysis = cProfileAnalysis(
    local_profile_dir=tf_python_stats_dir, 
    s3_path=tj.profiler_s3_output_path
)
```

下列方法可供 `cProfileAnalysis` 和 `PyinstrumentAnalysis` 類別擷取 Python 分析統計資料：
+ `python_analysis.fetch_python_profile_stats_by_time(start_time_since_epoch_in_secs, end_time_since_epoch_in_secs)`—接受開始時間和結束時間，並傳回其開始或結束時間與提供的間隔重疊的步驟統計資料的函式統計資料。
+ `python_analysis.fetch_python_profile_stats_by_step(start_step, end_step, mode, start_phase, end_phase)`—接受開始步驟和結束步驟，並傳回效能分析的 `step` 滿足 `start_step <= step < end_step` 的步驟統計資料的函式統計資料。
  + `start_step` 和 `end_step` (str) — 指定擷取 Python 效能分析的分析統計資料的開始步驟和結束步驟。
  + `mode` (str) — 使用 `PythonProfileModes` 列舉器指定訓練工作的模式。預設值為 `PythonProfileModes.TRAIN`。在 [訓練模式和階段 Python 效能分析](#debugger-access-data-python-profiling-modes)一節內，提供可用性選項。
  + `start_phase` (str) —使用 `StepPhase` 列舉器類別指定目標步驟中的開始階段。此參數可在不同訓練階段之間啟用分析。預設值為 `StepPhase.STEP_START`。在[訓練模式和階段 Python 效能分析](#debugger-access-data-python-profiling-phases)一節中提供可用性選項。
  + `end_phase` (str) —使用 `StepPhase` 列舉器類別指定目標步驟中的結束階段。此參數設定訓練的結束階段。可用性選項與 `start_phase` 參數的選項相同。預設值為 `StepPhase.STEP_END`。在[訓練模式和階段 Python 效能分析](#debugger-access-data-python-profiling-phases)一節中提供可用性選項。
+ `python_analysis.fetch_profile_stats_between_modes(start_mode, end_mode)`—從開始和結束模式之間的 Python 分析中擷取統計資料。
+ `python_analysis.fetch_pre_step_zero_profile_stats()`—從 Python 分析中擷取統計資訊，直到步驟 0。
+ `python_analysis.fetch_post_hook_close_profile_stats()`—勾點關閉後，從 Python 分析中擷取統計資料。
+ `python_analysis.list_profile_stats()`—傳回 Python 分析統計資料的 DataFrame。每一列都會保留每次效能分析執行個體的中繼資料，以及對應的統計資料檔案 (每個步驟一個)。
+ `python_analysis.list_available_node_ids()`—傳回 Python 效能分析統計資料的可用節點 ID 清單。

`cProfileAnalysis` 類別特定方法：
+  `fetch_profile_stats_by_training_phase()`—為開始和結束模式的每個可能組合，擷取和彙總 Python 分析統計資料。例如，如果在啟用詳細分析時訓練和驗證階段已完成，則組合為`(PRE_STEP_ZERO, TRAIN)`、`(TRAIN, TRAIN)`、`(TRAIN, EVAL)`、`(EVAL, EVAL)` 和 `(EVAL, POST_HOOK_CLOSE)`。這些組合中的所有統計資料檔案都會被彙總。
+  `fetch_profile_stats_by_job_phase()`—按作業階段擷取和彙總 Python 分析統計資訊。工作階段為 `initialization` (分析直到步驟 0)、`training_loop` (訓練和驗證) 和 `finalization` (勾點關閉後的效能分析)。