Vathek training frame: tile partitions, accumulator, tiled gradient
DefinitionVathekFrameThe core vocabulary of the tiled-graft training frame. Parameter space is (as EuclideanSpace ℝ (Fin d)) and shared-state space is . A valid tile partition of a finite occurrence set is a finite list of pairwise-disjoint tiles whose union is exactly ; tiles may be empty or uneven. The streaming accumulator carries three slots — the running weighted loss, the running direct parameter-gradient contribution (directAccum), and the running shared-state cotangent contribution (sharedAccum) — updated by an executable left-to-right fold in which every tile reads the same fixed pre-update data. The monolithic objective is . The tiled gradient is : accumulated direct contributions plus one reverse pass of the shared derivative applied to the summed cotangent. Radius- global clipping is a total function with both branches specified (the zero gradient is fixed). The trainable projection zeroes every frozen coordinate. The logical step applies the deterministic optimizer exactly once to the masked, clipped gradient.
import Mathlib.Tactic
import Mathlib.Analysis.InnerProductSpace.Adjoint
import Mathlib.Analysis.Calculus.Gradient.Basic
import Mathlib.Data.List.Basic
/-!
# VathekProof — the tiled-graft training frame (white paper §4)
Shared vocabulary for the mission "Vathek I: Tiled graft training preserves the
mathematical update". Parameter space is `W = EuclideanSpace ℝ (Fin d)` (the `d`
logical parameter coordinates, §4.1) and shared-state space is
`V = EuclideanSpace ℝ (Fin m)`.
-/
namespace VathekProof
/-- §4.4: a *valid tile partition* of a finite occurrence set `I` is a finite list of
pairwise-disjoint tiles whose union is exactly `I`. Tiles may be empty or uneven; an
empty tile can be removed without changing the semantics. -/
def IsTilePartition {ι : Type*} [DecidableEq ι] (I : Finset ι) (ℬ : List (Finset ι)) : Prop :=
ℬ.foldr (· ∪ ·) ∅ = I ∧ ℬ.Pairwise (fun B₁ B₂ => Disjoint B₁ B₂)
/-- §4.4: streaming accumulator state — the running weighted loss, the running direct
parameter-gradient contribution (`directAccum`), and the running shared-state cotangent
contribution (`sharedAccum`). -/
structure TileAccum (W V : Type*) where
/-- running weighted loss over the occurrences visited so far -/
loss : ℝ
/-- running direct parameter-gradient contribution -/
direct : W
/-- running shared-state cotangent contribution -/
shared : V
/-- §4.4: one step of the streaming accumulator over tile `B`. Everything is read at
the fixed pre-update data (`val`, `a`, `c`); no parameter is updated between tiles. -/
def tileAccumStep {ι W V : Type*} [AddCommMonoid W] [AddCommMonoid V] [Module ℝ W] [Module ℝ V]
(α : ι → ℝ) (val : ι → ℝ) (a : ι → W) (c : ι → V)
(B : Finset ι) (acc : TileAccum W V) : TileAccum W V where
loss := acc.loss + ∑ i ∈ B, α i * val i
direct := acc.direct + ∑ i ∈ B, α i • a i
shared := acc.shared + ∑ i ∈ B, α i • c i
/-- §4.4: the streaming fold itself, processing tiles left to right from an initial
empty accumulator. -/
def tileAccumFold {ι W V : Type*} [AddCommMonoid W] [AddCommMonoid V] [Module ℝ W] [Module ℝ V]
(α : ι → ℝ) (val : ι → ℝ) (a : ι → W) (c : ι → V) :
List (Finset ι) → TileAccum W V → TileAccum W V
| [], acc => acc
| B :: ℬ, acc => tileAccumFold α val a c ℬ (tileAccumStep α val a c B acc)
/-- §4.4: the tiled evaluator's accumulator after processing all tiles. -/
def tileAccum {ι W V : Type*} [AddCommMonoid W] [AddCommMonoid V] [Module ℝ W] [Module ℝ V]
(α : ι → ℝ) (val : ι → ℝ) (a : ι → W) (c : ι → V) (ℬ : List (Finset ι)) : TileAccum W V :=
tileAccumFold α val a c ℬ ⟨0, 0, 0⟩
/-- §4.3 (1): the monolithic logical objective
`L_ξ(w) = ∑_{i ∈ I} αᵢ · fᵢ(w, h(w))`. -/
noncomputable def frameLoss {ι : Type*} {W V : Type*} (h : W → V) (f : ι → W × V → ℝ)
(α : ι → ℝ) (I : Finset ι) (w : W) : ℝ :=
∑ i ∈ I, α i * f i (w, h w)
/-- §4.4 (3): the tiled gradient — the accumulated direct contributions `A`, plus one
reverse pass of the shared derivative applied to the summed shared cotangent `C`:
`g_tile = A + Dh(w₀)ᵀ C`. -/
noncomputable def tiledGrad {ι : Type*} {d m : ℕ} (α : ι → ℝ) (val : ι → ℝ)
(a : ι → EuclideanSpace ℝ (Fin d)) (c : ι → EuclideanSpace ℝ (Fin m))
(Dh : EuclideanSpace ℝ (Fin d) →L[ℝ] EuclideanSpace ℝ (Fin m))
(ℬ : List (Finset ι)) : EuclideanSpace ℝ (Fin d) :=
(tileAccum α val a c ℬ).direct + Dh.adjoint (tileAccum α val a c ℬ).shared
/-- §4.5: radius-`c` clipping of the whole (already masked) logical gradient. Both
branches are total: at the zero gradient the clip fixes `0`. -/
noncomputable def clipVec {W : Type*} [NormedAddCommGroup W] [NormedSpace ℝ W]
(c : ℝ) (g : W) : W :=
if ‖g‖ ≤ c then g else (c / ‖g‖) • g
/-- §4.1: `P_T` — the coordinate projection that zeroes every frozen coordinate
(`j ∉ T`) and keeps every trainable coordinate (`j ∈ T`). -/
def coordMask {d : ℕ} (T : Finset (Fin d)) (w : EuclideanSpace ℝ (Fin d)) :
EuclideanSpace ℝ (Fin d) :=
WithLp.toLp 2 (fun j => if j ∈ T then w j else 0)
/-- §4.5 (4): one logical optimizer application — project the accumulated gradient
onto trainable coordinates, clip globally, and apply the (deterministic) optimizer
exactly once. -/
noncomputable def logicalStep {d : ℕ} {σ : Type*} (T : Finset (Fin d)) (c : ℝ)
(U : σ → EuclideanSpace ℝ (Fin d) → σ) (S : σ)
(g : EuclideanSpace ℝ (Fin d)) : σ :=
U S (clipVec c (coordMask T g))
end VathekProof
Read-back
What the Lean code literally says, in plain math · glm-5.3 (independent auditor subagent)
{"text": "{\n "readback": "IsTilePartition. For any type equipped with decidable equality (the instance used to form finite-set unions), any finite set , and any finite list of finite sets of subsets of , is the proposition asserting two things simultaneously:\n\n
\n\nThe union is the iterated binary union of the list, taken in order and starting from , so an empty list gives ; hence the empty list is a valid partition exactly when . Disjointness is required pairwise between entries at distinct positions of the list; consequently, a tile occurring twice in the list must be disjoint from itself, which forces it to be empty \u2014 a repeated nonempty tile violates the predicate, while repeated empty tiles are allowed. Tiles may be empty and may be of arbitrary, unequal sizes; the union is required to equal exactly, not merely to cover it or be contained in it.\n\nTileAccum. For two arbitrary types and , with no algebraic structure assumed, is the type of ordered triples whose first component is a real number (field loss), whose second component is an element of (field direct), and whose third component is an element of (field shared). It is pure data: the declaration provides the three projection maps selecting , , and , and nothing more.\n\ntileAccumStep. For any types such that and are commutative additive monoids carrying the structure of real vector spaces (so each has a zero, an associative commutative addition, and a scalar multiplication by reals), given weight and value functions , families and , a finite set , and an accumulator , one step returns the new accumulator with components\n\n
\n\nwhere denotes the scalar action of on (similarly on ). Each component is the old one plus the -sum of -weighted contributions; the sums range over the finite set , so each index is counted once, and makes the step the identity on the accumulator. The step is a total function: no hypothesis is imposed on , and the weights are arbitrary reals (possibly negative or larger than ). All four data functions are read at their given arguments; the step computes and returns a new record and modifies nothing.\n\ntileAccumFold. With the same type assumptions and the same arguments as the step, this is a total function of a list of tiles and an accumulator, defined by structural recursion on the list: on the empty list it returns the accumulator unchanged; on it applies one step at and recurses on the remaining list with the updated accumulator. Tiles are thus processed in list order, first to last, threading the accumulator through. The recursion is structural, so the function is defined for every list of finite sets: tiles may repeat or overlap, and the list need not satisfy any partition property.\n\ntileAccum. The accumulator after processing all tiles: the fold above applied to with the initial accumulator , the zeros being those of , , and . Unfolding the recursion, the three components are\n\n
\n\nthe outer sums running over the positions of the list: an index belonging to several tiles, or a tile repeated in the list, contributes once per occurrence. For the empty list the result is . Because all additions involved are commutative, this final value does not depend on the order of the list, although the intermediate accumulator states do.\n\nframeLoss. For a type and arbitrary types carrying no structure whatsoever, given an arbitrary map , a family , weights , a finite set , and a point , this defines the real number\n\n
\n\nEach summand evaluates at the pair whose first component is itself and whose second component is ; the shared value is the same in every term of the sum. No assumption is placed on or on the (in particular no continuity or differentiability), none on the weights (arbitrary reals, possibly negative), and may be empty, in which case .\n\ntiledGrad. For a type and natural numbers : given , families and (Euclidean spaces of - and -tuples with the standard inner product), a continuous -linear map , and a list of finite sets , this defines the vector in \n\n
\n\nwhere is the accumulator expanded above, and is the adjoint of with respect to the standard inner products, i.e. the unique continuous linear map satisfying for all , (over the reals, the transpose). Only the components and of the accumulator enter the value: the loss component \u2014 and hence the entire argument \u2014 has no influence on the result. Furthermore, no hypothesis connects to a partition of any set: it is an arbitrary list, and indices occurring in several tiles are counted with multiplicity.\n\nclipVec. For a real number and a vector in a real normed space (a normed abelian group with a compatible scalar multiplication by ; in particular exactly when ), the clipped vector is defined by an if-then-else on the inclusive norm test :\n\n
\\\\mathrm{clip}_c(g) = \\\\begin{cases} g, & \\\\text{if } \\\\|g\\\\| \\\\le c, \\\\\\\\[4pt] \\\\dfrac{c}{\\\\|g\\\\|}\\\\, g, & \\\\text{if } \\\\|g\\\\| > c. \\\\end{cases}\n\nThe function is total for every , and real division is total with the convention . Consequences of the literal clauses: if and , the result is rescaled by the positive factor , i.e. a vector of norm exactly pointing along (and yields the zero vector); if and , the test necessarily fails and the else-branch multiplies by the negative scalar , producing a vector of norm pointing opposite to ; if , both branches return the zero vector \u2014 for via the then-branch, and for via the else-branch, where under the stated division convention.\n\ncoordMask. For a natural number , a finite set of coordinates (, the index type of length ), and a vector , this is the coordinatewise mask defined by\n\n
(P_T\\\\, w)_j = \\\\begin{cases} w_j, & j \\\\in T, \\\\\\\\ 0, & j \\\\notin T, \\\\end{cases} \\\\qquad j = 0, \\\\ldots, d-1,\n\ni.e. coordinates in are kept and all others are set to zero. (The code re-packages the coordinate function into the Euclidean-space type; on underlying coordinates this packaging is the identity, so the map is exactly the mask above.) The map is linear in ; if it is the zero map, and if is the full set of all coordinates it is the identity.\n\nlogicalStep. For a natural number and an arbitrary type carrying no structure, given a finite set of coordinates, a real number , an arbitrary function , a state , and a direction , one logical step returns the state\n\n
\n\nthat is: the direction is first masked to the coordinates in by ; the masked vector is then clipped by , so the radius test applies to \u2014 the norm of the already-masked vector, not of ; and finally is applied exactly once to the pair consisting of the starting state and the resulting clipped vector. No property whatsoever is assumed of : it is an arbitrary function of a state and a direction returning a state. Besides the clipping scalar above, no further scaling, normalization, or correction factor appears anywhere in the step, and is applied a single time, not iterated. The radius is an arbitrary real, so the negative-radius and zero-vector behavior of described above carries over verbatim."\n}", "details": {"resolvedPath": "/home/ajax/.omp/agent/sessions/-math/2026-09-22T19-31-08-510Z_01a0ca99-be5e-7000-93c6-dac7c74cc004/RB-VathekFrame.md", "contentType": "text/markdown", "totalLines": 3, "displayContent": {"text": "{\n "readback": "IsTilePartition. For any type equipped with decidable equality (the instance used to form finite-set unions), any finite set , and any finite list of finite sets of subsets of , is the proposition asserting two things simultaneously:\n\n
\n\nThe union is the iterated binary union of the list, taken in order and starting from , so an empty list gives ; hence the empty list is a valid partition exactly when . Disjointness is required pairwise between entries at distinct positions of the list; consequently, a tile occurring twice in the list must be disjoint from itself, which forces it to be empty \u2014 a repeated nonempty tile violates the predicate, while repeated empty tiles are allowed. Tiles may be empty and may be of arbitrary, unequal sizes; the union is required to equal exactly, not merely to cover it or be contained in it.\n\nTileAccum. For two arbitrary types and , with no algebraic structure assumed, is the type of ordered triples whose first component is a real number (field loss), whose second component is an element of (field direct), and whose third component is an element of (field shared). It is pure data: the declaration provides the three projection maps selecting , , and , and nothing more.\n\ntileAccumStep. For any types such that and are commutative additive monoids carrying the structure of real vector spaces (so each has a zero, an associative commutative addition, and a scalar multiplication by reals), given weight and value functions , families and , a finite set , and an accumulator , one step returns the new accumulator with components\n\n
\n\nwhere denotes the scalar action of on (similarly on ). Each component is the old one plus the -sum of -weighted contributions; the sums range over the finite set , so each index is counted once, and makes the step the identity on the accumulator. The step is a total function: no hypothesis is imposed on , and the weights are arbitrary reals (possibly negative or larger than ). All four data functions are read at their given arguments; the step computes and returns a new record and modifies nothing.\n\ntileAccumFold. With the same type assumptions and the same arguments as the step, this is a total function of a list of tiles and an accumulator, defined by structural recursion on the list: on the empty list it returns the accumulator unchanged; on it applies one step at and recurses on the remaining list with the updated accumulator. Tiles are thus processed in list order, first to last, threading the accumulator through. The recursion is structural, so the function is defined for every list of finite sets: tiles may repeat or overlap, and the list need not satisfy any partition property.\n\ntileAccum. The accumulator after processing all tiles: the fold above applied to with the initial accumulator , the zeros being those of , , and . Unfolding the recursion, the three components are\n\n
\n\nthe outer sums running over the positions of the list: an index belonging to several tiles, or a tile repeated in the list, contributes once per occurrence. For the empty list the result is . Because all additions involved are commutative, this final value does not depend on the order of the list, although the intermediate accumulator states do.\n\nframeLoss. For a type and arbitrary types carrying no structure whatsoever, given an arbitrary map , a family , weights , a finite set , and a point , this defines the real number\n\n
\n\nEach summand evaluates at the pair whose first component is itself and whose second component is ; the shared value is the same in every term of the sum. No assumption is placed on or on the (in particular no continuity or differentiability), none on the weights (arbitrary reals, possibly negative), and may be empty, in which case .\n\ntiledGrad. For a type and natural numbers : given , families and (Euclidean spaces of - and -tuples with the standard inner product), a continuous -linear map , and a list of finite sets , this defines the vector in \n\n
\n\nwhere is the accumulator expanded above, and is the adjoint of with respect to the standard inner products, i.e. the unique continuous linear map satisfying for all , (over the reals, the transpose). Only the components and of the accumulator enter the value: the loss component \u2014 and hence the entire argument \u2014 has no influence on the result. Furthermore, no hypothesis connects to a partition of any set: it is an arbitrary list, and indices occurring in several tiles are counted with multiplicity.\n\nclipVec. For a real number and a vector in a real normed space (a normed abelian group with a compatible scalar multiplication by ; in particular exactly when ), the clipped vector is defined by an if-then-else on the inclusive norm test :\n\n
\\\\mathrm{clip}_c(g) = \\\\begin{cases} g, & \\\\text{if } \\\\|g\\\\| \\\\le c, \\\\\\\\[4pt] \\\\dfrac{c}{\\\\|g\\\\|}\\\\, g, & \\\\text{if } \\\\|g\\\\| > c. \\\\end{cases}\n\nThe function is total for every , and real division is total with the convention . Consequences of the literal clauses: if and , the result is rescaled by the positive factor , i.e. a vector of norm exactly pointing along (and yields the zero vector); if and , the test necessarily fails and the else-branch multiplies by the negative scalar , producing a vector of norm pointing opposite to ; if , both branches return the zero vector \u2014 for via the then-branch, and for via the else-branch, where under the stated division convention.\n\ncoordMask. For a natural number , a finite set of coordinates (, the index type of length ), and a vector , this is the coordinatewise mask defined by\n\n
(P_T\\\\, w)_j = \\\\begin{cases} w_j, & j \\\\in T, \\\\\\\\ 0, & j \\\\notin T, \\\\end{cases} \\\\qquad j = 0, \\\\ldots, d-1,\n\ni.e. coordinates in are kept and all others are set to zero. (The code re-packages the coordinate function into the Euclidean-space type; on underlying coordinates this packaging is the identity, so the map is exactly the mask above.) The map is linear in ; if it is the zero map, and if is the full set of all coordinates it is the identity.\n\nlogicalStep. For a natural number and an arbitrary type carrying no structure, given a finite set of coordinates, a real number , an arbitrary function , a state , and a direction , one logical step returns the state\n\n
\n\nthat is: the direction is first masked to the coordinates in by ; the masked vector is then clipped by , so the radius test applies to \u2014 the norm of the already-masked vector, not of ; and finally is applied exactly once to the pair consisting of the starting state and the resulting clipped vector. No property whatsoever is assumed of : it is an arbitrary function of a state and a direction returning a state. Besides the clipping scalar above, no further scaling, normalization, or correction factor appears anywhere in the step, and is applied a single time, not iterated. The radius is an arbitrary real, so the negative-radius and zero-vector behavior of described above carries over verbatim."\n}", "startLine": 1, "lineNumbers": [1, 2, 3]}, "meta": {"source": {"type": "internal", "value": "agent://RB-VathekFrame"}}}}
Confirmed by the mission captain (proposal self-audit).