Skip to content

[Lane 1 · numerics] SDPA backward — derivation note for the attention adjoint #1385

Open

Description

@michalharakal

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

  1. Read the forward in DefaultCpuOps.scaledDotProductAttention (only to list its options: scale, causal, mask, key-padding lengths).
  2. 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.
  3. 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.

Coverage matrix: docs/modules/ROOT/pages/reference/ops-status-matrix.adoc · How lanes work: Issue taxonomy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomers; no prior SKaiNET codebase knowledge assumedresearchResearch and evaluation tasks (DARC: R)size:sA few hoursskill:numericsPyTorch/NumPy/math background, no Kotlin requiredsub-issueSub-issue of a tracking issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions