Versions and properties of ONNX Machine Learning model (opset, IR, domain...)

Versions and properties of ONNX Machine Learning model (opset, IR, domain...)

ONNX is an open format for Machine Learning (ML) models, allowing you to interchange models between various ML frameworks and tools. The file content is serialized using Google protobuf.

Because ONNX is a general purpose format for exchanging machine learning models that were created from various providers, the versioning of an ONNX file is somewhat complicated.

This post attempts to explain the versioning of a typical ONNX file.

Within an ONNX file, there are different versions. For a library that parses ONNX files, in order for the parsing to succeed, the ONNX file must have internal versions that are not greater than the maximum supported values.

ONNX - three classes of entities

The ONNX specification defines the versioning policy and mechanism for three classes of entities:

1 - IR version: The intermediate representation (IR) specification, which is the abstract model for graphs and operators and the concrete format that represents them. These are always versioned atomically and are referred to as the IR version.
2 - opset version: Operator specifications that may be referenced by a given ONNX graph. We refer to this as the operator version.
3 - model version: A defined/trained model that defines a specific graph in terms of specific operators. We refer to this as the model version.

Versioning: Semantic or else simply increasing numbers

Only model and release versioning are ALWAYS semantically versioned. Other versions can be either Semantic or else monotonically increasing numbers, depending on the provider.


Operator sets (opsets)

ONNX uses operator sets to group together operator specifications.
Each set of opsets has a domain.
The default domain is an empty string.

Other typical domains that may be present in the versioning information:

- ai.onnx - for general purpose operators. This domain is important when consuming an ONNX file for execution.
- ai.onnx.ml - for machine learning specific operators
- ai.onnx.training - operators used during model training

Depending on the provider, there may be other domains.

ONNX version

The overall versioning of a particular ONNX file or library is the ONNX version. This is semantically versioned, and can be used to know the maximum supported values for the other versions.
The full table of ONNX versions and other versions including IR and also opset for typical domains, can be seen on github.

Here is an example for the earlier versions of ONNX:


From this part of the table, we see that a library that supports ONNX version 1.2 can read files with IR version 3, and with opset[domain = ai.onnx] version 1, 5, 6 or 7. For opset domain 'ai.onnx.ml' only version 1 is supported.

Conversion between different versions of ONNX

Typically, a library with a particular ONNX version can parse ONNX files that have the same or lower  IR and opset versions.

In general, it is possible to convert some ONNX files to lower versions, assuming they do not use newer operators.  The ONNX github repository has a version converter, which has adapters between consecutive versions. The adapters and the versions they handle can be seen in the source code.


Tools:

- Simple command line tool to view the properties of an ONNX file, or to create ONNX file for specific opset version: (github)
- Online ONNX viewer: Lutz Roeder's netron
- ONNX version converter (Python): (github)
- Convert Machine Learning files into ONNX format (https://pypi.org/project/onnxmltools/)

References:

- ONNX versions (github)
- Tensorflow and ONNX (github)

Comments