ONNX (Open Neural Network Exchange) 是一種 AI 檔案交換標準 最近在 porting DeepSeek 到 NPU 的過程中, 卡住了一下, 所以特別記錄下來.
首先 ONNX 有兩個主要的 domain: ai.onnx 和 ai.onnx.ml. 雖然他們都是為 AI 而生, 但ai.onnx domain 的 operator (=運算子, 算子, 運算符) 更加常用, 像是加減、乘加這種很基本的運算, 或是比較複雜的 convolution, activation function, 以及 DNN 有關的 RNN, dropput…等等.
ai.onnx.ml 的算子適用於傳統的機器學習, 包括非深度學習的算法, 像是 one hot encoder, 還有 SVM, imputer…等等. 至於其他的 domain, 屬於客製化擴展, 官方不維護 [1]. 包括: ai.onnx.training, com.microsoft, com.ms.internal….等等.
接下來也很妙. ONNX 進版後, 雖然看起來也是以 1.0, 1.1,….這樣長大, 但是官方又出了個 IR version, 用來表示是否有 breaking change. 所以顯然很多版 version 會對應到同一版 IR version.
“The IR format is versioned using simple numbers, which MUST be monotonically increasing. Breaking changes to the format or semantics of the ONNX specification require an increment of the version. Non-breaking changes to the IR format do not require changing the version number."
光是這樣也就罷了, 由於還有 ai.onnx 和 ai.onnx.ml 兩個 domain 之分. 如果對某個 domain 是 breaking change, 但對另外一個 domain 不是呢? 所以對於不同的 domain, 又有 Opset 的 version.
“ONNX uses operator sets to group together immutable operator specifications. An operator set represents a specific version of a domain, indicated by a pair (domain, version). This represents the set of all operators belonging to the specified domain with the specified version (referred to as the opset_version). When the inventory of a given operator set changes either by addition, removal, or a change in semantics of a contained operator, its version MUST increase."
因此我們可以得到這樣的表格 – Released Versions
| ONNX version | IR version | Opset version ai.onnx | Opset version ai.onnx.ml | Opset version ai.onnx.training |
|---|---|---|---|---|
| 1.0 | 3 | 1 | 1 | – |
| 1.1 | 3 | 5 | 1 | – |
| 1.1.2 | 3 | 6 | 1 | – |
| 1.2 | 3 | 7 | 1 | – |
| 1.3 | 3 | 8 | 1 | – |
| 1.4.1 | 4 | 9 | 1 | – |
| 中間省略, 詳情參考 [1] | ||||
| 1.14.1 | 9 | 19 | 3 | 1 |
| 1.15.0 | 9 | 20 | 4 | 1 |
| 1.16.0 | 10 | 21 | 5 | 1 |
| 1.17.0 | 10 | 22 | 5 | 1 |
當我試圖將 ONNX INT4 的 DeepSeek 翻成 NPU 指令時, converter 跑出下面這個 error, 表示在 domain version 14 當中找不到 SimplifiedLayerNormalization 這個算子. 而我們安裝的 ONNX 通常是 ai.onnx domain. 既然在預設的 domain 裡面找不到, 我們就要檢查一下從 huggingface 抓來的 DeepSeek 是哪個 domain 的 ONNX?
File "/local/workspace/hailo_virtualenv/lib/python3.10/site-packages/onnx/checker.py", line 163, in check_model
C.check_model_path(
onnx.onnx_cpp2py_export.checker.ValidationError: No Op registered for SimplifiedLayerNormalization with domain_version of 14
==> Context: Bad node spec for node. Name: /model/layers.0/input_layernorm/LayerNorm OpType: SimplifiedLayerNormalization
寫個小程式檢查 node 的 domain:
import onnx
model = onnx.load("model.onnx")
# 查看所有算子的 domain
for node in model.graph.node:
print(f"Op: {node.op_type}, Domain: {node.domain}")
結果悲劇了… 大部分都是 com.microsoft domain, 難怪會出錯. 經過測試, 這個 int4 版本除了回答品質不太穩之外, 還會無限迴圈重複自己的話, 所以暫且就不 debug 下去了, 僅記錄下 ONNX version 的複雜性.

[REF]