"""Set the seed for reproducibility in different libraries and frameworks."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/seed.ipynb.

# %% auto 0
__all__ = ['set_tf', 'set_torch', 'set_np', 'set_python', 'set']

# %% ../nbs/seed.ipynb 3
def set_tf(seed: int):
    """
    Set the seed for TensorFlow's random number generators

    https://www.tensorflow.org/api_docs/python/tf/random/set_seed
    """
    try:
        import tensorflow as tf
    except ImportError:
        # "TensorFlow not installed"
        return

    tf.keras.utils.set_random_seed(seed)
    tf.random.set_seed(seed)
    tf.config.experimental.enable_op_determinism()

# %% ../nbs/seed.ipynb 13
def set_torch(seed: int):
    """
    Set the seed for PyTorch's random number generators
    
    Warning: Deterministic operations are often slower than nondeterministic operations, so single-run 
    performance may decrease for your model. However, determinism may save time in development by 
    facilitating experimentation, debugging, and regression testing.

    https://pytorch.org/docs/stable/notes/randomness.html
    """
    try:
        import torch
    except ImportError:
        # "PyTorch not installed"
        return

    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    if hasattr(torch, "backends"):
        if hasattr(torch.backends, "cudnn.deterministic"): # type: ignore
            if torch.backends.cudnn.is_available(): # type: ignore
                torch.backends.cudnn.deterministic = True # type: ignore

# %% ../nbs/seed.ipynb 19
def set_np(seed: int):
    try:
        import numpy as np
    except ImportError:
        # "Numpy not installed"
        return
    np.random.seed(seed)

# %% ../nbs/seed.ipynb 25
def set_python(seed: int):
    import random
    random.seed(seed)

# %% ../nbs/seed.ipynb 30
def set(seed: int):
    set_python(seed)
    set_np(seed)
    set_torch(seed)
    set_tf(seed)
