helios.core.rng

Classes

DefaultNumpyRNG

Default RNG from Numpy.

Functions

get_default_seed(→ int)

Return the default seed.

create_default_numpy_rng([seed])

Initialize the default RNG with the given seed.

get_default_numpy_rng(→ DefaultNumpyRNG)

Return the default RNG.

seed_rngs(→ None)

Seed the default RNGs with the given seed.

get_rng_state_dict(→ dict[str, Any])

Get the state dict for the default RNGs.

load_rng_state_dict(→ None)

Restore the default RNGs from the given state dict.

Module Contents

helios.core.rng.get_default_seed() int[source]

Return the default seed.

class helios.core.rng.DefaultNumpyRNG(seed: int | list[int] | tuple[int] | None = None)[source]

Default RNG from Numpy.

This is intended to serve as a replacement for the legacy random API from Numpy. The class wraps the new Generator instance, which is set to use PCG64. Functionality similar to the modules for PyTorch is provided for easy serialization and restoring.

Parameters:

seed – (optional) the initial seed to use.

property generator: numpy.random.Generator

Return the Numpy Generator instance.

state_dict() collections.abc.Mapping[str, Any][source]

Create a dictionary containing the RNG state.

Returns:

The state of the RNG.

load_state_dict(state_dict: collections.abc.Mapping[str, Any]) None[source]

Restore the RNG from the given state dictionary.

Parameters:

state_dict – the state dictionary.

helios.core.rng.create_default_numpy_rng(seed: int | list[int] | tuple[int] | None = None)[source]

Initialize the default RNG with the given seed.

Parameters:

seed – (optional) the seed to use.

helios.core.rng.get_default_numpy_rng() DefaultNumpyRNG[source]

Return the default RNG.

Returns:

The random generator.

Raises:

RuntimeError – if the default Numpy RNG hasn’t been created.

helios.core.rng.seed_rngs(seed: int | None = None, skip_torch: bool = False) None[source]

Seed the default RNGs with the given seed.

If no seed is given, then the default seed from Helios will be used. The RNGs that will be seeded are: PyTorch (+ CUDA if available), stdlib random, and the default Numpy generator. The skip_torch flag is intended to be used when seeding worker processes for dataloaders. In those cases, the RNGs for PyTorch have already been seeded, so we shouldn’t be re-seeding them.

Parameters:
  • seed – optional value to seed the random generators with.

  • skip_torch – if True, torch RNGs won’t be seeded.

helios.core.rng.get_rng_state_dict() dict[str, Any][source]

Get the state dict for the default RNGs.

Default RNGs are: PyTorch (+ CUDA if available) and Random.

Returns:

The state of all RNGs.

helios.core.rng.load_rng_state_dict(state_dict: dict[str, Any]) None[source]

Restore the default RNGs from the given state dict.

See get_rng_state_dict() for the list of default RNGs.

Parameters:

state_dict – the state of the RNGs