Skip to content

Commit 29c0396

Browse files
amoreheadpre-commit-ci[bot]Bordalantiga
authored
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 <[email protected]> Co-authored-by: Luca Antiga <[email protected]>
1 parent 1f4a77c commit 29c0396

File tree

1 file changed

+4
-1
lines changed
  • src/lightning/fabric/utilities

1 file changed

+4
-1
lines changed

src/lightning/fabric/utilities/seed.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ def pl_worker_init_function(worker_id: int, rank: Optional[int] = None) -> None:
104104
if _NUMPY_AVAILABLE:
105105
import numpy as np
106106

107-
np.random.seed(seed_sequence[3] & 0xFFFFFFFF) # numpy takes 32-bit seed only
107+
ss = np.random.SeedSequence([base_seed, worker_id, global_rank])
108+
np_rng_seed = ss.generate_state(4)
109+
110+
np.random.seed(np_rng_seed)
108111

109112

110113
def _generate_seed_sequence(base_seed: int, worker_id: int, global_rank: int, count: int) -> list[int]:

0 commit comments

Comments
 (0)