Reshape instead of view in TiledFusedLogitsLoss - #8362
Open
alanhuangyoo wants to merge 1 commit into
Open
Conversation
deepspeedai#8348 fixed this in TiledMLP.backward: the flatten of batch and sequence into one axis needs a copy for a caller that hands in a non-contiguous activation, so it cannot be a view. TiledFusedLogitsLoss.forward does the same flatten, under the same comment, and is still on view: # flatten bs+seqlen to avoid having stride issues when narrowing into seqlen w/ bs>1 x = x.view(-1, *x.shape[2:]) y = y.view(-1, *y.shape[2:]) A transposed activation -- the layout deepspeedai#8348's test covers -- fails there: RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces) A channel slice happens to survive, because its row stride still admits the flatten, so only the transposed case is reachable today. y and mask go through the same flatten and are changed with it. The unflatten at the end stays a view: x_grad comes from zeros_like() of the already-flattened x, so it is contiguous by construction. Signed-off-by: alanhuangyoo <alanhuangyoo@gmail.com>
alanhuangyoo
requested review from
loadams,
tjruwase and
tohtana
as code owners
August 30, 2026 01:29
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.
Follow-up to #8348, in the same file.
That PR moved
TiledMLP.backwardoffviewbecause the flatten of batch and sequence into one axis needs a copy when a caller hands in a non-contiguous activation.TiledFusedLogitsLoss.forwarddoes the same flatten, under the same comment, and is still onview:A transposed activation — one of the two layouts #8348's test covers — fails there:
What is and is not reachable
I ran the four input shapes through
TiledFusedLogitsLoss.applyon master:xcontiguousxtransposedxchannel slicey/maskstridedOnly the transposed case is reachable today. A hidden-dimension slice keeps the wider row stride, and that still admits merging batch into sequence, so it survives — same for the strided
yandmaskI tried.yandmaskgo through the same flatten and are changed with it rather than left on a spelling that happens to hold.The unflatten at the end of
forwardstays aview:x_gradcomes fromzeros_like()of the already-flattenedx, so it is contiguous by construction. Same reasoning for thex_grad.view(x_shape_orig)that #8348 left alone inTiledMLP.backward.TiledLosshas no such flatten, so nothing to do there.Test
TestTiledFusedLogitsLossInputLayoutmirrorsTestTiledMLPInputLayoutand checks the loss against the same input made contiguous, so it pins the value and not just the absence of a throw.On master:
With this PR: