shape_broadcast.h#
Shared helpers for shape-inference functions of binary ONNX operators that support numpy-style (multidirectional) broadcasting.
-
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
Enums
Functions
-
SymShape BroadcastShapes(const SymShape &a, const SymShape &b)#
Computes the broadcast result shape of two :cpp:class:
SymShapeoperands following the ONNX (numpy-style) multidirectional broadcasting rules.The shapes are right-aligned and the dimensions are paired starting from the trailing axis (missing leading dimensions are treated as
1). For each paired dimension(d_a, d_b)the resulting dimension is computed as follows:if both are concrete integers: standard broadcasting rules are enforced — equal dimensions or a dimension of
1paired with anything are accepted; mismatching non-unit integers throwstd::invalid_argument;if either is the integer
1: the result is the other dimension;if both are equal (same integer or same symbolic expression): the result is that dimension;
if one is a concrete integer (different from
1) and the other is symbolic: the concrete integer wins (it is the only value compatible with broadcasting against itself);if one symbolic expression is empty (an anonymous dynamic dimension): the other symbolic expression wins;
if both are different symbolic expressions: a fresh symbolic dimension is produced, encoding the broadcast as
"broadcast(<a>, <b>)"so that the symbolic information is preserved. Nested broadcasts are flattened and duplicate symbolic operands removed.
- Throws:
std::invalid_argument – when two concrete integer dimensions are incompatible under broadcasting.
-
void ComputeShapeBinaryBroadcast(ShapesContext &ctx, const NodeProto &node, const char *input_a, const char *input_b, const char *expected_op_type, TensorType output_dtype)#
Generic shape-inference helper for binary ONNX operators that support numpy-style broadcasting. Reads the descriptors of
input_aandinput_bfromctx, computes the broadcast output shape and stores a new entry undernode.output(0)with the givenoutput_dtype.The helper enforces the following preconditions:
node.op_type()must equalexpected_op_type;nodemust declare at least one output;both
input_aandinput_bmust be present inctx.
-
void PropagateValueAsShapeArithmetic(ShapesContext &ctx, const NodeProto &node, const char *input_a, const char *input_b, BroadcastDimOp op)#
Propagates the
ValueAsShapeannotation through a numpy-broadcast binary operator that performs elementwise integer arithmetic (currentlyAddandSub).If both inputs of
nodecarry aValueAsShapeannotation, the helper pairs values elementwise for equal-length vectors or repeats the actual value of a length-one operand using the matching :cpp:func:expressions::dim_add/ :cpp:func:expressions::dim_suboperation, and writes the resulting 1-D dim vector back as theValueAsShapeofnode.output(0)(which must already exist inctx). Empty vectors paired with empty or length-one vectors produce an empty annotation.Does nothing if either input is missing a
ValueAsShape, if their lengths differ and neither is one, or if the resulting shape would exceed :cpp:var:kMaxOptimRankdims.
-
SymShape BroadcastShapes(const SymShape &a, const SymShape &b)#
-
namespace shapes
-
namespace core