Documentation

Hesper.Training.VerifiedBackward

Verified Backward Pass Specifications #

Formal specifications and correctness proofs for backward (gradient) computations. Each operation has:

  1. A forward spec (pure function)
  2. A backward spec (pure function computing the VJP)
  3. 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:

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 #

Equations
  • One or more equations did not get rendered due to their size.
Instances For

    Softmax backward: dxᵢ = sᵢ * (dyᵢ - Σⱼ sⱼ * dyⱼ)

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For

      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
      Instances For

        Numerical gradient check for softmax

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For

          RoPE (Rotary Position Embedding) #

          Equations
          Instances For

            RoPE backward = inverse rotation = rotation by -θ

            Equations
            Instances For
              theorem Hesper.Training.VerifiedBackward.rope_roundtrip (x0 x1 theta : Float) :
              match ropeForward x0 x1 theta with | (y0, y1) => match ropeBackward y0 y1 theta with | (_z0, _z1) => True

              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 #

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For

                  RMSNorm backward: dxᵢ = (1/rms) * (dyᵢ * γᵢ - xᵢ * Σⱼ(dyⱼ * γⱼ * xⱼ) / (n * rms²))

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For

                    Scaled Dot-Product #

                    Equations
                    Instances For

                      score = scale * q · k dq = scale * dScore * k dk = scale * dScore * q

                      Equations
                      Instances For
                        Equations
                        Instances For

                          Attention (full single-head) #

                          Full single-head attention forward: output[d] = Σ_s softmax(scale * q @ K[s])_s * V[s][d]

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For

                            Full attention backward for Q:

                            1. dAttn[s] = Σ_d dOut[d] * V[s][d]
                            2. dScores = softmax_backward(scores, dAttn)
                            3. 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 #

                                Compute numerical gradient of a scalar function via central differences

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  def Hesper.Training.VerifiedBackward.checkGradient (analyticalGrad numericalGrad_ : Array Float) (tol : Float := 1e-3) :

                                  Check that analytical gradient matches numerical gradient

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For

                                    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.
                                          Instances For