在 ExecuTorch 中调试模型¶
借助 ExecuTorch 开发者工具,用户可以调试模型的数值不准确性,并从设备中提取模型输出,以进行质量分析(例如信噪比、均方误差等)。
目前,ExecuTorch 支持以下调试流程
通过 ETDump 提取模型级别的输出。
通过 ETDump 提取中间输出(委托之外)
将这些中间输出链接回 eager 模式模型 Python 代码。
在 ExecuTorch 中调试模型的步骤¶
运行时¶
有关反映以下步骤的真实示例,请参阅 example_runner.cpp。
[可选] 在导出模型时生成 ETRecord。如果提供,这将使用户能够将分析信息链接回 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);
已执行(非委托)操作的中间输出(也将包括程序级别输出)
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 缓冲区,如 此处 所述。(对上述配置的调试缓冲区执行类似操作)
在运行后使用检查器 API 访问调试输出¶
一旦模型运行完毕,用户可以使用生成的 ETDump 和调试缓冲区,利用 检查器 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)