Expand description
§onnx-runtime-shape-inference
Symbolic shape inference over the onnx_runtime_ir::Graph IR.
This crate is the general, extensible successor to the bounded shape-
inference stopgaps elsewhere in the runtime (the loader’s const-fold-lite
pass and the session’s just-in-time data-dependent resolution). Its design
mirrors the reference implementation
justinchuby/onnx-shape-inference:
- Extensible per-op registry keyed by
(domain, op_type, opset)with range-based version matching (InferenceRegistry). Unregistered ops leave their outputs unresolved rather than failing. - Symbolic dimension arithmetic (
DimExpr) — a small canonical integer polynomial that captures the affine/product forms the op set produces (d0*d1,d0+k,d0/k, reshape-1cancellation), lowered back to IRDims only when writing results. - Shape-DATA propagation (
ShapeData) — tracks the known element values of the small integer tensors inShape → Slice → Concat → Gather → Unsqueeze → Reshapechains, so computed shapes resolve without executing the graph. This is what lets transformer graphs infer statically. - Merge policies (
MergePolicy) —Strict(concrete disagreements are errors) andPermissive(prefer the more specific dim and keep going; the robust default).
§Usage
use onnx_runtime_shape_inference::{InferenceRegistry, MergePolicy};
let registry = InferenceRegistry::default_registry();
let opsets = graph.opset_imports.clone();
let report = registry
.infer_graph(graph, &opsets, MergePolicy::Permissive)
.expect("inference");
assert!(report.fully_resolved());Single-node inference (for testing or custom passes) is available via
InferenceRegistry::infer_node.
§Design invariants
- Model-agnostic. Rules dispatch purely on
(domain, op_type, opset)and tensor metadata — never on model names or op counts. - The IR contract is not modified. Derived dimensions live in this
crate’s
DimExprand are lowered to a fresh symbol when they cannot be expressed as an IRDim. - Permissive by default, never panics on unknown input. Errors are
reserved for genuine contract violations (see
ShapeInferError).
Re-exports§
pub use context::InferenceContext;pub use context::MergePolicy;pub use context::NodeIo;pub use context::SymbolInterner;pub use context::TypeInfo;pub use context::TypedShape;pub use context::merge_shapes;pub use dim_expr::DimExpr;pub use shape_data::MAX_SHAPE_DATA_ELEMS;pub use shape_data::ShapeData;
Modules§
- context
- The inference context handed to each op rule, plus the supporting type
model:
TypeInfo,TypedShape,MergePolicy, and theSymbolInternerthat lowers derivedDimExprs back to IRDims. - dim_
expr - Symbolic dimension arithmetic:
DimExpr. - shape_
data - Shape-DATA propagation:
ShapeData.
Structs§
- Inference
Registry - A registry mapping
(domain, op_type, opset)to anInferenceFn. - Inference
Report - A summary of what whole-graph inference resolved.
Enums§
- Shape
Infer Error - An error produced while inferring shapes.
Type Aliases§
- Inference
Fn - An operator inference rule: reads inputs from the
InferenceContextand sets its outputs’ types (and, where applicable, shape-data).