Documentation

LeanMlir.Proofs.Foundation.Batched.PoolSelB

The batched 2×2 pool at a fixed selection — the per-example kit, lifted over the batch #

SmallParamGrad proves the chapter nets' loss gradients through their 2×2 pools by freezing each pool's routing at a selection σ (one cell per window): the pool is then a gather, linear and differentiable everywhere, and maxPool_relu_eventuallyEq_sel says the pooled ReLU IS that gather along any parameter, near a point where every window is dead or tied only between twins. This file is the same kit at the batched index N·(c·h·w): a selection per example (poolSelIdxB), the selection and smoothness predicates per example (PoolSelDomB, MaxPool2SmoothUpToB), the pooled batch as a gather (batchMap_maxPoolFlat_eq_gatherB), the germ along a parameter (maxPoolB_relu_eventuallyEq_sel, row by row from the per-example lemma), and the render's pool backward as the scatter at the first argmax (batchMapAux_maxPoolBackFlat_eq_selScatter), which is what makes a batched step tie's pool cotangent the loss-gradient chain's at σ = argmax.

The pull-backs a batched chain needs that ParamGradNodes does not have are here too: through the gather with the ReLU mask spelled reluMaskB (hasGradAt_gatherReluB), and through a batched dense layer with its backward spelled rowDenseBackFlat (hasGradAt_denseB).

theorem Proofs.SmallParamGrad.batchMap_apply_slice {N a b : ℕ} (f : Vec a → Vec b) (u : Vec (N * a)) (r : Fin N) (j : Fin b) :

A batched map read at example r, entry j: the per-example map at that example's slice.

theorem Proofs.SmallParamGrad.batchSlice_relu (N a : ℕ) (Z : Vec (N * a)) (r : Fin N) :

ReLU slices: it is coordinatewise.

theorem Proofs.SmallParamGrad.continuous_batchSlice (N a : ℕ) (r : Fin N) :
Continuous fun (v : Vec (N * a)) => StableHLO.batchSlice N a v r

Taking example r's slice is continuous (a reindex).

def Proofs.SmallParamGrad.poolSelIdxB (N : ℕ) {c h w : ℕ} (σ : Fin N → Fin c → Fin h → Fin w → Fin 2 × Fin 2) (k : Fin (N * (c * h * w))) :
Fin (N * (c * (2 * h) * (2 * w)))

The flat batched index of the cell σ selects for pooled entry k: example k's row, and poolSelIdx of that example's selection within it.

Equations
Instances For
    theorem Proofs.SmallParamGrad.poolSelIdxB_apply (N : ℕ) {c h w : ℕ} (σ : Fin N → Fin c → Fin h → Fin w → Fin 2 × Fin 2) (r : Fin N) (j : Fin (c * h * w)) :
    def Proofs.SmallParamGrad.PoolSelDomB (N : ℕ) {c h w : ℕ} (σ : Fin N → Fin c → Fin h → Fin w → Fin 2 × Fin 2) (u : Vec (N * (c * (2 * h) * (2 * w)))) :

    Every example's selection names a maximum of every window of its slice.

    Equations
    Instances For
      theorem Proofs.SmallParamGrad.poolSelDomB_argmax (N : ℕ) {c h w : ℕ} (u : Vec (N * (c * (2 * h) * (2 * w)))) :
      PoolSelDomB N (fun (r : Fin N) => maxPool2Argmax (Tensor3.unflatten (StableHLO.batchSlice N (c * (2 * h) * (2 * w)) u r))) u

      The per-example first argmax is a selection of the batch.

      @[reducible, inline]
      abbrev Proofs.SmallParamGrad.MaxPool2SmoothUpToB (N : ℕ) {c h w : ℕ} (T : Fin N → Fin (2 * h) × Fin (2 * w) → Fin (2 * h) × Fin (2 * w) → Prop) (u : Vec (N * (c * (2 * h) * (2 * w)))) :

      Smooth, dead, or tied only between twins, in every example's windows; the twin relation T r is that example's.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        theorem Proofs.SmallParamGrad.batchMap_maxPoolFlat_eq_gatherB {N c h w : ℕ} (σ : Fin N → Fin c → Fin h → Fin w → Fin 2 × Fin 2) (u : Vec (N * (c * (2 * h) * (2 * w)))) (hσ : PoolSelDomB N σ u) :
        StableHLO.batchMap N (maxPoolFlat c h w) u = fun (k : Fin (N * (c * h * w))) => u (poolSelIdxB N σ k)

        The pooled batch is the gather at σ wherever σ names a maximum of every window.

        theorem Proofs.SmallParamGrad.maxPoolB_relu_eventuallyEq_sel {P N c h w : ℕ} (Z : Vec P → Vec (N * (c * (2 * h) * (2 * w)))) (σ : Fin N → Fin c → Fin h → Fin w → Fin 2 × Fin 2) (T : Fin N → Fin (2 * h) × Fin (2 * w) → Fin (2 * h) × Fin (2 * w) → Prop) (hT : ∀ (θ : Vec P) (r : Fin N) (ci : Fin c) (p q : Fin (2 * h) × Fin (2 * w)), T r p q → Z θ (finProdFinEquiv (r, t3Idx ci p.1 p.2)) = Z θ (finProdFinEquiv (r, t3Idx ci q.1 q.2))) (θ₀ : Vec P) (hZc : ContinuousAt Z θ₀) (hz : ∀ (k : Fin (N * (c * (2 * h) * (2 * w)))), Z θ₀ k ≠ 0) (hs : MaxPool2SmoothUpToB N T (Z θ₀)) (hσ : PoolSelDomB N σ (relu (N * (c * (2 * h) * (2 * w))) (Z θ₀))) :
        ∀ᶠ (θ : Vec P) in nhds θ₀, StableHLO.batchMap N (maxPoolFlat c h w) (relu (N * (c * (2 * h) * (2 * w))) (Z θ)) = fun (k : Fin (N * (c * h * w))) => relu (N * (c * (2 * h) * (2 * w))) (Z θ) (poolSelIdxB N σ k)

        Along a parameter, ReLU then the batched 2×2 pool is the gather at a fixed selection. Z θ is the batch's pool pre-activation as the parameter moves; at θ₀ it has no zero entry, every example's windows are dead or tied only between that example's T-twins, σ names a maximum of every window of its ReLU, and twins are equal at EVERY θ. Each example is maxPool_relu_eventuallyEq_sel on its own slice; the batch is the conjunction over the finitely many examples.

        theorem Proofs.SmallParamGrad.selScatter_poolSelIdxB_apply {N c h w : ℕ} (σ : Fin N → Fin c → Fin h → Fin w → Fin 2 × Fin 2) (dy : Vec (N * (c * h * w))) (r : Fin N) (i : Fin (c * (2 * h) * (2 * w))) :

        The batched scatter at example r is the per-example scatter of that example's slices.

        maxPoolBackFlat is the flattened first-argmax routing maxPoolBackDenote.

        theorem Proofs.SmallParamGrad.maxPoolBackFlat_eq_selScatter {c h w : ℕ} (xv : Vec (c * (2 * h) * (2 * w))) (dyv : Vec (c * h * w)) :

        The per-example pool backward is the scatter at the first argmax, at every point.

        theorem Proofs.SmallParamGrad.batchMapAux_maxPoolBackFlat_eq_selScatter {N c h w : ℕ} (x : Vec (N * (c * (2 * h) * (2 * w)))) (dy : Vec (N * (c * h * w))) :

        The render's batched pool backward (den maxPoolBackB) is the scatter at the per-example first argmax, at every point, ties included — so a batched step tie's pool cotangent is the loss-gradient chain's at that selection.

        theorem Proofs.SmallParamGrad.hasGradAt_gatherReluB {m n : ℕ} (σ : Fin n → Fin m) (z : Vec m) (hz : ∀ (k : Fin m), z k ≠ 0) {G : Vec n → Vec 1} {dy : Vec n} (hG : HasGradAt G (fun (k : Fin n) => relu m z (σ k)) dy) :
        HasGradAt (fun (y : Vec m) => G fun (k : Fin n) => relu m y (σ k)) z (BackLinks.reluMaskB m z (selScatter σ dy))

        Through ReLU then a gather, off the ReLU kinks: the scatter along σ, then the mask reluMaskB. hasGradAt_gatherRelu with its cotangent in the batched ties' spelling.

        theorem Proofs.SmallParamGrad.hasGradAt_denseB {N a c : ℕ} (W : Mat a c) (b : Vec c) (u : Vec (N * a)) {G : Vec (N * c) → Vec 1} {dy : Vec (N * c)} (hG : HasGradAt G (StableHLO.batchMap N (dense W b) u) dy) :
        HasGradAt (fun (y : Vec (N * a)) => G (StableHLO.batchMap N (dense W b) y)) u (StableHLO.rowDenseBackFlat N a c W dy)

        Through a batched dense layer: the backward is rowDenseBackFlat (den of .denseRowBack).