Attention Backward GPU Kernels #
GPU kernels implementing the backward pass through attention for LoRA training.
Each kernel corresponds to a verified CPU spec in VerifiedBackward.lean.
Gradient Flow (reverse order of forward) #
dOutput [dim]
↓ O projection backward (BitLinear transpose)
dAttnOut [dim]
↓ RMSNorm backward (sub-norm)
dAttnWeighted [numHeads * headDim]
↓ Attention apply backward
dAttn [numHeads * cacheLen] + dV [kvDim] (not needed for LoRA Q)
↓ Softmax backward
dScores [numHeads * cacheLen]
↓ Score backward (Q @ K^T)
dQ [numHeads * headDim]
↓ RoPE backward (inverse rotation)
dQpre [numHeads * headDim] ← This is ∂L/∂(BitLinear_Q output) = LoRA Q gradient signal
Softmax Backward #
Softmax backward kernel: dScores[h, s] = attn[h, s] * (dAttn[h, s] - Σ_s' attn[h, s'] * dAttn[h, s'])
One thread per (head, seq_pos) pair. Uses shared memory for the dot product reduction per head.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Attention Score Backward (dQ from dScores) #
Score backward kernel for Q: dQ[h, d] = scale * Σ_s dScores[h, s] * K_cache[kvHead(h), s, d]
One thread per (head, dim) pair. GQA: multiple heads map to the same KV head.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Attention Apply Backward (dAttn from dOutput @ V^T) #
Attention apply backward kernel: dAttn[h, s] = Σ_d dOutput[h, d] * V_cache[kvHead(h), s, d]
One thread per (head, seq_pos) pair.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
RoPE Backward (inverse rotation) #
RoPE backward kernel: apply inverse rotation R(-θ) to gradient. For NeoX split-half layout: dx[h, d] = dy[h, d] * cos(θ) + dy[h, d+half] * sin(θ) dx[h, d+half] = -dy[h, d] * sin(θ) + dy[h, d+half] * cos(θ)
where θ = pos * base^(-2d/headDim), same as forward.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
RMSNorm Backward #
RMSNorm backward kernel (single workgroup, shared memory reduction): dx[i] = (1/rms) * (dy[i]γ[i] - x[i] * dot(dyγ, x) / (n * rms²))
Uses the same workgroup reduction pattern as forward RMSNorm.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.