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_NAMESPACEso 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 withONNX_APIcompile without modification.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_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 fromonnx.-
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 asshape. An emptyshapeproduces a single value (count 1).The result element type
Tis selected by the template parameter (defaults todouble). Samples are computed indoubleprecision and thenstatic_casttoT, mirroring the convention used by :cpp:func:Randn. Explicit instantiations are provided fordoubleandfloat.- Template Parameters:
T – Floating-point output element type (
doubleorfloat).- Parameters:
shape – Output shape; dimensions must be non-negative.
seed – Optional 64-bit seed.
std::nulloptselects the default seed (0).
- Throws:
std::invalid_argument – when any dimension is negative.
- Returns:
Flat row-major
std::vector<T>of lengthprod(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::nulloptselects the default seed.
- Throws:
std::invalid_argument – when
high <= lowor any dimension is negative.- Returns:
Flat row-major
std::vector<int64_t>of lengthprod(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
Tis selected by the template parameter (defaults todouble). Samples are computed indoubleprecision and thenstatic_casttoT, which avoids the repeated cast-to-float loops previously written at each call site. Explicit instantiations are provided fordoubleandfloat.- Template Parameters:
T – Floating-point output element type (
doubleorfloat).- Parameters:
shape – Output shape; dimensions must be non-negative.
seed – Optional 64-bit seed.
std::nulloptselects the default seed.
- Throws:
std::invalid_argument – when any dimension is negative.
- Returns:
Flat row-major
std::vector<T>of lengthprod(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:
Randndistribution as the upstreamnp.random.randn(...).astype(np.intN)pattern. Values are truncated viastatic_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 lengthprod(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 upstreamnp.random.randint(high, ...)pattern used by the upstreamGreater/Lessuint 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 lengthprod(shape).
-
Tensor RandBool(const std::vector<int64_t> &shape, std::optional<uint64_t> seed = std::nullopt)#
Generates a deterministic
BOOLTensorof the requested shape by drawing approximately-normal values from :cpp:func:Randnand thresholding at 0. Mirrors the upstream ONNX test pattern(np.random.randn(...) > 0).astype(bool)used byonnx.backend.test.case.nodecases (e.g.And/Or/Xor).- Parameters:
shape – Output tensor shape; dimensions must be non-negative.
seed – Optional 64-bit seed.
std::nulloptselects the default seed.
- Throws:
std::invalid_argument – when any dimension is negative.
- Returns:
A
Tensorwithdata_type == BOOLwhosedatais a flat row-major buffer of one byte per element (0or1).
Variables
-
constexpr uint64_t kDefaultSeed = 0#
Default seed used when no explicit seed is provided.
-
std::pair<uint64_t, uint64_t> NextUint64(uint64_t state)#
-
namespace onnx_kernels