Files
Ajit Singh 6cf13e8b98 enhancement: reduced usage of numpy and substituted built-in libraries (#8418)
* reduced usage of numpy and substituted built-in libraries

* added release note

* edited expit function to support both float as well as list (this case was giving error CI)

* revert code , numpy can't be removed here

* more cleaning

* fix relnote

---------

Co-authored-by: anakin87 <stefanofiorucci@gmail.com>
2024-10-18 15:42:19 +02:00

37 lines
1.2 KiB
Python

# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import os
import random
from haystack import logging
logger = logging.getLogger(__name__)
def set_all_seeds(seed: int, deterministic_cudnn: bool = False) -> None:
"""
Setting multiple seeds to make runs reproducible.
Important: Enabling `deterministic_cudnn` gives you full reproducibility with CUDA,
but might slow down your training (see https://pytorch.org/docs/stable/notes/randomness.html#cudnn) !
:param seed:number to use as seed
:param deterministic_cudnn: Enable for full reproducibility when using CUDA. Caution: might slow down training.
"""
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
try:
import torch
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if deterministic_cudnn:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
except (ImportError, ModuleNotFoundError) as exc:
logger.info("Could not set PyTorch seed because torch is not installed. Exception: {exception}", exception=exc)