Docs / Proofs

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]:

Why each clause is there

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.

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.

Instance and twins (MojoProofTest/Precond.lean)

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]:

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