Skip to content

set_random_seed

Random seed setup method.

It sets up the random seeds for the RPN model prior to model training and testing.

Specifically, this method will set up the random seeds and related configurations of multiple packages, including * numpy * random * torch * torch.cuda * torch.cudnn * torch.mps

Parameters:

Name Type Description Default
random_seed int

The random seed to be setup.

0

Returns:

Type Description
None

This method doesn't have any return values.

Source code in tinybig/util/utility.py
def set_random_seed(random_seed: int = 0, deterministic: bool = False):
    """
    Random seed setup method.

    It sets up the random seeds for the RPN model prior to model training and testing.

    Specifically, this method will set up the random seeds and related configurations of multiple packages,
    including
    * numpy
    * random
    * torch
    * torch.cuda
    * torch.cudnn
    * torch.mps

    Parameters
    ----------
    random_seed: int, default = 0
        The random seed to be setup.

    Returns
    -------
    None
        This method doesn't have any return values.
    """
    torch.manual_seed(random_seed)
    random.seed(random_seed)
    np.random.seed(random_seed)

    if torch.cuda.is_available():
        torch.cuda.manual_seed(random_seed)
        torch.cuda.manual_seed_all(random_seed)  # if using multi-GPU.

    if torch.backends.mps.is_available():
        torch.mps.manual_seed(random_seed)

    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    if deterministic:
        if not torch.backends.mps.is_available():
            torch.use_deterministic_algorithms(True)
            os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8'  # Specific to CUDA
        else:
            print("Warning: Deterministic algorithms disabled for MPS backend to avoid performance degradation.")