utils.seed.seed_everything

utils.seed.seed_everything(seed)

Seed every random number generator relevant to a torch experiment.

Seeds Python’s random, PYTHONHASHSEED, NumPy, and torch (CPU and CUDA unconditionally, MPS when available), and sets torch.backends.cudnn.deterministic = True and torch.backends.cudnn.benchmark = False.

Parameters

Name Type Description Default
seed int The seed value. required

Note

  • cuDNN determinism and benchmark flags are process-global side effects; they may slow down convolutional workloads.
  • Unlike the schu25a original, the MPS generator is only seeded when the MPS backend is available, so the function also works on CPU-only Linux hosts.

Examples

import torch
from spotoptim.utils.seed import seed_everything

seed_everything(42)
a = torch.rand(3)
seed_everything(42)
b = torch.rand(3)
print(torch.equal(a, b))
True