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 checksonnx::version_conversion::check_numpy_unibroadcastable_and_require_broadcast() and onnx::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 assertionsonnx::version_conversion::assertNotParams(), onnx::version_conversion::assertInputsAvailable(), and the inline utility onnx::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_sizes is unidirectionally broadcastable into input1_sizes under NumPy rules, and whether broadcasting is actually required.

“Unidirectional” means that input2 is broadcast into input1 (i.e. input1 must be at least as large as input2 in every dimension). This is the semantics used by pre-opset-7 binary operators that accepted an explicit axis attribute.

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:

  • -1 if the inputs are not unidirectionally broadcastable (the shapes are incompatible or input2 has higher rank than input1).

  • 0 if the shapes are identical (no broadcasting needed).

  • 1 if 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_sizes and input2_sizes are 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_ASSERTM when any dimension pair is incompatible (neither equal nor 1).

void assertNotParams(const std::vector<Dimension> &sizes)#

Asserts that every dimension in sizes is 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_ASSERTM when 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 inputs collection has exactly num_inputs elements 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 Value pointers to validate.

  • name – Human-readable operator name used in error messages.

  • num_inputs – Expected number of inputs.

Throws:

OnnxReleaseError – Throws via ONNX_ASSERTM when the input count does not match num_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 INT64 tensor into a std::vector<int64_t>.

Validates that tensor has element type INT64 and, when stored as raw bytes, that the byte count is consistent with the tensor’s declared dimensions. The actual value extraction is delegated to ParseData.

Parameters:

tensor – Source tensor to decode. Must have element type TensorProto_DataType_INT64.

Throws:

OnnxReleaseError – Throws via ONNX_ASSERTM when the element type is not INT64 or 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 of tensor in row-major order. Returns a single-element vector for scalar tensors (empty dims).