Repository navigation
Keep masked nan and inf out of nnx.metrics.Average - #5619
Open
vineethsaivs wants to merge 1 commit into
Open
vineethsaivs wants to merge 1 commit into
vineethsaivs wants to merge 1 commit into
Conversation
Average.update zeroed masked entries with values * mask, but 0 * nan and 0 * inf are nan, so one non-finite value in a masked position made the average nan. Drop masked entries with jnp.where instead.
This branch has not been deployed
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.
nnx.metrics.Average.updatedrops masked entries withvalues * mask. A masked slot that holds nan or inf still poisons the total, since0 * nanand0 * infare nan, so one padded position makes the average nan. That is common with per-token losses, where padding can sit over-inflogits.AccuracyandMultiMetricgo through the same update.The fix uses
jnp.where(mask, values * mask, 0), so masked entries never reach the total. The count is unchanged.Test:
test_average_mask_skips_nonfiniteputs nan and inf under a zero mask. On main it fails (total nan); with this change it passes, andtests/nnx/metrics_test.pypasses 20/20 withJAX_NUMPY_RANK_PROMOTION=raise. ruff 0.1.3 and pyupgrade are clean.This is separate from #5588 (count for broadcast masks). The two touch adjacent lines, so whichever lands second needs a small rebase.