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.
- The writes are proved once, through a cell map: writers
k : ι(a listks, in order, with repetitions), each writing its bytesbs kat tileoff jforcell k = some jand nothing forcell k = none. A tile with one writer inksreads that writer's bytes (Block.read_cells), a tile with none its own (Block.read_cells_none); the writes change only the bytes (Block.cells_bytes); and when the writers cover every tile, the block holds the tensor of a writer's value at each tile (Block.holds_cells).Block.read_putsandBlock.holds_putsare the cell mapsome. The threads of a round that each store at their cell (Turns.cells, MojoProof/Sym/SegTrace.lean) are the general case. Block.read_putsis stated for every index typeιwith decidable equality and everyn(bytesbs iof lengthnat offsetsnapart), not only for coordinates andl.stored t. For a tile not in the list it is the frame, so corollary 2 (Block.puts_frame, a byte frame) is not stated: nothing uses it.- Corollary 3 (
Block.holds_rowMajor) is not a theorem. Its two premises are the lemmasapart_rowMajorandrowMajor_inb(rowMajor l t base i + l.stored t ≤ base + Shape.size s * l.size t), and the instance appliesBlock.holds_putsto them directly (MojoProofTest/Tiles.lean,holds). - The stride arithmetic is one lemma,
stride_lt : a < b → a * w + w ≤ b * w.apart_strideis stated forNatindices;apart_rowMajorisapart_stridecomposed withlinearize, andTiling.strided's disjointness isapart_stridecomposed withFin.val.rowMajor_inb,Tiling.strided's bound and the bound insideMem.holds_allocalso usestride_lt, so the arithmetic is proved once. Tiling.stridedtakes a base offset (Tiling.strided base n w);racy_kept_pointwiseuses base 0.
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.
Block.Holds l t enc off x blk: for every coordinatei, thel.stored tbytes ofblkfromoff ilie in the block and decode toenc (x i)(MojoProof/Sem/Buffer.lean).Layout.Codec l t enc:encis injective, and eachenc aencodes tol.stored tbytes that decode back to it (Codec.round).Tensor.Rep.ofFn f: the TorchLean tensor withx i = f i(Tensor.Rep.get_ofFn).
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.
- Writes in any order (
Block.holds_puts). Lethap : Apart off (l.stored t), and lethbs : ∀ i, l.encode t (enc (f i)) = some (bs i). Letis : 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), givenhinforblk. - Frame (
Block.puts_frame). If byteolies outside every tile (∀ i ∈ is, o < off i ∨ off i + l.stored t ≤ o), then(is.foldl …).bytes o = blk.bytes o. - Row-major (
Block.holds_rowMajor). Corollary 1 withoff := rowMajor l t baseandhap := apart_rowMajor l t base.hinfollows frombase + Shape.size s * l.size t ≤ blk.size, becausel.stored t ≤ l.size t(stored_le_size) andlinearize i < Shape.size s. - The tiling for ownership (
Tiling.ofApart). Offsets that arenapart and in bounds form aTiling ιwithlen _ := n:disjisApart, andinbis the bound. WithrowMajor, this is theTthatOwned(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 byracy_kept_pointwise) is the same tiling forι := Fin n, and itsdisjwould repeat the arithmetic ofapart_rowMajor. So:Apartis stated forι → Nat, not onlyShape.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, withApart.comp : Apart off n → Function.Injective g → Apart (off ∘ g) n; apart_rowMajorisapart_stridecomposed withlinearize(linearize_injective);Tiling.strided's disjointness isapart_stridecomposed withFin.val.
- Imports.
TilingandTiling.ofApartare in Trace.lean, which imports Buffer.lean, whereApartis.
- No duplicate.
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.
- Lemma (
read_cells_none), by induction onks, generalizingblk: if nokofkshascell k = some i, the fold keepsblk.read (off i) n. Fork :: ks, the induction hypothesis at the block afterk's write leaves that write; it is nothing forcell k = none, and forcell k = some jit is atoff jwithj ≠ i, apart fromoff ibyhap i j(Block.read_put_apart). - Lemma (
read_cells), by induction onks: ifcell k = some i,k ∈ ks, and everyk'ofkswithcell k' = some iisk, the fold readsbs katoff i. Fork' :: ks: ifk ∈ ks, the induction hypothesis. Otherwisek' = k; no element ofksmaps toi, soread_cells_noneleavesk's write, which readsbs kbyBlock.read_put_self, as(bs k).length = n. Block.read_putsis the two lemmas atcell := some, split oni ∈ is.Block.holds_cells: choose a writerwr i ∈ kswithcell (wr i) = some ifor every tile. The writes keepsize(cells_bytes), sohinholds for the folded block;read_cellsatwr igiveshwwithhbs, the lengthsl.stored tfromhbsandhc.round; andBlock.holds_of_tilesapplies atf i := val (wr i).Block.holds_putsisBlock.holds_cellsatcell := someandval := f: therewr i = i.
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:
Layout.Codec.round(Buffer.lean),Block.read_put_self,Block.read_put_apart,Layout.stored_le_size;Tensor.Rep.get_ofFn(TorchLean/Tensor/Rep.lean:340);Shape.Coord.linearize_injective(TorchLean/Tensor/Shape/Coord.lean:161).
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
Block.Holds,Layout.Codec,Apart,rowMajor,apart_rowMajor(MojoProof/Sem/Buffer.lean);Block.read,Block.put(MojoProof/Sem/Value.lean).Block.putchanges onlybytes, so it keepssize;Tiling,Tiling.strided,Owned(MojoProof/Sem/Trace.lean), for corollary 4;Tensor.Rep,Tensor.Rep.ofFn,Shape.Coord,Shape.Coord.linearize(TorchLean).
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.
- Two
i32elements. UseCodec.int l 32, shape[2],off := rowMajor l (.int 32) 0, a layout withl.size (.int 32) = 4, and an 8-byte block. Letfbe 5 at coordinate 0 and 7 at coordinate 1, asBitVec 32. The encodings areleBytes 4 5andleBytes 4 7(Codec.int'sround, with(32 + 7) / 8 = 4). Writing them at 0 and 4, in either order, gives a block that holdsofFn f. This is corollary 3, with both orders listed. - Its negative twin. Write the two encodings at swapped tiles, 7 at 0 and 5 at 4. That block holds
ofFn f', wheref'is 7 at coordinate 0 and 5 at 1, by corollary 3. So it does not holdofFn f:Block.holds_inj(Buffer.lean) would makeofFn f' = ofFn f, andget_ofFnat coordinate 0 gives 7 = 5. This is the tilingoff 0 = 4,off 1 = 0(the counterexample with tiles at offsets 4 and 0). The theorem does not exclude it. The theorem names the tensor the block does hold, at the offsets the premise uses. vector_addition, one launch (evaluated).runF32(MojoProofTest/Eval.lean) reads the output of the launch by oneMem.loadof the whole array.check(withisF32) compares each element with FloatLib'sModel.addof the inputs, bit for bit, and accepts any NaN when the sum is a NaN.- The written-tiles check runs on the same final block:
hwwithCodec.float l .float,rowMajorfrom 0, andf ithe float decoded at tilei. For eachi,f imust also passisF32againstModel.addof the inputs. fis read off the block, not given asModel.add, because NaN payloads may differ.- It covers one launch, not the kernel.
- The written-tiles check runs on the same final block: