Skip to content

Honor mask in nnx.metrics.Welford - #5613

Open
vineethsaivs wants to merge 1 commit into
google:mainfrom
vineethsaivs:fix/nnx-welford-mask
Open

vineethsaivs wants to merge 1 commit into
google:mainfrom
vineethsaivs:fix/nnx-welford-mask

Conversation

@vineethsaivs

Copy link
Copy Markdown

What does this PR do?

nnx.metrics.Welford ignores mask. Updating MultiMetric(loss=Average(), stats=Welford()) with values [1, 2, 3, 100] and mask [1, 1, 1, 0] gives loss 2.0, but Welford reports mean 26.5 and std 42.44, not 2.0 and 0.8165.

Welford.update takes mask through **kwargs and never reads it, while MultiMetric passes an array mask to every metric.

The fix merges only the kept entries (masked count, mean and M2), makes a fully masked batch a no-op, and raises for a scalar with a mask, like Average. Unmasked results are bit-identical. Behavior change: a global mask that doesn't broadcast to a Welford's values now raises instead of being ignored; use a dict mask there.

The new test fails on main (mean 51.0, expected 2.0) and passes with the fix.

Checklist

  • This PR fixes a minor issue (e.g.: typo or small bug) or improves the docs (you can dismiss the other checks if that's the case).

Welford.update took mask through **kwargs and dropped it, so a masked
MultiMetric put padding into the Welford mean, std and SEM. Merge only
the kept entries (masked count, mean and M2), guard the division so a
fully masked batch is a no-op, and raise for a scalar with a mask, as
Average does.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant