ViT scaling pass — multi-head rendering + faithfulness #
The MATH is general in heads (mhsaHasVJPMat, transformerBlockVHasVJPMat); this file
supplies the RENDERING + faithfulness at heads > 1:
mhsaLayer_spelled— the general-headstie, on which the rest of the file builds:mhsaLayer 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).vitBlockGraphMHVover the two new ch10 tokensheadSliceF/headPadF(+headsSumG, a left-assocaddVfold), with the block denotationvitBlockGraphMHV_den_auxthatViTDepthK'svitFwdGraphKMHV_faithfulchains per block atheads := hm1 + 1— faithfulness againstmhsaLayer 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.
References #
- Vaswani et al. 2017, Attention Is All You Need. https://arxiv.org/abs/1706.03762
Head h's [N,d] column block of an [N,heads·d] matrix — the
finProdFinEquiv (h, ·) column gather mhsaLayer 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 mhsaLayer'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 — mhsaLayer's concat indexing.
MHSA at general heads is per-head slice → matmul-spelled SDPA →
pad-scatter, summed over heads. The tie the multi-head graph faithfulness
rests on: 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 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 spelled per head (mhsaLayer_spelled).
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 mhsaLayer_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.
The vector-LN multi-head block over the tokens: each LN site is
lnRowF(1,0) → rowScaleF γ → rowBiasF β (the ViTRender
decomposition); attention per head: headSliceF → SDPA → headPadF, folded by headsSumG.
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.