Lean4 numerical gradcheck harness (no numpy) #
Shells out to iree-run-module to execute compiled @*_fwd/@*_back .vmfb,
then runs the adjoint / finite-difference dot-product test: for a forward
f with VJP J·ᵀ, the backward gives g_i = (Jᵀ dOut)_i, and for random
perturbation directions v_i,
Σ_i ⟨g_i, v_i⟩ = ⟨Jᵀ dOut, v⟩ = ⟨dOut, J v⟩ = (Φ(+ε) − Φ(−ε)) / 2ε,
where Φ(s) := ⟨f(inputs + s·v), dOut⟩. One backward run + two forward runs
validate ALL input gradients at once — catching transpose/axis bugs that
iree-compile (type-checking only) cannot.
Used by the ch10 ViT de-risk tests (TestSDPA/TestMHSA/TestViTBlock). All Lean4.
10^k as a Float (k may be negative).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- ViTGradcheck.digitsToNat cs = List.foldl (fun (acc : Nat) (c : Char) => acc * 10 + (c.toNat - '0'.toNat)) 0 cs
Instances For
Adjoint/finite-difference gradcheck of a compiled fwd/back pair.
inShapes/inLens describe the forward inputs (in arg order); the backward
is expected to return one gradient per input in the same order. outShape/
outLen describe the forward's single output (the dOut cotangent). Returns
true iff the relative error is below tol (default 1e-2, for f32).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Like adjointGradcheck but with fixed inputs (concrete (shape,values))
that are passed to BOTH fwd and back, never perturbed, and have no expected
gradient — e.g. a ViT input image (first layer ⇒ no image grad). The forward
arg order is fixed ++ params; the backward is fixed ++ params ++ dOut and
returns one grad per PARAM (in order).
Equations
- One or more equations did not get rendered due to their size.