ViT scaling pass — multi-head rendering + faithfulness #
The representative ViT close (Items A–D) rendered at heads = 1, where MHSA
collapses to three matmuls + a row-softmax (mhsa_layer_one_head). The MATH
was always general in heads (mhsa_has_vjp_mat, transformerBlock(V)_has_vjp_mat,
vitForward2(V)_has_vjp); what was missing is RENDERING + faithfulness at
heads > 1. This file closes that:
mhsa_layer_spelled— the load-bearing tie, the general-headssuccessor ofmhsa_layer_one_head:mhsa_layer N heads dIS, per head, slice → matmul-spelled SDPA → pad-scatter, summed over heads. The concat is spelled asΣ_h headPadMat h ∘ (per-head SDPA)— every output column receives exactly one head's value, and the sum stays at the single indexN·(heads·d)(no(N·a)+(N·b)Nat-cast trouble a binary concat would hit).vitBlockGraphMH(V)over the two new ch10 tokensheadSliceF/headPadF(+headsSumG, a left-assocaddVfold), withvitFwdGraphMH(V)_faithful: the multi-head forward graphs denote the provenvitForward2/vitForward2Vatheads := hm1 + 1— faithfulness againstmhsa_layer N heads dDIRECTLY, not a 1-head specialization.
The graph layer is stated at heads = hm1 + 1 (the head fold needs a first
head); the Mat-level spelling is fully general in heads.
Head h's [N,d] column block of an [N,heads·d] matrix — the
finProdFinEquiv (h, ·) column gather mhsa_layer feeds each head's SDPA.
Equations
- Proofs.headSliceMat N heads d h A r j = A r (finProdFinEquiv (h, j))
Instances For
Scatter an [N,d] head block into head h's columns of a zero
[N,heads·d]. Summed over heads this is mhsa_layer's concat; it is also
the slice's VJP.
Equations
- Proofs.headPadMat N heads d h A r hj = if (finProdFinEquiv.symm hj).1 = h then A r (finProdFinEquiv.symm hj).2 else 0
Instances For
The pad-sum IS the head concat: every column hj lands in exactly one
head's block, so the sum over heads of pad-scatters reads off head
(symm hj).1 at column (symm hj).2 — mhsa_layer's concat indexing.
MHSA at general heads is per-head slice → matmul-spelled SDPA →
pad-scatter, summed over heads. The general-heads successor of
mhsa_layer_one_head and the load-bearing tie for the multi-head graph
faithfulness: each head's SDPA is exactly the ch10 token spelling
(Q_h·K_hᵀ → ·1/√d → row-softmax → P_h·V_h) on the sliced Q/K/V, and
the concat is the pad-sum (sum_headPadMat_apply).
The ch10 spelled pre-norm transformer block at general heads (Mat level) —
the exact op sequence vitBlockGraphMH denotes: LN₁ → Q/K/V per-token dense
→ per head (slice → Q_h·K_hᵀ → ·1/√d → row-softmax → P_h·V_h → pad) →
Σ heads → output dense → +res → LN₂ → fc1 → GELU → fc2 → +res.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The spelled multi-head block IS transformerBlock at general heads.
The sublayer/residual structure matches definitionally once
mhsa_layer_spelled turns the per-head plumbing into the pad-sum.
The spelled multi-head block at vector-[D] LN — each LN site decomposed as
the graph (and ViTRender) emit it: pure normalize (scalar-LN at 1,0) →
per-channel scale → per-channel bias; attention as vitBlockSpelledMH.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The spelled multi-head vector-LN block IS transformerBlockV at general
heads — the three-stage LN decomposition collapses to layerNormVec
definitionally; the per-head plumbing via mhsa_layer_spelled.
Left-assoc addV fold of one graph per head — the token-level Σ over
heads (heads = hm1 + 1: the fold needs a first head).
Equations
- Proofs.StableHLO.headsSumG f = f 0
- Proofs.StableHLO.headsSumG f = (Proofs.StableHLO.headsSumG fun (i : Fin (hm1 + 1)) => f i.castSucc).addV (f (Fin.last (hm1 + 1)))
Instances For
Per-head slice commutation bridge.
Per-head pad commutation bridge.
One spelled pre-norm transformer block over the ch10 tokens at
heads = hm1 + 1: lnRowF → Q/K/V denseRowF → per head
(headSliceF → matmulF(Q_h, transposeF K_h) → scaleF →
softmaxRowF → matmulF(P_h, V_h) → headPadF) → headsSumG →
output denseRowF → addV residual → lnRowF → fc1 → geluF → fc2 →
addV residual.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The vector-LN multi-head block over the tokens: each LN site is
lnRowF(1,0) → rowScaleF γ → rowBiasF β (the ViTRender
decomposition); attention as vitBlockGraphMH.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Multi-head vector-LN block-graph denotation. Public — the depth-k
faithfulness induction (ViTDepthK.lean) chains it per block.
Whole multi-head ViT forward graph (scalar LN): patch embed → 2 spelled
multi-head blocks (distinct params) → final per-token LN → CLS slice →
dense head, at heads = hm1 + 1, D = (hm1+1)·d.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Multi-head ViT forward faithfulness — the scaling-pass apex: the
multi-head forward graph denotes the proven distinct-param 2-block
vitForward2 at heads := hm1 + 1 — against mhsa_layer directly
(per-block vitBlockGraphMH_den_aux + vitBlockSpelledMH_eq,
mhsa_layer_spelled under the hood).
Whole multi-head vector-LN ViT forward graph: patch embed → 2 spelled
multi-head vector-LN blocks (distinct params) → final vector-LN (the same
three-token decomposition) → CLS slice → dense head. The production
ViTRender shape at the graph layer.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Multi-head vector-LN ViT forward faithfulness — the multi-head graph
at the production LN form denotes vitForward2V at heads := hm1 + 1.