Skip to content

random

Random seeding and determinism control.

Functions:

  • seed_everything –

    Set the seed for the random number generators in PyTorch, NumPy and Python.

  • set_determinism –

    Set the TensorFloat-32 flags for fp32 CUDA matmul and cuDNN convolutions.

seed_everything

seed_everything(seed: Optional[int] = None) -> int

Set the seed for the random number generators in PyTorch, NumPy and Python.

Parameters:

  • seed (Optional[int], default: None ) –

    The seed to set for the random number generators. If None, a random seed will be selected.

Returns:

  • int –

    The seed that was set.

set_determinism

set_determinism(*, tf32: bool = False) -> None

Set the TensorFloat-32 flags for fp32 CUDA matmul and cuDNN convolutions.

TensorFloat-32 rounds fp32 matmul/convolution inputs to 19-bit mantissas on Ampere+ GPUs, which shifts benchmark metrics relative to references measured with it off. Neither the Lightning Trainer(precision=...) flag nor torch.set_float32_matmul_precision covers the cuDNN convolution path (torch.backends.cudnn.allow_tf32 defaults to True), so both backends are pinned here. Nothing else is touched: RNG seeding is seed_everything, and deterministic kernel selection (torch.use_deterministic_algorithms, torch.backends.cudnn.deterministic) is not enabled.

Parameters:

  • tf32 (bool, default: False ) –

    Allow TensorFloat-32 in fp32 CUDA matmul and cuDNN convolutions.

Example
>>> set_determinism(tf32=False)
>>> torch.backends.cudnn.allow_tf32
False