onnx-mlir

Logo

ONNX 模型在 MLIR 编译器基础设施中的表示和参考下推

在 GitHub 上查看项目 onnx/onnx-mlir

操作指南

使用 Python 进行推理
使用 C/C++ 进行推理
使用 Java 进行推理

参考资料

ONNX 方言
OMTensor C99 运行时 API
OMTensorList C99 运行时 API
OMTensor Java 运行时 API
OMTensorList Java 运行时 API
生成 ONNX 方言
关于文档

开发

添加操作
测试指南
错误处理
命令行选项
插桩
常量传播
添加加速器

工具

工具

RunONNXModel.py
DocCheck

此项目由 onnx 维护

托管于 GitHub Pages — 主题来自 orderedlist

插桩

测量工具在onnx-mlir中进行原型设计,可用于调试运行时问题。

为测量工具编译

默认情况下,测量工具处于关闭状态。您需要使用以下命令行选项将其打开。通过使用 --instrument-stage 选项,测量工具的传递将在某些阶段插入。例如,当您指定 Onnx 时,测量工具将在onnx-to-onnx转换后插入,以获取onnx级别的性能分析。 --instrument-ops 选项是一个用于指定要测量操作的选项。例如,您可以对onnx Conv操作使用 onnx.Conv。此外,您可以对所有onnx操作使用星号,例如 onnx.*,并使用 , 指定两个表达式,例如 onnx.Conv,onnx.Add,用于Conv和Add操作。 --InstrumentBeforeOp--InstrumentAfterOp 是在指定操作之前和/或之后插入测量工具的选项。当您使用 --instrument-ops=onnx.* --InstrumentBeforeOp --InstrumentAfterOp 时,测量工具将在所有onnx操作之前和之后插入。对于NNPA,提供了 ZHighZLow 的附加阶段。您可以使用 --instrument-stage=ZHigh--instrument-ops=onnx.*,zhigh.* 获取onnx和zhigh操作的配置文件,以及使用 --instrument-stage=ZLow--instrument-ops=zlow.* 获取zlow操作的配置文件。

  --instrument-stage=<value>                        - Specify stage to be instrumented:
    =Onnx                                             -   Profile for onnx ops. For NNPA, profile onnx ops before lowering to zhigh.
    =ZHigh                                            -   NNPA profiling for onnx and zhigh ops.
    =ZLow                                             -   NNPA profiling for zlow ops.

  --instrument-ops=<string>                         - Specify operations operations to be instrumented:
                                                      "NONE" or "" for no instrument,
                                                      "ops1,ops2, ..." for the multiple ops.
                                                      e.g. "onnx.Conv,onnx.Add" for Conv and Add ops.
                                                      Asterisk is also available.
                                                      e.g. "onnx.*" for all onnx operations.

  Specify what instrumentation actions at runtime:
      --InstrumentBeforeOp                          - insert instrument before op,
      --InstrumentAfterOp                           - insert instrument after op,
      --InstrumentReportTime                        - instrument runtime reports time usage,
      --InstrumentReportMemory                      - instrument runtime reports memory usage.

目前,初始化调用OMInstrumentInit需要在加载动态库之前添加。目前正在考虑由编译器将其添加到主图的开头。

运行测量工具

以通常的方式运行模型。测量库将在每个测量点打印出时间和内存使用情况。例如,一个模型 mymodel.onnx 使用 onnx-mlir --instrument-stage=Onnx --instrument-ops=onnx.* --InstrumentAfterOp --InstrumentReportMemory --InstrumentReportTime mymodel.onnx 进行编译。其运行时输出如下所示

==PERF-REPORT==, onnx.Cast, bert/encoder/Reshape__27, before, 0.000001, 1692654182.738546
==PERF-REPORT==, onnx.Cast, bert/encoder/Reshape__27, after, 0.000001, 1692654182.738547
==PERF-REPORT==, onnx.Concat, bert/encoder/Reshape__27, before, 0.000000, 1692654182.738547
==PERF-REPORT==, onnx.Concat, bert/encoder/Reshape__27, after, 0.000001, 1692654182.738548
==PERF-REPORT==, onnx.Reshape, bert/encoder/Reshape, before, 0.000001, 1692654182.738549
==PERF-REPORT==, onnx.Reshape, bert/encoder/Reshape, after, 0.000001, 1692654182.738550

这里解释了时间测量的输出。

这里解释了内存测量的输出。

NNPA的其他示例

运行时控制测量工具

通过在运行时提供某些环境变量,可以禁用测量库的报告。

请注意,启用测量工具的唯一方法是在编译时请求它。如果运行时没有启用任何详细报告(例如时间和内存),测量点的进度仍将打印出来。此功能被认为可作为进度指示器。要完全禁用编译时请求的任何输出,您必须设置 ONNX_MLIR_NO_INSTRUMENT

在gdb中使用

测量点的函数称为 OMInstrumentPoint。可以在此函数内部设置断点,以逐步调试onnx操作。