Skip to content

xlm.utils.seed

seed_everything(seed=None, workers=False)

Function that sets the seed for pseudo-random number generators in: torch, numpy, and Python's random module. In addition, sets the following environment variables:

  • PL_GLOBAL_SEED: will be passed to spawned subprocesses (e.g. ddp_spawn backend).
  • PL_SEED_WORKERS: (optional) is set to 1 if workers=True.

Parameters:

Name Type Description Default
seed Optional[int]

the integer value seed for global random state in Lightning. If None, it will read the seed from PL_GLOBAL_SEED env variable. If None and the PL_GLOBAL_SEED env variable is not set, then the seed defaults to 0.

None
workers bool

if set to True, will properly configure all dataloaders passed to the Trainer with a worker_init_fn. If the user already provides such a function for their dataloaders, setting this argument will have no influence. See also: :func:~lightning.fabric.utilities.seed.pl_worker_init_function.

False

reset_seed()

Reset the seed to the value that :func:~lightning.fabric.utilities.seed.seed_everything previously set.

If :func:~lightning.fabric.utilities.seed.seed_everything is unused, this function will do nothing.

pl_worker_init_function(worker_id, rank=None)

The worker_init_fn that Lightning automatically adds to your dataloader if you previously set the seed with seed_everything(seed, workers=True).

See also the PyTorch documentation on randomness in DataLoaders <https://pytorch.org/docs/stable/notes/randomness.html#dataloader>_.