Skip to main content

onnx_extractor/
lib.rs

1//! # onnx-extractor
2//!
3//! A minimal ONNX model loader for extracting weights, tensors, operations, and graph structure.
4//!
5//! This crate provides a simple interface to load ONNX models and extract:
6//! - Tensor weights & raw data (zero-copy slices, owned buffers, external files)
7//! - Tensor shapes, dimensions, and data types
8//! - Operations and attributes (nodes, inputs/outputs, subgraphs)
9//! - Graph topology and execution order
10//!
11//! ## Zero-Copy Design
12//!
13//! `Tensor::data()` returns a `TensorDataRef` which borrows tensor data without copying:
14//! - Raw variant uses shared ownership (`Bytes`)
15//! - Numeric variants borrow directly (`&[T]`)
16//! - Strings variant borrows elements (`&[Bytes]`)
17//!
18//! `Tensor::into_data()` returns owned `TensorData` without copying:
19//! - Raw variant returns shared ownership (`Bytes`)
20//! - Numeric variants return owned vectors (`Vec<T>`)
21//! - Strings variant returns owned vectors of shared elements (`Vec<Bytes>`)
22//!
23//! Endianness: Raw tensor data uses little-endian byte order as defined in the ONNX specification. Typed fields use native host representation.
24//!
25//! ## Quick Start
26//!
27//! ```rust,no_run
28//! use onnx_extractor::Model;
29//!
30//! let model = Model::load_from_file("model.onnx")?;
31//! println!("{}", model);
32//!
33//! // Access tensor shape and data
34//! if let Some(tensor) = model.graph().tensors().get("weight_1") {
35//!     println!("Weight shape: {:?}", tensor.shape());
36//!     let data = tensor.data()?;
37//!     if let Some(bytes) = data.as_slice() {
38//!         println!("Data size: {} bytes", bytes.len());
39//!     }
40//! }
41//! # Ok::<(), onnx_extractor::Error>(())
42//! ```
43
44// include generated protobuf code inside a small module so we can silence
45// lints and doc warnings originating from the generated file only.
46#[allow(clippy::all, clippy::pedantic, rustdoc::all)]
47mod onnx_generated {
48    include!(concat!(env!("OUT_DIR"), "/onnx.rs"));
49}
50
51pub(crate) mod proto_adapter;
52pub(crate) use onnx_generated::*;
53
54pub mod attribute_value;
55pub mod data_type;
56pub mod error;
57mod external_data;
58pub mod graph;
59pub mod model;
60pub mod operation;
61pub mod tensor;
62
63pub use attribute_value::AttributeValue;
64pub use data_type::DataType;
65pub use error::Error;
66pub use graph::Graph;
67pub use model::Model;
68pub use operation::Operation;
69pub use prost::bytes::Bytes;
70pub use tensor::{Tensor, TensorData, TensorDataRef};