-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.py
165 lines (130 loc) · 5.34 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
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, dirname=None):
file_name = "working_dir.tmp" # % os.environ["SLURM_JOB_ID"]
world_size = comm.get_world_size()
if world_size > 1 and not dist.is_initialized():
comm.init_process_group("nccl", init_method="env://")
if "dataset" in cfg:
dataset_class = cfg.dataset["class"]
else:
dataset_class = cfg.train_set["class"]
working_dir = os.path.join(os.path.expanduser(cfg.output_dir),
cfg.task["class"], dataset_class, cfg.task.model["class"],
time.strftime("%Y-%m-%d-%H-%M-%S"))
if dirname is not None:
working_dir = os.path.join(working_dir, dirname)
# 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=0)
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_atom3d_solver(cfg, train_set, valid_set, test_set, use_solver=False):
if cfg.task['class'] == "EC":
cfg.task.task = [_ for _ in range(len(train_set.dataset.tasks))]
task = core.Configurable.load_config_dict(cfg.task)
if use_solver:
cfg.optimizer.params = task.parameters()
optimizer = core.Configurable.load_config_dict(cfg.optimizer)
# Need to define a solver for preprocessing
solver = core.Engine(task, train_set, valid_set, test_set, optimizer, **cfg.engine)
else:
task.preprocess(train_set, valid_set, test_set)
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)
if use_solver:
solver.scheduler = scheduler
if use_solver and 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)
if use_solver:
return solver, scheduler
else:
return task, optimizer, 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)
cfg.optimizer.params = task.parameters()
optimizer = core.Configurable.load_config_dict(cfg.optimizer)
solver = core.Engine(task, dataset, None, None, optimizer, **cfg.engine)
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'))
model_dict.pop("alphas")
task.load_state_dict(model_dict, strict=False)
return solver