§B: the ViT MHSA backward chain IS the certified VJP (the sdpa adjoint) #
The substantive vit-specific §B leaf: the hand-composed multi-head self-attention backward mhsaBackFlat
(ViTBackChains.lean) IS the certified MHSA input-gradient VJP mhsa_has_vjp_mat (Attention.lean),
flattened — the attention analogue of the depthwise/conv adjoint gates.
Unlike the CNN convFlatBack (a free reversed-kernel conv that needed a gate), the ViT sdpa cores are
ALREADY certified-sdpa_back-grounded by construction (coreQFlat = flatten ∘ mhsaSdpaBackQ ∘ unflatten,
mhsaSdpaBackQ = sdpa_back_Q per mhSlab head). What this file closes is the assembly reconciliation:
mhsaBackFlat is a flat per-head fan-in with SEPARATE dense Wᵀq/Wᵀk/Wᵀv projection-backwards
(perRowFlat), while the certified mhsa_has_vjp_mat.backward is a Mat-space VJP over the qkv-MERGED
projection. ViTBackB0's mhsa_backward_collapseMH already collapses the certified Mat backward to the clean
per-head merged sum mhsaBackCollapsedMH = ∑ₕ (Σⱼ Wq c (h,j)·dQ + Σⱼ Wk·dK + Σⱼ Wv·dV); this file shows
mhsaBackFlat (Q/K/V pinned to the actual projections dense W· bq (X·)) equals that, coordinatewise:
dense Wᵀ 0 = Mat.mulVec W, the Σ k over h·dh reindexes to Σₕ Σⱼ, and the chain's separate projBack
sums regroup into the certified ∑ₕ(Q+K+V) by Finset.sum_add_distrib. So mhsaBackFlat IS the
certified attention gradient — the genuinely-new (sdpa) half of the ViT block §B tie.
The block tie that wraps this in the per-token LN/dense/gelu sublayer reconciliations and the residual
fan-in is vitBlockBackV_eq_transformerBlockV_vjp (ViTVecLNBackCertifiedTie.lean). 3-axiom-clean.
The projection-back leaf coordinate. A per-token dense (Wᵀ) 0 projection-backward
(perRowFlat) applied to a flattened saved cotangent Mat.flatten S reads, at output coordinate
(r, c), as Σ k, W c k · S r k — i.e. Mat.mulVec W of row r. The dense_transpose = mulVec
fact lifted through the perRowFlat / flatten-unflatten reindex. The shared leaf for all four
projections (Wq/Wk/Wv contracted against the cores, Wo against the block cotangent).
The Wo-back, unflattened. unflatten (perRowFlat (dense Wᵀo 0) dconcat) = fun i c => mulVec Wo (unflatten dconcat i) c — the block cotangent run through the output-projection backward, in Mat
form. This is the per-head slab the cores read (mhSlab h (unflatten woflat) = dAttg h).
THE ViT MHSA BACKWARD §B TIE. The MHSA backward chain mhsaBackFlat, with its saved Q/K/V
projections pinned to the actual dense W· b· (X·) projections at the saved block input X, IS the
certified MHSA input-gradient VJP (mhsa_has_vjp_mat …).backward X, flattened. So the attention
backward the ViT chain is spelled in IS the certified attention gradient, not a look-alike.
Closes under [propext, Classical.choice, Quot.sound].
The chain's dense input-VJP dense (Wᵀ) 0 IS the certified contraction Mat.mulVec W
(the certified dense_has_vjp.backward, which ignores its affine activation); mul_comm per
term. The function-level form (no x arg) the simp matches against.
The chain's GELU backward diagBack (act'(s)) IS the certified gelu_has_vjp.backward
at the saved pre-activation s (the elementwise derivative scaling — gelu_has_vjp.backward s dy i = dy i · geluScalarDeriv (s i), diagBack is the same scaling, mul_comm).
The transformerMlp backward in explicit per-token form. The nested vjpMat_comp
(dense₂ ∘ gelu ∘ dense₁, per token) reduces to: each token's dz r runs mulVec Wfc2,
the GELU backward at the saved pre-activation dense₁(Y r), then mulVec Wfc1. Pure
rfl (the per-token VJPs are rowwise/vjpMat_comp structure projections).
L2 — the transformerMlp backward, flattened, IS perRowFlatPR of the flat chain.
The certified per-token MLP-body backward (mulVec Wfc1 ∘ gelu-back ∘ mulVec Wfc2) equals the
chain's dense Wᵀ₁ 0 ∘ diagBack(act'(dense₁ Y)) ∘ dense Wᵀ₂ 0, row by row.