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.
Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when
lib_onnx_protouses hidden visibility by default.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 core
-
namespace runtime
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 Shape &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 Shape &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 Shape &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).
-
Tensor RandnTensor(int32_t element_type, const Shape &shape, std::optional<uint64_t> seed = std::nullopt, RawBufferAllocator *allocator = nullptr)#
Generates a deterministic normally distributed tensor of the requested type and shape.
Supports
FLOAT,DOUBLE,FLOAT16,BFLOAT16,INT8,INT16,INT32,INT64, andBOOL. Integer samples follow :cpp:func:RandnInt; boolean samples are obtained by thresholding normally distributed values at zero.- Parameters:
element_type – Tensor element type.
shape – Output shape; dimensions must be non-negative.
seed – Optional 64-bit seed.
std::nulloptselects the default seed.allocator – Optional allocator for the tensor buffer.
- Throws:
std::invalid_argument – when the element type is unsupported or a dimension is negative.
- Returns:
A tensor containing deterministic normally distributed values.
-
template<typename T>
void RandUniformInto(T *dst, int64_t count, double low, double high, std::optional<uint64_t> seed = std::nullopt)# Generates
countuniform samples in[low, high)and writes them directly intodstwithout an intermediate allocation. Samples are computed indoubleprecision andstatic_casttoT.- Template Parameters:
T – Floating-point output element type (
doubleorfloat).- Parameters:
dst – Destination buffer of at least
countelements.count – Number of values to generate.
low – Inclusive lower bound.
high – Exclusive upper bound.
seed – Optional 64-bit seed.
std::nulloptselects the default seed.
-
template<typename T>
void RandNormalInto(T *dst, int64_t count, double mean, double scale, std::optional<uint64_t> seed = std::nullopt)# Generates
countnormal samples using the Irwin-Hall approximation and writes them directly intodstwithout an intermediate allocation. Samples are computed indoubleprecision andstatic_casttoT.- Template Parameters:
T – Floating-point output element type (
doubleorfloat).- Parameters:
dst – Destination buffer of at least
countelements.count – Number of values to generate.
mean – Distribution mean.
scale – Distribution standard deviation.
seed – Optional 64-bit seed.
std::nulloptselects the default seed.
-
template<typename TInt>
std::vector<TInt> RandnInt(const Shape &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 Shape &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 Shape &shape, std::optional<uint64_t> seed = std::nullopt, RawBufferAllocator *allocator = nullptr)#
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.allocator – Optional allocator for the tensor buffer.
- 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 runtime
-
namespace core