Fix #3328: MinMaxMetric min/max values are silently polluted by Lig...#3354
Merged
bhimrazy merged 4 commits intoLightning-AI:masterfrom Apr 9, 2026
Conversation
Register min_val and max_val via add_state() so Metric.reset() properly resets them to inf/-inf, preventing sanity-check pollution (issue Lightning-AI#3328).
- Fix tensor equality assertions to use `.item()` for proper scalar comparison - Add explicit type annotations to suppress Pylance false positive on `.compute()` - Add `test_reset_no_pollution_across_epochs` to directly reproduce the Lightning sanity-check pollution scenario from issue Lightning-AI#3328
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3354 +/- ##
=======================================
- Coverage 37% 34% -3%
=======================================
Files 364 349 -15
Lines 20096 19901 -195
=======================================
- Hits 7458 6696 -762
- Misses 12638 13205 +567 🚀 New features to boost your workflow:
|
bhimrazy
approved these changes
Apr 9, 2026
justusschock
approved these changes
Apr 9, 2026
Borda
approved these changes
Apr 9, 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.
Closes #3328
What does this PR do?
Fixes #3328
Registers
min_valandmax_valinMinMaxMetricviaadd_state()instead of assigning them as plain attributes. This ensuresreset()properly restores both to their initialization bounds (infand-infrespectively), fixing silent pollution from Lightning's sanity validation check.Root cause: In
src/torchmetrics/wrappers/minmax.py,min_valandmax_valwere set as plaintorch.tensorattributes in__init__. SinceMetric.reset()only resets states registered viaadd_state(), these values were never cleared onreset()— despite the docstring claiming otherwise. This meant values computed during Lightning's 2-batch sanity check persisted across the entire training run.Fix: Replace the two plain attribute assignments (lines 79–80) with
add_state()calls usingdist_reduce_fx="min"anddist_reduce_fx="max"respectively. This also makes distributed reduction semantically correct for these values.A new test
test_reset_clears_min_maxintests/unittests/wrappers/test_minmax.pydirectly verifies that after callingreset(),min_valequalsinfandmax_valequals-inf, regardless of what was computed beforehand.Before submitting
PR review
Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.
Did you have fun?
Make sure you had fun coding 🙃
This PR was created with AI assistance (Claude). The changes were reviewed by quality gates and a critic model before submission.
📚 Documentation preview 📚: https://torchmetrics--3354.org.readthedocs.build/en/3354/