The precondition of a binary elementwise kernel, and its satisfiability
Lean: MojoProof/Sem/Precond.lean; instances and twins in MojoProofTest/Precond.lean.
Scope. The codecs, Mem.Closed.alloc, Map2.Pre and Map2.pre_mem are for every layout, element
type with a codec, length, inputs, element count, alignment and address choice the pointer widths
admit. pre_starts (the precondition gives the start and W1) is for one program, vector_addition
(Kernels.vadd), and every memory meeting the precondition, launch, address choice and oracle.
byval_codec and pre5 are one layout (vadd's) and one memory. Mem.allocs and
Mem.allocs_spec are for every number of blocks; Mem.three, Mem.three_spec,
Mat.Pre and Mat.pre_mem (the matmul precondition) and the store forms Mem.store_blocks,
Mem.Buffer.store, Mem.Fits.store and Mem.Holds.store are for every layout, element type,
size, stride and store; preN and its twins are one layout (the naive matmul kernel's) and one
memory.
Statement
Map2.Pre l t enc al z m a b o n x y, for tensors x y : Tensor α [N]:
closed:mis closed (Mem.Closed) under the argumentsMap2.args z a b o n, which are three structures.agg [.ptr ⟨c, 0⟩, z]forc = a, b, oand the i32n;ne:a,bandoare distinct;bufs: each is a live global buffer (Mem.Buffer: space 1, owned by all threads) of at leastn * l.size tbytes at addresses in space 1 and in the generic space thataldivides;aandbare read-only andois writable;x,y:aholdsxandbholdsyrow-major from offset 0 (Mem.Holds);le,lt:n ≤ Nandn < 2 ^ 31;fits: the firstn * l.size tbytes of each ofa,bandohave generic addresses below2 ^ w,wthe pointer width of address space 0 (Mem.Fits l c (n * l.size t): the block'sgaddrplus that size is at most2 ^ w).
Why each clause is there
closed:Grid.startchecks the caller's memory (Mem.checkClosed), andMem.Closedpasses the check (Mem.checkClosed_of_closed); it also givesm.BytesIn m.Known(Mem.Closed.bytesIn), the premise ofGrid.start_reach_closed(W1 from the start) and ofGrid.owned_sound.ne: an output that overlaps an input makes the kernel an in-place update.alias_twinshows the start does not see it.bufs: the kernel's loads and stores go throughMem.check(live, owner, bounds, alignment).x,y: the observation of the inputs (Mem.tensor_eq) and the values the loads give (Mem.load_holds).le,lt: the guardicmp slt i, ncompares the i32 count as a signed number, which isnonly below2 ^ 31.fits:vector_addition'sgetelementptr inboundsinto a buffer ispoisonwhen the address wraps2 ^ 64, and the load through it then faults.Mem.BufferandMem.Closedallow such a block; withMem.Fits,addr_buffer(MojoProof/Sym/OblLemmas.lean) gives the addresses without wrap that the geps need.
The byval arguments
The caller passes values, not blocks: launchArgs (MojoProof/Sem/Thread.lean) encodes each value at
its byval type into a new block per thread, and the kernel loads { ptr, { { {} }, { {} } } } back
from it. So the kernel needs decode after encode to be the identity at that type.
Layout.ptrOf_range: for every pointerpandn > 0,ptrOfof.ptr p 0, ..., .ptr p (n - 1)isp. Proof: the list starts with.ptr p 0, contains nopoisonorundef, and is the listptrOfcompares it with.Layout.Codec.ptr: for every layout and address space whose pointers have at least one byte,Val.ptris a codec.encodegives those bytes, of lengthstoreSize, andptrOf_rangedecodes them.Layout.Codec.ptrArg: for every layout, address space as above, and typeuof size 0 whose valuezis defined, encodes to[]and decodes from[],p ↦ .agg [.ptr p, z]is a codec for.struct [.ptr a, u]. Proof: the field offsets are 0 (roundUp_zero) andu's stored size is 0 (stored_le_size), so the encoding is the pointer's bytes followed by padding; the first slice is the pointer's bytes (List.take_left'), the second is empty, anddecodeFieldsgives the structure back. Atvadd's layout and{ { {} }, { {} } }the premises hold by evaluation (byval_codec).
Satisfiability
Map2.mem builds a memory from {} by three Mem.alloc: blocks 1 and 2 hold tensorBytes of x
and y (read-only), block 3 has no undefined elements (writable). Map2.pre_mem: if the codec's
values name no block, n ≤ N, n < 2 ^ 31, n ≤ no, the addresses A i * al are below
2 ^ w₁ (hA, w₁ the pointer width of space 1), G i * al + n * l.size t ≤ 2 ^ w₀ (hG, w₀
the generic space's: room for the n elements below 2 ^ w₀), and z names no block, then Map2.mem meets Map2.Pre at blocks 1, 2
and 3.
Proof.
closed:Mem.Closed.allocthree times fromMem.closed_empty. An allocation at the next number whose initial bytes name numbers belownextkeeps a memory closed under every list of values naming numbers up to the new block's:posandnullsincenextstays positive and block 0 is not allocated,abovesince the new block is at the oldnext, andbytesfrom the initial bytes (mem_of_getD) or the old memory. The tensors' bytes name no block (tensorBytes_namesIn, fromencode_namesIn), and the arguments name blocks 1, 2 and 3 only (Val.boundsis 4).ne: by evaluation.bufs: the fields areMem.alloc's; the sizes fromn ≤ Nandn ≤ no; the addresses areA i * alandG i * alreduced modulo the pointer width. That leavesA i * alunchanged, andG i * altoo unless it is2 ^ w₀(only whenn * l.size t = 0), where it becomes 0, whichaldivides as well.fits: the generic address isG i * al % 2 ^ w₀ ≤ G i * al(Nat.mod_le), sohGbounds it plusn * l.size t.x,y:Mem.holds_alloc.
Instance and twins (MojoProofTest/Precond.lean)
byval_codec:Codec.ptrArgatvadd's layout andbyvaltype.pre5: binary32 inputs of length 5 (x5,y5),n = 5, alignment 16, blocki + 1at address16 (i + 1)in both spaces (sohGis16 (i + 1) + 20 ≤ 2 ^ 64, by evaluation);memXmeetsMap2.Pre,fitsincluded.pre_starts: for every memory meetingMap2.Prewithvadd's tail value, every launch, address choice and oracle,vaddstarts onMap2.args(SymLaunch.vadd_starts), and every grid reachable from the start is closed (Grid.start_reach_closed).start5is it atmemX.- Twins: the output at block 1, the first input:
memXis closed for the arguments andvaddstarts, butMap2.Prefails (alias_twin); the output at block 7, whichmemXdoes not have:memXis not closed for the arguments (missing_twin);memW,memXwith every block's generic address16 (2 ^ 60 - 1): block 1 is an input buffer of 20 bytes (Mem.Buffer) but does not fit below2 ^ 64(¬ Mem.Fits), andpre_mem'shGfails at that address (fits_twin).
The precondition of a matmul kernel
C[r][c] = Σₖ<K A[r][k] B[c][k] for r < m, c < n, over row-major buffers with row strides
sA, sB, sC. The naive kernel (matmul_kernel_naive, tests/corpus/mgemm/) takes C, A, B
as three byval structures { ptr, { { {}, {} }, { {}, {} } } }, then the i32 m, n, K; it
loads A at row * 768 + k, B at col * 768 + k and stores C at row * 50257 + col, so
there sA = sB = 768 and sC = 50257.
Mat.args z o a b m n K is [.agg [.ptr ⟨o, 0⟩, z], .agg [.ptr ⟨a, 0⟩, z], .agg [.ptr ⟨b, 0⟩, z]]
followed by the i32 words of m, n and K: the output first, as the kernel takes it.
Mat.Pre l t enc al z mem o a b m n K sA sB sC x y, for x : Tensor α [m, sA] and
y : Tensor α [n, sB]:
closed:memis closed underMat.args z o a b m n K; somem.BytesIn mem.Knownisclosed.bytesIn(Mem.Closed.bytesIn), asKernel.sound_map2gets it fromMap2.Pre;ne:o,aandbare distinct, so the output overlaps no input;bufs:aandbare read-only andowritable live global buffers (Mem.Buffer) ofm * sA,n * sBandm * sCelements (timesl.size tbytes) at addressesaldivides;x,y:aholdsxandbholdsyrow-major from offset 0 (Mem.Holds);le:n ≤ sC,K ≤ sAandK ≤ sB: a row ofChas room forncells and a row ofAorBforKelements;lt:m * sC,m * sAandn * sBare below2 ^ 31, so the kernel's i32 productsrow * sC + col,row * sA + kandcol * sB + kdo not wrap (NaiveSpec.At.dimswith the strides as parameters);fits: the three buffers' bytes have generic addresses below2 ^ w(Mem.Fits).
At sA = sB = 768, sC = 50257, alignment 4 and binary32, bufs, x, y, le and lt are
the buffer facts and dims of NaiveSpec.At on the caller's memory.
Map2.Pre is not Mat.Pre at m = 1: Map2.args lists the two inputs, then the output, then
one i32 count, and Map2.Pre's tensors are vectors read at the same index. The two share their
three allocations, Mem.three_spec below.
Satisfiability. Mem.allocs l al k S R I A G is the memory of k live global allocations
owned by all threads from {}, for every k: block i + 1 (i : Fin k) of S i bytes,
read-only exactly if R i, whose first bytes are I i, at addresses A i * al and G i * al.
Mem.allocs_spec: if no I i holds a pointer byte, n i ≤ S i, A i * al < 2 ^ w₁ and
G i * al + n i ≤ 2 ^ w₀, then the memory is closed under every list of values naming numbers
up to k (Val.bounds vs ≤ k + 1), and block i + 1 is a Mem.Buffer of n i bytes, writable
exactly if not R i, that fits (Mem.Fits). Proof: by induction on k; the memory's next is
k + 1, so Mem.Closed.alloc extends the closed memory and the last block is the new one, while
the earlier blocks carry over (Mem.Buffer.of_block, Mem.Fits.of_block). Mem.three is
Mem.allocs at k = 3 unrolled, and Mem.three_spec is Mem.allocs_spec at 3. Map2.pre_mem
is Mem.three_spec at Map2.mem (which is Mem.three by unfolding) plus Mem.holds_alloc
twice. A second size: allocs5 (MojoProofTest/Precond.lean), five blocks of 20 bytes.
Mat.mem l t enc al x y sC A G is Mem.three with block 1 the output (m * sC undefined
elements, writable), blocks 2 and 3 the bytes of x and y (read-only). Mat.pre_mem: if the
codec's values name no block, n ≤ sC ∧ K ≤ sA ∧ K ≤ sB, the three products are below 2 ^ 31,
A i * al < 2 ^ w₁, G i * al plus the size of block i + 1 is at most 2 ^ w₀, and z names
no block, then Mat.mem meets Mat.Pre at o = 1, a = 2, b = 3. Proof: Mem.three_spec,
Mem.holds_alloc for x and y, and the arguments name blocks 1 to 3 only.
Instance and twins (MojoProofTest/Precond.lean). preN: the naive kernel's layout, binary32,
alignment 4, strides 768, 768, 50257, m = 2, n = 3, K = 5, inputs whose cells differ
(xN, yN), block i + 1 at address 4 (i + 1). overlap_twin: with the output at block 2,
the input A, the memory is closed for the arguments but Mat.Pre fails. absent_twin: with
the output at block 7, which the memory does not have, Mat.Pre fails (the memory is not closed for
the arguments).
Stores and logs at other blocks
Mem.log changes only the trace, so (m.log e).blocks = m.blocks by definition, and Mem.Buffer,
Mem.Fits and Mem.Holds of m.log e are those of m. Mem.store_blocks: a store that succeeds
at a pointer into block p.block ≠ q keeps m.blocks q (Mem.store_inv: the store is a
Mem.set at p.block and a log). So Mem.Buffer, Mem.Fits and Mem.Holds at q carry over
(Mem.Buffer.store, Mem.Fits.store, Mem.Holds.store). store_x (MojoProofTest/Precond.lean):
a store of a float at block 3 of memX succeeds and keeps pre5.x, the first input at block 1.
Not covered
- The kernel's run: it is not in this file. The kernel theorem
vadd.correct(MojoProof/Kernels/Vadd.lean, kernel.md) composesMap2.Prewith the start and the block summaries. - Inputs that alias each other:
a = bis excluded byne, thoughvaddwould compute the right result on it. - Layouts with a non-empty runtime part (a dynamic
LayoutTensor):Codec.ptrArgneedsuof size 0.