elementwise_helpers.h#

Defines

ONNX_LIGHT_COMPARE_CASE(ENUM, NAME, CTYPE)#
ONNX_LIGHT_COMPARE_CASE(ENUM, NAME, CTYPE)
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
namespace detail#

Typedefs

using HalfDecodeFunc = float (*)(uint16_t)#
using HalfEncodeFunc = uint16_t (*)(float)#

Functions

Shape BroadcastShape(const char *op_name, const Shape &a, const Shape &b)#

Returns the multidirectional-broadcast output shape of a and b. Throws std::invalid_argument with a message prefixed by op_name when the shapes are not broadcastable.

BroadcastInfo CheckBinaryBroadcast(const char *op_name, const char *dtype_name, int32_t expected_dtype, const Tensor &x, const Tensor &y)#

Verifies both inputs have expected_dtype and that their shapes are multidirectional-broadcastable per the standard NumPy/ONNX rules. Throws std::invalid_argument otherwise.

BroadcastInfo CheckBinaryBroadcastInOut(const char *op_name, const char *in_dtype_name, int32_t expected_in_dtype, const Tensor &x, const Tensor &y)#

Variant of :cpp:func:CheckBinaryBroadcast for kernels whose input and output dtypes differ (e.g. Greater/Less take numeric inputs and return BOOL outputs). Validates that both inputs have expected_in_dtype and computes the broadcast info; the caller is responsible for validating the output against its own dtype.

void CheckPreallocatedOutput(const char *op_name, const char *dtype_name, int32_t expected_dtype, const Shape &expected_shape, size_t expected_bytes, const Tensor &output)#

Verifies the caller-supplied preallocated output tensor matches the expected dtype, shape and byte buffer size.

template<typename TIn, typename TOut, typename Op>
void BinaryElementwise(const char *op_name, const char *dtype_name, int32_t expected_dtype, const Tensor &x, const Tensor &y, Tensor &output, Op op, int64_t parallel_minimum_elements = std::numeric_limits<int64_t>::max())#

In-place element-wise binary kernel driver. Validates inputs + output then invokes op(a, b) -> TOut for each element pair, with full multidirectional broadcasting. TIn and TOut must match the byte layout of the expected_dtype.

template<typename TIn, typename TOut, typename Op>
Tensor BinaryElementwiseAlloc(const char *op_name, const char *dtype_name, int32_t expected_dtype, const Tensor &x, const Tensor &y, Op op, RawBufferAllocator *allocator = nullptr, int64_t parallel_minimum_elements = std::numeric_limits<int64_t>::max())#

Allocating element-wise binary kernel driver. Builds the output tensor with the broadcasted shape and expected_dtype, then delegates to :cpp:func:BinaryElementwise to fill it in.

When allocator is non-null the output buffer is acquired from it directly, so no copy is needed later when the tensor is stored in a :cpp:class:RuntimeContext. Pass nullptr (or omit the argument) to fall back to the legacy inline-allocation path.

template<typename TIn, typename TOut, typename Op>
void BinaryElementwiseInOut(const char *op_name, const char *in_dtype_name, int32_t in_dtype, const char *out_dtype_name, int32_t out_dtype, const Tensor &x, const Tensor &y, Tensor &output, Op op, int64_t parallel_minimum_elements = std::numeric_limits<int64_t>::max())#

Variant of :cpp:func:BinaryElementwise for kernels whose input and output dtypes differ (e.g. Greater/Less). Validates that both inputs have in_dtype and that the preallocated output has out_dtype and the broadcasted shape, then invokes op(a, b) -> TOut for each element pair with full multidirectional broadcasting.

template<typename TIn, typename TOut, typename Op>
Tensor BinaryElementwiseAllocInOut(const char *op_name, const char *in_dtype_name, int32_t in_dtype, const char *out_dtype_name, int32_t out_dtype, const Tensor &x, const Tensor &y, Op op, RawBufferAllocator *allocator = nullptr, int64_t parallel_minimum_elements = std::numeric_limits<int64_t>::max())#

Allocating variant of :cpp:func:BinaryElementwiseInOut. Builds the output tensor with the broadcasted shape and out_dtype, then delegates to :cpp:func:BinaryElementwiseInOut to fill it in.

When allocator is non-null the output buffer is acquired from it directly. Pass nullptr (or omit the argument) to use inline allocation.

template<typename Op>
void BinaryHalfElementwise(const char *op_name, const char *dtype_name, int32_t dtype, const Tensor &x, const Tensor &y, Tensor &output, HalfDecodeFunc decode, HalfEncodeFunc encode, Op op, int64_t parallel_minimum_elements = std::numeric_limits<int64_t>::max())#

In-place half-precision binary element-wise kernel.

template<typename Op>
Tensor BinaryHalfElementwiseAlloc(const char *op_name, const char *dtype_name, int32_t dtype, const Tensor &x, const Tensor &y, HalfDecodeFunc decode, HalfEncodeFunc encode, Op op, RawBufferAllocator *allocator = nullptr, int64_t parallel_minimum_elements = std::numeric_limits<int64_t>::max())#

Allocating half-precision binary element-wise kernel.

When allocator is non-null the output buffer is acquired from it directly. Pass nullptr (or omit the argument) to use inline allocation.

template<typename TOut, typename Op>
void BinaryHalfElementwiseInOut(const char *op_name, const char *in_dtype_name, int32_t in_dtype, const char *out_dtype_name, int32_t out_dtype, const Tensor &x, const Tensor &y, Tensor &output, HalfDecodeFunc decode, Op op)#

In-place half-precision binary kernel for FLOAT16/BFLOAT16 inputs and a distinct POD output dtype (for example BOOL in comparison operators).

template<typename TOut, typename Op>
Tensor BinaryHalfElementwiseAllocInOut(const char *op_name, const char *in_dtype_name, int32_t in_dtype, const char *out_dtype_name, int32_t out_dtype, const Tensor &x, const Tensor &y, HalfDecodeFunc decode, Op op, RawBufferAllocator *allocator = nullptr)#

Allocating variant of :cpp:func:BinaryHalfElementwiseInOut.

When allocator is non-null the output buffer is acquired from it directly. Pass nullptr (or omit the argument) to use inline allocation.

template<typename Op>
void UnaryHalfElementwise(const Tensor &x, Tensor &output, HalfDecodeFunc decode, HalfEncodeFunc encode, int64_t grain_size, Op op)#

Unary half-precision element-wise kernel (single-pass, no allocation).

template<typename Op>
void UnaryHalfElementwise(const Tensor &x, Tensor &output, HalfDecodeFunc decode, HalfEncodeFunc encode, Op op)#

Unary half-precision element-wise kernel using the default parallel grain.

template<typename Op>
void BinaryHalfCompareElementwise(const char *op_name, const char *dtype_name, int32_t dtype, const Tensor &x, const Tensor &y, Tensor &output, HalfDecodeFunc decode, Op op, int64_t parallel_minimum_elements = std::numeric_limits<int64_t>::max())#

In-place half-precision binary comparison kernel (decode→compare→BOOL).

template<typename Op>
Tensor BinaryHalfCompareElementwiseAlloc(const char *op_name, const char *dtype_name, int32_t dtype, const Tensor &x, const Tensor &y, HalfDecodeFunc decode, Op op, RawBufferAllocator *allocator = nullptr, int64_t parallel_minimum_elements = std::numeric_limits<int64_t>::max())#

Allocating half-precision binary comparison kernel (decode→compare→BOOL).

When allocator is non-null the output buffer is acquired from it directly. Pass nullptr (or omit the argument) to use inline allocation.

template<typename Op>
Tensor BinaryComparisonAlloc(const char *op_name, const Tensor &x, const Tensor &y, Op op, RawBufferAllocator *allocator, int64_t parallel_minimum_elements)#

Dispatches one numeric binary comparison and allocates its BOOL output.

template<typename Op>
void BinaryComparison(const char *op_name, const Tensor &x, const Tensor &y, Tensor &output, Op op, int64_t parallel_minimum_elements)#

Dispatches one numeric binary comparison into a preallocated BOOL output.

struct BroadcastInfo#
#include <elementwise_helpers.h>

Information about a validated binary broadcast: the output shape, total element count, the individual input element counts, and per-input element-strides aligned to the output rank (a stride of 0 marks a broadcast dimension). The rank-aligned shape_x/shape_y are also reported for diagnostics. nx/ny are kept for fast-path detection (equal-shape or scalar broadcasting).

Public Members

Shape shape#
Shape shape_x#
Shape shape_y#
std::vector<int64_t> strides_x#
std::vector<int64_t> strides_y#
int64_t element_count = 0#
int64_t nx = 0#
int64_t ny = 0#