-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtune.py
320 lines (264 loc) · 11.2 KB
/
tune.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Usage:
# python tune.py -C runs/template_run/config.json -T config/test_tune_config.hocon \
# -R runs/test_tune
#
# [more arguments:]
# --tune_max_processes 16
# --tune_random 16
# --tune_local 32
# --tune_maximize -eval.perplexity.mean+eval.accuracy.mean+-eval.num_params
#
# See config/test_tune_config for format and options of the -T file.
import argparse
import itertools
import math
import multiprocessing as mp
import os
import Queue
import time
import warnings
import pygtrie as trie
from stanza.research import config
from stanza.research.rng import get_rng
import run_experiment
parser = config.get_options_parser()
parser.add_argument('--tune_config', '-T',
help='Path to config file containing options for tuning search')
parser.add_argument('--tune_max_processes', type=int, default=16,
help='Number of processes to run at once for tuning search')
parser.add_argument('--tune_delay', type=float, default=10.0,
help='Number of seconds to wait in between starting each process. '
'If non-positive, do not wait.')
parser.add_argument('--tune_random', type=int, default=16,
help='Number of random parameter settings to try before switching '
'to local search')
parser.add_argument('--tune_local', type=int, default=0,
help='Maximum number of local search parameter settings to try.'
'If non-positive, keep searching until a local optimum is found.')
parser.add_argument('--tune_maximize', default='-eval.perplexity.gmean',
help='Name of result to optimize. Can be multiple results separated with a '
'"+" to add them. Prefix a result with "-" to minimize it.')
rng = get_rng()
def tune_queue(main_fn):
config.redirect_output()
options = config.options()
if any('tune' not in s for s in options.data_source):
warnings.warn('expected all --data_source\'s to contain "tune", instead got "{}". '
'Are you polluting your dev/test set?'.format(options.data_source))
if 'gpu' in options.device or 'cuda' in options.device:
warnings.warn('device is "{}". Have you checked that all processes will fit '
'on one GPU? (Random GPU assignment has not been implemented '
'yet.)'.format(options.device))
with open(options.tune_config, 'r') as infile:
tune_options = config.HoconConfigFileParser().parse(infile)
reg = ProcessRegistry(main_fn, tune_options, options.tune_maximize)
remaining_random = options.tune_random
remaining_local = options.tune_local
if options.tune_local <= 0:
remaining_local = None
try:
reg.start_default()
while remaining_random > 0 and reg.running_processes < options.tune_max_processes:
reg.start_random()
remaining_random -= 1
while remaining_local > 0 and reg.running_processes < options.tune_max_processes:
reg.start_local()
remaining_random -= 1
while reg.running_processes > 0:
name, objective = reg.get()
print('\nTUNE: {:10.3f} {}\n'.format(objective, name[:70]))
while remaining_random > 0 and reg.running_processes < options.tune_max_processes:
reg.start_random()
remaining_random -= 1
while (remaining_local is None or remaining_local > 0) and \
reg.running_processes < options.tune_max_processes:
try:
reg.start_local()
if remaining_local is not None:
remaining_local -= 1
except StopIteration:
print('no new local search candidates')
break
except KeyboardInterrupt:
reg.terminate()
print('')
print('best result:')
print('{:10.3f} {}'.format(reg.best_objective, str(reg.best_name)[:70]))
class ProcessRegistry(object):
def __init__(self, main_fn, tune_options, objective):
self.main_fn = main_fn
self.tune_options = tune_options
self.objective = objective
self.abbreviations = abbreviate(tune_options['options'].keys())
self.proc_for_name = {}
self.name_for_options = {}
self.options_for_name = {}
self.best_objective = None
self.best_name = None
self.running_processes = 0
self.results_queue = mp.Queue()
def start_default(self):
self.start(self.base_options(), mode='default')
def start_random(self):
self.start(self.generate_new_random_options(), mode='random')
def start_local(self):
self.start(self.generate_new_local_options(), mode='local')
def start(self, tuned_options, mode='manual'):
name = self.short_name(tuned_options)
options_dict = dict(config.options().__dict__)
options_dict['run_dir'] = os.path.join(options_dict['run_dir'], name)
options_dict['overwrite'] = False
options_dict['config'] = None
for k, v in tuned_options:
options_dict[k] = v
options = argparse.Namespace(**options_dict)
if options_dict['tune_delay'] > 0:
time.sleep(options_dict['tune_delay'])
proc = mp.Process(target=queue_results,
args=(self.main_fn, options, name, self.results_queue))
self.proc_for_name[name] = proc
self.name_for_options[tuned_options] = name
self.options_for_name[name] = tuned_options
self.running_processes += 1
print('starting {}: {}'.format(mode, name))
proc.start()
def get(self):
name, results = self.results_queue.get()
try:
self.proc_for_name[name].join(timeout=10)
except Queue.Empty:
self.proc_for_name[name].terminate()
self.proc_for_name[name] = None
self.running_processes -= 1
objective = get_objective(results, self.objective)
if self.best_objective is None or self.best_objective < objective:
self.best_objective = objective
self.best_name = name
return name, objective
def terminate(self):
running_names = []
for name, proc in self.proc_for_name.iteritems():
running_names.append(name)
if proc is not None:
proc.terminate()
for name in running_names:
self.proc_for_name[name] = None
self.running_processes -= 1
def generate_new_random_options(self):
while True:
tuned_options_list = []
for key, values in self.tune_options['options'].iteritems():
tuned_options_list.append((key, values[rng.randint(len(values))]))
tuned_options = tuple(tuned_options_list)
if tuned_options not in self.name_for_options:
return tuned_options
def generate_new_local_options(self):
if self.best_name is None:
base = self.base_options()
else:
base = self.options_for_name[self.best_name]
unexplored_neighbors = []
for k, v_orig in base:
changes = [
[(k, v_new) for v_new in self.tune_options['options'][k] if v_new != v_orig]
]
if k in self.tune_options['interactions']:
for k2 in self.tune_options['interactions'][k]:
changes.append(
[(k2, v_new) for v_new in self.tune_options['options'][k2]]
)
for changeset in itertools.product(*changes):
changeset = dict(changeset)
neighbor = tuple(
(k2, (changeset[k2] if k2 in changeset else v))
for k2, v in base
)
if neighbor not in self.name_for_options:
unexplored_neighbors.append(neighbor)
if unexplored_neighbors:
return unexplored_neighbors[rng.randint(len(unexplored_neighbors))]
else:
raise StopIteration
def base_options(self):
options = config.options()
return tuple((key, getattr(options, key))
for key, _ in self.tune_options['options'].iteritems())
def short_name(self, tuned_options):
return '+'.join(
'{}={}'.format(self.abbreviations[k], v)
for k, v in tuned_options
).replace('/', '_').replace(' ', '_').replace('\\', '_')
def abbreviate(all_keys):
'''
Find a minimal abbreviation for each key in a set of string keys.
This is the series of character decisions you have to make to reach
that key, or, perhaps more familiar, the series of characters you
would have to type if you hit 'tab' before each character to
auto-complete any unique prefix.
>>> abbreviate(['spe_lea_rate', 'spe_cell_size', 'spe_opt'])['spe_lea_rate']
'l'
>>> abbreviate(['spe_lea_rate', 'ste_lea_rate'])['spe_lea_rate']
'p'
>>> abbreviate(['spe_lea_rate', 'spe_lea_rates'])['spe_lea_rate']
''
>>> abbreviate(['spe_lea_rate', 'spe_lea_rates'])['spe_lea_rates']
's'
'''
t = trie.CharTrie({k: True for k in all_keys})
# Add a node for each string that is a branching point in the trie
internal_nodes = {}
def collect_internal_nodes(path_conv, path, children, value=None):
children = list(children)
if len(children) > 1:
internal_nodes[path_conv(path)] = True
t.traverse(collect_internal_nodes)
t.update(internal_nodes)
abbrev_map = {}
for key in all_keys:
# t.prefixes(key) gives all branching points on the path leading to key.
# Extract the next character after each branching point and concatenate them.
abbrev_map[key] = ''.join(
key[len(prefix):len(prefix) + 1]
for prefix, _ in t.prefixes(key)
)
return abbrev_map
def get_objective(results, spec):
if isinstance(results, Exception):
return float('-inf')
total = 0.0
pieces = spec.split('+')
for piece in pieces:
if piece.startswith('-'):
total -= results[piece[1:]]
else:
total += results[piece]
if math.isnan(total):
# Any nan is worse than any finite number
total = float('-inf')
return total
def test_main():
options = config.options()
import sys
print('stdout')
sys.stderr.write('stderr\n')
return {}, {
'eval.perplexity.gmean': (options.speaker_learning_rate +
options.speaker_cell_size +
len(options.speaker_optimizer))
}
def queue_results(main_fn, options, name, q):
try:
# Create run dir and force options to become the passed Namespace object
config.set_options(options)
config.redirect_output()
(train, test) = main_fn()
results = dict(train)
results.update(test)
q.put((name, results))
except Exception as e:
import sys
import traceback
traceback.print_exc(file=sys.stderr)
q.put((name, e))
if __name__ == '__main__':
tune_queue(run_experiment.main)