Skip to main content

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 domain;
50mod dtype;
51mod error;
52mod graph;
53mod layout;
54mod node;
55mod shape;
56mod tensor;
57mod value;
58
59pub use arena::{Arena, ArenaKey};
60pub use device::{DeviceId, DeviceType};
61pub use domain::{AI_ONNX_DOMAIN, is_default_domain, normalize_domain};
62pub use dtype::DataType;
63pub use error::{GraphError, IrError, Result};
64pub use graph::Graph;
65pub use layout::{
66    MemoryFormat, TensorLayout, broadcast_shapes, compute_contiguous_strides, is_contiguous,
67};
68pub use node::{Attribute, Node, NodeId};
69pub use shape::{
70    Dim, Shape, SymbolConstraints, SymbolId, as_static_shape, is_fully_static, static_shape,
71};
72pub use tensor::{SparseTensorData, TensorData, TypeProto, WeightRef};
73pub use value::{Consumers, Usage, Value, ValueId};