Shortcuts

Fabric Utilities

seed_everything

This function sets the random seed in important libraries. In a single line of code, you can seed PyTorch, NumPy, and Python:

+ from lightning.fabric import seed_everything

  seed = 42
- random.seed(seed)
- numpy.random.seed(seed)
- torch.manual_seed(seed)
- torch.cuda.manual_seed(seed)

+ seed_everything(seed)

The same is also available as a method on the Fabric object if you don’t want to import it separately:

from lightning.fabric import Fabric

fabric.Fabric()
fabric.seed_everything(42)

In distributed settings, you may need to set a different seed per process, depending on the application. For example, when generating noise or data augmentations. This is very straightforward:

fabric = Fabric(...)
fabric.seed_everything(seed + fabric.global_rank)

By default, seed_everything also handles the initialization of the seed in DataLoader worker processes:

fabric = Fabric(...)

# By default, we handle DataLoader workers too:
fabric.seed_everything(..., workers=True)

# Can be turned off:
fabric.seed_everything(..., workers=False)

print

Avoid duplicated print statements in the logs in distributed training by using Fabric’s print() method:

print("This message gets printed in every process. That's a bit messy!")

fabric = Fabric(...)
fabric.print("This message gets printed only in the main process. Much cleaner!")

© Copyright Copyright (c) 2018-2023, Lightning AI et al...

Built with Sphinx using a theme provided by Read the Docs.