onnx-mlir

Logo

MLIR 編譯器基礎架構中 ONNX 模型的表示法和參考降低

在 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 中進行原型設計,可用於偵錯執行階段問題。

編譯以進行儀器化

預設情況下,儀器化已關閉。您需要使用以下命令列選項將其開啟。儀器化的 pass 會使用 --instrument-stage 選項插入在某些階段。例如,當您指定 Onnx 時,儀器化會在 onnx 到 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。正在考慮將其透過編譯器新增到 main_graph 的開頭。

執行儀器化

以通常的方式執行模型。儀器化函式庫將在每個儀器化點印出時間和記憶體使用量。例如,一個模型 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 操作。