torch.random
- torch.random.fork_rng(devices=None, enabled=True, _caller='fork_rng', _devices_kw='devices', device_type=None)
Forks the RNG, so that when you return, the RNG is reset to the state that it was previously in.
devices (iterable of Device IDs) – devices for which to fork
the RNG. CPU RNG state is always forked. By default, fork_rng() operates
on all devices, but will emit a warning if your machine has a lot
of devices, since this function will run very slowly in that case.
If you explicitly specify devices, this warning will be suppressed
enabled (bool) – if False, the RNG is not forked. This is a convenience
argument for easily disabling the context manager without having
to delete it and unindent your Python code under it.
device_type (str) – device type str, default is None, in which case the type
is taken from torch.accelerator.current_accelerator(), falling back
to "cuda" when the type cannot be determined. As for supported devices,
see details in accelerator
Returns the random number generator state as a torch.ByteTensor.
See also: torch.random.fork_rng().
Returns the initial seed for generating random numbers as a Python long.
Sets the seed for generating random numbers on all devices. Returns a torch.Generator object.
Sets the seed for generating random numbers to a non-deterministic random number on all devices. Returns a 64 bit number used to seed the RNG.
Sets the random number generator state.
new_state (torch.ByteTensor) – The desired state
Returns a thread-safe random number generator for use in DataLoader workers.
This function provides a convenient way for transforms and user code to use
thread-safe random number generation without manually checking worker context.
When called in a DataLoader thread worker, returns the worker’s thread-local
torch.Generator. When called in the main process or process workers,
returns None (which causes PyTorch functions to use the default global RNG).
:returns: Thread-local generator in thread workers, None otherwise.
:rtype: Optional[torch.Generator]
>>> from torch.random import thread_safe_generator
>>> generator = thread_safe_generator()
>>> torch.randint(0, 10, (5,), generator=generator)
>>> from torch.random import thread_safe_generator
>>> class MyRandomTransform:
... def __call__(self, img):
... generator = thread_safe_generator()
... offset = torch.randint(
Generator | None