在 ExecuTorch 中除錯模型¶
透過 ExecuTorch 開發人員工具,使用者可以除錯其模型中的數值不準確性,並從其裝置提取模型輸出以進行品質分析(例如訊號雜訊比、均方誤差等)。
目前,ExecuTorch 支援以下除錯流程
透過 ETDump 提取模型層級輸出。
透過 ETDump 提取中介輸出 (delegate 之外)
將這些中介輸出連結回 eager 模型的 Python 程式碼。
在 ExecuTorch 中偵錯模型的步驟¶
執行時期 (Runtime)¶
若要查看反映以下步驟的實際範例,請參閱 example_runner.cpp。
[可選] 在匯出模型時產生 ETRecord。如果提供此選項,使用者便能將 profiling 資訊連結回 eager 模型原始碼 (包含堆疊追蹤和模組層級結構)。
將 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);
已執行 (非 delegated) 操作的中介輸出 (也會包含程式層級輸出)
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);
使用啟用追蹤偵錯事件的預處理器旗標來建置執行時期。說明位於 ETDump 文件 中。
執行您的模型並傾印出 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)