Skip to content

set_seed

Set all RNG seeds and env vars for reproducible runs. Call at the very start of your script (before creating the trainer or loading data) for best reproducibility. The Trainer also calls this when TrainingArguments.seed is set.

Sets: Python random, numpy, PyTorch (CPU + CUDA), cuDNN deterministic, and CUBLAS_WORKSPACE_CONFIG / OMP_NUM_THREADS / MKL_NUM_THREADS.

Source code in gradiend/trainer/trainer.py
def set_seed(seed: int) -> None:
    """
    Set all RNG seeds and env vars for reproducible runs. Call at the very start of your
    script (before creating the trainer or loading data) for best reproducibility.
    The Trainer also calls this when TrainingArguments.seed is set.

    Sets: Python random, numpy, PyTorch (CPU + CUDA), cuDNN deterministic, and
    CUBLAS_WORKSPACE_CONFIG / OMP_NUM_THREADS / MKL_NUM_THREADS.
    """
    if not isinstance(seed, int):
        raise TypeError(f"seed must be int, got {type(seed).__name__}")
    _apply_seed(seed)