-
Notifications
You must be signed in to change notification settings - Fork 7
/
misc.py
58 lines (46 loc) · 1.65 KB
/
misc.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import random
from typing import List
import numpy as np
import torch
from GPUtil import getGPUs, GPU
from packaging.version import parse as V
def get_idle_gpu(gpu_num: int = 1, id_only: bool = True) -> List[GPU]:
"""
find idle GPUs for distributed learning.
"""
sorted_gpus = sorted(getGPUs(), key=lambda g: g.memoryUtil)
if len(sorted_gpus) < gpu_num:
raise RuntimeError(
f"Your machine doesn't have enough GPUs ({len(sorted_gpus)}) as you specified ({gpu_num})!")
sorted_gpus = sorted_gpus[:gpu_num]
if id_only:
return [gpu.id for gpu in sorted_gpus]
else:
return sorted_gpus
def get_idle_port() -> str:
"""
find an idle port to used for distributed learning
"""
pscmd = "netstat -ntl |grep -v Active| grep -v Proto|awk '{print $4}'|awk -F: '{print $NF}'"
procs = os.popen(pscmd).read()
procarr = procs.split("\n")
tt = str(random.randint(15000, 30000))
if tt not in procarr:
return tt
else:
return get_idle_port()
def set_randomness():
random.seed(3407)
np.random.seed(3407)
torch.manual_seed(3407)
torch.cuda.manual_seed(3407)
os.environ['PYTHONHASHSEED'] = str(3407)
# For more details about 'CUBLAS_WORKSPACE_CONFIG',
# please refer to https://docs.nvidia.com/cuda/cublas/index.html#cublasApi_reproducibility
if V(torch.version.cuda) >= V("10.2"):
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
torch.use_deterministic_algorithms(mode=True, warn_only=True)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.set_float32_matmul_precision('medium')