1 from torch
import _C, device
2 from .
import _lazy_init, _lazy_call, device_count, device
as device_ctx_manager
4 __all__ = [
'get_rng_state',
'get_rng_state_all',
5 'set_rng_state',
'set_rng_state_all',
6 'manual_seed',
'manual_seed_all',
7 'seed',
'seed_all',
'initial_seed']
10 def get_rng_state(device=device(
'cuda')):
11 r"""Returns the random number generator state of the current 15 device (torch.device or int, optional): The device to return the RNG state of. 16 Default: ``torch.device('cuda')`` (i.e., the current CUDA device). 19 This function eagerly initializes CUDA. 22 with device_ctx_manager(device):
23 return _C._cuda_getRNGState()
26 def get_rng_state_all():
27 r"""Returns a tuple of ByteTensor representing the random number states of all devices.""" 30 for i
in range(device_count()):
31 with device_ctx_manager(i):
32 results.append(get_rng_state())
36 def set_rng_state(new_state, device=device(
'cuda')):
37 r"""Sets the random number generator state of the current GPU. 40 new_state (torch.ByteTensor): The desired state 41 device (torch.device or int, optional): The device to set the RNG state. 42 Default: ``torch.device('cuda')`` (i.e., the current CUDA device). 44 new_state_copy = new_state.clone()
53 with device_ctx_manager(device):
54 _C._cuda_setRNGState(new_state_copy)
59 def set_rng_state_all(new_states):
60 r"""Sets the random number generator state of all devices. 63 new_state (tuple of torch.ByteTensor): The desired state for each device""" 64 for i, state
in enumerate(new_states):
65 set_rng_state(state, i)
68 def manual_seed(seed):
69 r"""Sets the seed for generating random numbers for the current GPU. 70 It's safe to call this function if CUDA is not available; in that 71 case, it is silently ignored. 74 seed (int): The desired seed. 77 If you are working with a multi-GPU model, this function is insufficient 78 to get determinism. To seed all GPUs, use :func:`manual_seed_all`. 81 _lazy_call(
lambda: _C._cuda_manualSeed(seed))
84 def manual_seed_all(seed):
85 r"""Sets the seed for generating random numbers on all GPUs. 86 It's safe to call this function if CUDA is not available; in that 87 case, it is silently ignored. 90 seed (int): The desired seed. 93 _lazy_call(
lambda: _C._cuda_manualSeedAll(seed))
97 r"""Sets the seed for generating random numbers to a random number for the current GPU. 98 It's safe to call this function if CUDA is not available; in that 99 case, it is silently ignored. 102 If you are working with a multi-GPU model, this function will only initialize 103 the seed on one GPU. To initialize all GPUs, use :func:`seed_all`. 105 _lazy_call(
lambda: _C._cuda_seed())
109 r"""Sets the seed for generating random numbers to a random number on all GPUs. 110 It's safe to call this function if CUDA is not available; in that 111 case, it is silently ignored. 113 _lazy_call(
lambda: _C._cuda_seedAll())
117 r"""Returns the current random seed of the current GPU. 120 This function eagerly initializes CUDA. 123 return _C._cuda_initialSeed()