You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Lane 1 · numerics] SDPA backward — derivation note for the attention adjoint #1385
Sub-issue of #1384 ([Feature]: scaledDotProductAttention backward — gradients currently stop at attention).
Lane: 1 · Numerics/Research Skill needed: Linear algebra / autodiff on paper, PyTorch to check. No Kotlin needed. Size: s (a few hours) Blocked by: nothing
What to do
Read the forward in DefaultCpuOps.scaledDotProductAttention (only to list its options: scale, causal, mask, key-padding lengths).
Write the backward: with S = scale·QKᵀ (+mask), P = softmax(S), O = PV: dV = Pᵀ dO, dP = dO Vᵀ, dS = P ⊙ (dP − rowsum(dP ⊙ P)), dQ = scale·dS K, dK = scale·dSᵀ Q. Explain how masks and causal masking affect it.
Verify numerically with PyTorch autograd on a tiny case and post the note (Markdown is fine) here.
Acceptance
Note covers every forward option
Numerical check against autograd included
Result reported back on the parent issue
Notes
FlashAttention-2 (Dao, 2023) Algorithm 2 is a good reference for the recomputation trick.
Activity