LION.utils.rng

Random numbers that can be reproduced.

  • seed_from_parts(*parts): a seed from strings (a stable hash).

  • seeded(seed, *parts, device=...): run code with torch’s random numbers seeded from seed and parts, e.g. the noise of one data sample, so that sample is the same in every epoch, run, order and DataLoader worker. The global random state is restored afterwards.

  • seed_all(seed): seed torch, numpy and Python’s random, as the solvers do when training starts; random_state() / set_random_state() keep and restore their state (checkpoints).

One seed gives the same numbers on the same device type and torch version. torch’s CPU and CUDA generators are different algorithms, so CPU and GPU give different (statistically equivalent) numbers.

Functions

random_state()

The state of every generator seed_all seeds, as tensors, lists and numbers (so a checkpoint holding it loads with weights_only=True).

seed_all(seed)

Seed every random generator training draws from: torch's (CPU and all CUDA devices), numpy's and Python's random.

seed_from_parts(*parts)

A 32-bit seed from parts (anything, as strings), the same on every machine and run: a stable hash (BLAKE2b), not Python's hash.

seeded(seed, *parts[, device])

Run the code in the with block with torch's random numbers seeded from seed and parts (seed_from_parts), on the CPU and, if device is a CUDA device, on it.

set_random_state(state)

Restore the generators from random_state().


LION.utils.rng.random_state()[source]

The state of every generator seed_all seeds, as tensors, lists and numbers (so a checkpoint holding it loads with weights_only=True).

LION.utils.rng.seed_all(seed)[source]

Seed every random generator training draws from: torch’s (CPU and all CUDA devices), numpy’s and Python’s random. The solvers call it when training starts (their seed); call it at the top of a script to also make the model’s initial weights repeatable.

LION.utils.rng.seed_from_parts(*parts)[source]

A 32-bit seed from parts (anything, as strings), the same on every machine and run: a stable hash (BLAKE2b), not Python’s hash.

LION.utils.rng.seeded(seed, *parts, device=None)[source]

Run the code in the with block with torch’s random numbers seeded from seed and parts (seed_from_parts), on the CPU and, if device is a CUDA device, on it. The random state of those devices is restored afterwards, so the rest of the program draws the numbers it would have drawn anyway.

seed=None does nothing: the code draws from the global random state.

with seeded(0, “noise”, patient_id, slice_index, device=sinogram.device):

noisy = sinogram_add_noise(sinogram)

LION.utils.rng.set_random_state(state)[source]

Restore the generators from random_state(). CUDA states are only restored on a machine with as many CUDA devices. The states may have been loaded onto a GPU (torch.load(map_location=...)): torch takes them as CPU tensors.