From 434d457ad1c17732dc00ad34db6243cfc43aaa1f Mon Sep 17 00:00:00 2001 From: Antoni Baum Date: Thu, 7 Apr 2022 08:53:27 -0700 Subject: [PATCH] [tune] Improve missing search dependency info (#23691) Replaces FLAML searchers with a dummy class that throws an informative error on init if FLAML is not installed, removes ConfigSpace import in BOHB example code, adds a note to examples using external dependencies. --- python/ray/tune/examples/ax_example.py | 2 ++ python/ray/tune/examples/bayesopt_example.py | 2 ++ python/ray/tune/examples/blendsearch_example.py | 2 ++ python/ray/tune/examples/bohb_example.py | 9 +++++++-- python/ray/tune/examples/cfo_example.py | 2 ++ python/ray/tune/examples/dragonfly_example.py | 2 ++ python/ray/tune/examples/hebo_example.py | 2 ++ .../hyperopt_conditional_search_space_example.py | 2 ++ python/ray/tune/examples/nevergrad_example.py | 2 ++ .../ray/tune/examples/optuna_define_by_run_example.py | 2 ++ python/ray/tune/examples/optuna_example.py | 2 ++ .../tune/examples/optuna_multiobjective_example.py | 2 ++ python/ray/tune/examples/sigopt_example.py | 2 ++ .../tune/examples/sigopt_multi_objective_example.py | 6 +++++- .../ray/tune/examples/sigopt_prior_beliefs_example.py | 6 +++++- python/ray/tune/examples/skopt_example.py | 4 +++- python/ray/tune/examples/zoopt_example.py | 4 +++- python/ray/tune/suggest/flaml.py | 11 ++++++++++- python/ray/tune/suggest/hebo.py | 2 +- python/ray/tune/tests/test_soft_imports.py | 2 +- 20 files changed, 59 insertions(+), 9 deletions(-) diff --git a/python/ray/tune/examples/ax_example.py b/python/ray/tune/examples/ax_example.py index 00f057c00..b7eb02463 100644 --- a/python/ray/tune/examples/ax_example.py +++ b/python/ray/tune/examples/ax_example.py @@ -1,6 +1,8 @@ """This example demonstrates the usage of AxSearch with Ray Tune. It also checks that it is usable with a separate scheduler. + +Requires the Ax library to be installed (`pip install ax-platform sqlalchemy`). """ import numpy as np import time diff --git a/python/ray/tune/examples/bayesopt_example.py b/python/ray/tune/examples/bayesopt_example.py index ad2f286fb..b1b985b57 100644 --- a/python/ray/tune/examples/bayesopt_example.py +++ b/python/ray/tune/examples/bayesopt_example.py @@ -1,6 +1,8 @@ """This example demonstrates the usage of BayesOpt with Ray Tune. It also checks that it is usable with a separate scheduler. + +Requires the BayesOpt library to be installed (`pip install bayesian-optimization`). """ import time diff --git a/python/ray/tune/examples/blendsearch_example.py b/python/ray/tune/examples/blendsearch_example.py index 3403c2b8d..441dbfb7a 100644 --- a/python/ray/tune/examples/blendsearch_example.py +++ b/python/ray/tune/examples/blendsearch_example.py @@ -1,6 +1,8 @@ """This example demonstrates the usage of BlendSearch with Ray Tune. It also checks that it is usable with a separate scheduler. + +Requires the FLAML library to be installed (`pip install flaml`). """ import time diff --git a/python/ray/tune/examples/bohb_example.py b/python/ray/tune/examples/bohb_example.py index a394f9829..8d61b834a 100644 --- a/python/ray/tune/examples/bohb_example.py +++ b/python/ray/tune/examples/bohb_example.py @@ -1,5 +1,11 @@ #!/usr/bin/env python +"""This example demonstrates the usage of BOHB with Ray Tune. + +Requires the HpBandSter and ConfigSpace libraries to be installed +(`pip install hpbandster ConfigSpace`). +""" + import argparse import json import time @@ -45,8 +51,6 @@ class MyTrainableClass(Trainable): if __name__ == "__main__": - import ConfigSpace as CS # noqa: F401 - parser = argparse.ArgumentParser() parser.add_argument( "--server-address", @@ -70,6 +74,7 @@ if __name__ == "__main__": } # Optional: Pass the parameter space yourself + # import ConfigSpace as CS # config_space = CS.ConfigurationSpace() # config_space.add_hyperparameter( # CS.UniformFloatHyperparameter("width", lower=0, upper=20)) diff --git a/python/ray/tune/examples/cfo_example.py b/python/ray/tune/examples/cfo_example.py index 30fc2acee..b6192165f 100644 --- a/python/ray/tune/examples/cfo_example.py +++ b/python/ray/tune/examples/cfo_example.py @@ -1,6 +1,8 @@ """This example demonstrates the usage of CFO with Ray Tune. It also checks that it is usable with a separate scheduler. + +Requires the FLAML library to be installed (`pip install flaml`). """ import time diff --git a/python/ray/tune/examples/dragonfly_example.py b/python/ray/tune/examples/dragonfly_example.py index aa614b9ea..3a2ff6cc3 100644 --- a/python/ray/tune/examples/dragonfly_example.py +++ b/python/ray/tune/examples/dragonfly_example.py @@ -1,6 +1,8 @@ """This example demonstrates the usage of Dragonfly with Ray Tune. It also checks that it is usable with a separate scheduler. + +Requires the Dragonfly library to be installed (`pip install dragonfly-opt`). """ from __future__ import absolute_import from __future__ import division diff --git a/python/ray/tune/examples/hebo_example.py b/python/ray/tune/examples/hebo_example.py index 928583c14..18ccfe8fc 100644 --- a/python/ray/tune/examples/hebo_example.py +++ b/python/ray/tune/examples/hebo_example.py @@ -1,6 +1,8 @@ """This example demonstrates the usage of HEBO with Ray Tune. It also checks that it is usable with a separate scheduler. + +Requires the HEBO library to be installed (`pip install 'HEBO>=0.2.0'`). """ import time diff --git a/python/ray/tune/examples/hyperopt_conditional_search_space_example.py b/python/ray/tune/examples/hyperopt_conditional_search_space_example.py index 0f7dfcbea..f3c4a1dd6 100644 --- a/python/ray/tune/examples/hyperopt_conditional_search_space_example.py +++ b/python/ray/tune/examples/hyperopt_conditional_search_space_example.py @@ -2,6 +2,8 @@ It also checks that it is usable with a separate scheduler. +Requires the HyperOpt library to be installed (`pip install hyperopt`). + For an example of using a Tune search space, see :doc:`/tune/examples/hyperopt_example`. """ diff --git a/python/ray/tune/examples/nevergrad_example.py b/python/ray/tune/examples/nevergrad_example.py index 3e0a568eb..2c97a5a06 100644 --- a/python/ray/tune/examples/nevergrad_example.py +++ b/python/ray/tune/examples/nevergrad_example.py @@ -1,6 +1,8 @@ """This example demonstrates the usage of Nevergrad with Ray Tune. It also checks that it is usable with a separate scheduler. + +Requires the Nevergrad library to be installed (`pip install nevergrad`). """ import time diff --git a/python/ray/tune/examples/optuna_define_by_run_example.py b/python/ray/tune/examples/optuna_define_by_run_example.py index 2aaabd224..db5698a93 100644 --- a/python/ray/tune/examples/optuna_define_by_run_example.py +++ b/python/ray/tune/examples/optuna_define_by_run_example.py @@ -2,6 +2,8 @@ It also checks that it is usable with a separate scheduler. +Requires the Optuna library to be installed (`pip install optuna`). + For an example of using a Tune search space, see :doc:`/tune/examples/optuna_example`. """ diff --git a/python/ray/tune/examples/optuna_example.py b/python/ray/tune/examples/optuna_example.py index 2be0709ec..a6cac1d1e 100644 --- a/python/ray/tune/examples/optuna_example.py +++ b/python/ray/tune/examples/optuna_example.py @@ -2,6 +2,8 @@ It also checks that it is usable with a separate scheduler. +Requires the Optuna library to be installed (`pip install optuna`). + For an example of using an Optuna define-by-run function, see :doc:`/tune/examples/optuna_define_by_run_example`. """ diff --git a/python/ray/tune/examples/optuna_multiobjective_example.py b/python/ray/tune/examples/optuna_multiobjective_example.py index 8e6f11b83..439c1c93c 100644 --- a/python/ray/tune/examples/optuna_multiobjective_example.py +++ b/python/ray/tune/examples/optuna_multiobjective_example.py @@ -3,6 +3,8 @@ multi-objective optimization. Please note that schedulers may not work correctly with multi-objective optimization. + +Requires the Optuna library to be installed (`pip install optuna`). """ import time diff --git a/python/ray/tune/examples/sigopt_example.py b/python/ray/tune/examples/sigopt_example.py index edeab4f78..d1e353408 100644 --- a/python/ray/tune/examples/sigopt_example.py +++ b/python/ray/tune/examples/sigopt_example.py @@ -1,6 +1,8 @@ """This example demonstrates the usage of SigOpt with Ray Tune. It also checks that it is usable with a separate scheduler. + +Requires the SigOpt library to be installed (`pip install sigopt`). """ import sys import time diff --git a/python/ray/tune/examples/sigopt_multi_objective_example.py b/python/ray/tune/examples/sigopt_multi_objective_example.py index 994215256..86157f944 100644 --- a/python/ray/tune/examples/sigopt_multi_objective_example.py +++ b/python/ray/tune/examples/sigopt_multi_objective_example.py @@ -1,4 +1,8 @@ -"""Example using Sigopt's multi-objective functionality.""" +""" +Example using Sigopt's multi-objective functionality. + +Requires the SigOpt library to be installed (`pip install sigopt`). +""" import sys import time diff --git a/python/ray/tune/examples/sigopt_prior_beliefs_example.py b/python/ray/tune/examples/sigopt_prior_beliefs_example.py index f3f7a39ad..6da4f02c8 100644 --- a/python/ray/tune/examples/sigopt_prior_beliefs_example.py +++ b/python/ray/tune/examples/sigopt_prior_beliefs_example.py @@ -1,4 +1,8 @@ -""""Example using Sigopt's support for prior beliefs.""" +"""" +Example using Sigopt's support for prior beliefs. + +Requires the SigOpt library to be installed (`pip install sigopt`). +""" import sys import numpy as np diff --git a/python/ray/tune/examples/skopt_example.py b/python/ray/tune/examples/skopt_example.py index a2097b8d0..fe5f783e3 100644 --- a/python/ray/tune/examples/skopt_example.py +++ b/python/ray/tune/examples/skopt_example.py @@ -1,6 +1,8 @@ -"""This example demonstrates the usage of Skopt with Ray Tune. +"""This example demonstrates the usage of SkOpt with Ray Tune. It also checks that it is usable with a separate scheduler. + +Requires the SkOpt library to be installed (`pip install scikit-optimize`). """ import time diff --git a/python/ray/tune/examples/zoopt_example.py b/python/ray/tune/examples/zoopt_example.py index 609237ada..2795c46dd 100644 --- a/python/ray/tune/examples/zoopt_example.py +++ b/python/ray/tune/examples/zoopt_example.py @@ -1,13 +1,14 @@ """This example demonstrates the usage of ZOOptSearch. It also checks that it is usable with a separate scheduler. + +Requires the ZOOpt library to be installed (`pip install zoopt`). """ import time from ray import tune from ray.tune.suggest.zoopt import ZOOptSearch from ray.tune.schedulers import AsyncHyperBandScheduler -from zoopt import ValueType # noqa: F401 def evaluation_fn(step, width, height): @@ -50,6 +51,7 @@ if __name__ == "__main__": num_samples = 10 if args.smoke_test else 1000 # Optional: Pass the parameter space yourself + # from zoopt import ValueType # space = { # # for continuous dimensions: (continuous, search_range, precision) # "height": (ValueType.CONTINUOUS, [-10, 10], 1e-2), diff --git a/python/ray/tune/suggest/flaml.py b/python/ray/tune/suggest/flaml.py index a159ea13f..ca2ce787d 100644 --- a/python/ray/tune/suggest/flaml.py +++ b/python/ray/tune/suggest/flaml.py @@ -1,4 +1,13 @@ try: from flaml import BlendSearch, CFO except ImportError: - BlendSearch = CFO = None + + class _DummyErrorRaiser: + def __init__(self, *args, **kwargs) -> None: + raise ImportError( + "FLAML must be installed! " + "You can install FLAML with the command: " + "`pip install flaml`." + ) + + BlendSearch = CFO = _DummyErrorRaiser diff --git a/python/ray/tune/suggest/hebo.py b/python/ray/tune/suggest/hebo.py index 5b11f8317..f796f0a84 100644 --- a/python/ray/tune/suggest/hebo.py +++ b/python/ray/tune/suggest/hebo.py @@ -139,7 +139,7 @@ class HEBOSearch(Searcher): ): assert hebo is not None, ( "HEBO must be installed! You can install HEBO with" - " the command: `pip install 'HEBO>=0.2.0'." + " the command: `pip install 'HEBO>=0.2.0'`." "This error may also be caused if HEBO" " dependencies have bad versions. Try updating HEBO" " first." diff --git a/python/ray/tune/tests/test_soft_imports.py b/python/ray/tune/tests/test_soft_imports.py index bf7344ae7..1c216e8b1 100644 --- a/python/ray/tune/tests/test_soft_imports.py +++ b/python/ray/tune/tests/test_soft_imports.py @@ -15,7 +15,7 @@ class TestSoftImports(unittest.TestCase): # ensure that the dependencies aren't actually installed if searcher and name not in ("variant_generator", "random"): - with self.assertRaises(AssertionError): + with self.assertRaises((AssertionError, ImportError)): searcher()