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

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 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
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
28 changes: 23 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,15 @@ 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`."
)
self._storage = storage
else:
self._storage = ot.storages.InMemoryStorage()

self._completed_trials = set()

self._ot_trials = {}
Expand All @@ -380,7 +399,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 +420,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
37 changes: 35 additions & 2 deletions python/ray/tune/tests/test_searchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,32 @@ def testNevergradWithRequiredOptimizerKwargs(self):

def testOptuna(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)),
search_alg=OptunaSearch(
sampler=RandomSampler(seed=1234),
study_name="my_test_study",
storage=JournalStorage(
JournalFileBackend(file_path=storage_file_path)
),
),
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 +368,18 @@ 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

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}],
Expand All @@ -373,6 +389,7 @@ def testOptuna(self):
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,
Expand Down Expand Up @@ -608,9 +625,19 @@ def testNevergrad(self):
self._restore(searcher)

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

from ray.tune.search.optuna import OptunaSearch

searcher = OptunaSearch(space=self.config, metric=self.metric_name, mode="max")
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()
Expand Down Expand Up @@ -662,15 +689,20 @@ def tearDownClass(cls):

def testOptuna(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 +717,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