torch.func._random.fold_in#
- torch.func._random.fold_in(key, data)[source]#
Deterministically derive a new key by folding in an integer value.
datamay be a Pythonintor a single-itemuint64tensor on the same device askey. Note that passingdataas a tensor prevents it from being baked into a captured CUDA graph, so the graph can be replayed with a different value without recapture.Equivalent to
split(key, data + 1)[data], but more efficient when only a single derived key is needed. Useful for associating a key with a loop iteration, layer index, or other integer identifier.Supports batched keys: if
keyhas shape(*batch, K),datais folded into each key independently.- Parameters:
key (Tensor) – A PRNG key returned by
key(),split(), orfold_in().data (int or Tensor) – The value to fold into the key, interpreted as uint64. An
intmust be within the inclusive range[-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]. Negative inputs are remapped to positive values with the formula0x1_0000_0000_0000_0000 + data. A tensor must have dtypeuint64, contain a single value, and reside on the same device askey.
- Returns:
A new key tensor with the same shape as
key.- Return type:
Example:
>>> key = torch.func._random.key(42, device="cuda") >>> k0 = torch.func._random.fold_in(key, 0) >>> k1 = torch.func._random.fold_in(key, 1) >>> # Equivalent to split: >>> keys = torch.func._random.split(key, 2) >>> assert torch.equal(k0, keys[0]) >>> assert torch.equal(k1, keys[1])