torch.func._random.key#
- torch.func._random.key(seed, *, device=None, impl='philox4x32-10')[source]#
Create a PRNG key from a seed.
A key is a tensor that encodes the state needed to deterministically produce random values. Keys are consumed by generation functions to produce reproducible random tensors without any global state. The internal representation of the key depends on the chosen PRNG algorithm.
- Parameters:
seed (int) – The seed value for the PRNG.
device (
torch.device, optional) – The desired device for the returned key. Default: the current default device (seetorch.set_default_device()).impl (str) – PRNG algorithm. Currently only
"philox4x32-10"is supported.
- Returns:
A tensor representing the PRNG key.
- Return type:
Note
For the
"philox4x32-10"algorithm, the key is a uint64 tensor of shape(2,)encoding a(seed, offset)pair. The offset determines the starting position in the Philox output stream.Each Philox call on a
(seed, offset)pair produces 128 random bits, which yieldE = 4values for dtypes of 32 bits or fewer, orE = 2for 64-bit dtypes. Each value ofuniform(),randint(), andbits()comes from its own128 / Ebits, whilenormal()derives each pair of values from each pair’s worth of bits via the Box-Muller transform. Elementi(in row-major order) of a key’s output, or of each slice a batched key generates, is valuei % Eof the call at(offset + i // E) % 2**64. Thus the keytorch.tensor([seed, offset + k], dtype=torch.uint64)skips the firstk * Eelements, generating a contiguous range of the output starting there (e.g. a row shard of a parameter) directly. Such keys share counters withsplit()andfold_in()children of the original key, so don’t both shift and derive from the same key.Example:
>>> key = torch.func._random.key(42, device="cuda")