在 ExecuTorch 中调试模型¶
使用 ExecuTorch SDK,用户可以调试其模型以查找数值精度问题,并将模型输出从其设备中提取出来以进行质量分析(例如信噪比、均方误差等)。
目前,ExecuTorch 支持以下调试流程
通过 ETDump 提取模型级输出。
通过 ETDump 提取中间输出(委托外部)
将这些中间输出链接回渴望模式的 Python 代码。
在 ExecuTorch 中调试模型的步骤¶
运行时¶
有关反映以下步骤的真实示例,请参阅 sdk_example_runner.cpp。
[可选] 在导出模型时生成 ETRecord。提供时,这将使用户能够将分析信息链接回渴望模式的源代码(具有堆栈跟踪和模块层次结构)。
将 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.sdk 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)
我们还提供了一套简单的实用程序,使用户能够针对一组参考输出(可能来自渴望模式模型)执行其模型输出的质量分析。
from executorch.sdk.inspector._inspector_utils 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)