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 編譯的推論執行檔產生與訓練框架產生不一致的數值結果時,請使用 utils/RunONNXModel.py Python 腳本來除錯數值錯誤。此 Python 腳本將透過 onnx-mlir 和參考後端執行模型,並逐層比較這兩個後端產生的中間結果。

先決條件

參考後端

onnx-mlir 的輸出可以使用參考 ONNX 後端或 protobuf 中的參考輸入和輸出進行驗證。

用法

utils/RunONNXModel.py 支援以下命令列選項

$ python ../utils/RunONNXModel.py  --help
usage: RunONNXModel.py [-h] [--log-to-file [LOG_TO_FILE]] [--model MODEL] [--compile-args COMPILE_ARGS] [--compile-only] [--compile-using-input-shape] [--print-input]
                       [--print-output] [--save-onnx PATH] [--verify {onnxruntime,ref}] [--verify-all-ops] [--verify-with-softmax] [--verify-every-value] [--rtol RTOL]
                       [--atol ATOL] [--save-so PATH | --load-so PATH] [--save-ref PATH] [--load-ref PATH | --shape-info SHAPE_INFO] [--lower-bound LOWER_BOUND]
                       [--upper-bound UPPER_BOUND]

optional arguments:
  -h, --help                  show this help message and exit
  --log-to-file [LOG_TO_FILE] Output compilation messages to file, default compilation.log
  --model MODEL               Path to an ONNX model (.onnx or .mlir)
  --compile-args COMPILE_ARGS Arguments passed directly to onnx-mlir command. See bin/onnx-mlir --help
  --compile-only              Only compile the input model
  --compile-using-input-shape Compile the model by using the shape info getting from the inputs in the reference folder set by --load-ref
  --print-input               Print out inputs
  --print-output              Print out inference outputs produced by onnx-mlir
  --save-onnx PATH            File path to save the onnx model. Only effective if --verify=onnxruntime
  --verify {onnxruntime,ref}  Verify the output by using onnxruntime or reference inputs/outputs. By default, no verification. When being enabled, --verify-with-softmax or --verify-every-value must be used to specify verification mode.
  --verify-all-ops            Verify all operation outputs when using onnxruntime
  --verify-with-softmax       Verify the result obtained by applying softmax to the output
  --verify-every-value        Verify every value of the output using atol and rtol
  --rtol RTOL                 Relative tolerance for verification
  --atol ATOL                 Absolute tolerance for verification
  --save-so PATH              File path to save the generated shared library of the model
  --load-so PATH              File path to load a generated shared library for inference, and the ONNX model will not be re-compiled
  --save-ref PATH             Path to a folder to save the inputs and outputs in protobuf
  --load-ref PATH             Path to a folder containing reference inputs and outputs stored in protobuf. If --verify=ref, inputs and outputs are reference data for verification
  --shape-info SHAPE_INFO     Shape for each dynamic input of the model, e.g. 0:1x10x20,1:7x5x3. Used to generate random inputs for the model if --load-ref is not set
  --lower-bound LOWER_BOUND   Lower bound values for each data type. Used inputs. E.g. --lower-bound=int64:-10,float32:-0.2,uint8:1. Supported types are bool, uint8, int8, uint16, int16, uint32, int32, uint64, int64,float16, float32, float64
  --upper-bound UPPER_BOUND   Upper bound values for each data type. Used to generate random inputs. E.g. --upper-bound=int64:10,float32:0.2,uint8:9. Supported types are bool, uint8, int8, uint16, int16, uint32, int32, uint64, int64, float16, float32, float64

比較在兩種不同編譯選項下模型的輔助腳本。

基於上述 utils/runONNXModel.pyutils/checkONNXModel.py 允許使用者在兩種不同的編譯選項下執行給定的模型兩次,並比較其結果。這讓使用者可以簡單地測試一個新選項,比較編譯器的安全版本(例如 -O0-O3)與更進階的版本(例如 -O3-O3 -march=x86-64)。只需使用 --ref-compile-args--test-compile-args 標誌指定編譯選項,使用 --model 標誌指定模型,並在存在動態形狀輸入的情況下指定 --shape-info。完整選項列在 --help 標誌下。

除錯為運算子產生的程式碼。

如果您知道或懷疑特定的 ONNX MLIR 運算子產生不正確的結果,並想縮小問題範圍,我們提供幾個有用的 Krnl 運算子,允許列印(在執行階段)張量的值或具有原始資料類型的值。

若要在特定的程式點列印出張量的值,請注入以下程式碼(其中 X 是要列印的張量)

create.krnl.printTensor("Tensor X: ", X);

注意:目前僅當張量的秩小於四時才會列印張量的內容。

若要列印訊息和一個值,請注入以下程式碼(其中 val 是要列印的值,而 valType 是其類型)

create.krnl.printf("inputElem: ", val, valType);

尋找記憶體錯誤

如果您知道或懷疑 onnx-mlir 編譯的推論執行檔存在與記憶體配置相關的問題,可以使用 valgrind 框架mtrace 記憶體工具來協助除錯。這些工具追蹤記憶體配置/釋放相關的 API,並且可以偵測記憶體問題,例如記憶體洩漏。

但是,如果問題與記憶體存取相關,特別是緩衝區溢位問題,則很難除錯,因為執行階段錯誤發生在包含問題的點之外。 「Electric Fence 函式庫」可以用於除錯這些問題。它可以協助您偵測兩個常見的程式設計問題:軟體溢出了 malloc() 記憶體配置的邊界,以及軟體存取了已被 free() 釋放的記憶體配置。與其他記憶體除錯器不同,Electric Fence 會偵測讀取存取以及寫入,並且會精確指出導致錯誤的指令。

由於 RedHat 並未正式支援 Electric Fence 函式庫,因此您需要自行下載、建置和安裝原始碼。安裝後,在產生推論執行檔時使用「-lefence」選項連結此函式庫。然後只需執行它,這會導致執行階段錯誤並停在導致記憶體存取問題的地方。您可以使用除錯器或前一節中描述的除錯列印函式來識別該位置。