Repository navigation
feat(compile-dag): scaledDotProductAttention backward (#1384) - #1489
Merged
Merged
Conversation
Replaces the stub that returned null gradients with the attention adjoint: dV = Pᵀ·dO dP = dO·Vᵀ dS = P ⊙ (dP − rowsum(dP ⊙ P)) dQ = s·dS·K dK = s·dSᵀ·Q dM = dS summed to the mask's shape P is recomputed, not stored. The rule runs in a rank-5 layout [batch, kvHeads, nRep, seqQ, …], so grouped-query K/V broadcast against the query heads of their group in matmul and their gradients reduce with one sum. The structural rules (causal, window, rightContext, keyLengths) are rebuilt from AttentionMask.additive, the definition the forward kernel reads, so a masked score has P = 0 and exactly zero gradient; the dense mask is added trailing-aligned as in the forward. keyLengths is integer and gets no gradient. SdpaBackwardTest: central finite differences for plain, causal, grouped query, key padding (padded keys get exactly 0), window with right context, dense mask (gradient on the mask, also per kv head), a decode step with one query, and a custom scale; PyTorch 2.5.1 float64 parity within 2e-4 for plain, causal and grouped+scaled cases. stubbedBackwardOpNames is empty; the ops matrix Backward column reads 59/59. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
📖 Documentation Preview The documentation has been built successfully for this PR. Generated Files:
Artifacts:
This comment will be updated automatically when the PR is updated. |
aharakal
approved these changes
Nov 10, 2026
Contributor
Author
|
Added |
This was referenced Nov 10, 2026
Merged
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.
Closes #1386, closes #1387. Part of #1384.
What
scaledDotProductAttentionBackwardinDefaultExecutionTapereturnednullfor every input, so training stopped at attention. This PR replaces the stub with the attention adjoint and removes the last entry fromstubbedBackwardOpNames.How
With
S = s·Q·Kᵀ + masks,P = softmax(S),O = P·Vand upstreamdO:Pis recomputed in the backward, not stored, as the issue asked.[batch, kvHeads, nRep, seqQ, …]. Grouped-query K/V broadcast against the query heads of their group insidematmul, anddK/dVreduce overnRepwith onesum. Gemma's layout works unchanged.window,rightContextandkeyLengthsare rebuilt fromAttentionMask.additive, the same definition the forward kernel reads, so a masked score hasP = 0and exactly zero gradient. The dense mask operand is added trailing-aligned exactly as in the forward and receives a gradient.keyLengthsis integer and gets none.Tests
SdpaBackwardTest(12 cases,skainet-compile-dagcommonTest):0fin dK and dV), window 2 with right context 1, dense mask[2,1,3,3], per-kv-head mask[1,2,1,3], a one-query decode step over 5 cached keys, and scale 0.5.torch.nn.functional.scaled_dot_product_attentionfor plain, causal and grouped+scaled cases. Fixtures were generated in float64 with seed 1384 and are embedded in the test.Full
:skainet-compile:skainet-compile-dag:jvmTest: 129/129.apiCheckclean (private and internal changes only).Acceptance criteria of #1384
scaledDotProductAttentionremoved fromstubbedBackwardOpNames; ops matrix Backward column reads ✅ (59 / 59)Notes for review
ops-status-matrix.adocis hand-edited to whatBackwardCoverageDumpTestemits for the now-empty stub set, since regenerating needs the full docs pipeline.🤖 Generated with Claude Code