dO → gating backward → dO_cmp, dO_slc, dO_swa, dg_* (#1254)
↓ ↓ ↓
compressed attn bwd selection attn bwd sliding window bwd
→ dQ_cmp, dK_cmp, → dQ_slc, dK_slc, → dQ_swa, dK_swa,
dV_cmp (#1253) dV_slc (#1252) dV_swa (FA bwd)
↓
mean-pool bwd
→ dK_pool, dV_pool
Aggregate: dQ = dQ_cmp + dQ_slc + dQ_swa
dK = dK_pool + dK_slc + dK_swa
dV = dV_pool + dV_slc + dV_swa
dg_cmp, dg_slc, dg_swa (from gating bwd)
Parent
Part of #1243 — DeepSeek NSA kernels for MI350
Description
Wire up all NSA forward and backward kernels into a PyTorch-compatible autograd Function that enables end-to-end training.
Deliverables
NSAFunction(torch.autograd.Function)with:forward(): runs the full NSA inference pipeline ([wave] NSA: end-to-end inference pipeline #1251), saves tensors for backwardbackward(): orchestrates all backward kernels in correct orderBackward orchestration order:
Memory optimization:
Convenience wrapper:
nsa_attention(q, k, v, g_cmp, g_slc, g_swa, config) -> othat calls NSAFunction.applyTesting
torch.autograd.gradcheckwith FP64 referenceDepends on