ONNX-IR
ONNX-IR is a pure Rust library for parsing ONNX models into an intermediate representation (IR) that can be used to generate code for various ML/DL frameworks. It's a core component of the Burn model import system, providing a clean abstraction layer between ONNX protobuf structures and Burn's tensor operations.
Architecture
ONNX-IR converts ONNX protobuf models into a clean intermediate representation through a 5-phase pipeline:
- Initialization: Process initializers and create graph state
- Node Conversion: Convert ONNX nodes to IR with node remapping
- Type Inference: Infer output types with preference propagation
- Post-processing: Optimize graph (eliminate Identity nodes, lift constants)
- Finalization: Remove unused nodes and build final
OnnxGraph
The resulting IR provides:
- Enum-based node representation: Each node is a variant of the
Nodeenum with operation-specific configuration - Typed inputs/outputs: All node arguments are validated with type information
- Pre-extracted configuration: Attributes are parsed into strongly-typed config structs
- Static tensor data: Constant values are available for constant folding
- Support for 100+ ONNX operators: Including control flow (
If,Loop,Scan)
Node Representation
Nodes are represented using an enum where each variant wraps an operation-specific node struct:
// Each node struct contains name, inputs, outputs, and optional config
This design provides type safety, enables trait implementations on specific node types, and uses a
unified macro (define_node_enum!) to generate both NodeType and Node enums from a single
source of truth.
For detailed module documentation, see the inline docs in each module.
Public API
ONNX-IR exposes a clean public API with three main components:
irmodule - Core IR types (OnnxGraph,Node,Argument,TensorType,DType, etc.)nodemodule - Node configurations for all supported operations (e.g.,SoftmaxConfig,Conv2dConfig)OnnxGraphBuilder- Builder for parsing ONNX models from files, bytes, or readersError- Error type for parsing failures
Usage
ONNX-IR is typically used through the burn-import crate, but can also be used standalone:
use ;
// Parse an ONNX model from file (uses mmap when available)
let graph: OnnxGraph = new
.parse_file?;
// Or parse from bytes
let graph = new.parse_bytes?;
// Or parse from a reader
let graph = new.parse_reader?;
// Work with the IR - nodes are represented as an enum
for node in &graph.nodes
// Access node configurations
use ;
// Convert to another framework's representation
// (This is typically done by burn-import or another conversion layer)
Memory-Mapped Loading
By default, ONNX-IR uses memory-mapped file I/O (mmap) when loading models from files. This provides:
- Reduced memory usage: Tensor data is read directly from the file on demand
- Faster startup: No need to copy the entire file into memory upfront
- Lazy loading: Data is only copied when actually accessed
The mmap feature is enabled by default. To disable it:
[]
= { = "...", = false }
When parsing from bytes or readers, the data is copied into memory (mmap only applies to file paths).
ONNX Compatibility
This library recommends ONNX models use opset version 16 or higher for best compatibility. While models with older opset versions may work, opset 16+ ensures access to all supported operators and their latest behavior. If you encounter issues with an older model, consider upgrading it using the ONNX version converter.
Upgrading ONNX Models
You can upgrade your ONNX models using the following Python script:
# Load your ONNX model
=
# Convert the model to opset version 16
=
# Apply shape inference to the upgraded model
=
# Save the converted model
Adding New Node Types
To add support for a new ONNX operator:
Resources
-
ONNX to Burn Conversion Guide: For detailed implementation guidance on adding new operators, see the ONNX to Burn conversion guide.
-
Supported ONNX Operators: For a full list of currently supported ONNX operators, please see the Supported ONNX Operators table.
-
Burn Integration: ONNX-IR serves as the foundation for the ONNX import support in Burn. The conversion from ONNX-IR to Burn graphs is implemented in
burn-import/src/onnx/to_burn.rs.