Closing the ViT render — the parameter-gradient close (ch10 Item C) #
planning/archive/vit_close.md Item C, applied to the representative 2-block / 1-head ViT
(vitForward2, the proven whole-net VJP vitForward2_has_vjp; graph vitFwdGraph).
Generic in the cotangent dy the backward chain delivers at each site's output
(pinning that cotangent to the actual attention chain is the optional Item D), batch-1 —
everything in a ViT is per-example separable (the EfficientNet contrast).
| family (render SSA) | forward fn | certified by |
|---|---|---|
| Wq/Wk/Wv/Wo, Wfc1/Wfc2 + biases | per-token dense (rowwise) | vit_render_rowdense{W,b}_certified (new family): dW = Σ_tokens xᵣ ⊗ dyᵣ, db = Σ_tokens dyᵣ — the M2 outer-product bridge row-lifted |
classifier Wcls/bcls | dense on the CLS row | M2 weight/bias_grad_bridge (reuse — single-vector dense) |
| LN γ/β ×5 sites (scalar, per-token) | rowwise layerNormForward | vit_render_rowln{gamma,beta}_certified (new): the ConvNeXtClose Vec 1 embedding row-lifted — dγ = Σ_{tokens} Σ_D dy·x̂, dβ = Σ Σ dy (affine in the params ⇒ no 0 < ε) |
pos_embed | additive (patchEmbed_flat) | vit_render_pos_certified: the pos-Jacobian is the identity ⇒ dPos = dy |
cls_token | row-0 scatter (patchEmbed_flat) | vit_render_cls_certified: masked-gather Jacobian ⇒ dCls = dy row-0 slice |
patch conv Wp/bp | stride-P conv (patchEmbed_flat) | vit_render_patch{W,b}_certified: kernel-linear w/ constant guarded reads ⇒ dWp = Σ_p read·dy_(p+1), dbp = Σ_p dy_(p+1) (CLS row excluded) |
| attention internals (softmax, scale) | — | no parameters |
Two genuinely-new bridge families (everything else is reuse or a reindex):
- Per-token dense W/b — every row of
[N,a]through the sameW : [a,c](+b). The W-Jacobian of the flattened rowwise dense is block-sparse —∂y_(r,k)/∂W_(i,j) = X_(r,i)·δ_(k,j)— so the rendered per-token outer-product reducedW_(i,j) = Σ_r X_(r,i)·dY_(r,j)(onedot_generalcontracting the token axis) is the certified contraction. Covers Wq/Wk/Wv/Wo, Wfc1/Wfc2 and their biases at every block. - Row-lifted scalar-LN γ/β — the ConvNeXtClose
Vec 1embedding generalized from one LN site overVec nto N token rows: as a function ofγ' : Vec 1the rowwise LN is affine,γ' ↦ fun (r,k) => x̂_r(k)·γ'(0) + β, so the Jacobian is the flattened per-row x̂ and the rendered whole-tensor reducedγ = Σ_r Σ_k dY_(r,k)·x̂_r(k) = Σ_r bn_grad_gamma (X r) (dY r)is certified. Likewisedβ = Σ_r Σ_k dY_(r,k).
The classifier head (dense on the CLS vector) is VERBATIM M2 weight/bias_grad_bridge
reuse at [D, nClasses]. The patch-embed conv Wp/bp (§ E) closes over
patchEmbed_flat directly — the kernel is the VARIABLE and the pad-guarded image reads
are CONSTANT coefficients (the mirror of the input-grad case), so the same const×reindex
recipe applies with the CLS row masked out. 3-axiom clean by construction.
Jacobian of the per-token dense w.r.t. the (flattened) shared weight —
∂y_(r,k)/∂W_(i,j') = X_(r,i)·δ_(k,j'). The row-lift of pdiv_dense_W.
The rendered per-token dense weight gradient: the token-axis-contracted
outer product dW_(i,j) = Σ_r X_(r,i)·dY_(r,j) (one dot_general contracting
the token axis — the row-lift of dense_weight_grad = x ⊗ dy).
Equations
- Proofs.rowDense_weight_grad X dY i j = ∑ r : Fin N, X r i * dY r j
Instances For
The rendered per-token dense bias gradient: the token-axis reduce
db_j = Σ_r dY_(r,j).
Equations
- Proofs.rowDense_bias_grad dY j = ∑ r : Fin N, dY r j
Instances For
Per-token dense W-gradient bridge. The rendered token-contracted outer
product equals the certified Jacobian of the rowwise dense (as a function of
the flattened shared W) contracted with the cotangent.
Per-token dense b-gradient bridge. The rendered token-axis reduce equals the certified rowwise-dense ∂/∂b contraction.
Per-token dense W output, certified. Wⁿ = W − lr·(Σ_tokens xᵣ ⊗ dyᵣ) denotes
W − lr·(certified ∂(rowwise dense)/∂W · cotangent). Covers Wq/Wk/Wv/Wo and
Wfc1/Wfc2 at every block of the representative ViT (each at its own [a,c]).
Per-token dense b output, certified. bⁿ = b − lr·(Σ_tokens dyᵣ) denotes the
certified rowwise-dense ∂/∂b contraction. Covers all six per-block biases.
Jacobian of the rowwise scalar-LN w.r.t. γ — ∂y_(r,k)/∂γ = x̂_r(k) (dense:
the shared scalar γ scales every token's every channel).
Jacobian of the rowwise scalar-LN w.r.t. β — ∂y_(r,k)/∂β = 1.
The rendered rowwise-LN γ gradient: the whole-tensor reduce
dγ = Σ_r Σ_k dY_(r,k)·x̂_r(k) — per-row bn_grad_gamma, summed over tokens.
Equations
- Proofs.rowLN_grad_gamma N D ε X dY = ∑ r : Fin N, Proofs.bn_grad_gamma D ε (X r) (dY r)
Instances For
The rendered rowwise-LN β gradient: dβ = Σ_r Σ_k dY_(r,k).
Equations
- Proofs.rowLN_grad_beta N D dY = ∑ r : Fin N, Proofs.bn_grad_beta D (dY r)
Instances For
Rowwise scalar-LN γ-gradient bridge. The rendered whole-tensor reduce equals the certified rowwise-LN ∂/∂γ contraction.
Rowwise scalar-LN β-gradient bridge. The rendered whole-tensor reduce
Σ_r Σ_k dY_(r,k) equals the certified rowwise-LN ∂/∂β contraction.
Rowwise scalar-LN γ output, certified. γⁿ = γ − lr·(Σ_tokens Σ_D dy·x̂)
denotes the certified rowwise-LN ∂/∂γ contraction. Covers all five LN sites
of the representative ViT (LN1/LN2 per block + the final LN).
Rowwise scalar-LN β output, certified.
pos-embed output, certified. The pos Jacobian is the identity, so the
rendered dPos = dy (the cotangent itself, batch-summed by the batched
render) is the certified contraction.
The rendered CLS-token gradient: the row-0 slice of the patch-embed
output cotangent (clsSliceF's shape, applied to the embed cotangent).
Equations
- Proofs.cls_token_grad dy i = dy (finProdFinEquiv (0, i))
Instances For
CLS-token output, certified. clsⁿ = cls − lr·(row-0 slice of the embed cotangent) denotes the certified ∂(patchEmbed)/∂cls contraction.
The pad-guarded patch read of patchEmbed_flat, named: input pixel
(c, h'·P + kh, w'·P + kw) of patch p (row-major patch grid of width
W/P), zero out of range. Constant in the kernel.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Jacobian of patchEmbed_flat w.r.t. the (flattened) patch kernel —
∂y_(n,dd)/∂W_(d,c,kh,kw) = [n ≠ 0]·δ_(dd,d)·read(c,kh,kw, patch n−1).
The rendered patch-kernel gradient: for each tap (d,c,kh,kw), the
patch-grid reduce Σ_p read(c,kh,kw,p)·dy_(p+1,d) — the "dilate dy /
valid conv" weight grad, with the CLS row (token 0) excluded.
Equations
- Proofs.patchEmbed_weight_grad ic H W P N D img dy d c kh kw = ∑ n : Fin N, Proofs.patchRead ic H W P img c kh kw ↑n * dy (finProdFinEquiv (n.succ, d))
Instances For
The rendered patch bias gradient: db_d = Σ_p dy_(p+1,d) (the CLS row
excluded — token 0 carries no conv bias).
Equations
- Proofs.patchEmbed_bias_grad N D dy d = ∑ n : Fin N, dy (finProdFinEquiv (n.succ, d))
Instances For
Patch-kernel gradient bridge. The rendered patch-grid reduce equals the certified ∂(patchEmbed)/∂W contraction.
Patch-kernel output, certified. Wpⁿ = Wp − lr·(patch-grid reduce)
denotes the certified ∂(patchEmbed)/∂Wp contraction.
Jacobian of patchEmbed_flat w.r.t. the patch bias — the row-masked
gather ∂y_(n,k)/∂bc_i = [n ≠ 0]·δ_(i,k) (token 0 is the CLS row).
Patch bias gradient bridge. The rendered CLS-row-excluded reduce equals the certified ∂(patchEmbed)/∂bc contraction.