From 29c03963212fa7155e28ad5add515e34d35f0489 Mon Sep 17 00:00:00 2001 From: Alex Morehead Date: Mon, 25 Nov 2024 16:40:33 -0600 Subject: [PATCH] Seed NumPy using `np.random.SeedSequence()` in `pl_worker_init_function()` to robustly seed NumPy-dependent dataloader workers (#20369) * Update seed.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update seed.py * Update seed.py * Update seed.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com> Co-authored-by: Luca Antiga --- src/lightning/fabric/utilities/seed.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lightning/fabric/utilities/seed.py b/src/lightning/fabric/utilities/seed.py index dac26f32f3..f9c0ddeb86 100644 --- a/src/lightning/fabric/utilities/seed.py +++ b/src/lightning/fabric/utilities/seed.py @@ -104,7 +104,10 @@ def pl_worker_init_function(worker_id: int, rank: Optional[int] = None) -> None: if _NUMPY_AVAILABLE: import numpy as np - np.random.seed(seed_sequence[3] & 0xFFFFFFFF) # numpy takes 32-bit seed only + ss = np.random.SeedSequence([base_seed, worker_id, global_rank]) + np_rng_seed = ss.generate_state(4) + + np.random.seed(np_rng_seed) def _generate_seed_sequence(base_seed: int, worker_id: int, global_rank: int, count: int) -> list[int]: