-
Notifications
You must be signed in to change notification settings - Fork 5k
Reshape instead of view in TiledFusedLogitsLoss #8362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stas00
merged 2 commits into
deepspeedai:master
from
alanhuangyoo:fix/tiled-logits-loss-reshape
Sep 6, 2026
+53
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new test compares the two losses but never the gradient, and the flatten you changed feeds the gradient path as well:
x_gradis azeros_likeof the flattenedx, it is scattered per shard throughx_grad.narrow(0, shard_offset, shard_step).view_as(x_shard)at ulysses_sp.py:1190, and unflattened at :1208.TestTiledMLPInputLayout, which your docstring names as the same caller contract, does assertx_tiled.gradagainst the reference.The test also never calls
backward(), soTiledFusedLogitsLoss.backwardand that savedx_gradare not exercised at all.Adding both passes at 307ff15, in a clean
python:3.12-slimcontainer with torch 2.14.0+cpu:Concretely that is
loss.backward()inside the loop, then comparingstrided.gradwithcontiguous.gradafter it. The parameter gradients match too, if you want the third assertion the MLP test makes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both added in ac714ff —
loss.backward()in the loop,strided.gradagainstcontiguous.grad, and the parameter gradients as the third comparison, matchingTestTiledMLPInputLayout.Same numbers you measured, on torch 2.13.0:
One thing I checked while doing this, since it changes what the assertion is worth: reverting the
reshapeand running the strengthened test fails at the forward flatten (ulysses_sp.py:1150,view size is not compatible ...) beforebackwardis ever reached. So the gradient assertion is not what catches this particular bug — the forward already did.What it does cover is the part your comment named that the forward does not.
reshapeon a non-contiguous input returns a copy, sox_gradis azeros_likeof that copy rather than of the tensor the caller handed in, and the shard scatter at :1190 and the unflatten at :1208 have to put those values back on the original layout. Nothing was checking that they did.max|dx| = 0.000e+00against the contiguous reference is what says the copy does not scramble it, andgrad.abs().sum() > 0says it is not passing by both sides being empty.Thanks — the test was asserting the shallower half of its own docstring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch on the revert check, and you are right that it changes what the assertion is worth. The forward flatten fails first, so the gradient comparison is not the guard against this particular regression and I should not have implied it was.
What it does pin is the part nothing was covering: the scatter at
:1190and the unflatten at:1208putting values back on the caller's layout afterreshapehands back a copy.grad.abs().sum() > 0alongside it is the right guard against both sides being empty.