helper.h#

Broadcasting compatibility checks and input-validity assertions shared by the per-opset adapter implementations in onnx_light::version_conversion.

The helpers fall into two groups:

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

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_NAMESPACE so both names resolve to the same namespace.

Symbol-visibility attribute for the public onnx-light C++ API.

Defined as empty because onnx-light does not require explicit __declspec(dllexport) or __attribute__((visibility("default"))) annotations — visibility is controlled at the shared-library level. The macro is provided so that vendored ONNX headers that decorate their declarations with ONNX_API compile without modification.

Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal onnx namespace — rather than the ONNX_NAMESPACE macro — resolves to the onnx-light namespace. The standard onnx package lives in namespace onnx; onnx-light uses onnx_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 from onnx.

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).