Verified Backward Pass Specifications #
Formal specifications and correctness proofs for backward (gradient) computations. Each operation has:
- A forward spec (pure function)
- A backward spec (pure function computing the VJP)
- A numerical gradient test to verify correctness
The GPU kernels must match these specs.
Verification Strategy #
Since full symbolic differentiation proofs require Mathlib's calculus, we use a pragmatic two-tier approach:
Tier 1 (Algebraic): Prove algebraic identities that must hold:
- RoPE: backward ∘ forward = identity (orthogonal rotation)
- Softmax: Σᵢ dxᵢ = 0 (gradient sums to zero)
- Linear: backward is self-consistent with transpose
Tier 2 (Numerical): Verify via finite differences: f'(x) ≈ (f(x+ε) - f(x-ε)) / (2ε)
GPU kernels are tested against the CPU spec at runtime.
Softmax #
Property: softmax backward gradient sums to zero. This must hold because softmax outputs sum to 1 (constant), so ∂(Σᵢ sᵢ)/∂xⱼ = 0 for all j.
Equations
- Hesper.Training.VerifiedBackward.softmaxBackwardSumsToZero x dy = Array.foldl (fun (x1 x2 : Float) => x1 + x2) 0.0 (Hesper.Training.VerifiedBackward.softmaxBackward x dy)
Instances For
RoPE (Rotary Position Embedding) #
Algebraic proof: RoPE backward ∘ forward = identity. R(-θ) @ R(θ) @ x = x for any x.
Verify RoPE roundtrip numerically
Equations
- One or more equations did not get rendered due to their size.
Instances For
RMSNorm #
Scaled Dot-Product #
Equations
- Hesper.Training.VerifiedBackward.dotProduct a b = Array.foldl (fun (x1 x2 : Float) => x1 + x2) 0.0 (Array.zipWith (fun (x1 x2 : Float) => x1 * x2) a b)
Instances For
Equations
Instances For
score = scale * q · k dq = scale * dScore * k dk = scale * dScore * q
Equations
Instances For
Attention (full single-head) #
Full attention backward for Q:
- dAttn[s] = Σ_d dOut[d] * V[s][d]
- dScores = softmax_backward(scores, dAttn)
- dQ[d] = scale * Σ_s dScores[s] * K[s][d]
Equations
- One or more equations did not get rendered due to their size.
Instances For
Attention backward for V cache at position s: dV[s][d] = attn[s] * dOut[d]
Equations
- One or more equations did not get rendered due to their size.
Instances For
Numerical Gradient Verification #
Verify softmax backward via numerical gradient
Equations
- One or more equations did not get rendered due to their size.
Instances For
Verify RoPE backward via numerical gradient
Equations
- One or more equations did not get rendered due to their size.
Instances For
Verify RMSNorm backward via numerical gradient
Equations
- One or more equations did not get rendered due to their size.
Instances For
Run all verification checks
Equations
- One or more equations did not get rendered due to their size.