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

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 onnx_kernels

Functions

std::pair<uint64_t, uint64_t> NextUint64(uint64_t state)#

Computes the next SplitMix64 state and the corresponding random 64-bit value. The implementation matches the canonical SplitMix64 generator and is bit-identical to the Python reference in onnx_light.backend.random.

Parameters:

state – Current 64-bit state.

Returns:

std::pair{next_state, random_value}.

template<typename T = double>
std::vector<T> Rand(const std::vector<int64_t> &shape, std::optional<uint64_t> seed = std::nullopt)#

Generates deterministic uniform random values in [0, 1) shaped as shape. An empty shape produces a single value (count 1).

The result element type T is selected by the template parameter (defaults to double). Samples are computed in double precision and then static_cast to T, mirroring the convention used by :cpp:func:Randn. Explicit instantiations are provided for double and float.

Template Parameters:

T – Floating-point output element type (double or float).

Parameters:
  • shape – Output shape; dimensions must be non-negative.

  • seed – Optional 64-bit seed. std::nullopt selects the default seed (0).

Throws:

std::invalid_argument – when any dimension is negative.

Returns:

Flat row-major std::vector<T> of length prod(shape).

std::vector<int64_t> RandInt(int64_t low, int64_t high, const std::vector<int64_t> &shape, std::optional<uint64_t> seed = std::nullopt)#

Generates deterministic pseudo-random integers in the half-open interval [low, high) using rejection sampling on the SplitMix64 output to obtain an unbiased distribution.

Parameters:
  • low – Inclusive lower bound.

  • high – Exclusive upper bound. Must be strictly greater than low.

  • shape – Output shape; dimensions must be non-negative.

  • seed – Optional 64-bit seed. std::nullopt selects the default seed.

Throws:

std::invalid_argument – when high <= low or any dimension is negative.

Returns:

Flat row-major std::vector<int64_t> of length prod(shape).

template<typename T = double>
std::vector<T> Randn(const std::vector<int64_t> &shape, std::optional<uint64_t> seed = std::nullopt)#

Generates deterministic pseudo-random values with an approximate normal distribution using the Irwin-Hall approximation (sum of 12 uniform values minus 6).

The result element type T is selected by the template parameter (defaults to double). Samples are computed in double precision and then static_cast to T, which avoids the repeated cast-to-float loops previously written at each call site. Explicit instantiations are provided for double and float.

Template Parameters:

T – Floating-point output element type (double or float).

Parameters:
  • shape – Output shape; dimensions must be non-negative.

  • seed – Optional 64-bit seed. std::nullopt selects the default seed.

Throws:

std::invalid_argument – when any dimension is negative.

Returns:

Flat row-major std::vector<T> of length prod(shape).

template<typename TInt>
std::vector<TInt> RandnInt(const std::vector<int64_t> &shape, uint64_t seed)#

Builds a signed-integer vector whose elements are drawn from the same Irwin-Hall-approximated :cpp:func:Randn distribution as the upstream np.random.randn(...).astype(np.intN) pattern. Values are truncated via static_cast<TInt> to the destination dtype (matching NumPy’s float-to-int cast semantics for in-range values).

Template Parameters:

TInt – Signed integer output element type.

Parameters:
  • shape – Output shape; dimensions must be non-negative.

  • seed – 64-bit seed forwarded to :cpp:func:Randn.

Returns:

Flat row-major std::vector<TInt> of length prod(shape).

template<typename TUInt>
std::vector<TUInt> RandUint(int64_t high, const std::vector<int64_t> &shape, uint64_t seed)#

Builds an unsigned-integer vector whose elements are drawn uniformly from [0, high) via :cpp:func:RandInt, mirroring the upstream np.random.randint(high, ...) pattern used by the upstream Greater/Less uint variants.

Template Parameters:

TUInt – Unsigned integer output element type.

Parameters:
  • high – Exclusive upper bound forwarded to :cpp:func:RandInt.

  • shape – Output shape; dimensions must be non-negative.

  • seed – 64-bit seed forwarded to :cpp:func:RandInt.

Returns:

Flat row-major std::vector<TUInt> of length prod(shape).

Tensor RandBool(const std::vector<int64_t> &shape, std::optional<uint64_t> seed = std::nullopt)#

Generates a deterministic BOOL Tensor of the requested shape by drawing approximately-normal values from :cpp:func:Randn and thresholding at 0. Mirrors the upstream ONNX test pattern (np.random.randn(...) > 0).astype(bool) used by onnx.backend.test.case.node cases (e.g. And/Or/Xor).

Parameters:
  • shape – Output tensor shape; dimensions must be non-negative.

  • seed – Optional 64-bit seed. std::nullopt selects the default seed.

Throws:

std::invalid_argument – when any dimension is negative.

Returns:

A Tensor with data_type == BOOL whose data is a flat row-major buffer of one byte per element (0 or 1).

Variables

constexpr uint64_t kDefaultSeed = 0#

Default seed used when no explicit seed is provided.