Use Optuna v4 in Optuna Sweeper#3211
Conversation
|
Successfully ran the example (https://hydra.cc/docs/plugins/optuna_sweeper/) on my local environment. |
| "cmaes>=0.12.0", # Required for `optuna.samplers.CmaEsSampler` | ||
| "torch", # Required for `optuna.samplers.GPSampler` | ||
| "scipy", # Required for `optuna.samplers.GPSampler` |
There was a problem hiding this comment.
Not sure if it is acceptable to add them to requirements (it might be OK since it is a plugin, rather than hydra core itself?).
There was a problem hiding this comment.
Give me more context here.
Is this optional for Optuna? which components wants those?
I strongly prefer not pull those in by default.
| error | ||
| ; SQLAlchemy imported by old Optuna on Python 3.12+ | ||
| ignore:datetime.datetime.utcfromtimestamp\(\) is deprecated.*:DeprecationWarning:sqlalchemy.sql.sqltypes | ||
| ; Optuna's sqlite connections are finalized by the GC; ignore the ResourceWarning so it isn't escalated to an unraisable error on Python 3.12+ |
There was a problem hiding this comment.
Python 3.13+ started to warn when a connection to SQLite3 DB is not closed (🔗 lektor/lektor#1227, it is the issue of a third-party project though).
This potential problem already happens with old versions of Python and it might not be a critical immediately (https://docs.python.org/3.14/library/exceptions.html#ResourceWarning).
It is difficult to fix at this time, is it possible to add this warning filter for a workaround until a future upstream (Optuna-side) fix is introduced...?
https://github.com/facebookresearch/hydra/actions/runs/27461106520/job/81174946488
There was a problem hiding this comment.
ok. can you check if some other ignores here can be removed?
The one with hydra changing working directory seems stale for sure.
|
@omry I think tests that I should make happy are now working well (with one concern #3211 (comment)). Can I ask your review and opinion for the change? |
| consider_prior: bool = True | ||
| prior_weight: float = 1.0 | ||
| consider_magic_clip: bool = True | ||
| consider_endpoints: bool = False | ||
| n_startup_trials: int = 10 | ||
| n_ei_candidates: int = 24 | ||
| multivariate: bool = False | ||
| warn_independent_sampling: bool = True |
There was a problem hiding this comment.
Can you explain the config changes?
those will change existing user configs so I want to make sure we are being deliberate.
| https://optuna.readthedocs.io/en/stable/reference/generated/optuna.samplers.MOTPESampler.html | ||
| https://optuna.readthedocs.io/en/stable/reference/samplers/generated/optuna.samplers.GPSampler.html |
There was a problem hiding this comment.
is this the same sampler just renamed, or are you changing the default sampler?
| from optuna.exceptions import ExperimentalWarning | ||
|
|
||
| warnings.filterwarnings("ignore", category=ExperimentalWarning) |
There was a problem hiding this comment.
why is the default example using experimental features?
|
|
||
| ### API Change (Renames, deprecations and removals) | ||
|
|
||
| - Removed the `consider_prior`, `prior_weight`, `consider_magic_clip`, `consider_endpoints`, and `warn_independent_sampling` options from the TPE sampler config, as they are deprecated in Optuna 4.x |
There was a problem hiding this comment.
if they are just deprecated, we should not remove them but deprecate them.
What are users supposed to use instead?
This is might be an opportunity to try oc.deprecated. But I think its only a fit if we have another key to point people at.
| - Removed MOTPESampler support, as it has been removed in Optuna 4.x | ||
|
|
There was a problem hiding this comment.
Gotcha. this is a big change.
Maybe we should try to detect people trying to use the old one and give them a better error than "Not found".
| "cmaes>=0.12.0", # Required for `optuna.samplers.CmaEsSampler` | ||
| "torch", # Required for `optuna.samplers.GPSampler` | ||
| "scipy", # Required for `optuna.samplers.GPSampler` |
There was a problem hiding this comment.
Give me more context here.
Is this optional for Optuna? which components wants those?
I strongly prefer not pull those in by default.
| ### Experimental Samplers | ||
|
|
||
| GASampler and QMCSampler are experimental features in Optuna. | ||
| When you use these samplers, you might want to suppress the warnings from Optuna. | ||
| You can do this by adding the following code to your script: | ||
|
|
||
| ```python | ||
| import warnings | ||
| from optuna.exceptions import ExperimentalWarning | ||
|
|
||
| warnings.filterwarnings("ignore", category=ExperimentalWarning) | ||
| ``` | ||
|
|
There was a problem hiding this comment.
It would be better if we default to the non experimental sampler.
documented workaround for experimental is probably not the business of the plugin (unless to chooses to make experimental the default)
| error | ||
| ; SQLAlchemy imported by old Optuna on Python 3.12+ | ||
| ignore:datetime.datetime.utcfromtimestamp\(\) is deprecated.*:DeprecationWarning:sqlalchemy.sql.sqltypes | ||
| ; Optuna's sqlite connections are finalized by the GC; ignore the ResourceWarning so it isn't escalated to an unraisable error on Python 3.12+ |
There was a problem hiding this comment.
ok. can you check if some other ignores here can be removed?
The one with hydra changing working directory seems stale for sure.
Motivation
Use a newer versions of Optuna (v4) to use new sampling algorithms and more latest APIs.
Have you read the Contributing Guidelines on pull requests?
Yes
For non-trivial features, API changes, or behavior changes: is there an issue or design discussion where maintainers agreed on the direction?
Behavior is not change from users' perspective. But this PR will let users install additional dependencies (cmaes, torch, and scipy). Would it be better to make them as optional dependencies?
Test Plan
Just run on a local environment and check the result on CI.
Related Issues and PRs
I added my patch on #2995 by @michaelriedl to include his contributions on the update.