[Fix] Non-16-aligned D_qk scale + Autograd non-contiguous gradient (#1669) - #1670
Open
wch0810 wants to merge 1 commit into
Open
[Fix] Non-16-aligned D_qk scale + Autograd non-contiguous gradient (#1669)#1670wch0810 wants to merge 1 commit into
wch0810 wants to merge 1 commit into
Conversation
|
👋 Hi! Thank you for contributing to the TileLang project. Please remember to run We appreciate you taking this step! Our team will review your contribution, and we look forward to your awesome work! 🚀 |
…ile-ai#1669) Bug 1: run_bwd used padded dim_qk_padded for softmax scale instead of logical D_qk. When D_qk=129 (padded to 144), scale was 1/sqrt(144) instead of 1/sqrt(129), producing wrong gradients. Fix: run_bwd accepts explicit dim_qk param; phase2 uses logical dim_qk for sm_scale, phase1/3 use dim_qk_padded for buffer shapes. Bug 2: backward() did not materialize non-contiguous gradient from output.sum().backward() (zero-stride view), causing ValueError. Fix: if not do.is_contiguous(): do = do.contiguous() Added regression tests: D_qk=129 gradient comparison + Autograd output.sum().backward() compatibility. Based on tile-ai#1623 (Developer-mode 3-sub-kernel rewrite) + issue tile-ai#1669 fixes.
wch0810
force-pushed
the
fix/gqa_bwd_1669
branch
from
August 21, 2026 13:56
255e93e to
0919797
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.
Summary
Fixes #1669.
Bug 1: Non-16-aligned D_qk returns wrong gradients
run_bwdused paddeddim_qk_padded(e.g. 144) for softmax scale instead of logicalD_qk(e.g. 129), causingsm_scale = 1/sqrt(144)instead of1/sqrt(129).Fix:
run_bwdaccepts explicitdim_qkparameter. Phase 2 (softmax) uses logicaldim_qkforsm_scale; Phase 1/3 usedim_qk_paddedfor buffer shapes. The Autograd wrapper passesctx.D_qkthrough.Bug 2: PyTorch Autograd non-contiguous gradient
output.sum().backward()produces a zero-stride gradient view. The wrapper did not materialize it, causingValueError: Input tensor must be contiguous.Fix:
backward()entry addsif not do.is_contiguous(): do = do.contiguous().Regression Tests Added
attentionwrapper (padded to 144, scale uses 129)output.sum().backward(): Autograd compatibility with zero-stride gradientVerification
Based on #1623 (Developer-mode 3-sub-kernel rewrite).