-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.py
130 lines (101 loc) · 4.41 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import argparse
import os
import yaml
import copy
import torch
import random
import numpy as np
from utils import dict2namespace, get_runner, namespace2dict
import torch.multiprocessing as mp
import torch.distributed as dist
def parse_args_and_config():
parser = argparse.ArgumentParser(description=globals()['__doc__'])
parser.add_argument('-c', '--config', type=str, default='BB_base.yml', help='Path to the config file')
parser.add_argument('-s', '--seed', type=int, default=1234, help='Random seed')
parser.add_argument('-r', '--result_path', type=str, default='results', help="The directory to save results")
parser.add_argument('-t', '--train', action='store_true', default=False, help='train the model')
parser.add_argument('--sample_to_eval', action='store_true', default=False, help='sample for evaluation')
parser.add_argument('--sample_at_start', action='store_true', default=False, help='sample at start(for debug)')
parser.add_argument('--save_top', action='store_true', default=False, help="save top loss checkpoint")
parser.add_argument('--gpu_ids', type=str, default='0', help='gpu ids, 0,1,2,3 cpu=-1')
parser.add_argument('--port', type=str, default='12355', help='DDP master port')
parser.add_argument('--resume_model', type=str, default=None, help='model checkpoint')
parser.add_argument('--resume_optim', type=str, default=None, help='optimizer checkpoint')
parser.add_argument('--max_epoch', type=int, default=None, help='optimizer checkpoint')
parser.add_argument('--max_steps', type=int, default=None, help='optimizer checkpoint')
args = parser.parse_args()
with open(args.config, 'r') as f:
dict_config = yaml.load(f, Loader=yaml.FullLoader)
namespace_config = dict2namespace(dict_config)
namespace_config.args = args
if args.resume_model is not None:
namespace_config.model.model_load_path = args.resume_model
if args.resume_optim is not None:
namespace_config.model.optim_sche_load_path = args.resume_optim
if args.max_epoch is not None:
namespace_config.training.n_epochs = args.max_epoch
if args.max_steps is not None:
namespace_config.training.n_steps = args.max_steps
dict_config = namespace2dict(namespace_config)
return namespace_config, dict_config
def set_random_seed(SEED=1234):
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.cuda.manual_seed_all(SEED)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def DDP_run_fn(rank, world_size, config):
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = config.args.port
dist.init_process_group(backend='nccl', rank=rank, world_size=world_size)
set_random_seed(config.args.seed)
local_rank = dist.get_rank()
torch.cuda.set_device(local_rank)
config.training.device = [torch.device("cuda:%d" % local_rank)]
print('using device:', config.training.device)
config.training.local_rank = local_rank
runner = get_runner(config.runner, config)
if config.args.train:
runner.train()
else:
with torch.no_grad():
runner.test()
return
def CPU_singleGPU_launcher(config):
set_random_seed(config.args.seed)
runner = get_runner(config.runner, config)
if config.args.train:
runner.train()
else:
with torch.no_grad():
runner.test()
return
def DDP_launcher(world_size, run_fn, config):
mp.spawn(run_fn,
args=(world_size, copy.deepcopy(config)),
nprocs=world_size,
join=True)
def main():
nconfig, dconfig = parse_args_and_config()
args = nconfig.args
gpu_ids = args.gpu_ids
if gpu_ids == "-1": # Use CPU
nconfig.training.use_DDP = False
nconfig.training.device = [torch.device("cpu")]
CPU_singleGPU_launcher(nconfig)
else:
gpu_list = gpu_ids.split(",")
if len(gpu_list) > 1:
os.environ['CUDA_VISIBLE_DEVICES'] = gpu_ids
nconfig.training.use_DDP = True
DDP_launcher(world_size=len(gpu_list), run_fn=DDP_run_fn, config=nconfig)
else:
nconfig.training.use_DDP = False
nconfig.training.device = [torch.device(f"cuda:{gpu_list[0]}")]
CPU_singleGPU_launcher(nconfig)
return
if __name__ == "__main__":
main()