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)