onnx_runtime_ir/lib.rs
1//! # `onnx-runtime-ir`
2//!
3//! The Graph intermediate representation (IR) for the ORT 2.0 runtime.
4//!
5//! This crate is the **stable contract** that every downstream runtime crate
6//! (`onnx-runtime-loader`, `onnx-runtime-ep-api`, `onnx-runtime-session`, …)
7//! builds against. It is intentionally pure, safe Rust with no FFI and no
8//! device dependencies so it compiles standalone on any target.
9//!
10//! It is a Rust port of the design captured in `docs/ORT2.md` §3 (Graph IR),
11//! §5 (Striding & Layout) and §11 (Dynamic Shape), itself inspired by the
12//! Python [`onnx-ir`](https://github.com/onnx/ir-py) package.
13//!
14//! ## What lives here
15//!
16//! | Concept | Type |
17//! |---------|------|
18//! | Element type | [`DataType`] |
19//! | Symbolic / static shapes | [`Shape`], [`Dim`], [`SymbolConstraints`] |
20//! | Physical strided layout | [`TensorLayout`], [`MemoryFormat`] |
21//! | Device placement | [`DeviceType`], [`DeviceId`] |
22//! | Graph values (SSA edges) | [`Value`], [`ValueId`] |
23//! | Graph operations | [`Node`], [`NodeId`], [`Attribute`] |
24//! | Constant / weight storage | [`TensorData`], [`SparseTensorData`], [`WeightRef`] |
25//! | The graph itself | [`Graph`] |
26//! | Errors | [`IrError`], [`GraphError`] |
27//!
28//! ## Design guarantees
29//!
30//! * **SSA-like:** every [`Value`] has at most one producer [`Node`]; node
31//! outputs are unique.
32//! * **First-class layout & device:** every value carries a [`TensorLayout`]
33//! and an optional [`DeviceId`], unlike upstream ONNX / `onnx-ir`.
34//! * **Mutable during optimization:** the [`Graph`] mutation API keeps
35//! producer/consumer edges consistent so optimization passes can rewrite it,
36//! then it is shared immutably via `Arc` once frozen.
37//!
38//! Deep algorithms whose full implementation belongs to a later task (e.g.
39//! per-op shape inference) are represented here only by their data model; the
40//! `Graph` operations that are cheap and foundational (topological ordering,
41//! validation, edge rewiring, broadcasting, stride arithmetic) are fully
42//! implemented and unit-tested so downstream crates compile against a stable,
43//! working surface.
44
45#![forbid(unsafe_code)]
46
47mod arena;
48mod device;
49mod dtype;
50mod error;
51mod graph;
52mod layout;
53mod node;
54mod shape;
55mod tensor;
56mod value;
57
58pub use arena::{Arena, ArenaKey};
59pub use device::{DeviceId, DeviceType};
60pub use dtype::DataType;
61pub use error::{GraphError, IrError, Result};
62pub use graph::Graph;
63pub use layout::{
64 broadcast_shapes, compute_contiguous_strides, is_contiguous, MemoryFormat, TensorLayout,
65};
66pub use node::{Attribute, Node, NodeId};
67pub use shape::{
68 as_static_shape, is_fully_static, static_shape, Dim, Shape, SymbolConstraints, SymbolId,
69};
70pub use tensor::{SparseTensorData, TensorData, TypeProto, WeightRef};
71pub use value::{Consumers, Usage, Value, ValueId};