• 文档 >
  • 在 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 的完整开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题解答

查看资源