Repository navigation
Avoid All-NaN RuntimeWarning in percentile pruner best-value lookup - #6873
Merged
Merged
Conversation
- 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
not522
reviewed
Sep 30, 2026
Comment on lines
+155
to
+156
| # No "All-NaN slice encountered" RuntimeWarning must be leaked to the user. | ||
| warnings.simplefilter("error", category=RuntimeWarning) |
Member
There was a problem hiding this comment.
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?
Contributor
Author
There was a problem hiding this comment.
Removed in d149fbf, thanks for the review!
Member
|
@y0z Could you review this PR? |
825pranav
force-pushed
the
fix-medianpruner-all-nan-warning
branch
from
September 30, 2026 08:40
2c3a110 to
d149fbf
Compare
not522
reviewed
Oct 2, 2026
| @@ -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(): | |||
Member
There was a problem hiding this comment.
Since we're removing simplefilter, catch_warnings is no longer needed. Please remove it.
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
approved these changes
Oct 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Fixes #6872.
When a trial reports only NaN intermediate values,
PercentilePruner/MedianPrunercorrectly prunes the trial, but_get_best_intermediate_result_over_stepslets numpy'sRuntimeWarning: All-NaN slice encountered(fromnp.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_stepswraps the all-NaN case insimplefilter("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_stepsreturns NaN directly when the values array is non-empty and all-NaN, instead of callingnp.nanmin/np.nanmaxon it. The returned value is identical to before (NaN), so pruning behavior is unchanged. The empty case is untouched and still raisesValueError(covered by the existing test).tests/pruners_tests/test_percentile.py: the all-NaN block intest_get_best_intermediate_result_over_stepsnow usessimplefilter("error", RuntimeWarning)instead of"ignore", turning the old suppression into a regression guard. This test fails on currentmasterwithRuntimeWarning: All-NaN slice encounteredand passes with this change.Verified:
tests/pruners_tests/test_percentile.pypasses 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