• 文件 >
  • 在 ExecuTorch 中除錯模型
快捷方式

在 ExecuTorch 中除錯模型

使用 ExecuTorch 開發者工具,使用者可以除錯其模型的數值精度問題,並從裝置中提取模型輸出以進行質量分析(如信噪比、均方誤差等)。

目前,ExecuTorch 支援以下除錯流程:

  • 透過 ETDump 提取模型級別輸出。

  • 透過 ETDump 提取中間輸出(委託之外的)。

    • 將這些中間輸出連結回 Eager 模型 Python 程式碼。

在 ExecuTorch 中除錯模型的步驟

執行時

關於反映以下步驟的實際示例,請參閱 example_runner.cpp

  1. [可選] 在匯出模型時生成 ETRecord。提供此資訊後,使用者可以將效能分析資訊連結回 Eager 模型原始碼(包含堆疊跟蹤和模組層次結構)。

  2. ETDump 生成 整合到執行時中,並透過配置 ETDumpGen 物件來設定除錯級別。然後,提供一個額外的緩衝區,用於寫入中間輸出和程式輸出。目前我們支援兩個除錯級別:

    • 程式級別輸出

    Span<uint8_t> buffer((uint8_t*)debug_buffer, debug_buffer_size);
    etdump_gen.set_debug_buffer(buffer);
    etdump_gen.set_event_tracer_debug_level(
        EventTracerDebugLogLevel::kProgramOutputs);
    
    • 已執行(非委託)操作的中間輸出(也將包含程式級別輸出)

    Span<uint8_t> buffer((uint8_t*)debug_buffer, debug_buffer_size);
    etdump_gen.set_debug_buffer(buffer);
    etdump_gen.set_event_tracer_debug_level(
        EventTracerDebugLogLevel::kIntermediateOutputs);
    
  3. 使用啟用除錯事件跟蹤的預處理器標誌構建執行時。說明可在 ETDump 文件 中找到。

  4. 執行模型並按照此處所述轉儲 ETDump 緩衝區。(如果上面配置了除錯緩衝區,也類似操作)

執行後使用 Inspector API 訪問除錯輸出

模型執行完成後,使用生成的 ETDump 和除錯緩衝區,使用者可以利用 Inspector API 來檢查這些除錯輸出。

from executorch.devtools import Inspector

# Create an Inspector instance with etdump and the debug buffer.
inspector = Inspector(etdump_path=etdump_path,
            buffer_path = buffer_path,
            # etrecord is optional, if provided it'll link back
            # the runtime events to the eager model python source code.
            etrecord = etrecord_path)

# Accessing program outputs is as simple as this:
for event_block in inspector.event_blocks:
    if event_block.name == "Execute":
        print(event_blocks.run_output)

# Accessing intermediate outputs from each event (an event here is essentially an instruction that executed in the runtime).
for event_block in inspector.event_blocks:
    if event_block.name == "Execute":
        for event in event_block.events:
            print(event.debug_data)
            # If an ETRecord was provided by the user during Inspector initialization, users
            # can print the stacktraces and module hierarchy of these events.
            print(event.stack_traces)
            print(event.module_hierarchy)

我們還提供了一套簡單的實用工具,允許使用者對照一組參考輸出(可能來自 Eager 模式模型)對其模型輸出進行質量分析。

from executorch.devtools.inspector import compare_results

# Run a simple quality analysis between the model outputs sourced from the
# runtime and a set of reference outputs.
#
# Setting plot to True will result in the quality metrics being graphed
# and displayed (when run from a notebook) and will be written out to the
# filesystem. A dictionary will always be returned which will contain the
# results.
for event_block in inspector.event_blocks:
    if event_block.name == "Execute":
        compare_results(event_blocks.run_output, ref_outputs, plot = True)

文件

訪問 PyTorch 的完整開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源