Docs / Proofs

Written tiles give the tensor (Block.holds_of_tiles)

Lean: proved, in MojoProof/Sem/Buffer.lean: stride_lt, Apart, Apart.comp, apart_stride, apart_rowMajor, rowMajor_inb, Block.holds_of_tiles, Block.read_cells_none, Block.read_cells, Block.read_puts, Block.cells_bytes, Block.holds_cells, Block.holds_puts; in MojoProof/Sem/Trace.lean: Tiling.ofApart, Tiling.strided. Instances: MojoProofTest/Tiles.lean and the vector_addition check in MojoProofTest/Eval.lean.

Where the Lean differs from the statements below.

Scope. Every layout l, type t, codec enc, shape s, offset map off, coordinate function f and block. It is a statement about bytes, not about a program. A kernel is covered once a run's final output block meets the premise hw. Showing that (each tile's last write is its owner's, with the bytes of enc (f i)) is the summary's job (T3, T4), and it is not part of this proof.

Statement

Data.

theorem Block.holds_of_tiles {blk : Block} (hc : l.Codec t enc) {f : Shape.Coord s → α}
    (hin : ∀ i, off i + l.stored t ≤ blk.size)
    (hw : ∀ i, l.encode t (enc (f i)) = some (blk.read (off i) (l.stored t))) :
    blk.Holds l t enc off (Tensor.Rep.ofFn f)

The premise is per tile and does not depend on order: the bytes at tile i are the encoding of enc (f i). The form "the final block equals the initial block with enc (f i) written at tile i for every i" is the corollary Block.holds_puts below. That corollary is where the tiles must be apart.

Corollaries.

  1. Writes in any order (Block.holds_puts). Let hap : Apart off (l.stored t), and let hbs : ∀ i, l.encode t (enc (f i)) = some (bs i). Let is : List (Shape.Coord s) contain every coordinate, in any order and with repetitions allowed. Then (is.foldl (fun b i => b.put (off i) (bs i)) blk).Holds l t enc off (Tensor.Rep.ofFn f), given hin for blk.
  2. Frame (Block.puts_frame). If byte o lies outside every tile (∀ i ∈ is, o < off i ∨ off i + l.stored t ≤ o), then (is.foldl …).bytes o = blk.bytes o.
  3. Row-major (Block.holds_rowMajor). Corollary 1 with off := rowMajor l t base and hap := apart_rowMajor l t base. hin follows from base + Shape.size s * l.size t ≤ blk.size, because l.stored t ≤ l.size t (stored_le_size) and linearize i < Shape.size s.
  4. The tiling for ownership (Tiling.ofApart). Offsets that are n apart and in bounds form a Tiling ι with len _ := n: disj is Apart, and inb is the bound. With rowMajor, this is the T that Owned (MojoProof/Sem/Trace.lean) needs for an output tensor. It connects the written tiles to race freedom by ownership: the tiles of ownership are the layout's.
    • No duplicate. Tiling.strided n w (Trace.lean, used by racy_kept_pointwise) is the same tiling for ι := Fin n, and its disj would repeat the arithmetic of apart_rowMajor. So:
      • Apart is stated for ι → Nat, not only Shape.Coord s → Nat (its definition uses nothing of the shape);
      • the arithmetic is proved once, as apart_stride : n ≤ w → Apart (fun k : Fin m => base + k * w) n, with Apart.comp : Apart off n → Function.Injective g → Apart (off ∘ g) n;
      • apart_rowMajor is apart_stride composed with linearize (linearize_injective);
      • Tiling.strided's disjointness is apart_stride composed with Fin.val.
    • Imports. Tiling and Tiling.ofApart are in Trace.lean, which imports Buffer.lean, where Apart is.

Proof

Block.holds_of_tiles. Fix i. The bounds conjunct is hin i. By hc.round (f i) there are bytes bs with l.encode t (enc (f i)) = some bs, bs.length = l.stored t and l.decode t bs = some (enc (f i)). By hw i, bs = blk.read (off i) (l.stored t), so the read decodes to enc (f i), which is enc ((Tensor.Rep.ofFn f) i) by Tensor.Rep.get_ofFn.

Block.holds_cells, and so Block.holds_puts.

Block.puts_frame. By induction on is: Block.put at off j changes only the bytes from off j to off j + (bs j).length, and (bs j).length = l.stored t.

Block.holds_rowMajor is substitution. Tiling.ofApart is a definition. Its two fields are the premises.

apart_stride. For k ≠ k', take k < k'. Then base + k * w + n ≤ base + (k + 1) * w ≤ base + k' * w, by n ≤ w. This is the arithmetic apart_rowMajor and Tiling.strided each do (Nat.mul_le_mul_right, Nat.succ_mul, omega). Apart.comp. g i ≠ g j follows from i ≠ j by injectivity.

Lemmas used

All are proved, in MojoProof/Sem/Buffer.lean unless marked:

Tensor.Rep.ofFn needs [Storage α]. Buffer.lean has variable [Storage α], and TorchLean's instArrayStorage (low priority, every type) covers BitVec 32 and the FloatLib floats.

Dependencies

Instances and twins

All three are written: the first two are theorems in MojoProofTest/Tiles.lean (holds_both, swapped, and holds, for every tensor of shape [2] and every order listing both coordinates; also tiling, the row-major tiles as a Tiling.ofApart). The third is vadd (written tiles) in MojoProofTest/Eval.lean (runF32 with tiles), with the negative twin vadd (swapped tiles): checked against the reference of the neighboring element (i ^^^ 1), tile 0 fails.