-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinitialize.py
More file actions
executable file
·38 lines (32 loc) · 1.24 KB
/
Copy pathinitialize.py
File metadata and controls
executable file
·38 lines (32 loc) · 1.24 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
33
34
35
36
37
import os
import random
import torch
import numpy as np
import torch.distributed as dist
from .logger import LOGGER,add_log_to_file
def initialize(opts):
# if not os.path.exists(opts.run_cfg.output_dir):
os.makedirs(os.path.join(opts.run_cfg.output_dir, 'log'), exist_ok=True)
os.makedirs(os.path.join(opts.run_cfg.output_dir, 'ckpt'), exist_ok=True)
local_rank = opts.local_rank
print(local_rank)
torch.cuda.set_device(local_rank)
print("DEVICE SET")
dist.init_process_group(backend='nccl')
if opts.run_cfg.gradient_accumulation_steps < 1:
raise ValueError("Invalid gradient_accumulation_steps parameter: {}, "
"should be >= 1".format(
opts.run_cfg.gradient_accumulation_steps))
set_random_seed(opts.run_cfg.seed)
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
if dist.get_rank() == 0:
# TB_LOGGER.create(os.path.join(opts.output_dir, 'log'))
add_log_to_file(os.path.join(opts.run_cfg.output_dir, 'log', 'log.txt'))
else:
LOGGER.disabled = True
def set_random_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)