Skip to content

Commit 67bdcde

Browse files
Copilotthinkall
andauthored
Fix BlendSearch OptunaSearch warning for non-hierarchical spaces with Ray Tune domains (#1477)
* Initial plan * Fix BlendSearch OptunaSearch warning for non-hierarchical spaces Co-authored-by: thinkall <3197038+thinkall@users.noreply.github.com> * Clean up test file Co-authored-by: thinkall <3197038+thinkall@users.noreply.github.com> * Add regression test for BlendSearch UDF mode warning fix Co-authored-by: thinkall <3197038+thinkall@users.noreply.github.com> * Improve the fix and tests * Fix Define-by-run function passed in argument is not yet supported when using --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: thinkall <3197038+thinkall@users.noreply.github.com> Co-authored-by: Li Jiang <bnujli@gmail.com> Co-authored-by: Li Jiang <lijiang1@microsoft.com>
1 parent 46a406e commit 67bdcde

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

flaml/tune/searcher/blendsearch.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,24 @@ def __init__(
217217
if global_search_alg is not None:
218218
self._gs = global_search_alg
219219
elif getattr(self, "__name__", None) != "CFO":
220-
if space and self._ls.hierarchical:
220+
# Use define-by-run for OptunaSearch when needed:
221+
# - Hierarchical/conditional spaces are best supported via define-by-run.
222+
# - Ray Tune domain/grid specs can trigger an "unresolved search space" warning
223+
# unless we switch to define-by-run.
224+
use_define_by_run = bool(getattr(self._ls, "hierarchical", False))
225+
if (not use_define_by_run) and isinstance(space, dict) and space:
226+
try:
227+
from .variant_generator import parse_spec_vars
228+
229+
_, domain_vars, grid_vars = parse_spec_vars(space)
230+
use_define_by_run = bool(domain_vars or grid_vars)
231+
except Exception:
232+
# Be conservative: if we can't determine whether the space is
233+
# unresolved, fall back to the original behavior.
234+
use_define_by_run = False
235+
236+
self._use_define_by_run = use_define_by_run
237+
if use_define_by_run:
221238
from functools import partial
222239

223240
gs_space = partial(define_by_run_func, space=space)
@@ -487,7 +504,7 @@ def on_trial_complete(self, trial_id: str, result: Optional[Dict] = None, error:
487504
self._ls_bound_max,
488505
self._subspace.get(trial_id, self._ls.space),
489506
)
490-
if self._gs is not None and self._experimental and (not self._ls.hierarchical):
507+
if self._gs is not None and self._experimental and (not getattr(self, "_use_define_by_run", False)):
491508
self._gs.add_evaluated_point(flatten_dict(config), objective)
492509
# TODO: recover when supported
493510
# converted = convert_key(config, self._gs.space)

test/tune/test_searcher.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,26 @@ def test_no_optuna():
324324
import flaml.tune.searcher.suggestion
325325

326326
subprocess.check_call([sys.executable, "-m", "pip", "install", "optuna==2.8.0"])
327+
328+
329+
def test_unresolved_search_space(caplog):
330+
import logging
331+
332+
from flaml import tune
333+
from flaml.tune.searcher.blendsearch import BlendSearch
334+
335+
if caplog is not None:
336+
caplog.set_level(logging.INFO)
337+
338+
BlendSearch(metric="loss", mode="min", space={"lr": tune.uniform(0.001, 0.1), "depth": tune.randint(1, 10)})
339+
try:
340+
text = caplog.text
341+
except AttributeError:
342+
text = ""
343+
assert (
344+
"unresolved search space" not in text and text
345+
), "BlendSearch should not produce warning about unresolved search space"
346+
347+
348+
if __name__ == "__main__":
349+
test_unresolved_search_space(None)

0 commit comments

Comments
 (0)