Skip to content

Use Optuna v4 in Optuna Sweeper#3211

Open
himkt wants to merge 16 commits into
facebookresearch:mainfrom
himkt:optuna-v4
Open

Use Optuna v4 in Optuna Sweeper#3211
himkt wants to merge 16 commits into
facebookresearch:mainfrom
himkt:optuna-v4

Conversation

@himkt

@himkt himkt commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 13, 2026
@himkt

himkt commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

Successfully ran the example (https://hydra.cc/docs/plugins/optuna_sweeper/) on my local environment.

@himkt himkt marked this pull request as ready for review June 13, 2026 06:52
Comment on lines +33 to +35
"cmaes>=0.12.0", # Required for `optuna.samplers.CmaEsSampler`
"torch", # Required for `optuna.samplers.GPSampler`
"scipy", # Required for `optuna.samplers.GPSampler`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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?).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Give me more context here.
Is this optional for Optuna? which components wants those?
I strongly prefer not pull those in by default.

Comment thread pytest.ini
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+

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ok. can you check if some other ignores here can be removed?
The one with hydra changing working directory seems stale for sure.

@himkt himkt changed the title (WIP) Use Optuna v4 in Optuna Sweeper Use Optuna v4 in Optuna Sweeper Jun 13, 2026
@himkt

himkt commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

@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?

Comment on lines -46 to -53
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you explain the config changes?
those will change existing user configs so I want to make sure we are being deliberate.

Comment on lines -105 to +100
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

is this the same sampler just renamed, or are you changing the default sampler?

Comment on lines +6 to +8
from optuna.exceptions import ExperimentalWarning

warnings.filterwarnings("ignore", category=ExperimentalWarning)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment on lines +13 to +14
- Removed MOTPESampler support, as it has been removed in Optuna 4.x

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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".

Comment on lines +33 to +35
"cmaes>=0.12.0", # Required for `optuna.samplers.CmaEsSampler`
"torch", # Required for `optuna.samplers.GPSampler`
"scipy", # Required for `optuna.samplers.GPSampler`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Give me more context here.
Is this optional for Optuna? which components wants those?
I strongly prefer not pull those in by default.

Comment on lines +107 to +119
### 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)
```

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

Comment thread pytest.ini
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+

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ok. can you check if some other ignores here can be removed?
The one with hydra changing working directory seems stale for sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants