Skip to content

Conversation

@aditya0by0
Copy link
Member

Using seed_everything(seed) helps with reproducibility by setting seeds for Python's random, NumPy, and PyTorch (both CPU and CUDA), and it also sets:

  1. random.seed(seed) — Python’s built-in random module.
  2. np.random.seed(seed) — NumPy's random number generator.
  3. torch.manual_seed(seed) — PyTorch’s RNG on CPU.
  4. torch.cuda.manual_seed(seed) — PyTorch’s RNG on current GPU.
  5. torch.cuda.manual_seed_all(seed) — For all GPUs.
  6. torch.backends.cudnn.deterministic = True — Forces cuDNN to use deterministic algorithms.
  7. torch.backends.cudnn.benchmark = False — Disables the autotuner that selects the fastest convolution algorithm (introduces randomness).

However, this doesn't imply that Trainer(deterministic=True) is set. The deterministic flag in the Trainer does more — it enforces deterministic algorithms during training and can catch known non-deterministic ops (especially on GPU), which is useful for debugging reproducibility issues.

Recommendation: Use both together for maximum reproducibility:

seed_everything(42)
trainer = Trainer(deterministic=True, ...)

Pytorch-lightning sets seed_everything to 0 by default, if no seed is provided but not the trainer's deterministic flag which is set to None by default.

Please check the below links:

@aditya0by0 aditya0by0 requested a review from sfluegel05 July 5, 2025 21:13
@aditya0by0 aditya0by0 self-assigned this Jul 5, 2025
@aditya0by0 aditya0by0 added enhancement New feature or request bug:fix labels Jul 5, 2025
@aditya0by0 aditya0by0 changed the title make trainer op deterministic Make training on deterministic Jul 5, 2025
@aditya0by0
Copy link
Member Author

aditya0by0 commented Jul 5, 2025

This PR will complete this issue #12 in its entirety.

@aditya0by0 aditya0by0 changed the title Make training on deterministic Make training deterministic Jul 5, 2025
@aditya0by0 aditya0by0 linked an issue Jul 5, 2025 that may be closed by this pull request
2 tasks
@aditya0by0
Copy link
Member Author

aditya0by0 commented Jul 6, 2025

@sfluegel05, I tested this by running the same training configuration twice using the same seed.

✅ This confirms that setting deterministic=True in addition to seed_everything() is essential for full reproducibility in PyTorch Lightning.

@sfluegel05 sfluegel05 merged commit 1a13718 into dev Jul 11, 2025
6 checks passed
@sfluegel05
Copy link
Collaborator

That's really good to know, thanks for finding this out.

@sfluegel05 sfluegel05 deleted the fix/trainer_deterministic branch July 11, 2025 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Runs should be reproducable

3 participants