Docs / Proofs

Mixed-radix indices at symbolic sizes

Lean: GPUProof/MixedRadix.lean. Tests: MojoProofTest/GPUProof/MixedRadix.lean.

Scope. MixedRadix.lt_prod, MixedRadix.eq_iff_digits, MixedRadix.val_append and MixedRadix.fits hold for every size assignment (every two, for eq_iff_digits), radix list and width; WordFit.of_lt for every width and number. The tests prove gemv's write index and the naive matmul's offset facts for every size and thread, and check the theorems by evaluation at two launches and two matrices of unrelated sizes. Nothing here is about one kernel's program: the gemv and matmul statements are about the index expressions, not about the IR that computes them.

Definitions

No definition fixes a machine constant: there is no warp width, word width or grid size in the module.

Statements

Proof

What exists elsewhere

Where the Lean form departs from the design

Tests (MojoProofTest/GPUProof/MixedRadix.lean)

For every size and thread:

By evaluation (#guard), over every pair of threads of the launches ntid = 96, W = 32 with three blocks and ntid = 15, W = 5 with seven blocks (launchOk), and over every pair of entries of 3 × 4 and 5 × 7 matrices (matrixOk): InRange, lt_prod, eq_iff_digits, gemv_val, gemv_warp and matmul_eq, and radixProd as the size (ntid / W) * grid (SizeExpr.mul).

The mutants, decided, one per rule:

Footprints at symbolic sizes: the statements of View.tiling and View.coarsen_tile

Status. Statements and plan only: no module defines them yet. The Lean below elaborates against this tree's GPUProof.Sig, GPUProof.MixedRadix and the layout algebra of MojoProof/Layout/ (checked with the proofs left open, outside the repository); the proofs are the plan after it. It goes in a new core module, GPUProof/View.lean, after the layout algebra's move into the core (below), and imports Mathlib's Set.iUnion and Pairwise beside it. The code opens GPUProof.Layout and GPUProof.Layout.Tup (the moved namespaces) and takes variable {S : Sig}.

Definitions

/-- The instances of level `ℓ` in launch `L`: those that a thread of `L` lies in. -/
def Sig.Inst (S : Sig) (L : S.Launch) (ℓ : S.Level) (g : S.Gid) : Prop :=
  ∃ t ∈ S.threads L, S.group L ℓ t = g

/-- The sets `T i` tile `D`: each lies in `D`, and two of different indices are disjoint. -/
def Tiling {ι α : Type} (T : ι → Set α) (D : Set α) : Prop :=
  (∀ i, T i ⊆ D) ∧ Pairwise fun i j => Disjoint (T i) (T j)

/-- A footprint at symbolic sizes: at each size assignment `σ`, instance `g` of `level` in
launch `L` covers the bytes of buffer `buf` at the offsets `key · cosize + lay(c)`, for the
coordinates `c` below the layout's size, where `key` is valued at `bind L g σ`, the assignment
`σ` with the variables of the key's digits set to `g`'s coordinates. -/
structure View (S : Sig) where
  lay : Sizes → Lay
  level : S.Level
  buf : S.Buf
  key : MixedRadix
  bind : S.Launch → S.Gid → Sizes → Sizes

namespace View

/-- The offset of coordinate `c` of instance `g`. -/
def off (v : View S) (σ : Sizes) (L : S.Launch) (g : S.Gid) (c : Nat) : Nat :=
  v.key.val (v.bind L g σ) * (v.lay σ).cosize + (v.lay σ).call (leaf c false)

/-- The bytes of instance `g`. -/
def tile (v : View S) (σ : Sizes) (L : S.Launch) (g : S.Gid) : Set S.Loc :=
  {x | ∃ c < (v.lay σ).size, x = S.place v.buf (v.off σ L g c)}

/-- The bytes the view may cover: the first `radixProd · cosize` offsets of its buffer. -/
def bytes (v : View S) (σ : Sizes) : Set S.Loc :=
  {x | ∃ i : Nat, i < v.key.radixProd σ * (v.lay σ).cosize ∧ x = S.place v.buf i}

/-- The tiles of the instances of the view's level in `L`. -/
def tiles (v : View S) (σ : Sizes) (L : S.Launch) : {g // S.Inst L v.level g} → Set S.Loc :=
  fun g => v.tile σ L g.1

/-- A well-formed view, at every size assignment: its layout is congruent with positive
extents (so `Lay.call_lt_cosize` bounds its offsets), and at every instance of its level the
key is in range, its radices do not read the bound digits, and the digits tell the instances
apart. -/
structure WF (v : View S) : Prop where
  congruent : ∀ σ, congruentL (v.lay σ).shape (v.lay σ).stride = true
  pos : ∀ σ, ∀ s ∈ leavesL (v.lay σ).shape, 0 < s
  range : ∀ σ L g, S.Inst L v.level g → v.key.InRange (v.bind L g σ)
  radices : ∀ σ L g, S.Inst L v.level g →
    v.key.digits.map (fun p => p.2.eval (v.bind L g σ)) = v.key.digits.map (fun p => p.2.eval σ)
  key_inj : ∀ σ L g g', S.Inst L v.level g → S.Inst L v.level g' →
    v.key.digitVals (v.bind L g σ) = v.key.digitVals (v.bind L g' σ) → g = g'

/-- The key's `n` least significant digits, and the others. -/
def inner (v : View S) (n : Nat) : MixedRadix := ⟨v.key.digits.take n⟩
def outer (v : View S) (n : Nat) : MixedRadix := ⟨v.key.digits.drop n⟩

/-- Instance `j` of the view's level lies in instance `i` of `ℓ`: a thread of `L` is in both. -/
def Inside (v : View S) (L : S.Launch) (ℓ : S.Level) (i j : S.Gid) : Prop :=
  ∃ t ∈ S.threads L, S.group L v.level t = j ∧ S.group L ℓ t = i

/-- The view at level `ℓ`: the key's `n` least significant digits become one more layout mode,
of extent their radix product and stride the cosize; `bind'` binds the other digits at an
instance of `ℓ`. -/
def coarsen (v : View S) (ℓ : S.Level) (n : Nat) (bind' : S.Launch → S.Gid → Sizes → Sizes) :
    View S where
  lay σ := ⟨(v.lay σ).shape ++ [leaf ((v.inner n).radixProd σ) false],
    (v.lay σ).stride ++ [leaf (v.lay σ).cosize false]⟩
  level := ℓ
  buf := v.buf
  key := v.outer n
  bind := bind'

Statements

/-- For every machine whose placement is injective, well-formed view, size assignment and
launch, the tiles of the instances of the view's level are in bounds and pairwise disjoint. -/
theorem View.tiling (v : View S) (hp : PlaceInj S.place) (hv : v.WF) (σ : Sizes)
    (L : S.Launch) : Tiling (v.tiles σ L) (v.bytes σ)

/-- For every machine, well-formed view, level `ℓ`, digit count `n`, binding, size assignment,
launch and instance `i` of `ℓ`: when the instances inside `i` agree on the outer digits and
realize every inner key value, the coarsened view's tile of `i` is the union of their tiles. -/
theorem View.coarsen_tile (v : View S) (hv : v.WF) {ℓ : S.Level} (n : Nat)
    (bind' : S.Launch → S.Gid → Sizes → Sizes) {σ : Sizes} {L : S.Launch} {i : S.Gid}
    (hn : ∀ j, v.Inside L ℓ i j → (v.outer n).val (v.bind L j σ) = (v.outer n).val (bind' L i σ))
    (ho : ∀ k < (v.inner n).radixProd σ,
      ∃ j, v.Inside L ℓ i j ∧ (v.inner n).val (v.bind L j σ) = k) :
    (v.coarsen ℓ n bind').tile σ L i = ⋃ j ∈ {j | v.Inside L ℓ i j}, v.tile σ L j

Two changes to this module that they need are made (Status. Proved, with the tests warp_two, mutant_assign, warp_split and warp_split_37 above):

Proof plan

Where the statements depart from the design

Instances and tests planned

The layout algebra's move into the core

The laws the views reindex by (rowMajor_inj and Lay.compose_call in Layout/Laws.lean, Swizzle.call_call in Layout/Swizzle.lean, placement_call in Layout/Bits.lean, Swizzle.twoLevel_call in Layout/TwoLevel.lean) are mathematics about layouts. The move, one build of its own, with no aliases and no import-only files: