Skip to content

Commit

Permalink
Seed all workers when using DDP (#7942)
Browse files Browse the repository at this point in the history
* Seed all workers when using DDP

* Fix to dataloader seeding

* Make argument name explicit

Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>

* Use f-strings when logging

* Removed a redundant log message

Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
  • Loading branch information
senarvi and carmocca committed Jun 15, 2021
1 parent 9940ff2 commit a0c31be
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pytorch_lightning/utilities/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ def reset_seed() -> None:
If :func:`pytorch_lightning.utilities.seed.seed_everything` is unused, this function will do nothing.
"""
seed = os.environ.get("PL_GLOBAL_SEED", None)
workers = os.environ.get("PL_SEED_WORKERS", False)
if seed is not None:
seed_everything(int(seed))
seed_everything(int(seed), workers=bool(workers))


def pl_worker_init_function(worker_id: int, rank: Optional = None) -> None: # pragma: no cover
Expand All @@ -100,6 +101,9 @@ def pl_worker_init_function(worker_id: int, rank: Optional = None) -> None: # p
process_seed = torch.initial_seed()
# back out the base seed so we can use all the bits
base_seed = process_seed - worker_id
log.debug(
f'Initializing random number generators of process {global_rank} worker {worker_id} with base seed {base_seed}'
)
ss = np.random.SeedSequence([base_seed, worker_id, global_rank])
# use 128 bits (4 x 32-bit words)
np.random.seed(ss.generate_state(4))
Expand Down

0 comments on commit a0c31be

Please sign in to comment.