onnx-extractor
A minimal ONNX model loader designed for extracting weights, tensor data, operations, and graph structure. Built for zero-copy access with memory mapping (mmap) and lazy external data support.
Model Loading
use Model;
// Load from file (uses mmap)
let model = load_from_file?;
// Load from bytes (e.g. Vec<u8>, Bytes, &'static [u8])
let bytes = read?;
let model = load_from_bytes?;
// Load from bytes with a directory path for external data files
let bytes = read?;
let model = load_from_bytes_with_path?;
Model and Graph Functions
Global metadata is accessed from the Model container, while graph structure and state is accessed via model.graph().
// Model summary
println!;
let graph = model.graph;
// Tensor access
let tensor = graph.tensors.get; // Option<&Tensor>
let tensor_names = graph.tensors.keys; // Iterator<Item = &String>
let inputs = graph.get_input_tensors; // Iterator<Item = &Tensor>
let outputs = graph.get_output_tensors; // Iterator<Item = &Tensor>
let weights = graph.get_weight_tensors; // Iterator<Item = &Tensor>
// Operation access
let operation = graph.get_operation; // Option<&Operation>
let conv_ops = graph.get_operations_by_type; // Iterator<Item = &Operation>
let op_types = graph.operation_types; // HashSet<&str>
let op_counts = graph.count_operations_by_type; // HashMap<&str, usize>
// Topological order
let topo_order = graph.topological_order?; // Vec<&Operation>
// Extracting a tensor (moves out of graph so data can outlive it)
// (Note: `model` must be declared as `mut model`)
let owned_tensor = model.graph_mut.tensors_mut.remove; // Option<Tensor>
Tensor Functions
use TensorDataRef;
let tensor = model.graph.tensors.get.unwrap;
// Name, shape and data type
println!;
println!;
println!;
// Borrow tensor data
let data_ref = tensor.data?; // TensorDataRef<'_>
println!;
// Get data as contiguous byte slice
// Returns Some(&[u8]) for Raw and Numeric variants; returns None for Strings as they are not contiguous in memory
if let Some = data_ref.as_slice
// Access string elements for String tensors
if let Some = data_ref.strings
// Access Raw data
if let Raw = &data_ref
// Consume tensor and get owned data
// This allows the data to outlive the model itself
// (Note: `model` must be declared as `mut model`)
if let Some = model.graph_mut.tensors_mut.remove
Tensor Data Enums
The data() method returns a TensorDataRef which borrows from the model:
The into_data() method returns an owned TensorData (using Bytes or Vec storage from the protobuf):
Operation Functions
let op = model.graph.get_operation.unwrap;
// Basic info
println!;
println!;
println!;
// Attribute access
let attributes = op.attributes;
if let Some = attributes.get
let stride = attributes.get.and_then; // Option<i64>
let activation = attributes.get.and_then; // Option<&Bytes>
// Or for validated UTF-8:
let activation_str = attributes.get.and_then; // Option<&str>
// Subgraph access (Control Flow subgraphs)
if let Some = attributes.get.and_then
Data Types
Access the DataType enum for type checking:
use DataType;
let tensor = model.graph.tensors.get.unwrap;
match tensor.data_type
// Type properties
let size = tensor.data_type.size_in_bytes; // Option<usize>
let is_float = tensor.data_type.is_float;
let is_int = tensor.data_type.is_integer;
External Data Support
ONNX models can store large tensor data in external files. This crate supports lazy loading of external data with automatic caching:
// Load model with external data files
let model = load_from_file?;
// External data files (e.g., "large_model.onnx.data") are automatically discovered
// from the model directory and loaded lazily when tensor data is accessed
let tensor = model.graph.tensors.get.unwrap;
// Data is loaded from external file on first access and cached for subsequent use
let data = tensor.data?;
println!;
// Multiple tensors can share the same external file efficiently
// The file is only loaded once and cached
External Data Features
- Lazy Loading: External files are only loaded when tensor data is accessed
- Shared Caching: Multiple tensors sharing the same external file benefit from caching
- Offset & Length: Supports reading specific ranges from large external files
About the protobuf (onnx.proto)
This crate generates Rust types from the ONNX protobuf at build time using prost-build.
Platform Notes
- Endianness: Raw tensor data uses little-endian byte order as defined in the ONNX specification. Typed fields use native host representation.
License
MIT