Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated optuna_search to allow users to configure optuna storage #48547

Merged
merged 55 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
2828f4c
updated optuna_search to allow users to configure optuna storage
ravi-dalal Nov 4, 2024
79f6bc8
Merge branch 'master' into master
ravi-dalal Nov 4, 2024
9484070
Merge branch 'master' into master
ravi-dalal Nov 4, 2024
0dbeee5
fixed linting error
ravi-dalal Nov 4, 2024
ea8d81d
initialized _storage attribute
ravi-dalal Nov 5, 2024
63a0d0c
Merge branch 'master' into master
ravi-dalal Nov 5, 2024
770db2f
optimized code
ravi-dalal Nov 5, 2024
af1e14d
Merge branch 'master' into master
ravi-dalal Nov 5, 2024
86aad9a
Merge branch 'master' into master
ravi-dalal Nov 6, 2024
e16b55e
Merge branch 'master' into master
ravi-dalal Nov 6, 2024
1e27f12
addressed PR comments
ravi-dalal Nov 6, 2024
13ed996
Merge branch 'master' into master
ravi-dalal Nov 6, 2024
ccefd5c
added comment for study_name
ravi-dalal Nov 7, 2024
ea92f5b
Merge branch 'master' into master
ravi-dalal Nov 7, 2024
1f3aa16
Merge branch 'master' of https://github.com/ravi-dalal/ray
ravi-dalal Nov 7, 2024
41eda2e
Merge branch 'master' into master
ravi-dalal Nov 8, 2024
046aaab
Merge branch 'master' into master
ravi-dalal Nov 11, 2024
fe6b14e
Merge branch 'master' into master
ravi-dalal Nov 14, 2024
7de6478
Merge branch 'master' into master
ravi-dalal Nov 20, 2024
a9c4fbf
Merge branch 'master' into master
ravi-dalal Nov 20, 2024
053bfba
Merge branch 'master' into master
ravi-dalal Nov 25, 2024
4ab211b
reverted versionadded
ravi-dalal Nov 25, 2024
d367bb0
Merge branch 'master' into master
ravi-dalal Nov 25, 2024
01a29b0
Update python/ray/tune/search/optuna/optuna_search.py
ravi-dalal Nov 25, 2024
09cb437
Update python/ray/tune/search/optuna/optuna_search.py
ravi-dalal Nov 25, 2024
e4a8546
Update python/ray/tune/search/optuna/optuna_search.py
ravi-dalal Nov 25, 2024
902850a
Merge branch 'master' into master
ravi-dalal Nov 25, 2024
fa7b596
Merge branch 'master' into master
ravi-dalal Nov 26, 2024
a2f706d
added storage to testOptuna unit tests
ravi-dalal Nov 26, 2024
9becabe
Merge branch 'master' into master
ravi-dalal Nov 26, 2024
12364f8
added optunasearch with storage unit tests
ravi-dalal Nov 26, 2024
40f2348
Merge branch 'master' into master
ravi-dalal Nov 26, 2024
f76d42a
Merge branch 'master' into master
ravi-dalal Nov 28, 2024
f166190
updated optuna version
ravi-dalal Nov 28, 2024
87ab2a1
Merge branch 'master' into master
ravi-dalal Dec 5, 2024
080bb58
updated python/requirements_compiled.txt file
ravi-dalal Dec 5, 2024
eba9000
Merge branch 'master' into master
ravi-dalal Dec 5, 2024
a30b1e5
fix optuna upgrade issue
hongpeng-guo Dec 6, 2024
1acc9dd
Merge branch 'master' into master
hongpeng-guo Dec 6, 2024
7536ef8
Merge branch 'master' into master
ravi-dalal Dec 6, 2024
dd9a12f
Merge branch 'master' into master
ravi-dalal Dec 6, 2024
7898787
Merge branch 'master' into master
hongpeng-guo Dec 6, 2024
75f98b8
Merge branch 'master' into master
hongpeng-guo Dec 6, 2024
df706ac
Merge branch 'master' of https://github.com/ray-project/ray into hpgu…
hongpeng-guo Dec 6, 2024
39384da
Merge branch 'master' of https://github.com/ravi-dalal/ray into hpguo…
hongpeng-guo Dec 6, 2024
9a02042
fix py-spy version
hongpeng-guo Dec 6, 2024
42cd4ee
Merge branch 'master' into master
hongpeng-guo Dec 6, 2024
0e84c73
Merge branch 'master' into master
hongpeng-guo Dec 7, 2024
4cec1dc
Merge branch 'master' into master
hongpeng-guo Dec 9, 2024
2c407c7
update storage default behavior
hongpeng-guo Dec 9, 2024
2079f3c
Merge branch 'master' of https://github.com/ravi-dalal/ray into hpguo…
hongpeng-guo Dec 9, 2024
5f330b5
Update python/ray/tune/search/optuna/optuna_search.py
hongpeng-guo Dec 10, 2024
2df22ac
Update python/ray/tune/search/optuna/optuna_search.py
hongpeng-guo Dec 10, 2024
9e107c5
Merge branch 'master' into master
ravi-dalal Dec 10, 2024
6bc312f
Merge branch 'master' into master
ravi-dalal Dec 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions python/ray/tune/search/optuna/optuna_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import optuna as ot
from optuna.distributions import BaseDistribution as OptunaDistribution
from optuna.samplers import BaseSampler
from optuna.storages import BaseStorage
from optuna.trial import Trial as OptunaTrial
from optuna.trial import TrialState as OptunaTrialState
except ImportError:
ot = None
OptunaDistribution = None
BaseSampler = None
BaseStorage = None
OptunaTrialState = None
OptunaTrial = None

Expand Down Expand Up @@ -133,7 +135,11 @@ class OptunaSearch(Searcher):
a delay when suggesting new configurations.
This is an Optuna issue and may be fixed in a future
Optuna release.

study_name: Optuna study name that uniquely identifies the trial
results. Defaults to ``"optuna"``.
storage: Optuna storage used for storing trial results to
storages other than in-memory storage,
for instance optuna.storages.RDBStorage.
seed: Seed to initialize sampler with. This parameter is only
used when ``sampler=None``. In all other cases, the sampler
you pass should be initialized with the seed already.
Expand Down Expand Up @@ -322,6 +328,8 @@ def __init__(
mode: Optional[Union[str, List[str]]] = None,
points_to_evaluate: Optional[List[Dict]] = None,
sampler: Optional["BaseSampler"] = None,
study_name: Optional[str] = None,
storage: Optional["BaseStorage"] = None,
seed: Optional[int] = None,
evaluated_rewards: Optional[List] = None,
):
Expand All @@ -343,8 +351,10 @@ def __init__(

self._points_to_evaluate = points_to_evaluate or []
self._evaluated_rewards = evaluated_rewards

self._study_name = "optuna" # Fixed study name for in-memory storage
if study_name:
self._study_name = study_name
else:
self._study_name = "optuna" # Fixed study name for in-memory storage

if sampler and seed:
logger.warning(
Expand All @@ -362,6 +372,17 @@ def __init__(
self._sampler = sampler
self._seed = seed

if storage:
assert isinstance(storage, BaseStorage), (
"The `storage` parameter in `OptunaSearcher` must be an instance "
"of `optuna.samplers.BaseStorage`."
hongpeng-guo marked this conversation as resolved.
Show resolved Hide resolved
)
# If storage is not provided, just set self._storage to None
# so that the default in-memory storage is used. We don't explicitly
# set self._storage to InMemoryStorage because it is a new API in Optuna
# and we want to keep the backward compatibility.
self._storage = storage
hongpeng-guo marked this conversation as resolved.
Show resolved Hide resolved

self._completed_trials = set()

self._ot_trials = {}
Expand All @@ -380,7 +401,6 @@ def _setup_study(self, mode: Union[str, list]):
self._metric = DEFAULT_METRIC

pruner = ot.pruners.NopPruner()
storage = ot.storages.InMemoryStorage()

if self._sampler:
sampler = self._sampler
Expand All @@ -402,7 +422,7 @@ def _setup_study(self, mode: Union[str, list]):
)

self._ot_study = ot.study.create_study(
storage=storage,
storage=self._storage,
sampler=sampler,
pruner=pruner,
study_name=self._study_name,
Expand Down Expand Up @@ -595,6 +615,15 @@ def add_evaluated_point(
else:
intermediate_values_dict = None

# If the trial state is FAILED, the value must be `None` in Optuna==4.1.0
# Reference: https://github.com/optuna/optuna/pull/5211
# This is a temporary fix for the issue that Optuna enforces the value
# to be `None` if the trial state is FAILED.
# TODO (hpguo): A better solution may requires us to update the base class
# to allow the `value` arg in `add_evaluated_point` being `Optional[float]`.
if ot_trial_state == OptunaTrialState.FAIL:
value = None

trial = ot.trial.create_trial(
state=ot_trial_state,
value=value,
Expand Down
114 changes: 112 additions & 2 deletions python/ray/tune/tests/test_searchers.py
ravi-dalal marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def testOptuna(self):
with self.check_searcher_checkpoint_errors_scope():
out = tune.run(
_invalid_objective,
search_alg=OptunaSearch(sampler=RandomSampler(seed=1234)),
search_alg=OptunaSearch(sampler=RandomSampler(seed=1234), storage=None),
config=self.config,
metric="_metric",
mode="max",
Expand All @@ -249,6 +249,35 @@ def testOptuna(self):
)
self.assertCorrectExperimentOutput(out)

def testOptunaWithStorage(self):
from optuna.samplers import RandomSampler
from optuna.storages import JournalStorage
from optuna.storages.journal import JournalFileBackend

from ray.tune.search.optuna import OptunaSearch

np.random.seed(1000) # At least one nan, inf, -inf and float
storage_file_path = "/tmp/my_test_study.log"

with self.check_searcher_checkpoint_errors_scope():
out = tune.run(
_invalid_objective,
search_alg=OptunaSearch(
sampler=RandomSampler(seed=1234),
study_name="my_test_study",
storage=JournalStorage(
JournalFileBackend(file_path=storage_file_path)
),
),
Comment on lines +265 to +271
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment here is to test both cases when the storage arg is set or is None. We need to make sure the tests pass under both scenarios. Maybe consider using @pytest.mark.parametrize fixture to enable both cases.

Here is one example of how to use the fixtures.

@pytest.mark.parametrize("count_preemption_errors", [False, True])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added separate storage unit tests. It's not possible to use parameterized tests with unittest.TestCase subclasses. PTAL and let me know your thoughts.

config=self.config,
metric="_metric",
mode="max",
num_samples=8,
reuse_actors=False,
)
self.assertCorrectExperimentOutput(out)
self.assertTrue(os.path.exists(storage_file_path))

def testOptunaReportTooOften(self):
from optuna.samplers import RandomSampler

Expand Down Expand Up @@ -358,12 +387,16 @@ def run_add_evaluated_trials(self, searcher, get_len_X, get_len_y):
searcher_copy.suggest("1")

def testOptuna(self):
from optuna.storages import JournalStorage
from optuna.storages.journal import JournalFileBackend
from optuna.trial import TrialState

from ray.tune.search.optuna import OptunaSearch

# OptunaSearch with in-memory storage
searcher = OptunaSearch(
space=self.space,
storage=None,
metric="metric",
mode="max",
points_to_evaluate=[{self.param_name: self.valid_value}],
Expand All @@ -374,6 +407,23 @@ def testOptuna(self):

self.assertGreater(get_len(searcher), 0)

# OptunaSearch with external storage
storage_file_path = "/tmp/my_test_study.log"
searcher = OptunaSearch(
space=self.space,
study_name="my_test_study",
storage=JournalStorage(JournalFileBackend(file_path=storage_file_path)),
metric="metric",
mode="max",
points_to_evaluate=[{self.param_name: self.valid_value}],
evaluated_rewards=[1.0],
)

get_len = lambda s: len(s._ot_study.trials) # noqa E731

self.assertGreater(get_len(searcher), 0)
self.assertTrue(os.path.exists(storage_file_path))

searcher = OptunaSearch(
space=self.space,
metric="metric",
Expand Down Expand Up @@ -610,13 +660,40 @@ def testNevergrad(self):
def testOptuna(self):
from ray.tune.search.optuna import OptunaSearch

searcher = OptunaSearch(space=self.config, metric=self.metric_name, mode="max")
searcher = OptunaSearch(
space=self.config,
storage=None,
metric=self.metric_name,
mode="max",
)
self._save(searcher)

searcher = OptunaSearch()
self._restore(searcher)

assert "not_completed" in searcher._ot_trials

def testOptunaWithStorage(self):
from optuna.storages import JournalStorage
from optuna.storages.journal import JournalFileBackend

from ray.tune.search.optuna import OptunaSearch

storage_file_path = "/tmp/my_test_study.log"
searcher = OptunaSearch(
space=self.config,
study_name="my_test_study",
storage=JournalStorage(JournalFileBackend(file_path=storage_file_path)),
metric=self.metric_name,
mode="max",
)
self._save(searcher)

searcher = OptunaSearch()
self._restore(searcher)

assert "not_completed" in searcher._ot_trials
self.assertTrue(os.path.exists(storage_file_path))

def testZOOpt(self):
from ray.tune.search.zoopt import ZOOptSearch
Expand Down Expand Up @@ -671,6 +748,38 @@ def testOptuna(self):
_multi_objective,
search_alg=OptunaSearch(
sampler=RandomSampler(seed=1234),
storage=None,
metric=["a", "b", "c"],
mode=["max", "min", "max"],
),
config=self.config,
num_samples=16,
reuse_actors=False,
)

best_trial_a = out.get_best_trial("a", "max")
self.assertGreaterEqual(best_trial_a.config["a"], 0.8)
best_trial_b = out.get_best_trial("b", "min")
self.assertGreaterEqual(best_trial_b.config["b"], 0.8)
best_trial_c = out.get_best_trial("c", "max")
self.assertGreaterEqual(best_trial_c.config["c"], 0.8)

def testOptunaWithStorage(self):
from optuna.samplers import RandomSampler
from optuna.storages import JournalStorage
from optuna.storages.journal import JournalFileBackend

from ray.tune.search.optuna import OptunaSearch

np.random.seed(1000)
storage_file_path = "/tmp/my_test_study.log"

out = tune.run(
_multi_objective,
search_alg=OptunaSearch(
sampler=RandomSampler(seed=1234),
study_name="my_test_study",
storage=JournalStorage(JournalFileBackend(file_path=storage_file_path)),
metric=["a", "b", "c"],
mode=["max", "min", "max"],
),
Expand All @@ -685,6 +794,7 @@ def testOptuna(self):
self.assertGreaterEqual(best_trial_b.config["b"], 0.8)
best_trial_c = out.get_best_trial("c", "max")
self.assertGreaterEqual(best_trial_c.config["c"], 0.8)
self.assertTrue(os.path.exists(storage_file_path))


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion python/requirements/ml/tune-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ hpbandster==0.7.4; python_version < "3.12"
hyperopt @ git+https://github.com/hyperopt/hyperopt.git@2504ee61419737e814e2dec2961b15d12775529c
future
nevergrad==0.4.3.post7
optuna==3.2.0
optuna==4.1.0
5 changes: 1 addition & 4 deletions python/requirements_compiled.txt
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ cloudpickle==2.2.0
# tensorflow-probability
cma==3.2.2
# via nevergrad
cmaes==0.10.0
# via optuna
cmdstanpy==1.2.0
# via prophet
colorama==0.4.6
Expand Down Expand Up @@ -1166,7 +1164,6 @@ numpy==1.26.4
# bayesian-optimization
# bokeh
# cma
# cmaes
# cmdstanpy
# configspace
# contourpy
Expand Down Expand Up @@ -1309,7 +1306,7 @@ opt-einsum==3.3.0
# via
# pyro-ppl
# tensorflow
optuna==3.2.0
optuna==4.1.0
# via -r /ray/ci/../python/requirements/ml/tune-requirements.txt
orjson==3.9.10
# via gradio
Expand Down
Loading