Source code for torchfoo.utils

__all__ = [
    "seed_everything",
    "current_device",
]

import random

import torch


[docs] def seed_everything(seed: int, deterministic: bool = False): r"""Seed all random number generators for reproducibility. Seeds ``random``, ``torch``, and ``torch.cuda`` (all devices). Optionally seeds ``numpy`` if it is installed. Args: seed: the seed value to use deterministic: if True, sets ``torch.use_deterministic_algorithms(True)`` and ``torch.backends.cudnn.benchmark = False``. This will ensure reproducible results at the cost of reduced performance. """ random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) try: import numpy as np np.random.seed(seed) except ImportError: pass if deterministic: torch.use_deterministic_algorithms(True) torch.backends.cudnn.benchmark = False
[docs] def current_device(): r"""Return the current device. In a distributed context, returns the device assigned to the current process. Outside of a distributed context, returns the current CUDA device if CUDA is available, otherwise ``torch.device("cpu")``. Returns: The current :class:`torch.device`, or an :class:`int` CUDA device index when running in a distributed CUDA context. """ from torchfoo.distributed.distributed import _distributed_state if _distributed_state.initialized: if _distributed_state.use_cuda: return torch.cuda.current_device() else: return torch.device("cpu") else: if torch.cuda.is_available(): return torch.cuda.current_device() else: return torch.device("cpu")