Skip to content

Avoid All-NaN RuntimeWarning in percentile pruner best-value lookup - #6873

Merged
y0z merged 3 commits into
optuna:masterfrom
825pranav:fix-medianpruner-all-nan-warning
Oct 13, 2026
Merged

y0z merged 3 commits into
optuna:masterfrom
825pranav:fix-medianpruner-all-nan-warning

Conversation

@825pranav

@825pranav 825pranav commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Fixes #6872.

When a trial reports only NaN intermediate values, PercentilePruner/MedianPruner correctly prunes the trial, but _get_best_intermediate_result_over_steps lets numpy's RuntimeWarning: All-NaN slice encountered (from np.nanmin/np.nanmax) escape to the user. The code path is working as intended, so the warning is pure noise — and it breaks users who run with warnings-as-errors. The test suite currently has to suppress exactly this warning (test_get_best_intermediate_result_over_steps wraps the all-NaN case in simplefilter("ignore", RuntimeWarning)), which shows the leak is known but unfixed at the source.

Description of the changes

  • optuna/pruners/_percentile.py: _get_best_intermediate_result_over_steps returns NaN directly when the values array is non-empty and all-NaN, instead of calling np.nanmin/np.nanmax on it. The returned value is identical to before (NaN), so pruning behavior is unchanged. The empty case is untouched and still raises ValueError (covered by the existing test).
  • tests/pruners_tests/test_percentile.py: the all-NaN block in test_get_best_intermediate_result_over_steps now uses simplefilter("error", RuntimeWarning) instead of "ignore", turning the old suppression into a regression guard. This test fails on current master with RuntimeWarning: All-NaN slice encountered and passes with this change.

Verified: tests/pruners_tests/test_percentile.py passes 11/11 under -W error::RuntimeWarning, and the reproduction script from #6872 runs warning-free with this branch while still pruning the NaN trial.

Checklist

  • I used an LLM while working on this PR.

- return NaN directly from _get_best_intermediate_result_over_steps when
  every intermediate value is NaN, instead of letting np.nanmin/np.nanmax
  emit "All-NaN slice encountered" (the empty case still raises ValueError)
- tighten the existing all-NaN test from ignoring RuntimeWarning to
  treating it as an error so the leak cannot come back

Fixes optuna#6872
Comment thread tests/pruners_tests/test_percentile.py Outdated
Comment on lines +155 to +156
# No "All-NaN slice encountered" RuntimeWarning must be leaked to the user.
warnings.simplefilter("error", category=RuntimeWarning)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for you PR. It's almost LGTM.

Testing warnings in unit tests is fragile, so we avoid doing this often in this project. Could you remove these lines?

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.

Removed in d149fbf, thanks for the review!

@not522 not522 assigned not522 and y0z Sep 30, 2026
@not522

not522 commented Sep 30, 2026

Copy link
Copy Markdown
Member

@y0z Could you review this PR?

@825pranav
825pranav force-pushed the fix-medianpruner-all-nan-warning branch from 2c3a110 to d149fbf Compare September 30, 2026 08:40
Comment thread tests/pruners_tests/test_percentile.py Outdated
@@ -152,7 +152,6 @@ def test_get_best_intermediate_result_over_steps(
trial_nan.report(float("nan"), step=0)
frozen_trial_nan = study._storage.get_trial(trial_nan._trial_id)
with warnings.catch_warnings():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since we're removing simplefilter, catch_warnings is no longer needed. Please remove it.

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.

Removed in 83a36de, thanks!

@not522 not522 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@not522 not522 removed their assignment Oct 6, 2026
@825pranav

Copy link
Copy Markdown
Contributor Author

@y0z gentle ping — not522 approved this on the 6th and CI is green. Would you have a moment to take a look? Thanks!

@y0z y0z closed this Oct 13, 2026
@y0z y0z reopened this Oct 13, 2026
@y0z
y0z merged commit 988b115 into optuna:master Oct 13, 2026
25 checks passed
@not522 not522 added this to the v5.1.0 milestone Oct 21, 2026
@not522 not522 added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MedianPruner leaks numpy's "All-NaN slice encountered" RuntimeWarning when a trial reports only NaN intermediate values

3 participants