

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

# 合併多個分析追蹤檔案的時間軸
<a name="debugger-merge-timeline"></a>

SMDebug 用戶端程式庫提供效能分析的分析和視覺化工具，用於合併由偵錯工具收集的系統指標、架構指標和 Python 分析資料的時間軸。

**提示**  
在繼續之前，您需要設定一個 TrainingJob 物件，本頁中的範例將使用該物件。如需設定 TrainingJob 物件的更多相關資訊，請參閱[存取分析資料](debugger-analyze-data-profiling.md)。

`MergedTimeline` 類別提供工具，可在單一時間軸內整合和關聯不同的效能分析資訊。偵錯工具從訓練工作的不同階段擷取效能分析資料和註釋之後，追蹤事件的 JSON 檔案會儲存在預設的 `tracefolder` 目錄中。
+ 對於 Python 各層中的註釋，追蹤檔案會儲存在 `*pythontimeline.json` 內。
+ 對於 TensorFlow C\+\+ 各層中的註釋，追蹤檔案會儲存在 `*model_timeline.json` 內。
+ Tensorflow 效能分析工具會將事件儲存為一個 `*trace.json.gz` 檔。

**提示**  
如果要列出所有 JSON 追蹤檔案，請使用下列 AWS CLI 命令：  

```
! aws s3 ls {tj.profiler_s3_output_path} --recursive | grep '\.json$'
```

如下列螢幕擷取畫面動畫所示，將從不同效能分析來源擷取的追蹤事件會置於單一繪圖中並對齊，可提供訓練工作不同階段中發生的所有事件的概觀。

![合併時間軸的範例](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/debugger/debugger-merged-timeline.gif)


**提示**  
要使用鍵盤與追蹤應用程式上的合併時間軸互動，請使用 `W` 鍵放大、`A`鍵向左移動、`S` 鍵縮小、`D` 鍵向右移動。

使用 `smdebug.profiler.analysis.utils.merge_timelines` 模組中的下列 `MergedTimeline` API 作業和類別方法，可將多個事件追蹤 JSON 檔案合併到一個追蹤事件 JSON 檔案。

```
from smdebug.profiler.analysis.utils.merge_timelines import MergedTimeline

combined_timeline = MergedTimeline(path, file_suffix_filter, output_directory)
combined_timeline.merge_timeline(start, end, unit)
```

`MergedTimeline` API 作業會傳遞下列參數：
+ `path` (str) — 指定包含系統和架構效能分析追蹤檔案的根資料夾 (`/profiler-output`)。您可以使用 SageMaker AI 估算器類別方法，或 TrainingJob 物件來尋找 `profiler-output`。例如 `estimator.latest_job_profiler_artifacts_path()` 或 `tj.profiler_s3_output_path`。
+ `file_suffix_filter` (清單) — 指定要合併時間軸的檔案尾碼篩選條件清單。可用的附加篩選條件為 `["model_timeline.json", "pythontimeline.json", "trace.json.gz"].`，如果未手動指定此參數，則會依預設值合併所有追蹤檔案。
+ `output_directory` (str) — 指定儲存合併時間軸 JSON 檔案的路徑。預設值為 `path` 參數指定的目錄。

`merge_timeline()` 類別方法傳遞下列參數來執行合併程序：
+ `start` (int) — 指定開始時間 (以微秒和 Unix 時間格式表示) 或開始步驟以合併時間軸。
+ `end` (int) — 指定結束時間 (以微秒和 Unix 時間格式表示) 或結束步驟以合併時間軸。
+ `unit` (str) — 在 `"time"` 和 `"step"` 之間選擇。預設值為 `"time"`。

使用下列範例程式碼，執行 `merge_timeline()` 方法並下載合併的 JSON 檔案。
+ 將時間軸與 `"time"` 單位選項合併。下列範例程式碼會合併 Unix 開始時間 (絕對零 Unix 時間) 與目前 Unix 時間之間的所有可用追蹤檔案，這表示您可以合併整個訓練持續時間的時間軸。

  ```
  import time
  from smdebug.profiler.analysis.utils.merge_timelines import MergedTimeline
  from smdebug.profiler.profiler_constants import CONVERT_TO_MICROSECS
  
  combined_timeline = MergedTimeline(tj.profiler_s3_output_path, output_directory="./")
  combined_timeline.merge_timeline({{0}}, int({{time.time() * CONVERT_TO_MICROSECS}}))
  ```
+ 將時間軸與 `"step"` 單位選項合併。下列範例程式碼會合併步驟 3 和步驟 9 之間的所有可用時間軸。

  ```
  from smdebug.profiler.analysis.utils.merge_timelines import MergedTimeline
  
  combined_timeline = MergedTimeline(tj.profiler_s3_output_path, output_directory="./")
  combined_timeline.merge_timeline({{3}}, {{9}}, unit="{{step}}")
  ```

用 `chrome://tracing` 在 Chrome 瀏覽器上打開 Chrome 追蹤應用程式，然後打開 JSON 檔案。您可以探索輸出以繪製合併的時間軸。