Skip to main content

Module dim_expr

Module dim_expr 

Source
Expand description

Symbolic dimension arithmetic: DimExpr.

onnx-runtime-ir’s Dim can only represent a concrete extent (Dim::Static) or an opaque symbol (Dim::Symbolic). Shape inference for reshape / conv / pool / flatten needs derived dimensions such as d0 * d1, d0 + k, or d0 / k. We do not modify the frozen IR contract; instead this crate reasons over dimensions using a small canonical integer polynomial and only lowers back to an IR [Dim] when writing an inferred shape into the graph (see crate::context::SymbolInterner).

§Representation

A DimExpr is a multivariate polynomial with integer coefficients over the graph’s SymbolIds: a sum of monomials, where each monomial is a sorted product of symbols paired with an i64 coefficient. Canonicalisation (constant folding, term merging, sorted monomials) means two structurally equal expressions compare equal — so identical derived dimensions (e.g. two batch * seq products) intern to the same fresh symbol and stay unified.

This is deliberately not a general CAS: it captures exactly the affine and product forms the Phase-1 op set produces. Operations it cannot represent exactly (floor division by a symbol, non-exact division) surface as None, and the caller falls back to a fresh opaque symbol — the same permissive degrade the reference implementation uses.

§Overflow contract

Coefficients are i64. A pathological (but not necessarily malicious) graph can drive a concrete total past i64::MAX — e.g. a Size/Reshape product over four 2^20 dims is 2^80. Every coefficient combiner (add, sub, mul) is therefore checked: on overflow it does not panic (as unchecked debug arithmetic would) and does not wrap to a bogus — possibly zero or negative — static dim (as unchecked release arithmetic would). Instead the result degrades to an opaque unknown (DimExpr::overflow): an expression that reports as neither a constant nor a bare symbol, poisons any further arithmetic it participates in, and lowers to a fresh symbol (see crate::context::SymbolInterner::lower). This matches the crate’s permissive philosophy: a single pathological dim degrades to “unknown” rather than aborting whole-graph inference. checked_div likewise returns None on overflow (including the i64::MIN / -1 edge) so the caller degrades to a fresh symbol.

Structs§

DimExpr
A canonical integer polynomial over symbolic dimensions.