utils.seed

utils.seed

Global seeding for fully reproducible torch experiments.

Ported from the seed_everything helper of the schu25a compressor-map study (src/rnn/utils.py): seeds Python, NumPy, and torch (CPU, CUDA, and MPS) and switches cuDNN to deterministic mode, so that repeated runs on the same device produce bit-identical results.

Requires the torch optional extra (pip install 'spotoptim[torch]').

Functions

Name Description
seed_everything Seed every random number generator relevant to a torch experiment.

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