-
-
Notifications
You must be signed in to change notification settings - Fork 604
/
test_ax_sweeper_plugin.py
356 lines (321 loc) · 11.5 KB
/
test_ax_sweeper_plugin.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
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import math
import os
from pathlib import Path
from typing import Any, List
from hydra.core.plugins import Plugins
from hydra.plugins.sweeper import Sweeper
from hydra.test_utils.test_utils import (
TSweepRunner,
chdir_plugin_root,
run_python_script,
)
from omegaconf import DictConfig, OmegaConf
from pytest import mark, raises
from hydra_plugins.hydra_ax_sweeper.ax_sweeper import AxSweeper
chdir_plugin_root()
def test_discovery() -> None:
"""
Tests that this plugin can be discovered via the plugins subsystem when looking for Sweeper
:return:
"""
assert AxSweeper.__name__ in [
x.__name__ for x in Plugins.instance().discover(Sweeper)
]
def quadratic(cfg: DictConfig) -> Any:
return 100 * (cfg.quadratic.x**2) + 1 * cfg.quadratic.y
@mark.parametrize(
"n,expected",
[
(None, [[1, 2, 3, 4, 5]]),
(1, [[1], [2], [3], [4], [5]]),
(2, [[1, 2], [3, 4], [5]]),
(5, [[1, 2, 3, 4, 5]]),
(6, [[1, 2, 3, 4, 5]]),
],
)
def test_chunk_method_for_valid_inputs(n: int, expected: List[List[int]]) -> None:
from hydra_plugins.hydra_ax_sweeper._core import CoreAxSweeper
chunk_func = CoreAxSweeper.chunks
batch = [1, 2, 3, 4, 5]
out = list(chunk_func(batch, n))
assert out == expected
@mark.parametrize("n", [-1, -11, 0])
def test_chunk_method_for_invalid_inputs(n: int) -> None:
from hydra_plugins.hydra_ax_sweeper._core import CoreAxSweeper
chunk_func = CoreAxSweeper.chunks
batch = [1, 2, 3, 4, 5]
with raises(ValueError):
list(chunk_func(batch, n))
def test_jobs_dirs(hydra_sweep_runner: TSweepRunner) -> None:
# Verify that the spawned jobs are not overstepping the directories of one another.
sweep = hydra_sweep_runner(
calling_file="tests/test_ax_sweeper_plugin.py",
calling_module=None,
task_function=quadratic,
config_path="config",
config_name="config.yaml",
overrides=[
"hydra/launcher=basic",
"hydra.sweeper.ax_config.max_trials=6",
"hydra.sweeper.max_batch_size=2",
"params=basic",
],
)
with sweep:
assert isinstance(sweep.temp_dir, str)
dirs = [
x
for x in os.listdir(sweep.temp_dir)
if os.path.isdir(os.path.join(sweep.temp_dir, x))
]
assert len(dirs) == 6 # and a total of 6 unique output directories
@mark.parametrize("test_conf", ["basic", "logscale"])
def test_jobs_configured_via_config(
hydra_sweep_runner: TSweepRunner, test_conf: str
) -> None:
sweep = hydra_sweep_runner(
calling_file="tests/test_ax_sweeper_plugin.py",
calling_module=None,
task_function=quadratic,
config_path="config",
config_name="config.yaml",
overrides=["hydra/launcher=basic", f"params={test_conf}"],
)
with sweep:
assert sweep.returns is None
returns = OmegaConf.load(f"{sweep.temp_dir}/optimization_results.yaml")
assert isinstance(returns, DictConfig)
assert returns["optimizer"] == "ax"
assert len(returns) == 2
best_parameters = returns.ax
assert math.isclose(best_parameters["quadratic.x"], 0.0, abs_tol=1e-4)
assert math.isclose(best_parameters["quadratic.y"], -1.0, abs_tol=1e-4)
@mark.parametrize(
"test_conf, override, expected_x",
[
("basic", "int(interval(1, 5))", 1.0),
("logscale", "tag(log, interval(0.00001, 1))", 0.00001),
],
)
def test_jobs_configured_via_cmd(
hydra_sweep_runner: TSweepRunner, test_conf: str, override: str, expected_x: float
) -> None:
sweep = hydra_sweep_runner(
calling_file="tests/test_ax_sweeper_plugin.py",
calling_module=None,
task_function=quadratic,
config_path="config",
config_name="config.yaml",
overrides=[
"hydra/launcher=basic",
f"quadratic.x={override}",
"quadratic.y=int(interval(-2, 2))",
f"params={test_conf}",
],
)
with sweep:
assert sweep.returns is None
returns = OmegaConf.load(f"{sweep.temp_dir}/optimization_results.yaml")
assert isinstance(returns, DictConfig)
assert returns["optimizer"] == "ax"
assert len(returns) == 2
best_parameters = returns.ax
assert math.isclose(best_parameters["quadratic.x"], expected_x, abs_tol=1e-4)
assert math.isclose(best_parameters["quadratic.y"], -2.0, abs_tol=1e-4)
def test_jobs_configured_via_cmd_and_config(hydra_sweep_runner: TSweepRunner) -> None:
sweep = hydra_sweep_runner(
calling_file="tests/test_ax_sweeper_plugin.py",
calling_module=None,
task_function=quadratic,
config_path="config",
config_name="config.yaml",
overrides=[
"hydra/launcher=basic",
"hydra.sweeper.ax_config.max_trials=2",
"quadratic.x=int(interval(-5, -2))",
"params=basic",
],
)
with sweep:
assert sweep.returns is None
returns = OmegaConf.load(f"{sweep.temp_dir}/optimization_results.yaml")
assert isinstance(returns, DictConfig)
assert returns["optimizer"] == "ax"
assert len(returns) == 2
best_parameters = returns.ax
assert math.isclose(best_parameters["quadratic.x"], -2.0, abs_tol=1e-4)
assert math.isclose(best_parameters["quadratic.y"], 1.0, abs_tol=1e-4)
def test_configuration_set_via_cmd_and_default_config(
hydra_sweep_runner: TSweepRunner,
) -> None:
sweep = hydra_sweep_runner(
calling_file="tests/test_ax_sweeper_plugin.py",
calling_module=None,
task_function=quadratic,
config_path="config",
config_name="config.yaml",
overrides=[
"hydra/launcher=basic",
"hydra.sweeper.ax_config.max_trials=2",
"hydra.sweeper.ax_config.early_stop.max_epochs_without_improvement=2",
"quadratic=basic",
"quadratic.x=interval(-5, -2)",
"quadratic.y=interval(-1, 1)",
],
)
with sweep:
assert sweep.returns is None
returns = OmegaConf.load(f"{sweep.temp_dir}/optimization_results.yaml")
assert isinstance(returns, DictConfig)
best_parameters = returns.ax
assert "quadratic.x" in best_parameters
assert "quadratic.y" in best_parameters
@mark.parametrize(
"cmd_arg, expected_str",
[
("polynomial.y=choice(-1, 0, 1)", "polynomial.y: choice=[-1, 0, 1]"),
("polynomial.y=range(-1, 2)", "polynomial.y: choice=[-1, 0, 1]"),
("polynomial.y=range(-1, 3, 1)", "polynomial.y: choice=[-1, 0, 1, 2]"),
(
"polynomial.y=range(-1, 2, 0.5)",
"polynomial.y: choice=[-1.0, -0.5, 0.0, 0.5, 1.0, 1.5]",
),
("polynomial.y=int(interval(-1, 2))", "polynomial.y: range=[-1, 2]"),
("polynomial.y=interval(-1, 2)", "polynomial.y: range=[-1.0, 2.0]"),
("polynomial.y=2", "polynomial.y: fixed=2"),
("polynomial.y=2.0", "polynomial.y: fixed=2.0"),
],
)
def test_ax_logging(tmpdir: Path, cmd_arg: str, expected_str: str) -> None:
cmd = [
"tests/apps/polynomial.py",
"-m",
"hydra.run.dir=" + str(tmpdir),
"hydra.job.chdir=True",
"polynomial.x=interval(-5, -2)",
"polynomial.z=10",
"hydra.sweeper.ax_config.max_trials=2",
] + [cmd_arg]
result, _ = run_python_script(cmd)
assert "polynomial.x: range=[-5.0, -2.0]" in result
assert expected_str in result
assert "polynomial.z: fixed=10" in result
@mark.parametrize(
"cmd_args",
[
["polynomial.y=choice(-1, 0, 1)", "polynomial.x=range(2,4)"],
["polynomial.y=1", "polynomial.x=range(2,4)"],
],
)
def test_search_space_exhausted_exception(tmpdir: Path, cmd_args: List[str]) -> None:
cmd = [
"tests/apps/polynomial.py",
"-m",
"hydra.run.dir=" + str(tmpdir),
"hydra.job.chdir=True",
"hydra.sweeper.ax_config.max_trials=2",
] + cmd_args
run_python_script(cmd)
@mark.parametrize(
"cmd_args",
[
["polynomial.y=choice(-1, 0, 1)", "polynomial.x=range(2,4)"],
["polynomial.y=1", "polynomial.x=range(2,4)"],
],
)
def test_search_space_with_constraint_metric(tmpdir: Path, cmd_args: List[str]) -> None:
# test that outcome_constraints experiment parameter `outcome_constraints`
# works correctly, and that the ax_sweeper supports outputting a dictionary
# from the evaluation function so that multiple metrics can be supported.
cmd = [
"tests/apps/polynomial_with_constraint.py",
"-m",
"hydra.run.dir=" + str(tmpdir),
"hydra.job.chdir=True",
"hydra.sweeper.ax_config.max_trials=2",
] + cmd_args
results, _ = run_python_script(cmd)
@mark.parametrize(
"cmd_arg, serialized_encoding, best_coefficients, best_value",
[
(
"polynomial.coefficients=[-1, 0, 1],[2, 3, 4],[5, 6, 7]",
"choice=['[-1,0,1]', '[2,3,4]', '[5,6,7]']",
"'[-1,0,1]'",
101.0,
),
(
"polynomial.coefficients=choice([8, 12, 11],[-1, -1, 1000], [-2, 4, 7])",
"choice=['[8,12,11]', '[-1,-1,1000]', '[-2,4,7]']",
"'[-2,4,7]'",
447,
),
],
)
def test_jobs_using_choice_between_lists(
tmpdir: Path,
cmd_arg: str,
serialized_encoding: str,
best_coefficients: str,
best_value: float,
) -> None:
cmd = [
"tests/apps/polynomial_with_list_coefficients.py",
"-m",
"hydra.run.dir=" + str(tmpdir),
"hydra.job.chdir=True",
"hydra.sweeper.ax_config.max_trials=3",
] + [cmd_arg]
result, _ = run_python_script(cmd)
assert f"polynomial.coefficients: {serialized_encoding}" in result
assert f"'polynomial.coefficients': {best_coefficients}" in result
assert f"New best value: {best_value}" in result
@mark.parametrize(
"cmd_arg, serialized_encoding, best_coefficients, best_value",
[
(
"+polynomial.coefficients=choice({x:-1, y:0, z:1},{x:2, y:3, z:4},{x:5, y:6, z:7})",
"choice=['{x:-1,y:0,z:1}', '{x:2,y:3,z:4}', '{x:5,y:6,z:7}']",
"'{x:-1,y:0,z:1}'",
101.0,
),
(
"+polynomial.coefficients=choice({x:8, y:12, z:11},{x:-1, y:-1, z:1000}, {x:-2, y:4, z:7})",
"choice=['{x:8,y:12,z:11}', '{x:-1,y:-1,z:1000}', '{x:-2,y:4,z:7}']",
"'{x:-2,y:4,z:7}'}",
447,
),
],
)
def test_jobs_using_choice_between_dicts(
tmpdir: Path,
cmd_arg: str,
serialized_encoding: str,
best_coefficients: str,
best_value: float,
) -> None:
cmd = [
"tests/apps/polynomial_with_dict_coefficients.py",
"-m",
"hydra.run.dir=" + str(tmpdir),
"hydra.job.chdir=True",
"hydra.sweeper.ax_config.max_trials=3",
] + [cmd_arg]
result, _ = run_python_script(cmd)
assert f"polynomial.coefficients: {serialized_encoding}" in result
assert f"'+polynomial.coefficients': {best_coefficients}" in result
assert f"New best value: {best_value}" in result
def test_example_app(tmpdir: Path) -> None:
cmd = [
"example/banana.py",
"-m",
"hydra.run.dir=" + str(tmpdir),
"hydra.job.chdir=True",
"banana.x=int(interval(-5, 5))",
"banana.y=interval(-5, 10.1)",
"hydra.sweeper.ax_config.max_trials=2",
]
result, _ = run_python_script(cmd)
assert "banana.x: range=[-5, 5]" in result
assert "banana.y: range=[-5.0, 10.1]" in result