Module Tolk_frontend.Rand
Random number generation.
Random tensors are produced by a counter-based generator: a per-device key tensor (derived from the process seed) and a counter tensor track a position in a fixed random stream, and each draw builds a graph that mixes the counter with the key (Elementwise.threefry) and advances the counter in place. Because the state lives in device tensors and the advance is part of the graph, a captured computation (Jit) that draws random numbers keeps drawing fresh values on every replay.
Results are deterministic given a seed: after manual_seed, the same sequence of calls produces the same values, on any device. Every dimension of a requested shape must be non-negative; Invalid_argument is raised otherwise. In this version rand (and the samplers built on it) only generates 32-bit floats; rand raises Invalid_argument for other float dtypes.
manual_seed seed sets the process seed and resets all generator state, restarting the random stream. Without a call to manual_seed the seed is taken from the clock at startup.
Uniform
val rand :
?dtype:Tolk_uop.Dtype.Val.t ->
?contiguous:bool ->
int list ->
Tensor.trand shape is a tensor of shape shape filled with uniform random values in [0, 1). dtype must be a 32-bit float type (default: the default float). contiguous (default true) materialises the result into its own buffer.
val rand_like :
?dtype:Tolk_uop.Dtype.Val.t ->
?contiguous:bool ->
Tensor.t ->
Tensor.trand_like t is rand with the shape of t and, unless overridden, the dtype of t.
val uniform :
?low:float ->
?high:float ->
?dtype:Tolk_uop.Dtype.Val.t ->
int list ->
Tensor.tuniform shape is filled with uniform random values in [low, high) (default [0, 1)), cast to dtype (default: the default float) before the lower bound is added.
val randint :
?low:int ->
?high:int ->
?dtype:Tolk_uop.Dtype.Val.t ->
int list ->
Tensor.trandint shape is filled with uniform random integers in [low, high) (default [0, 10)). dtype must be an integer type (default int32).
val scaled_uniform : ?dtype:Tolk_uop.Dtype.Val.t -> int list -> Tensor.tscaled_uniform shape is uniform over [-1, 1) scaled by 1/sqrt(numel).
val glorot_uniform : ?dtype:Tolk_uop.Dtype.Val.t -> int list -> Tensor.tglorot_uniform shape is the Glorot (Xavier) uniform initialisation: uniform over [-b, b) with b = sqrt (6 / (shape.(0) + prod rest)).
val kaiming_uniform :
?a:float ->
?dtype:Tolk_uop.Dtype.Val.t ->
int list ->
Tensor.tkaiming_uniform shape is the Kaiming (He) uniform initialisation for a leaky ReLU with negative slope a (default 0.01): uniform over [-b, b) with b = sqrt (6 / (1 + a^2) / prod (tl shape)).
Normal
val randn : ?dtype:Tolk_uop.Dtype.Val.t -> int list -> Tensor.trandn shape is filled with standard normal random values (mean 0, variance 1), sampled at 32-bit float precision and cast to dtype (default: the default float).
val randn_like : ?dtype:Tolk_uop.Dtype.Val.t -> Tensor.t -> Tensor.trandn_like t is randn with the shape of t and, unless overridden, the dtype of t.
val normal :
?mean:float ->
?std:float ->
?dtype:Tolk_uop.Dtype.Val.t ->
int list ->
Tensor.tnormal shape is filled with normal random values with the given mean (default 0) and standard deviation std (default 1).
val kaiming_normal :
?a:float ->
?dtype:Tolk_uop.Dtype.Val.t ->
int list ->
Tensor.tkaiming_normal shape is the Kaiming (He) normal initialisation for a leaky ReLU with negative slope a (default 0.01): normal with std = sqrt (2 / (1 + a^2) / prod (tl shape)).
Sampling
val randperm : ?dtype:Tolk_uop.Dtype.Val.t -> int -> Tensor.trandperm n is a uniformly random permutation of the integers 0 .. n-1, as a tensor of dtype dtype (default int32).
multinomial weights draws num_samples (default 1) indices from the categorical distribution proportional to weights, which must be 1- or 2-dimensional (a batch of rows). The result is int32, with one index per sample (per row for a 2-D input, shaped (rows, num_samples)). Without replacement (the default) the samples within a row are distinct.