tensor_compare.h#

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.

Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when lib_onnx_proto uses hidden visibility by default.

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 core
namespace runtime

Functions

TensorComparison CompareTensors(const Tensor &actual, const Tensor &expected, double rtol = 1e-5, double atol = 1e-8, bool equal_nan = false)#

Compares two tensors element-wise within an absolute and relative tolerance, mirroring numpy.allclose.

The comparison first requires actual and expected to share the same data_type and shape. STRING tensors are then compared for exact equality of their string values. Numeric tensors are compared element-wise: two finite values a and b are considered close when |a - b| <= atol + rtol * |b|. NaN and infinity must occur at the same positions in both tensors; a NaN (resp. infinity) in one tensor with a different value at the same position in the other is reported as a mismatch. Infinities must additionally match in sign. NaN values compare unequal unless equal_nan is true, in which case two NaN values at the same position are treated as equal.

The returned :cpp:class:TensorComparison also records the maximum absolute and relative errors over the finite element pairs together with their flat indices (see the struct documentation).

Half-precision (FLOAT16/BFLOAT16) elements are decoded to float before comparison. Element types that cannot be represented as double here (the FLOAT8* variants and the 4-bit / 2-bit packed types) fall back to an exact byte-for-byte comparison.

Parameters:
  • actual – The computed tensor.

  • expected – The reference tensor.

  • rtol – Relative tolerance (default 1e-5).

  • atol – Absolute tolerance (default 1e-8).

  • equal_nan – When true, NaN values in matching positions compare equal (default false).

Returns:

A :cpp:class:TensorComparison describing the outcome.

struct TensorComparison#
#include <tensor_compare.h>

Outcome of :cpp:func:CompareTensors.

close is true when the two tensors match within the requested tolerance. When it is false, message holds a human-readable description of the first mismatch (differing data type, shape, string value, mismatched NaN/infinity positions, or a numeric element outside tolerance).

The error statistics are computed over the finite element pairs of numeric tensors (they stay at their defaults for STRING tensors and for the byte-compared types that cannot be decoded to double). max_abs_error is max |actual - expected| and max_rel_error is max |actual - expected| / |expected| (an element with expected == 0 and actual != 0 contributes an infinite relative error). max_abs_error_index and max_rel_error_index are the flat element indices where those maxima occur (the first occurrence wins on ties), or -1 when no finite pair was compared.

Public Members

bool close = false#

Whether the tensors match within tolerance.

std::string message#

Human-readable description of the first mismatch (empty when close).

double max_abs_error = 0.0#

Largest absolute error |actual - expected| over finite pairs.

int64_t max_abs_error_index = -1#

Flat element index of max_abs_error (-1 if none computed).

double max_rel_error = 0.0#

Largest relative error |actual - expected| / |expected| over finite pairs.

int64_t max_rel_error_index = -1#

Flat element index of max_rel_error (-1 if none computed).