-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_environment.py
More file actions
32 lines (27 loc) · 1.02 KB
/
set_environment.py
File metadata and controls
32 lines (27 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os
import random
import cv2
import numpy as np
import torch
import torch.backends.cudnn as cudnn
def set_env(deterministic=True, seed=0, allow_tf32_on_cudnn=True, allow_tf32_on_matmul=True):
if deterministic:
torch.set_num_threads(1)
random.seed(seed)
np.random.seed(seed)
cv2.setRNGSeed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
cudnn.benchmark = False
cudnn.deterministic = True
# https://docs.nvidia.com/cuda/cublas/index.html#cublasApi_reproducibility
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
torch.use_deterministic_algorithms(True)
else:
cudnn.benchmark = True
torch.use_deterministic_algorithms(False)
# https://pytorch.org/docs/stable/notes/cuda.html#tf32-on-ampere
torch.backends.cuda.matmul.allow_tf32 = allow_tf32_on_matmul
torch.backends.cudnn.allow_tf32 = allow_tf32_on_cudnn