<jupyter_start><jupyter_text>consistency> Utils for set consistency.<jupyter_code># export
# hide
import torch, random
import numpy as np
# hide
# todo Check availability np, random and others...
# export
# hide
SEED_N = 2020
# hide
# export

# Copyed from Catalist fraimwork. 
# TODO find url to source.
def prepare_cudnn(deterministic: bool = None, benchmark: bool = None) -> None:
    """
    Prepares CuDNN benchmark and sets CuDNN
    to be deterministic/non-deterministic mode
    Args:
        deterministic (bool): deterministic mode if running in CuDNN backend.
        benchmark (bool): If ``True`` use CuDNN heuristics to figure out
            which algorithm will be most performant
            for your model architecture and input.
            Setting it to ``False`` may slow down your training.
    """
    if torch.cuda.is_available():
        # CuDNN reproducibility
        # https://pytorch.org/docs/stable/notes/randomness.html#cudnn
        if deterministic is None:
            import os
            deterministic = \
                os.environ.get("CUDNN_DETERMINISTIC", "False") == "True"
        torch.backends.cudnn.deterministic = deterministic

        # https://discuss.pytorch.org/t/how-should-i-disable-using-cudnn-in-my-code/38053/4
        if benchmark is None:
            import os
            benchmark = os.environ.get("CUDNN_BENCHMARK", "True") == "True"
        torch.backends.cudnn.benchmark = benchmark
# hide
# export
# benchmark = False
# deterministic = True
torch.set_printoptions(precision=10)
torch.backends.cudnn.benchmark
torch.backends.cudnn.deterministic
#hide
# prepare_cudnn(deterministic, benchmark)
prepare_cudnn()
# hide
# export
def set_global_seed(seed: int) -> None:
    """
    Sets random seed into PyTorch, TensorFlow, Numpy and Random
    Args: seed: random seed
    """
    try:
        import torch
    except ImportError:
        pass
    else:
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
    random.seed(seed)
    np.random.seed(seed)
# hide
# export
def set_consistance(SEED_N=SEED_N):
    print(f"Set consistance..... seed {SEED_N}")
    set_global_seed(SEED_N)
    prepare_cudnn()
# hide
set_consistance()<jupyter_output>Set consistance..... seed 2020
{'fraction_non_alphanumeric': 0.09991119005328597, 'fraction_numerical': 0.007104795737122558, 'mean_word_length': 2.9114583333333335, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '372562', 'license': 'Apache-2.0', 'n_tokens_mistral': 739, 'n_tokens_neox': 715, 'n_words': 184, 'path': '02_consistency.ipynb', 'repo_name': 'liuyao12/imagenette_experiments', 'size': '2252'}