Shift by the max in cross_entropy - #4188
Merged
Merged
Conversation
zcbenz
approved these changes
Aug 12, 2026
1 task
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.
Proposed changes
cross_entropyonly depends on the gaps between logits, but a large shared offset changes the answer and eventually zeroes it:Equal logits are the clearest case, since the answer has to be
log(2)at any magnitude:pytorch returns
0.6931472for all four. A loss of0.0is the bad one: it reports a perfect prediction and hands back a zero gradient.The cause is the composition, not any single op:
mx.logsumexpis right.logsumexp([1e8, 1e8])is1e8 + log(2), and the ulp of1e8in float32 is 8, so1e8is the correctly rounded answer. The problem is that the gap between the logits gets added to a large magnitude and rounds away before it is subtracted back off.Shifting the logits by their max first keeps the gap at a magnitude where it survives. The loss is invariant to that shift, so nothing else has to change, and
stop_gradientkeeps the graph the same function it was:The shift is applied before
scoreis taken so every path benefits, including label smoothing and probability targets, which were also returning0.0at1e8. It cancels for label smoothing too, since the smoothing term subtracts the mean logit and the shift moves the mean by the same amount.Checked against pytorch after the change with zero mismatches: every magnitude from
1e0to1e36, the offset cases above, random inputs for all three reductions,label_smoothingof 0, 0.1 and 0.5, probability targets, and gradients (max difference9e-10).Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesCPU only build (
MLX_BUILD_METAL=OFF) at 596dc79.test_losses.py,test_nn.pyandtest_optimizers.pypass, and the new assertions fail on main from1e4up. The existing-inflogits case is unaffected, since the row max is already0there. Python only, no rebuild involved.freshman contributor, i use Claude Code while digging. found this by feeding a large constant offset through the losses and watching which ones stopped being shift invariant. the thing that took me longest was convincing myself
logsumexpwas not at fault: at1e8the ulp is 8, so it is returning the best float32 value there is and the loss has to be assembled differently instead.