composer.utils.reproducibility#

Helper utilities for configuring deterministic training to ensure reproducibility.

Note

For deterministic model initialization, seed_all() and/or configure_deterministic_mode() should be invoked before creating and initializing a model, before creating the Trainer. For example:

>>> import torch.nn
>>> from composer.utils import reproducibility
>>> reproducibility.configure_deterministic_mode()
>>> reproducibility.seed_all(42)
>>> model = MyModel()
>>> def init_weights(m):
...     if isinstance(m, torch.nn.Linear):
...         torch.nn.init.xavier_uniform(m.weight)
>>> # model will now be deterministically initialized, since the seed is set.
>>> init_weights(model)
>>> trainer = Trainer(model=model, seed=42)

Note that the seed must also be passed to the Trainer, otherwise the Trainer would generate a random seed based on the timestamp (see get_random_seed()).

composer.utils.reproducibility.MAX_SEED#

The maximum allowed seed, which is \(2^{32} - 1\).

Type

int

Functions

configure_deterministic_mode

Configure PyTorch deterministic mode.

get_random_seed

Get a randomly created seed to use for seeding rng objects.

get_rng_state

The state of the RNG objects.

load_rng_state

Restore the RNG state.

seed_all

Seed all rng objects.

seed_context

Context manager to store rng_state and reseed for duration of context.