1
from typing import Iterable, List, Union
5
from . import _lazy_call, _lazy_init, current_device, device_count
20
def get_rng_state(device: Union[int, str, torch.device] = "cuda") -> Tensor:
21
r"""Return the random number generator state of the specified GPU as a ByteTensor.
24
device (torch.device or int, optional): The device to return the RNG state of.
25
Default: ``'cuda'`` (i.e., ``torch.device('cuda')``, the current CUDA device).
28
This function eagerly initializes CUDA.
31
if isinstance(device, str):
32
device = torch.device(device)
33
elif isinstance(device, int):
34
device = torch.device("cuda", device)
37
idx = current_device()
38
default_generator = torch.cuda.default_generators[idx]
39
return default_generator.get_state()
42
def get_rng_state_all() -> List[Tensor]:
43
r"""Return a list of ByteTensor representing the random number states of all devices."""
45
for i in range(device_count()):
46
results.append(get_rng_state(i))
51
new_state: Tensor, device: Union[int, str, torch.device] = "cuda"
53
r"""Set the random number generator state of the specified GPU.
56
new_state (torch.ByteTensor): The desired state
57
device (torch.device or int, optional): The device to set the RNG state.
58
Default: ``'cuda'`` (i.e., ``torch.device('cuda')``, the current CUDA device).
60
with torch._C._DisableFuncTorch():
61
new_state_copy = new_state.clone(memory_format=torch.contiguous_format)
62
if isinstance(device, str):
63
device = torch.device(device)
64
elif isinstance(device, int):
65
device = torch.device("cuda", device)
70
idx = current_device()
71
default_generator = torch.cuda.default_generators[idx]
72
default_generator.set_state(new_state_copy)
77
def set_rng_state_all(new_states: Iterable[Tensor]) -> None:
78
r"""Set the random number generator state of all devices.
81
new_states (Iterable of torch.ByteTensor): The desired state for each device.
83
for i, state in enumerate(new_states):
84
set_rng_state(state, i)
87
def manual_seed(seed: int) -> None:
88
r"""Set the seed for generating random numbers for the current GPU.
90
It's safe to call this function if CUDA is not available; in that
91
case, it is silently ignored.
94
seed (int): The desired seed.
97
If you are working with a multi-GPU model, this function is insufficient
98
to get determinism. To seed all GPUs, use :func:`manual_seed_all`.
103
idx = current_device()
104
default_generator = torch.cuda.default_generators[idx]
105
default_generator.manual_seed(seed)
107
_lazy_call(cb, seed=True)
110
def manual_seed_all(seed: int) -> None:
111
r"""Set the seed for generating random numbers on all GPUs.
113
It's safe to call this function if CUDA is not available; in that
114
case, it is silently ignored.
117
seed (int): The desired seed.
122
for i in range(device_count()):
123
default_generator = torch.cuda.default_generators[i]
124
default_generator.manual_seed(seed)
126
_lazy_call(cb, seed_all=True)
130
r"""Set the seed for generating random numbers to a random number for the current GPU.
132
It's safe to call this function if CUDA is not available; in that
133
case, it is silently ignored.
136
If you are working with a multi-GPU model, this function will only initialize
137
the seed on one GPU. To initialize all GPUs, use :func:`seed_all`.
141
idx = current_device()
142
default_generator = torch.cuda.default_generators[idx]
143
default_generator.seed()
148
def seed_all() -> None:
149
r"""Set the seed for generating random numbers to a random number on all GPUs.
151
It's safe to call this function if CUDA is not available; in that
152
case, it is silently ignored.
158
for i in range(device_count()):
159
default_generator = torch.cuda.default_generators[i]
161
default_generator.seed()
162
random_seed = default_generator.initial_seed()
165
default_generator.manual_seed(random_seed)
170
def initial_seed() -> int:
171
r"""Return the current random seed of the current GPU.
174
This function eagerly initializes CUDA.
177
idx = current_device()
178
default_generator = torch.cuda.default_generators[idx]
179
return default_generator.initial_seed()