shapes#

The shapes sub-namespace of onnx_core (core::shapes) hosts the generic shape-inference engine: ShapesContext, the node/graph traversal (ComputeShapeNode, ComputeShapeGraph, InferShapesModel()), broadcasting and node-checking helpers, and the dispatch table that maps an operator (domain, op_type) pair to the function that computes its output shapes.

onnx_core never depends on onnx_shapes, so the dispatch table starts out empty: it is a mutable registry (RegisterComputeShapeFn()) that onnx_shapes populates with its per-operator ComputeShape* functions (see dispatch_table.h) via onnx_light::onnx_shapes::RegisterShapeFunctions(). Any consumer of the shape-inference engine (Python bindings, tests, examples, …) must call that function once before using InferShapesModel() or ShapesContext.

Peak-memory estimation#

A second, parallel registry estimates each operator’s peak computation memory rather than its output shapes. Mirroring the shape dispatch table, it maps an (domain, op_type, device) identifier to a core::shapes::ComputePeakMemoryNodeFn — a function that takes the Device the operator runs on, the NodeProto, and the SymShape of each input and returns a core::shapes::PeakMemoryEstimate, containing a mandatory worst_case SymDim and an optional average_case. Both represent scratch bytes and preserve symbolic dimensions. Functions are registered with RegisterComputePeakMemoryFn() and looked up through ComputePeakMemory(); operators without a registered function report zero additional workspace. Registered estimators may return an explicit unresolved worst-case symbol. That symbol denotes an operator-wide upper bound covering every invocation and device, which the caller must supply before checking a memory budget. An average is absent unless justified; deterministic workspace requirements have equal worst and average estimates. onnx_kernels owns the built-in estimators (for example Attention), registering them alongside their factories via onnx_light::onnx_kernels::RegisterKernelFunctions(), or separately via onnx_light::onnx_kernels::RegisterPeakMemoryFunctions(). The Python bindings expose compute_peak_memory() together with the Device enum and load the optional kernels extension when available. Reduced builds retain the zero default for absent estimators.

Every estimator uses the same callback signature, PeakMemoryEstimate(Device, const NodeProto &, const std::vector<SymShape> &). Native node-aware calls forward attributes; shape-only calls supply a node with only its domain and operator type, leaving attribute-dependent bounds unresolved.