fix(losses): avoid NaN gradient in weighted_logsoftmax at -inf logits - #1762
Open
jaideeppyne wants to merge 1 commit into
Open
fix(losses): avoid NaN gradient in weighted_logsoftmax at -inf logits#1762jaideeppyne wants to merge 1 commit into
jaideeppyne wants to merge 1 commit into
Conversation
The custom JVP of `weighted_logsoftmax` multiplied `weights_dot` by `log_softmax(x)`, which is `-inf` at masked classes (`x_i = -inf`). This yielded `nan` output tangents even for a zero tangent direction (e.g. `jax.jvp(..., (0, 0))` or `jax.jacfwd`), contradicting the documented `0 log 0 = 0` convention that keeps the primal finite there. Substitute a finite value for the `-inf` entries of `log_softmax(x)` in the `weights_dot` term. This leaves the gradient unchanged wherever `log_softmax` is finite and removes the spurious NaN otherwise. Adds a forward-mode regression test over inputs with `-inf` logits.
jaideeppyne
force-pushed
the
fix/weighted-logsoftmax-jvp-nan
branch
from
August 31, 2026 12:46
c64cadf to
52b6742
Compare
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.
What
The custom JVP of
weighted_logsoftmax(used bysafe_softmax_cross_entropy) multipliesweights_dotbylog_softmax(x). At a masked class wherex_i = -inf,log_softmax(x)_i = -inf, so this term becomesweights_dot * -inf, producingNaNtangents — even for a zero tangent direction.Why it's a bug
The primal already implements the
0 log 0 = 0convention so masked-infentries stay finite (per its docstring). The gradient rule did not, sojax.jvp(weighted_logsoftmax, primals, (0, 0))andjax.jacfwd(safe_softmax_cross_entropy)returnNaNon inputs with-inflogits.Fix
Substitute a finite value for
-infentries oflog_softmax(x)in theweights_dotterm. This leaves the gradient unchanged whereverlog_softmaxis finite and removes the spuriousNaNotherwise.Tests
Adds a forward-mode regression test over inputs with
-inflogits (fails before, passes after). All existing_classificationtests pass;ruffclean.