Skip to content

Warn when GPSampler falls back to RandomSampler#6179

Merged
y0z merged 1 commit intooptuna:masterfrom
sisird864:warn-gp-sampler-on-fallback
Jul 7, 2025
Merged

Warn when GPSampler falls back to RandomSampler#6179
y0z merged 1 commit intooptuna:masterfrom
sisird864:warn-gp-sampler-on-fallback

Conversation

@sisird864
Copy link
Copy Markdown
Contributor

Motivation

Add a runtime warning to GPSampler when it falls back to RandomSampler, per the community Q&A suggestion. This informs users whenever the Gaussian-process sampler degrades to independent sampling (e.g. due to dynamic search-space changes or GP fitting errors) and keeps its behavior consistent with CmaEsSampler.

Description of the changes

Imports & logger

  • In optuna/samplers/_gp/sampler.py, imported the standard warnings module and initialized a module-level logger via get_logger.

Warning helper

  • Added a private method _log_independent_sampling(trial, param_name) that emits a warning describing the fallback, mirroring CmaEsSampler.

Guarded fallback
Modified the except Exception block in sample_independent to:

  • Check self._warn_independent_sampling
  • Call _log_independent_sampling(...) once
  • Disable further warnings
  • Delegate to the independent sampler

Testing
No new unit tests were added for the log message itself (logging was manually smoke-tested per maintainer guidance).

@nabenabe0928 nabenabe0928 added the enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. label Jun 26, 2025
@nabenabe0928
Copy link
Copy Markdown
Contributor

@y0z Could you review this PR?

@nabenabe0928
Copy link
Copy Markdown
Contributor

@fusawa-yugo
Could you also review this PR?

@nabenabe0928 nabenabe0928 requested a review from Copilot June 26, 2025 04:54
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a runtime warning to inform users when GPSampler falls back to RandomSampler for independent sampling. Key changes include importing and initializing a module-level logger, introducing the warn_independent_sampling parameter with corresponding logging helper, and integrating the warning log in sample_relative and sample_independent methods.

@sisird864
Copy link
Copy Markdown
Contributor Author

sisird864 commented Jun 26, 2025

Thanks for the feedback. I added these changes:

  • Emit only one warning per trial in sample_relative
  • Disable further warnings after the first occurrence

Pushed the update. Please have another look.

@nabenabe0928
Copy link
Copy Markdown
Contributor

@fusawa-yugo

Could you please review this PR?

Copy link
Copy Markdown
Contributor

@fusawa-yugo fusawa-yugo left a comment

Choose a reason for hiding this comment

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

Thank you for your PR!!
I left some comments. Could you check them?

Comment on lines +420 to +418
if self._warn_independent_sampling:
self._log_independent_sampling(trial, param_name)
Copy link
Copy Markdown
Contributor

@fusawa-yugo fusawa-yugo Jun 27, 2025

Choose a reason for hiding this comment

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

A warning should be displayed when sampling independently and the number of trials are sufficient for the same reason as previous comment

Suggested change
if self._warn_independent_sampling:
self._log_independent_sampling(trial, param_name)
if self._warn_independent_sampling:
states = (TrialState.COMPLETE,)
complete_trials = study._get_trials(deepcopy=False, states=states, use_cache=True)
if len(complete_trials) >= self._n_startup_trials:
self._log_independent_sampling(trial, param_name)

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 Jul 2, 2025

Choose a reason for hiding this comment

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

@sisird864
Could you please apply the suggestion?
Once the change is applied, this PR looks good to me:)

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.

Hello, I’ve added the startup-threshold guard to sample_independent as requested pushed the update. CI is green now. Please take another look. Thanks.

Copy link
Copy Markdown
Contributor

@fusawa-yugo fusawa-yugo left a comment

Choose a reason for hiding this comment

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

Sorry, please ignore this comment.

@sisird864
Copy link
Copy Markdown
Contributor Author

sisird864 commented Jun 27, 2025

Thanks for the review, I’ve updated the PR to:

  1. Revert the warning in sample_relative.
  2. Add a startup‐threshold guard in sample_independent so that we only log the fallback warning once we’ve passed n_startup_trials.

Please have another look. Thank you.

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Hey thank you for the PR, I left some comments:)

@1kastner
Copy link
Copy Markdown
Contributor

@nabenabe0928 I agree that repeated logging does not harm. Especially when the code regarding the search space gets more complex, debugging it becomes much more challenging and some support to the users could be great. I wouldn't mind the extra message in my log files, either.

@sisird864
Copy link
Copy Markdown
Contributor Author

sisird864 commented Jun 30, 2025

Thanks for the discussion. @1kastner makes a great point. Repeated warnings can help users debug unexpected independent sampling. I’ll remove the one-time disable in sample_independent so that every fallback logs a warning. Pushing that now.

@nabenabe0928
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 changed the title Warn when GPSampler falls back to RandomSampler Warn when GPSampler falls back to RandomSampler Jul 2, 2025
@nabenabe0928 nabenabe0928 added this to the v4.5.0 milestone Jul 2, 2025
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Thank you for the changes, LGTM")

@nabenabe0928 nabenabe0928 removed their assignment Jul 3, 2025
@fusawa-yugo
Copy link
Copy Markdown
Contributor

LGTM!!
Thank you for your changes.

@y0z y0z assigned fusawa-yugo and unassigned fusawa-yugo Jul 3, 2025
Copy link
Copy Markdown
Member

@y0z y0z left a comment

Choose a reason for hiding this comment

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

LGTM

@y0z y0z removed their assignment Jul 7, 2025
@y0z y0z merged commit 7871574 into optuna:master Jul 7, 2025
14 checks passed
@sisird864 sisird864 deleted the warn-gp-sampler-on-fallback branch July 9, 2025 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Change that does not break compatibility and not affect public interfaces, but improves performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants