-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.py
374 lines (319 loc) · 12.4 KB
/
launcher.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import random
import string
import os
import subprocess
from typing import Union, Optional, Tuple, Any, List
from shutil import rmtree
from collections import defaultdict
__VERSION__ = '0.1.0'
__all__ = ['Launcher']
class Hyperparameter:
def __init__(self, name, value, tunable=False):
self.name = name
self.value = value
self.tunable = tunable
def _spawn_set():
return set()
class Launcher:
"""
Launch a series of experiments against tunable hyperparameters.
Parameters
----------
name : str
Name of the model
configurable : str
The name that is used for `gin.configurable`.
sync : Optional[Union[List[str], str]]
A list of folders/files to sync in order to execute the script.
Default: `None`.
ignores : Optional[Union[List[str], str]]
Specify what folders/files/patterns to ignore.
Default: `None`.
server : int
Choose which server to perform the experiment.
The number depends on `messenger`.
Default: `None`.
num_gpus : int
The number of GPUs required for the experiment.
Default: `0`.
tmp_configs_folder : str
The temporary folder to store all the generated config files.
This folder should be in the local machine.
Default: `None`.
experiment_root : str
The temporary folder to execute the experiment from.
This folder should be in the remote machine.
By default, it will upload the experiment to the
`messenger` temp dir.
Default: `None`.
interpreter : str
Path to the Python interpreter used for the experiment.
This Python interpreter should be in the remote machine.
Default: `None`.
"""
def __init__(self,
name: str,
sync: Optional[Union[Tuple[str, ...], str]] = None,
ignores: Optional[Union[Tuple[str, ...], str]] = None,
server: int = None,
num_gpus: int = 0,
tmp_configs_folder: str = None,
experiment_root: str = None,
interpreter: str = None):
if sync is None:
sync = []
else:
if isinstance(sync, str):
sync = [sync]
if ignores is None:
ignores = []
else:
if isinstance(ignores, str):
ignores = [ignores]
self.name = name
self.sync = sync
self.ignores = list(ignores)
self.num_gpus = num_gpus
self.__hyperparameters = {}
if tmp_configs_folder is None:
tmp_configs_folder = '__tmp__'
if not os.path.exists(tmp_configs_folder):
os.mkdir(tmp_configs_folder)
self.tmp_folder = tmp_configs_folder
self.ignores.append(self.tmp_folder)
self.server = server
if experiment_root is None:
if self.server is None: # work locally
self.tmp_root = '/tmp/launcher-tmp'
if not os.path.exists(self.tmp_root):
os.mkdir(self.tmp_root)
else:
self.tmp_root = '/tmp/messenger-tmp'
else:
self.tmp_root = experiment_root
if self.server is None:
if not os.path.exists(self.tmp_root):
os.mkdir(self.tmp_root)
else:
assert os.path.isabs(self.tmp_root), f'Relative experiment root is not allowed. Got {experiment_root}'
self.interpreter = 'python' if interpreter is None else interpreter
self._skips = defaultdict(_spawn_set)
def set_tunable(self, hyperparameter: str) -> None:
"""
Sets a hyperparameter as tunable.
One can read all hyperparameters from a config file,
and use this method to mark some hyperparameter as tunable.
:param hyperparameter:
name of the hyperparameter
"""
self.__hyperparameters[hyperparameter].tunable = True
def add_hyperparameters(self, name: str, value, tunable=False) -> None:
"""
Adds a hyperparameter.
:param name:
name of the hyperparameter
:param value:
value of the hyperparameter.
If this hyperparameter is tunable,
the value should be a list/tuple of values
:param tunable:
whether this hyperparameter is tunable.
Default: `False`.
"""
if tunable:
assert isinstance(value, (list, tuple))
self.__hyperparameters[name] = Hyperparameter(name, value, tunable)
def load_config(self, config_file):
raise NotImplementedError
def save_config(self, config_name, config):
"""
The config file should be written in the tmp folder.
For e.g., `config_file = os.path.join(self.tmp_folder, f'{config_name}.txt')`
The function should return config_file.
:param config_name:
:param config:
:return:
path to config file.
"""
raise NotImplementedError
def hyperparameters_from_config(self, config: dict) -> None:
"""
Reads hyperparameters from a config dictionary.
:param config:
a config dictionary
"""
for k, v in config.items():
self.add_hyperparameters(k, v)
def skip_for(self, name: str, value: Union[Tuple[Any, ...], Any]) -> None:
if not isinstance(value, (list, tuple)):
value = [value]
for v in value:
self._skips[name].add(v)
def _skip_this(self, config):
for k, v in config.items():
if k in self._skips:
if v in self._skips[k]:
return True
return False
def generate_configs(self) -> dict:
"""
Generates a matrix of configurations.
"""
hyperparameters = list(self.__hyperparameters.values())
def _generate(i, name):
if i == len(hyperparameters):
return {name: {}}
configs = {}
hp = hyperparameters[i]
values = hp.value if hp.tunable else [hp.value]
for value in values:
if hp.tunable:
if name:
tmp_name = f'{name}-{hp.name}-{value}'
else:
tmp_name = f'{hp.name}-{value}'
else:
tmp_name = name
for key, conf in _generate(i + 1, tmp_name).items():
conf[hp.name] = value
configs[key] = conf
return configs
return _generate(0, '')
def _sync(self, files_and_folders: List[str], target: str):
if target is None:
tmpdir = os.path.join(self.tmp_root, 'tmp-')
tmpdir += ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
else:
tmpdir = target
exclude = [f'--exclude={exclude}' for exclude in self.ignores] if self.ignores else []
for to_sync in files_and_folders:
cmd = ['rsync', '-uar', to_sync]
cmd += exclude
cmd += [f'{tmpdir}/']
subprocess.call(cmd) # sync using rsync
return tmpdir
@staticmethod
def _standardize_folder_name(name: str):
for char in '/> |:&':
name = name.replace(char, '%')
return name
def launch_no_config(self, script: str, extra_args: List[str] = None) -> None:
"""
Launches the script without config file.
:param script:
name of the script to launch
:param extra_args:
extra arguments to be passed to script.
:return:
"""
to_sync = list(self.sync)
config_name = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
tmpdir = os.path.join(self.tmp_root, config_name)
print(f'Launching from {tmpdir}...')
if self.server is None: # work locally
working_dir = self._sync(to_sync, tmpdir)
cmd = ['ts']
current_dir = os.getcwd()
os.chdir(working_dir) # cd here to launch ts inside
else:
to_sync = ':'.join(to_sync)
cmd = ['ms', '-H', str(self.server), '--sync', to_sync]
if self.ignores:
exclude = ':'.join(self.ignores)
exclude = ['--exclude', f'{exclude}']
cmd.extend(exclude)
cmd += ['--sync_dest', tmpdir]
if self.num_gpus:
cmd += ['-G', f'{self.num_gpus}']
script_cmd = [self.interpreter, script]
if extra_args is not None:
script_cmd += list(extra_args)
if cmd[0] == 'ms':
cmd.append(' '.join(script_cmd))
elif cmd[0] == 'ts':
cmd += script_cmd
else:
raise ValueError # should never end up here
subprocess.call(cmd)
def launch(self, script: str, extra_args: List[str] = None) -> None:
"""
Launches the script based on the given hyperparameters.
:param script:
name of the script to launch
:param extra_args:
extra arguments to be passed to script.
"""
all_configs = self.generate_configs()
for config_name, config in all_configs.items():
if self._skip_this(config):
continue
if not config_name:
config_name = 'default'
config_name = self._standardize_folder_name(config_name)
config_file = self.save_config(config_name, config)
config_filename = os.path.basename(config_file)
to_sync = list(self.sync)
to_sync.append(config_file)
tmpdir = os.path.join(self.tmp_root, config_name)
tmpdir += '-'
tmpdir += ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
if self.server is None: # work locally
working_dir = self._sync(to_sync, tmpdir)
cmd = ['ts']
current_dir = os.getcwd()
os.chdir(working_dir) # cd here to launch ts inside
else:
to_sync = ':'.join(to_sync)
cmd = ['ms', '-H', str(self.server), '--sync', to_sync]
if self.ignores:
exclude = ':'.join(self.ignores)
exclude = ['--exclude', f'{exclude}']
cmd.extend(exclude)
cmd += ['--sync_dest', tmpdir]
if self.num_gpus:
cmd += ['-G', f'{self.num_gpus}']
cmd += ['-L', f'{self.name}-{config_name}']
script_cmd = [self.interpreter, script, config_filename]
if extra_args is not None:
script_cmd += list(extra_args)
if cmd[0] == 'ms':
cmd.append(' '.join(script_cmd))
elif cmd[0] == 'ts':
cmd += script_cmd
else:
raise ValueError # should never end up here
subprocess.call(cmd)
if self.server is None:
os.chdir(current_dir) # cd back
rmtree(self.tmp_folder)
class GinLauncher(Launcher):
def __init__(self,
name: str,
configurable: str,
sync: Optional[Union[Tuple[str, ...], str]] = None,
ignores: Optional[Union[Tuple[str, ...], str]] = None,
server: int = None,
num_gpus: int = 0,
tmp_configs_folder: str = None,
experiment_root: str = None,
interpreter: str = None):
super().__init__(name, sync, ignores, server, num_gpus, tmp_configs_folder, experiment_root, interpreter)
self.configurable = configurable
def save_config(self, config_name, config):
config_filename = f'{self.name}-{config_name}.gin'
config_file = os.path.join(self.tmp_folder, config_filename)
headers = [
f'{self.configurable}.name = "{self.name}" \n',
f'{self.configurable}.experiment = "{config_name}" \n'
]
contents = []
for k, v in config.items():
if isinstance(v, str):
contents.append(f'{self.configurable}.{k} = "{v}" \n')
else:
contents.append(f'{self.configurable}.{k} = {v} \n')
with open(config_file, 'w') as f:
f.writelines(headers)
f.writelines(contents)
f.close()
return config_file