hydra-optuna-sweeper 1.4.0.dev7__tar.gz → 1.4.0.dev8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/PKG-INFO +1 -1
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/hydra_optuna_sweeper.egg-info/PKG-INFO +1 -1
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/hydra_plugins/hydra_optuna_sweeper/__init__.py +1 -1
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/hydra_plugins/hydra_optuna_sweeper/_impl.py +3 -61
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/hydra_plugins/hydra_optuna_sweeper/config.py +0 -32
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/hydra_plugins/hydra_optuna_sweeper/optuna_sweeper.py +0 -3
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/tests/test_optuna_sweeper_plugin.py +3 -91
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/MANIFEST.in +0 -0
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/README.md +0 -0
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/hydra_optuna_sweeper.egg-info/SOURCES.txt +0 -0
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/hydra_optuna_sweeper.egg-info/dependency_links.txt +0 -0
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/hydra_optuna_sweeper.egg-info/requires.txt +0 -0
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/hydra_optuna_sweeper.egg-info/top_level.txt +0 -0
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/hydra_plugins/hydra_optuna_sweeper/py.typed +0 -0
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/pyproject.toml +0 -0
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/setup.cfg +0 -0
- {hydra_optuna_sweeper-1.4.0.dev7 → hydra_optuna_sweeper-1.4.0.dev8}/setup.py +0 -0
|
@@ -2,14 +2,11 @@
|
|
|
2
2
|
import functools
|
|
3
3
|
import logging
|
|
4
4
|
import sys
|
|
5
|
-
import warnings
|
|
6
|
-
from textwrap import dedent
|
|
7
5
|
from typing import (
|
|
8
6
|
Any,
|
|
9
7
|
Callable,
|
|
10
8
|
Dict,
|
|
11
9
|
List,
|
|
12
|
-
MutableMapping,
|
|
13
10
|
MutableSequence,
|
|
14
11
|
Optional,
|
|
15
12
|
Sequence,
|
|
@@ -17,7 +14,6 @@ from typing import (
|
|
|
17
14
|
)
|
|
18
15
|
|
|
19
16
|
import optuna
|
|
20
|
-
from hydra._internal.deprecation_warning import deprecation_warning
|
|
21
17
|
from hydra.core.override_parser.overrides_parser import OverridesParser
|
|
22
18
|
from hydra.core.override_parser.types import (
|
|
23
19
|
ChoiceSweep,
|
|
@@ -40,39 +36,11 @@ from optuna.distributions import (
|
|
|
40
36
|
)
|
|
41
37
|
from optuna.trial import Trial
|
|
42
38
|
|
|
43
|
-
from .config import Direction
|
|
39
|
+
from .config import Direction
|
|
44
40
|
|
|
45
41
|
log = logging.getLogger(__name__)
|
|
46
42
|
|
|
47
43
|
|
|
48
|
-
def create_optuna_distribution_from_config(
|
|
49
|
-
config: MutableMapping[str, Any],
|
|
50
|
-
) -> BaseDistribution:
|
|
51
|
-
kwargs = dict(config)
|
|
52
|
-
if isinstance(config["type"], str):
|
|
53
|
-
kwargs["type"] = DistributionType[config["type"]]
|
|
54
|
-
param = DistributionConfig(**kwargs)
|
|
55
|
-
if param.type == DistributionType.categorical:
|
|
56
|
-
assert param.choices is not None
|
|
57
|
-
return CategoricalDistribution(param.choices)
|
|
58
|
-
if param.type == DistributionType.int:
|
|
59
|
-
assert param.low is not None
|
|
60
|
-
assert param.high is not None
|
|
61
|
-
if param.log:
|
|
62
|
-
return IntDistribution(int(param.low), int(param.high), log=True)
|
|
63
|
-
step = int(param.step) if param.step is not None else 1
|
|
64
|
-
return IntDistribution(int(param.low), int(param.high), step=step)
|
|
65
|
-
if param.type == DistributionType.float:
|
|
66
|
-
assert param.low is not None
|
|
67
|
-
assert param.high is not None
|
|
68
|
-
if param.log:
|
|
69
|
-
return FloatDistribution(param.low, param.high, log=True)
|
|
70
|
-
if param.step is not None:
|
|
71
|
-
return FloatDistribution(param.low, param.high, step=param.step)
|
|
72
|
-
return FloatDistribution(param.low, param.high)
|
|
73
|
-
raise NotImplementedError(f"{param.type} is not supported by Optuna sweeper.")
|
|
74
|
-
|
|
75
|
-
|
|
76
44
|
def create_optuna_distribution_from_override(override: Override) -> Any:
|
|
77
45
|
if not override.is_sweep_override():
|
|
78
46
|
return override.get_value_element_as_str()
|
|
@@ -160,7 +128,6 @@ class OptunaSweeperImpl(Sweeper):
|
|
|
160
128
|
n_trials: int,
|
|
161
129
|
n_jobs: int,
|
|
162
130
|
max_failure_rate: float,
|
|
163
|
-
search_space: Optional[DictConfig],
|
|
164
131
|
custom_search_space: Optional[str],
|
|
165
132
|
params: Optional[DictConfig],
|
|
166
133
|
) -> None:
|
|
@@ -178,34 +145,12 @@ class OptunaSweeperImpl(Sweeper):
|
|
|
178
145
|
] = None
|
|
179
146
|
if custom_search_space:
|
|
180
147
|
self.custom_search_space_extender = get_method(custom_search_space)
|
|
181
|
-
self.search_space = search_space
|
|
182
148
|
self.params = params
|
|
183
149
|
self.job_idx: int = 0
|
|
184
|
-
self.search_space_distributions: Optional[Dict[str, BaseDistribution]] = None
|
|
185
150
|
|
|
186
151
|
def _process_searchspace_config(self) -> None:
|
|
187
|
-
|
|
188
|
-
if self.params is None and self.search_space is None:
|
|
152
|
+
if self.params is None:
|
|
189
153
|
self.params = OmegaConf.create({})
|
|
190
|
-
elif self.search_space is not None:
|
|
191
|
-
if self.params is not None:
|
|
192
|
-
warnings.warn(
|
|
193
|
-
"Both hydra.sweeper.params and hydra.sweeper.search_space are configured."
|
|
194
|
-
"\nHydra will use hydra.sweeper.params for defining search space."
|
|
195
|
-
f"\n{url}"
|
|
196
|
-
)
|
|
197
|
-
else:
|
|
198
|
-
deprecation_warning(
|
|
199
|
-
message=dedent(f"""\
|
|
200
|
-
`hydra.sweeper.search_space` is deprecated and will be removed in the next major release.
|
|
201
|
-
Please configure with `hydra.sweeper.params`.
|
|
202
|
-
{url}
|
|
203
|
-
"""),
|
|
204
|
-
)
|
|
205
|
-
self.search_space_distributions = {
|
|
206
|
-
str(x): create_optuna_distribution_from_config(y)
|
|
207
|
-
for x, y in self.search_space.items()
|
|
208
|
-
}
|
|
209
154
|
|
|
210
155
|
def setup(
|
|
211
156
|
self,
|
|
@@ -308,10 +253,7 @@ class OptunaSweeperImpl(Sweeper):
|
|
|
308
253
|
fixed_overrides,
|
|
309
254
|
) = create_params_from_overrides(params_conf)
|
|
310
255
|
|
|
311
|
-
search_space_distributions =
|
|
312
|
-
if self.search_space_distributions:
|
|
313
|
-
search_space_distributions = self.search_space_distributions.copy()
|
|
314
|
-
search_space_distributions.update(override_search_space_distributions)
|
|
256
|
+
search_space_distributions = override_search_space_distributions
|
|
315
257
|
|
|
316
258
|
if is_grid_sampler:
|
|
317
259
|
search_space_for_grid_sampler = {
|
|
@@ -15,12 +15,6 @@ def raise_motpe_removed(*args: Any, **kwargs: Any) -> NoReturn:
|
|
|
15
15
|
)
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
class DistributionType(Enum):
|
|
19
|
-
int = 1
|
|
20
|
-
float = 2
|
|
21
|
-
categorical = 3
|
|
22
|
-
|
|
23
|
-
|
|
24
18
|
class Direction(Enum):
|
|
25
19
|
minimize = 1
|
|
26
20
|
maximize = 2
|
|
@@ -144,30 +138,6 @@ class QMCSamplerConfig(SamplerConfig):
|
|
|
144
138
|
warn_independent_sampling: bool = True
|
|
145
139
|
|
|
146
140
|
|
|
147
|
-
@dataclass
|
|
148
|
-
class DistributionConfig:
|
|
149
|
-
# Type of distribution. "int", "float" or "categorical"
|
|
150
|
-
type: DistributionType
|
|
151
|
-
|
|
152
|
-
# Choices of categorical distribution
|
|
153
|
-
# List element type should be Union[str, int, float, bool]
|
|
154
|
-
choices: Optional[List[Any]] = None
|
|
155
|
-
|
|
156
|
-
# Lower bound of int or float distribution
|
|
157
|
-
low: Optional[float] = None
|
|
158
|
-
|
|
159
|
-
# Upper bound of int or float distribution
|
|
160
|
-
high: Optional[float] = None
|
|
161
|
-
|
|
162
|
-
# If True, space is converted to the log domain
|
|
163
|
-
# Valid for int or float distribution
|
|
164
|
-
log: bool = False
|
|
165
|
-
|
|
166
|
-
# Discritization step
|
|
167
|
-
# Valid for int or float distribution
|
|
168
|
-
step: Optional[float] = None
|
|
169
|
-
|
|
170
|
-
|
|
171
141
|
defaults = [{"sampler": "tpe"}]
|
|
172
142
|
|
|
173
143
|
|
|
@@ -203,8 +173,6 @@ class OptunaSweeperConf:
|
|
|
203
173
|
# Maximum authorized failure rate for a batch of parameters
|
|
204
174
|
max_failure_rate: float = 0.0
|
|
205
175
|
|
|
206
|
-
search_space: Optional[Dict[str, Any]] = None
|
|
207
|
-
|
|
208
176
|
params: Optional[Dict[str, str]] = None
|
|
209
177
|
|
|
210
178
|
# Allow custom trial configuration via Python methods.
|
|
@@ -19,7 +19,6 @@ class OptunaSweeper(Sweeper):
|
|
|
19
19
|
n_trials: int,
|
|
20
20
|
n_jobs: int,
|
|
21
21
|
max_failure_rate: float,
|
|
22
|
-
search_space: Optional[DictConfig],
|
|
23
22
|
custom_search_space: Optional[str],
|
|
24
23
|
params: Optional[DictConfig],
|
|
25
24
|
) -> None:
|
|
@@ -27,7 +26,6 @@ class OptunaSweeper(Sweeper):
|
|
|
27
26
|
|
|
28
27
|
sampler = instantiate(
|
|
29
28
|
sampler,
|
|
30
|
-
_skip_instantiate_full_deepcopy_=True,
|
|
31
29
|
_target_whitelist_=(
|
|
32
30
|
"hydra_plugins.hydra_optuna_sweeper.*",
|
|
33
31
|
"optuna.samplers.*",
|
|
@@ -42,7 +40,6 @@ class OptunaSweeper(Sweeper):
|
|
|
42
40
|
n_trials,
|
|
43
41
|
n_jobs,
|
|
44
42
|
max_failure_rate,
|
|
45
|
-
search_space,
|
|
46
43
|
custom_search_space,
|
|
47
44
|
params,
|
|
48
45
|
)
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
|
|
2
2
|
import os
|
|
3
3
|
import sys
|
|
4
|
-
from functools import partial
|
|
5
4
|
from pathlib import Path
|
|
6
|
-
from typing import Any, List
|
|
5
|
+
from typing import Any, List
|
|
7
6
|
|
|
8
7
|
import optuna
|
|
9
8
|
from hydra.core.override_parser.overrides_parser import OverridesParser
|
|
@@ -24,12 +23,10 @@ from optuna.distributions import (
|
|
|
24
23
|
FloatDistribution,
|
|
25
24
|
IntDistribution,
|
|
26
25
|
)
|
|
27
|
-
from
|
|
28
|
-
from pytest import mark, raises, warns
|
|
26
|
+
from pytest import mark, raises
|
|
29
27
|
|
|
30
28
|
from hydra_plugins.hydra_optuna_sweeper import _impl
|
|
31
|
-
from hydra_plugins.hydra_optuna_sweeper.
|
|
32
|
-
from hydra_plugins.hydra_optuna_sweeper.config import Direction, MOTPESamplerConfig
|
|
29
|
+
from hydra_plugins.hydra_optuna_sweeper.config import MOTPESamplerConfig
|
|
33
30
|
from hydra_plugins.hydra_optuna_sweeper.optuna_sweeper import OptunaSweeper
|
|
34
31
|
|
|
35
32
|
chdir_plugin_root()
|
|
@@ -60,39 +57,6 @@ def check_distribution(expected: BaseDistribution, actual: BaseDistribution) ->
|
|
|
60
57
|
assert set(expected.choices) == set(actual.choices)
|
|
61
58
|
|
|
62
59
|
|
|
63
|
-
@mark.parametrize(
|
|
64
|
-
"input, expected",
|
|
65
|
-
[
|
|
66
|
-
(
|
|
67
|
-
{"type": "categorical", "choices": [1, 2, 3]},
|
|
68
|
-
CategoricalDistribution([1, 2, 3]),
|
|
69
|
-
),
|
|
70
|
-
({"type": "int", "low": 0, "high": 10}, IntDistribution(0, 10)),
|
|
71
|
-
(
|
|
72
|
-
{"type": "int", "low": 0, "high": 10, "step": 2},
|
|
73
|
-
IntDistribution(0, 10, step=2),
|
|
74
|
-
),
|
|
75
|
-
({"type": "int", "low": 0, "high": 5}, IntDistribution(0, 5)),
|
|
76
|
-
(
|
|
77
|
-
{"type": "int", "low": 1, "high": 100, "log": True},
|
|
78
|
-
IntDistribution(1, 100, log=True),
|
|
79
|
-
),
|
|
80
|
-
({"type": "float", "low": 0, "high": 1}, FloatDistribution(0, 1)),
|
|
81
|
-
(
|
|
82
|
-
{"type": "float", "low": 0, "high": 10, "step": 2},
|
|
83
|
-
FloatDistribution(0, 10, step=2),
|
|
84
|
-
),
|
|
85
|
-
(
|
|
86
|
-
{"type": "float", "low": 1, "high": 100, "log": True},
|
|
87
|
-
FloatDistribution(1, 100, log=True),
|
|
88
|
-
),
|
|
89
|
-
],
|
|
90
|
-
)
|
|
91
|
-
def test_create_optuna_distribution_from_config(input: Any, expected: Any) -> None:
|
|
92
|
-
actual = _impl.create_optuna_distribution_from_config(input)
|
|
93
|
-
check_distribution(expected, actual)
|
|
94
|
-
|
|
95
|
-
|
|
96
60
|
@mark.parametrize(
|
|
97
61
|
"input, expected",
|
|
98
62
|
[
|
|
@@ -307,58 +271,6 @@ def test_optuna_custom_search_space_example(tmpdir: Path) -> None:
|
|
|
307
271
|
assert 0 <= w <= 1
|
|
308
272
|
|
|
309
273
|
|
|
310
|
-
@mark.parametrize(
|
|
311
|
-
"search_space,params,raise_warning,msg",
|
|
312
|
-
[
|
|
313
|
-
(None, None, False, None),
|
|
314
|
-
(
|
|
315
|
-
{},
|
|
316
|
-
{},
|
|
317
|
-
True,
|
|
318
|
-
r"Both hydra.sweeper.params and hydra.sweeper.search_space are configured.*",
|
|
319
|
-
),
|
|
320
|
-
(
|
|
321
|
-
{},
|
|
322
|
-
None,
|
|
323
|
-
True,
|
|
324
|
-
r"`hydra.sweeper.search_space` is deprecated and will be removed in the next major release.*",
|
|
325
|
-
),
|
|
326
|
-
(None, {}, False, None),
|
|
327
|
-
],
|
|
328
|
-
)
|
|
329
|
-
def test_warnings(
|
|
330
|
-
tmpdir: Path,
|
|
331
|
-
search_space: Optional[DictConfig],
|
|
332
|
-
params: Optional[DictConfig],
|
|
333
|
-
raise_warning: bool,
|
|
334
|
-
msg: Optional[str],
|
|
335
|
-
) -> None:
|
|
336
|
-
partial_sweeper = partial(
|
|
337
|
-
OptunaSweeperImpl,
|
|
338
|
-
sampler=RandomSampler(),
|
|
339
|
-
direction=Direction.minimize,
|
|
340
|
-
storage=None,
|
|
341
|
-
study_name="test",
|
|
342
|
-
n_trials=1,
|
|
343
|
-
n_jobs=1,
|
|
344
|
-
max_failure_rate=0.0,
|
|
345
|
-
custom_search_space=None,
|
|
346
|
-
)
|
|
347
|
-
if search_space is not None:
|
|
348
|
-
search_space = OmegaConf.create(search_space)
|
|
349
|
-
if params is not None:
|
|
350
|
-
params = OmegaConf.create(params)
|
|
351
|
-
sweeper = partial_sweeper(search_space=search_space, params=params)
|
|
352
|
-
if raise_warning:
|
|
353
|
-
with warns(
|
|
354
|
-
UserWarning,
|
|
355
|
-
match=msg,
|
|
356
|
-
):
|
|
357
|
-
sweeper._process_searchspace_config()
|
|
358
|
-
else:
|
|
359
|
-
sweeper._process_searchspace_config()
|
|
360
|
-
|
|
361
|
-
|
|
362
274
|
@mark.parametrize("max_failure_rate", (0.5, 1.0))
|
|
363
275
|
def test_failure_rate(max_failure_rate: float, tmpdir: Path) -> None:
|
|
364
276
|
cmd = [
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|