helper.h#
Broadcasting compatibility checks and input-validity assertions shared by the
per-opset adapter implementations in onnx::version_conversion.
The helpers fall into two groups:
Broadcasting checks –
onnx::version_conversion::check_numpy_unibroadcastable_and_require_broadcast()andonnx::version_conversion::assert_numpy_multibroadcastable()verify that a pair of input shapes conform to NumPy broadcasting rules (unidirectional for pre-opset-7 operators; multidirectional for opset ≥ 7).Input-validity assertions –
onnx::version_conversion::assertNotParams(),onnx::version_conversion::assertInputsAvailable(), and the inline utilityonnx::version_conversion::ReadInt64Tensor()ensure that adapters receive well-formed, concretely-shaped inputs before attempting any shape arithmetic or tensor decoding.
Helper utilities for version-conversion adapters.
This header provides broadcasting compatibility checks and input-validity assertions that are shared across the per-opset adapter implementations in onnx::version_conversion. All functions follow NumPy broadcasting rules.
-
namespace ONNX_LIGHT_NAMESPACE
-
namespace version_conversion
Functions
-
int check_numpy_unibroadcastable_and_require_broadcast(const std::vector<Dimension> &input1_sizes, const std::vector<Dimension> &input2_sizes)#
Checks whether
input2_sizesis unidirectionally broadcastable intoinput1_sizesunder NumPy rules, and whether broadcasting is actually required.“Unidirectional” means that
input2is broadcast intoinput1(i.e.input1must be at least as large asinput2in every dimension). This is the semantics used by pre-opset-7 binary operators that accepted an explicitaxisattribute.- Parameters:
input1_sizes – Shape dimensions of the larger (target) input tensor.
input2_sizes – Shape dimensions of the smaller (source) input tensor. Its rank must not exceed that of
input1_sizes.
- Returns:
-1if the inputs are not unidirectionally broadcastable (the shapes are incompatible orinput2has higher rank thaninput1).0if the shapes are identical (no broadcasting needed).1if the shapes are compatible and broadcasting is required.
-
void assert_numpy_multibroadcastable(const std::vector<Dimension> &input1_sizes, const std::vector<Dimension> &input2_sizes)#
Asserts that
input1_sizesandinput2_sizesare multidirectionally broadcastable under NumPy rules.“Multidirectional” broadcasting (opset ≥ 7) allows either input to be expanded; each dimension pair must satisfy: the values are equal, or at least one of them is 1. The shorter shape is right-aligned before comparison.
- Parameters:
input1_sizes – Shape dimensions of the first input tensor.
input2_sizes – Shape dimensions of the second input tensor.
- Throws:
OnnxReleaseError – Throws via
ONNX_ASSERTMwhen any dimension pair is incompatible (neither equal nor 1).
-
void assertNotParams(const std::vector<Dimension> &sizes)#
Asserts that every dimension in
sizesis a concrete integer value.Symbolic (parametric) dimensions, such as those created from named dimension parameters, are rejected. This is used before opset adapters that need to reason about exact dimension values.
- Parameters:
sizes – Shape dimensions to validate.
- Throws:
OnnxReleaseError – Throws via
ONNX_ASSERTMwhen any dimension is a symbolic parameter rather than a concrete integer.
-
void assertInputsAvailable(const ArrayRef<Value*> &inputs, const char *name, uint64_t num_inputs)#
Asserts that the given
inputscollection has exactlynum_inputselements and that each input has a known shape.All inputs must also pass
assertNotParams(), i.e. their shapes must consist entirely of concrete integer dimensions.- Parameters:
inputs – Collection of input
Valuepointers to validate.name – Human-readable operator name used in error messages.
num_inputs – Expected number of inputs.
- Throws:
OnnxReleaseError – Throws via
ONNX_ASSERTMwhen the input count does not matchnum_inputs, when any input lacks shape information, or when any input shape contains symbolic dimensions.
-
inline std::vector<int64_t> ReadInt64Tensor(const Tensor &tensor)#
Decodes an
INT64tensor into astd::vector<int64_t>.Validates that
tensorhas element typeINT64and, when stored as raw bytes, that the byte count is consistent with the tensor’s declared dimensions. The actual value extraction is delegated toParseData.- Parameters:
tensor – Source tensor to decode. Must have element type
TensorProto_DataType_INT64.- Throws:
OnnxReleaseError – Throws via
ONNX_ASSERTMwhen the element type is notINT64or when the raw byte count does not match the number of elements implied by the tensor’s dimensions.- Returns:
A
std::vector<int64_t>containing all elements oftensorin row-major order. Returns a single-element vector for scalar tensors (empty dims).
-
int check_numpy_unibroadcastable_and_require_broadcast(const std::vector<Dimension> &input1_sizes, const std::vector<Dimension> &input2_sizes)#
-
namespace version_conversion