TorchLean API

NN.GraphSpec.Models.MlpSpecEquivalence

MLP Spec Equivalence #

This is the first model-level alignment theorem for GraphSpec.

We prove that interpreting the GraphSpec MLP architecture (NN.GraphSpec.Models.mlp) via the GraphSpec Spec-interpreter (NN.GraphSpec.Interp.spec) agrees with the existing hand-written Spec reference implementation (NN.Spec.Models.Mlp.Examples.mlp_forward), after packaging the typed parameter list into two LinearSpecs in the obvious way.

Why this matters:

Related context (informal pointers):

@[reducible, inline]
abbrev NN.GraphSpec.Models.MLPParams (inDim hidDim outDim : ) :

Parameter ABI for the 2-layer MLP inDim → hidDim → outDim: (W₁,b₁,W₂,b₂).

Instances For
    theorem NN.GraphSpec.Models.mlp_interp_eq_spec_mlp_forward {α : Type} [Context α] {inDim hidDim outDim : } (params : Runtime.Autograd.Torch.TList α (MLPParams inDim hidDim outDim)) (x : Tensor.Tensor α (Tensor.Shape.Vec inDim)) :
    Interp.spec (mlp inDim hidDim outDim) params x = match match params with | Proofs.Autograd.Algebra.TList.cons w1 (Proofs.Autograd.Algebra.TList.cons b1 (Proofs.Autograd.Algebra.TList.cons w2 (Proofs.Autograd.Algebra.TList.cons b2 Proofs.Autograd.Algebra.TList.nil))) => (w1, b1, w2, b2) with | (w1, b1, w2, b2) => have l1 := { weights := w1, bias := b1 }; have l2 := { weights := w2, bias := b2 }; Examples.mlpForward l1 l2 x

    Theorem (GraphSpec MLP agrees with Spec reference).

    Fix dimensions inDim → hidDim → outDim. Let params be the 4-tensor parameter list (W₁, b₁, W₂, b₂) and x an input vector.

    Then the GraphSpec interpreter applied to the GraphSpec MLP graph computes exactly the same tensor as the reference Examples.mlp_forward from NN.Spec.Models.Mlp, after interpreting the parameter list as two LinearSpecs.

    Informally, both sides compute the same explicit formula:

    z₁ = W₁ · x + b₁
    a₁ = relu(z₁)
    out = W₂ · a₁ + b₂
    

    where the dot/plus are the Spec.linear_spec and Activation.relu_spec operations already used by the Spec model.