Repository navigation
[WIP] Fix Softmax Collapse for nn.losses.cross_entropy - #4840
Draft
austin362667 wants to merge 5 commits into
Draft
austin362667 wants to merge 5 commits into
austin362667 wants to merge 5 commits into
Conversation
Signed-off-by: Austin Liu <austin362667@gmail.com>
Fix softmax collapse for targets_as_probs path Signed-off-by: Austin Liu <austin362667@gmail.com>
Signed-off-by: Austin Liu <austin362667@gmail.com>
…nd argument as 1 - sigmoid(a - b). Signed-off-by: Austin Liu <austin362667@gmail.com>
austin362667
force-pushed
the
austin/fix-softmax-collapse
branch
from
November 6, 2026 03:44
2e8c8a3 to
7d9c64c
Compare
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.
Why?
When the target logit dominates, true gradient rounds to zero and the sum of the gradients is not.
It happens in softmax, log-softmax, and cross-entropy.
This numerical bug is called Softmax Collapse and paper proposed a neat fix.
For cross-entrop loss L, avoid 1 + tiny fractions that rounds to zero by separating the 1 (1 = e^0 = shifted true label logits) from others during logsumexp.
What?
MLX non-fast mode has two paths for computing cross entropy loss.
Targets are probabilities
Apply log-softmax fix as introduced in the paper. Ref JAX code:
Targets are class indices
Apply hard-target cross-entropy fix as introduced in the paper. Ref JAX code:
Verification?
The public interface is unchanged. The loss and gradients are correct.
Todo?
We need numerically correct implementations in fast mode (i.e., all the related Metal and CUDA kernels)