dispatch_table.h#
Shape functions opt into type-only tensor inputs with
accepts_type_only_tensor_inputs when registering. This permits builder
inference to run without a ranked tensor descriptor; it must preserve unknown
rank rather than treating it as a scalar. Identity and Abs support this
mode. Functions that require ranked descriptors retain their existing readiness
checks.
Per-(domain, op_type) dispatch table used by :cpp:func:core::shapes::ShapesContext::ComputeShapeNode to forward each NodeProto to the matching ComputeShape* implementation.
The generic dispatch mechanism (this file, :cpp:class:ShapesContext, :cpp:func:InferShapesModel, …) lives in onnx_core so it has no dependency on any particular set of operator implementations. The concrete ComputeShape* functions (“shape functions”) for every standard ONNX operator remain in onnx_shapes and register themselves here via :cpp:func:RegisterComputeShapeFn instead of being hard-coded in this table, which keeps the onnx_core -> onnx_shapes dependency direction from ever being introduced.
-
namespace onnx_light
Alias that makes onnx-light headers compatible with code that references
ONNX_LIGHT_NAMESPACE(the macro used in the standard onnx package).Set to
ONNX_LIGHT_NAMESPACEso both names resolve to the same namespace.Symbol-visibility attribute for the public onnx-light C++ API.
Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when
lib_onnx_protouses hidden visibility by default.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_light(via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs fromonnx.-
namespace core
-
namespace shapes
Typedefs
-
using ComputeShapeFn = std::function<void(ShapesContext&, const NodeProto&)>#
Signature of every per-operator
ComputeShape*trampoline registered in :cpp:func:DispatchTable: it reads the node’s inputs fromctxand inserts the resulting output descriptors back intoctx.
-
using ComputePeakMemoryNodeFn = std::function<PeakMemoryEstimate(Device, const NodeProto&, const std::vector<SymShape>&)>#
Node-aware peak-memory estimator, with access to attributes and optional input names.
Functions
-
const std::unordered_map<std::string, ComputeShapeFn> &DispatchTable()#
Returns the
(normalised_domain, op_type) -> ComputeShape*dispatch table. Empty until shape-function libraries (e.g.onnx_shapes) populate it via :cpp:func:RegisterComputeShapeFn.
-
void RegisterComputeShapeFn(const std::string &domain, const std::string &op_type, ComputeShapeFn fn, bool accepts_structured_inputs = false, bool accepts_type_only_tensor_inputs = false)#
Registers (or replaces) the
ComputeShape*function for (domain,op_type) in the shared :cpp:func:DispatchTable.Use an empty string for
domainto denote the default ONNX domain (normalised to :cpp:var:kOnnxDomain). Intended to be called once per operator during static initialization by shape-function libraries that must not be linked intoonnx_core(e.g.onnx_shapes); seeonnx_shapes::RegisterShapeFunctions.- Parameters:
domain – The operator domain (
""or"ai.onnx"for standard ONNX).op_type – The ONNX operator type name (e.g.
"Abs").fn – The shape function implementing the
ComputeShape*rule.accepts_structured_inputs – Whether the function explicitly handles structured values.
accepts_type_only_tensor_inputs – Whether the function handles tensor types without rank.
-
bool AcceptsStructuredShapeInputs(const std::string &domain, const std::string &op_type)#
Returns whether a registered shape function explicitly accepts structured/encoded inputs.
-
bool AcceptsTypeOnlyTensorShapeInputs(const std::string &domain, const std::string &op_type)#
Returns whether a registered shape function accepts tensor types without a shape descriptor.
-
bool ShapeInputsAvailable(const ShapesContext &shapes, const NodeProto &node)#
Returns whether all node inputs satisfy its shape function’s descriptor requirements.
-
bool HasShapeInference(const ShapesContext &shapes, const NodeProto &node)#
Returns whether model-local, context-local or global shape inference is available.
-
PeakMemoryEstimate UnknownPeakMemory(const std::string &domain, const std::string &op_type)#
Returns an unresolved operator-wide bound, covering every invocation and device.
-
const std::unordered_map<std::string, ComputePeakMemoryNodeFn> &PeakMemoryDispatchTable()#
Returns the
(normalised_domain, op_type) -> ComputePeakMemoryNodeFndispatch table. Empty until libraries populate it via :cpp:func:RegisterComputePeakMemoryFn; operators without an entry report zero additional peak memory through :cpp:func:ComputePeakMemory. Entries may be empty functions recording explicitly zero-memory implementations; :cpp:func:ComputePeakMemorysafely evaluates both callable and empty entries.
-
void RegisterComputePeakMemoryFn(const std::string &domain, const std::string &op_type, Device device, ComputePeakMemoryNodeFn fn, bool overwrite = true)#
Registers (or replaces) the peak-memory function for the identifier (
domain,op_type,device) in the shared :cpp:func:PeakMemoryDispatchTable.Use an empty string for
domainto denote the default ONNX domain (normalised to :cpp:var:kOnnxDomain).deviceis part of the identifier so that a distinct estimator can be registered per device; :cpp:enumerator:Device::kCPU(and :cpp:enumerator:Device::kUndefined) denote the default host entry. Mirrors :cpp:func:RegisterComputeShapeFnso that libraries that must not be linked intoonnx_corecan contribute their per-operator memory estimators. Shape-only queries pass a node containing only its domain and operator type.- Parameters:
domain – The operator domain (
""or"ai.onnx"for standard ONNX).op_type – The ONNX operator type name (e.g.
"Abs").device – The device the estimator applies to (e.g. :cpp:enumerator:
Device::kCPU).fn – The peak-memory function, or empty for zero additional memory. An empty entry also prevents non-overwriting default registration.
overwrite – Whether to replace an existing entry.
-
PeakMemoryEstimate ComputePeakMemory(const ComputePeakMemoryNodeFn &fn, Device device, const NodeProto &node, const std::vector<SymShape> &input_shapes)#
Evaluates and validates an implementation’s estimator without consulting the registry. An empty estimator reports zero additional memory.
-
PeakMemoryEstimate ComputePeakMemory(const std::string &domain, const std::string &op_type, Device device, const std::vector<SymShape> &input_shapes)#
Returns the estimated peak memory (in bytes) for (
domain,op_type) executed ondevicewith inputs of shapeinput_shapes.Looks the operator up in the shared :cpp:func:
PeakMemoryDispatchTableand forwards a node containing only its domain and operator type to its registered :cpp:type:ComputePeakMemoryNodeFn. When no function is registered, returns zero.- Parameters:
domain – The operator domain (
""or"ai.onnx"for standard ONNX).op_type – The ONNX operator type name (e.g.
"Abs").device – The device on which the operator executes.
input_shapes – The shapes of the operator’s inputs, in order.
- Returns:
The mandatory worst-case and optional average peak-memory estimates.
-
PeakMemoryEstimate ComputePeakMemory(Device device, const NodeProto &node, const std::vector<SymShape> &input_shapes)#
Returns additional peak memory for a node, forwarding it to the registered estimator. Unregistered operators return zero. Input shapes retain node input positions; absent optional inputs have empty shapes.
-
PeakMemoryEstimate ComputePeakMemory(const ShapesContext &ctx, Device device, const NodeProto &node)#
Returns additional peak memory using descriptors from a shapes context. Missing named inputs yield an unknown bound for registered estimators, not scalar shapes. Omitted optional inputs retain their empty positional shapes. Unregistered operators return zero, including with incomplete descriptors.
-
struct PeakMemoryEstimate#
- #include <dispatch_table.h>
Describes additional peak memory in bytes, excluding declared inputs and outputs.
worst_caseis always present, as a nonnegative integer or a nonempty symbolic expression. An unresolved symbol requires a caller-supplied bound before memory admission; it never means zero bytes.average_caseis absent when no justified expectation is available. Deterministic workspace requirements have equal worst and average cases.Public Functions
-
PeakMemoryEstimate() = default#
-
inline PeakMemoryEstimate(int64_t bytes)#
Constructs a concrete upper bound without assuming an average.
-
bool operator==(const PeakMemoryEstimate&) const = default#
Public Members
Public Static Functions
-
static inline PeakMemoryEstimate Exact(symbolic::SymDim bytes)#
-
PeakMemoryEstimate() = default#
-
using ComputeShapeFn = std::function<void(ShapesContext&, const NodeProto&)>#
-
namespace shapes
-
namespace core