Documentation

Hesper.Training.AttentionBackward

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
    def Hesper.Training.AttentionBackward.executeSoftmaxBackward (device : WebGPU.Device) (attnBuf dAttnBuf dScoresBuf : WebGPU.Buffer) (numHeads cacheLen : Nat) :
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      Attention Score Backward (dQ from dScores) #

      def Hesper.Training.AttentionBackward.scoreBackwardQKernel (numHeads numKVHeads cacheLen headDim : Nat) (scale : Float) :

      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
        def Hesper.Training.AttentionBackward.executeScoreBackwardQ (device : WebGPU.Device) (dScoresBuf kCacheBuf dQBuf : WebGPU.Buffer) (numHeads numKVHeads cacheLen headDim : Nat) (scale : Float) :
        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
            def Hesper.Training.AttentionBackward.executeApplyBackward (device : WebGPU.Device) (dOutputBuf vCacheBuf dAttnBuf : WebGPU.Buffer) (numHeads numKVHeads cacheLen headDim : Nat) :
            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
                def Hesper.Training.AttentionBackward.executeRopeBackward (device : WebGPU.Device) (dOutBuf dInBuf : WebGPU.Buffer) (numHeads headDim : Nat) (ropeBase : Float) (pos : Nat) :
                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
                    def Hesper.Training.AttentionBackward.executeRmsNormBackward (device : WebGPU.Device) (xBuf gammaBuf dOutBuf dInBuf : WebGPU.Buffer) (dim : Nat) (eps : Float := 1e-6) :
                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For