|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import random |
|
from typing import List, Optional, Union |
|
|
|
import numpy as np |
|
import torch |
|
|
|
from ..state import AcceleratorState |
|
from .constants import CUDA_DISTRIBUTED_TYPES |
|
from .dataclasses import DistributedType, RNGType |
|
from .imports import is_tpu_available |
|
|
|
|
|
if is_tpu_available(check_device=False): |
|
import torch_xla.core.xla_model as xm |
|
|
|
|
|
def set_seed(seed: int, device_specific: bool = False): |
|
""" |
|
Helper function for reproducible behavior to set the seed in `random`, `numpy`, `torch`. |
|
|
|
Args: |
|
seed (`int`): |
|
The seed to set. |
|
device_specific (`bool`, *optional*, defaults to `False`): |
|
Whether to differ the seed on each device slightly with `self.process_index`. |
|
""" |
|
if device_specific: |
|
seed += AcceleratorState().process_index |
|
random.seed(seed) |
|
np.random.seed(seed) |
|
torch.manual_seed(seed) |
|
torch.cuda.manual_seed_all(seed) |
|
|
|
if is_tpu_available(): |
|
xm.set_rng_state(seed) |
|
|
|
|
|
def synchronize_rng_state(rng_type: Optional[RNGType] = None, generator: Optional[torch.Generator] = None): |
|
|
|
if rng_type == RNGType.TORCH: |
|
rng_state = torch.get_rng_state() |
|
elif rng_type == RNGType.CUDA: |
|
rng_state = torch.cuda.get_rng_state() |
|
elif rng_type == RNGType.XLA: |
|
assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs." |
|
rng_state = torch.tensor(xm.get_rng_state()) |
|
elif rng_type == RNGType.GENERATOR: |
|
assert generator is not None, "Need a generator to synchronize its seed." |
|
rng_state = generator.get_state() |
|
|
|
|
|
state = AcceleratorState() |
|
if state.distributed_type == DistributedType.TPU: |
|
rng_state = xm.mesh_reduce("random_seed", rng_state, lambda x: x[0]) |
|
elif state.distributed_type in CUDA_DISTRIBUTED_TYPES: |
|
rng_state = rng_state.to(state.device) |
|
torch.distributed.broadcast(rng_state, 0) |
|
rng_state = rng_state.cpu() |
|
elif state.distributed_type == DistributedType.MULTI_CPU: |
|
torch.distributed.broadcast(rng_state, 0) |
|
|
|
|
|
if rng_type == RNGType.TORCH: |
|
torch.set_rng_state(rng_state) |
|
elif rng_type == RNGType.CUDA: |
|
torch.cuda.set_rng_state(rng_state) |
|
elif rng_type == RNGType.XLA: |
|
xm.set_rng_state(rng_state.item()) |
|
elif rng_type == RNGType.GENERATOR: |
|
generator.set_state(rng_state) |
|
|
|
|
|
def synchronize_rng_states(rng_types: List[Union[str, RNGType]], generator: Optional[torch.Generator] = None): |
|
for rng_type in rng_types: |
|
synchronize_rng_state(RNGType(rng_type), generator=generator) |
|
|