-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse_config.py
286 lines (222 loc) · 10.3 KB
/
parse_config.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os
import logging
from pathlib import Path
from functools import reduce
from operator import getitem
from datetime import datetime
from logger import setup_logging
from utils import read_json, write_json, setup_seed
class ConfigParser:
def __init__(self, args, options='', timestamp=True):
# parse default and custom cli options
for opt in options:
args.add_argument(*opt.flags, default=None, type=opt.type)
args = args.parse_args()
if args.device:
os.environ["CUDA_VISIBLE_DEVICES"] = args.device
self.bert_config_path = None
if args.resume:
self.resume = Path(args.resume)
self.cfg_fname = self.resume.parent / 'config.json'
self.bert_config_path = str(self.resume.parent / 'BertConfig.json')
else:
msg_no_cfg = "Configuration file need to be specified. Add '-c config.json', for example."
assert args.config is not None, msg_no_cfg
self.resume = None
self.cfg_fname = 'config' / Path(args.config)
# load config file and apply custom cli options
config = read_json(self.cfg_fname)
self._config = _update_config(config, options, args)
# set save_dir where trained model and log will be saved.
self.base_save_dir = Path(self.config['trainer']['save_dir'])
self.exper_name = self.config['processor']['args']['data_name'] + \
'_' + self.config['arch']['type']
timestamp = datetime.now().strftime(r'%m%d_%H%M%S') if timestamp else ''
self._save_dir = self.base_save_dir / 'models' / self.exper_name / timestamp
self._log_dir = self.base_save_dir / 'log' / self.exper_name / timestamp
self.log_dir.mkdir(parents=True, exist_ok=True)
# configure logging module
setup_logging(self.log_dir)
self.log_levels = {
0: logging.WARNING,
1: logging.INFO,
2: logging.DEBUG
}
setup_seed(self._config['seed'])
self.debug_mode = args.debug if "debug" in args else False
self.all = args.all if "all" in args else False
self.reset = args.reset if "reset" in args else False
self.search_mode = args.searchMode if "searchMode" in args else "disable"
self.gradient_accumulation_steps = self.config['trainer']['gradient_accumulation_steps']
if self.search_mode != 'disable':
self.config['trainer']['tensorboardX'] = False
if self.all:
self.config["data_loader"]["args"]["validation_split"] = 0.0
if self.debug_mode:
self.config["trainer"]["epochs"] = 2
def initialize(self, name, module, *args, **kwargs):
"""
finds a function handle with the name given as 'type' in config, and returns the
instance initialized with corresponding keyword args given as 'args'.
"""
module_name = self[name]['type']
module_args = dict(self[name]['args'])
if 'pretrained_model_name_or_path' in module_args:
module_args.pop("pretrained_model_name_or_path")
assert all([k not in module_args for k in kwargs]
), 'Overwriting kwargs given in config file is not allowed'
module_args.update(kwargs)
return getattr(module, module_name)(*args, **module_args)
def initialize_bert_model(self, name, module, *args, **kwargs):
"""
finds a function handle with the name given as 'type' in config, and returns the
instance initialized with corresponding keyword args given as 'args'.
"""
module_name = self[name]['type']
module_args = dict(self[name]['args'])
assert all([k not in module_args for k in kwargs]
), 'Overwriting kwargs given in config file is not allowed'
module_args.update(kwargs)
return getattr(module, module_name).from_pretrained(
cache_dir="data/.cache", *args, **module_args)
def __getitem__(self, name):
return self.config[name]
def get_logger(self, name, verbosity=2):
msg_verbosity = 'verbosity option {} is invalid. Valid options are {}.'.format(
verbosity, self.log_levels.keys())
assert verbosity in self.log_levels, msg_verbosity
logger = logging.getLogger(name)
logger.setLevel(self.log_levels[verbosity])
return logger
def save(self): # save updated config file to the checkpoint dir
if not os.path.exists(str(self.save_dir / 'config.json')):
self.save_dir.mkdir(parents=True, exist_ok=True)
write_json(self.config, self.save_dir / 'config.json')
# setting read-only attributes
@property
def config(self):
return self._config
@property
def save_dir(self):
return self._save_dir
@property
def log_dir(self):
return self._log_dir
def update_config(self, parameter):
for key, value in parameter.items():
_set_by_path(self.config, key, value)
self._save_dir = self.base_save_dir / 'models' / self.exper_name / 'SearchResult'
self._log_dir = self.base_save_dir / 'log' / self.exper_name / 'SearchResult'
self.log_dir.mkdir(parents=True, exist_ok=True)
# configure logging module
setup_logging(self.log_dir)
self.gradient_accumulation_steps = self.config['trainer']['gradient_accumulation_steps']
return
# helper functions used to update config dict with custom cli options
def _update_config(config, options, args):
for opt in options:
value = getattr(args, _get_opt_name(opt.flags))
if value is not None:
_set_by_path(config, opt.target, value)
return config
def _get_opt_name(flags):
for flg in flags:
if flg.startswith('--'):
return flg.replace('--', '')
return flags[0].replace('--', '')
def _set_by_path(tree, keys, value):
"""Set a value in a nested object in tree by sequence of keys."""
_get_by_path(tree, keys[:-1])[keys[-1]] = value
def _get_by_path(tree, keys):
"""Access a nested object in tree by sequence of keys."""
return reduce(getitem, keys, tree)
class MockConfigParser:
def __init__(self, cfg_path='ATEC_BERT/config.json', resume=None):
# parse default and custom cli options
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
if resume:
self.resume = Path(resume)
self.cfg_fname = self.resume.parent / 'config.json'
self.bert_config_path = str(self.resume.parent / 'BertConfig.json')
else:
self.cfg_fname = 'config' / Path(cfg_path)
# load config file and apply custom cli options
self._config = read_json(self.cfg_fname)
# set save_dir where trained model and log will be saved.
self.base_save_dir = Path(self.config['trainer']['save_dir'])
self.exper_name = self.config['processor']['args']['data_name'] + \
'_' + self.config['arch']['type']
self._save_dir = self.base_save_dir / 'models' / self.exper_name / 'mock'
self._log_dir = self.base_save_dir / 'log' / self.exper_name / 'mock'
self.save_dir.mkdir(parents=True, exist_ok=True)
self.log_dir.mkdir(parents=True, exist_ok=True)
# configure logging module
setup_logging(self.log_dir)
self.log_levels = {
0: logging.WARNING,
1: logging.INFO,
2: logging.DEBUG
}
self.debug_mode = True
self.all = False
self.reset = False
self.search_mode = "disable"
self.gradient_accumulation_steps = self.config['trainer']['gradient_accumulation_steps']
if self.search_mode != 'disable':
self.config['trainer']['tensorboardX'] = False
if self.debug_mode:
self.config["trainer"]["epochs"] = 2
def initialize(self, name, module, *args, **kwargs):
"""
finds a function handle with the name given as 'type' in config, and returns the
instance initialized with corresponding keyword args given as 'args'.
"""
module_name = self[name]['type']
module_args = dict(self[name]['args'])
assert all([k not in module_args for k in kwargs]
), 'Overwriting kwargs given in config file is not allowed'
module_args.update(kwargs)
return getattr(module, module_name)(*args, **module_args)
def initialize_bert_model(self, name, module, *args, **kwargs):
"""
finds a function handle with the name given as 'type' in config, and returns the
instance initialized with corresponding keyword args given as 'args'.
"""
module_name = self[name]['type']
module_args = dict(self[name]['args'])
assert all([k not in module_args for k in kwargs]
), 'Overwriting kwargs given in config file is not allowed'
module_args.update(kwargs)
return getattr(module, module_name).from_pretrained(
cache_dir="data/.cache", *args, **module_args)
def __getitem__(self, name):
return self.config[name]
def save(self): # save updated config file to the checkpoint dir
write_json(self.config, self.save_dir / 'config.json')
def get_logger(self, name, verbosity=2):
msg_verbosity = 'verbosity option {} is invalid. Valid options are {}.'.format(
verbosity, self.log_levels.keys())
assert verbosity in self.log_levels, msg_verbosity
logger = logging.getLogger(name)
logger.setLevel(self.log_levels[verbosity])
return logger
# setting read-only attributes
@property
def config(self):
return self._config
@property
def save_dir(self):
return self._save_dir
@property
def log_dir(self):
return self._log_dir
def update_config(self, parameter):
for key, value in parameter.items():
_set_by_path(self.config, key, value)
self._save_dir = self.base_save_dir / 'models' / self.exper_name / 'SearchResult'
self._log_dir = self.base_save_dir / 'log' / self.exper_name / 'SearchResult'
self.log_dir.mkdir(parents=True, exist_ok=True)
# configure logging module
setup_logging(self.log_dir)
self.gradient_accumulation_steps = self.config['trainer']['gradient_accumulation_steps']
return