Skip to content

Commit

Permalink
[air/tune] Catch empty hyperopt search space, raise better Tuner erro…
Browse files Browse the repository at this point in the history
…r message (#28503)
  • Loading branch information
krfricke authored Sep 14, 2022
1 parent ab036ed commit 70153f2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
16 changes: 16 additions & 0 deletions python/ray/tune/search/hyperopt/hyperopt_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
logger = logging.getLogger(__name__)


HYPEROPT_UNDEFINED_DETAILS = (
" This issue can also come up with HyperOpt if your search space only "
"contains constant variables, which is not supported by HyperOpt. In that case, "
"don't pass any searcher or add sample variables to the search space."
)


class HyperOptSearch(Searcher):
"""A wrapper around HyperOpt to provide trial suggestions.
Expand Down Expand Up @@ -192,6 +199,14 @@ def __init__(
def _setup_hyperopt(self) -> None:
from hyperopt.fmin import generate_trials_to_calculate

if not self._space:
raise RuntimeError(
UNDEFINED_SEARCH_SPACE.format(
cls=self.__class__.__name__, space="space"
)
+ HYPEROPT_UNDEFINED_DETAILS
)

if self._metric is None and self._mode:
# If only a mode was passed, use anonymous metric
self._metric = DEFAULT_METRIC
Expand Down Expand Up @@ -283,6 +298,7 @@ def suggest(self, trial_id: str) -> Optional[Dict]:
UNDEFINED_SEARCH_SPACE.format(
cls=self.__class__.__name__, space="space"
)
+ HYPEROPT_UNDEFINED_DETAILS
)
if not self._metric or not self._mode:
raise RuntimeError(
Expand Down
11 changes: 11 additions & 0 deletions python/ray/tune/tests/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,17 @@ def testConvertHyperOptNested(self):

self.assertIn(config["domain_nested"], ["M", "N", "O", "P"])

def testConvertHyperOptConstant(self):
from ray.tune.search.hyperopt import HyperOptSearch

config = {"a": 4}

searcher = HyperOptSearch()
with self.assertRaisesRegex(
RuntimeError, "This issue can also come up with HyperOpt"
):
searcher.set_search_properties(metric="a", mode="max", config=config)

def testSampleBoundsHyperopt(self):
from ray.tune.search.hyperopt import HyperOptSearch

Expand Down
18 changes: 12 additions & 6 deletions python/ray/tune/tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
_SELF = "self"


_TUNER_FAILED_MSG = (
"The Ray Tune run failed. Please inspect the previous error messages for a "
"cause. After fixing the issue, you can restart the run from scratch or "
"continue this run. To continue this run, you can use "
'`tuner = Tuner.restore("{path}")`.'
)


@PublicAPI(stability="beta")
class Tuner:
"""Tuner is the recommended way of launching hyperparameter tuning jobs with Ray Tune.
Expand Down Expand Up @@ -235,9 +243,9 @@ def fit(self) -> ResultGrid:
return self._local_tuner.fit()
except Exception as e:
raise TuneError(
f"Tune run failed. "
f'Please use tuner = Tuner.restore("'
f'{self._local_tuner.get_experiment_checkpoint_dir()}") to resume.'
_TUNER_FAILED_MSG.format(
path=self._local_tuner.get_experiment_checkpoint_dir()
)
) from e
else:
experiment_checkpoint_dir = ray.get(
Expand All @@ -247,7 +255,5 @@ def fit(self) -> ResultGrid:
return ray.get(self._remote_tuner.fit.remote())
except Exception as e:
raise TuneError(
f"Tune run failed. "
f'Please use tuner = Tuner.restore("'
f'{experiment_checkpoint_dir}") to resume.'
_TUNER_FAILED_MSG.format(path=experiment_checkpoint_dir)
) from e

0 comments on commit 70153f2

Please sign in to comment.