-
Notifications
You must be signed in to change notification settings - Fork 27
/
util.py
180 lines (143 loc) · 6.15 KB
/
util.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import time
import logging
import argparse
import yaml
import jinja2
from jinja2 import meta
import easydict
import torch
from torch import distributed as dist
from torch.optim import lr_scheduler
from torchdrug import core, utils, datasets, models, tasks
from torchdrug.utils import comm
logger = logging.getLogger(__file__)
def get_root_logger(file=True):
logger = logging.getLogger("")
logger.setLevel(logging.INFO)
format = logging.Formatter("%(asctime)-10s %(message)s", "%H:%M:%S")
if file:
handler = logging.FileHandler("log.txt")
handler.setFormatter(format)
logger.addHandler(handler)
return logger
def create_working_directory(cfg):
file_name = "working_dir.tmp"
world_size = comm.get_world_size()
if world_size > 1 and not dist.is_initialized():
comm.init_process_group("nccl", init_method="env://")
working_dir = os.path.join(os.path.expanduser(cfg.output_dir),
cfg.task["class"], cfg.dataset["class"], cfg.task.model["class"],
time.strftime("%Y-%m-%d-%H-%M-%S"))
# synchronize working directory
if comm.get_rank() == 0:
with open(file_name, "w") as fout:
fout.write(working_dir)
os.makedirs(working_dir)
comm.synchronize()
if comm.get_rank() != 0:
with open(file_name, "r") as fin:
working_dir = fin.read()
comm.synchronize()
if comm.get_rank() == 0:
os.remove(file_name)
os.chdir(working_dir)
return working_dir
def detect_variables(cfg_file):
with open(cfg_file, "r") as fin:
raw = fin.read()
env = jinja2.Environment()
ast = env.parse(raw)
vars = meta.find_undeclared_variables(ast)
return vars
def load_config(cfg_file, context=None):
with open(cfg_file, "r") as fin:
raw = fin.read()
template = jinja2.Template(raw)
instance = template.render(context)
cfg = yaml.safe_load(instance)
cfg = easydict.EasyDict(cfg)
return cfg
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", help="yaml configuration file", required=True)
parser.add_argument("-s", "--seed", help="random seed for PyTorch", type=int, default=1024)
args, unparsed = parser.parse_known_args()
# get dynamic arguments defined in the config file
vars = detect_variables(args.config)
parser = argparse.ArgumentParser()
for var in vars:
parser.add_argument("--%s" % var, default="null")
vars = parser.parse_known_args(unparsed)[0]
vars = {k: utils.literal_eval(v) for k, v in vars._get_kwargs()}
return args, vars
def build_downstream_solver(cfg, dataset):
train_set, valid_set, test_set = dataset.split()
if comm.get_rank() == 0:
logger.warning(dataset)
logger.warning("#train: %d, #valid: %d, #test: %d" % (len(train_set), len(valid_set), len(test_set)))
if cfg.task['class'] == 'MultipleBinaryClassification':
cfg.task.task = [_ for _ in range(len(dataset.tasks))]
else:
cfg.task.task = dataset.tasks
task = core.Configurable.load_config_dict(cfg.task)
cfg.optimizer.params = task.parameters()
optimizer = core.Configurable.load_config_dict(cfg.optimizer)
if "scheduler" not in cfg:
scheduler = None
elif cfg.scheduler["class"] == "ReduceLROnPlateau":
cfg.scheduler.pop("class")
scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, **cfg.scheduler)
else:
cfg.scheduler.optimizer = optimizer
scheduler = core.Configurable.load_config_dict(cfg.scheduler)
cfg.engine.scheduler = scheduler
solver = core.Engine(task, train_set, valid_set, test_set, optimizer, **cfg.engine)
if "lr_ratio" in cfg:
cfg.optimizer.params = [
{'params': solver.model.model.parameters(), 'lr': cfg.optimizer.lr * cfg.lr_ratio},
{'params': solver.model.mlp.parameters(), 'lr': cfg.optimizer.lr}
]
optimizer = core.Configurable.load_config_dict(cfg.optimizer)
solver.optimizer = optimizer
elif "sequence_model_lr_ratio" in cfg:
assert cfg.task.model["class"] == "FusionNetwork"
cfg.optimizer.params = [
{'params': solver.model.model.sequence_model.parameters(), 'lr': cfg.optimizer.lr * cfg.sequence_model_lr_ratio},
{'params': solver.model.model.structure_model.parameters(), 'lr': cfg.optimizer.lr},
{'params': solver.model.mlp.parameters(), 'lr': cfg.optimizer.lr}
]
optimizer = core.Configurable.load_config_dict(cfg.optimizer)
solver.optimizer = optimizer
if isinstance(scheduler, lr_scheduler.ReduceLROnPlateau):
scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, **cfg.scheduler)
elif scheduler is not None:
cfg.scheduler.optimizer = optimizer
scheduler = core.Configurable.load_config_dict(cfg.scheduler)
solver.scheduler = scheduler
if cfg.get("checkpoint") is not None:
solver.load(cfg.checkpoint)
if cfg.get("model_checkpoint") is not None:
if comm.get_rank() == 0:
logger.warning("Load checkpoint from %s" % cfg.model_checkpoint)
cfg.model_checkpoint = os.path.expanduser(cfg.model_checkpoint)
model_dict = torch.load(cfg.model_checkpoint, map_location=torch.device('cpu'))
task.model.load_state_dict(model_dict)
return solver, scheduler
def build_pretrain_solver(cfg, dataset):
if comm.get_rank() == 0:
logger.warning(dataset)
logger.warning("#dataset: %d" % (len(dataset)))
task = core.Configurable.load_config_dict(cfg.task)
if "fix_sequence_model" in cfg:
if cfg.task["class"] == "Unsupervised":
model_dict = cfg.task.model.model
else:
model_dict = cfg.task.model
assert model_dict["class"] == "FusionNetwork"
for p in task.model.model.sequence_model.parameters():
p.requires_grad = False
cfg.optimizer.params = [p for p in task.parameters() if p.requires_grad]
optimizer = core.Configurable.load_config_dict(cfg.optimizer)
solver = core.Engine(task, dataset, None, None, optimizer, **cfg.engine)
return solver