The tile layer
This document describes the tile layer of MojoProof/Tile/ (namespace MojoProof.Tile): a
small language of tile programs over tensor cells, its interpreter, and the theorems that
relate a kernel's compiled code to a tile program. The Lean statements are the reference; the
instances and the tests are in MojoProofTest/Tile.lean, MojoProofTest/TileNaive.lean,
MojoProofTest/TileGrade.lean, MojoProofTest/TileMatVec.lean and
MojoProofTest/TileLanes.lean.
1. What the layer is
- Programs. A tile program
Prog E αis a finite tree of events of a signatureE(a type of events and, for each event, the type of its answer):ret aends witha, andvis e kperforms the eventeand continues withkapplied to the answer. Every program is a finite tree, so every run ends; there is no loop construct, no coinduction and no library of effect handlers.Prog Eis a monad (Prog.bind). - Events (
TileE V, over cell valuesV):load b creads cellcof bufferb,store b c vwritesvthere,choose sasks for the number of a float choice at the sites,barrierends a phase, andub msgstops with undefined behavior. - Float sites are semantic. A
Siteis a thread, a cell, a float operation (FBinOp) and the term of a reduction it is for (0 for an element-wise operation). It never names a compiled position: the step at which a kernel's thread performs that operation is computed from the kernel's paths (siteStep?) and checked (it must be the same on every path that has the operation), and the oracle of the kernel translates to the tile oracle through it (oracle). - The interpreter (
run1,runAll) handles the events: faults (a load of a cell that was never set, andub, stop the run withSem.Fault.ub), float choices (the tile oracle's number for the site), memory (a cell map per buffer) with an access log (thread, buffer, cell, read or write, phase of every access) and phases (barrieradvances the phase number that the log records).runAllruns threads0toT - 1in turn.
The answer of a float choice is the raw oracle number, which the program decodes as the LLVM
semantics does: it delivers a NaN result at the number divided by 3 (Sem's deliver, the same
function the LLVM semantics applies to a float operation's result), and the remainder modulo 3
chooses a contraction (dotStep). So the tile oracle that a kernel's oracle gives is the
kernel's oracle at the site's thread and the computed step, unchanged, and a tile program and
the kernel deliver a NaN from the same number in the same way.
2. The value type
Prog's values are concrete: a cell holds a value of the type V (FloatLib's Model ft.fmt
for a float buffer), and the events answer concrete values. They are not symbolic terms.
The reason: a tile program then runs, so a program can be evaluated on concrete inputs by
decide +kernel or #eval, and a wrong program is refused by evaluation (section 5). The
generality is in the theorems instead: they quantify over every input tensor, every count and
every oracle. A symbolic value type would need a second evaluator of terms, next to the one that
already checks the compiled code (MojoProof/Sym/), and there is to be one semantics of LLVM.
3. What is proved
map2_run: for every float format, binary float operation with an IEEE operation (fbin), countnat most the lengthNof the tensorsxandy, tile oracle and numberTof threads, the element-wise programmap2 op n(threadk < nloads cellkof buffers 0 and 1, asks for the choice at site (k,k,op, 0) and stores the delivered result in cellkof buffer 2; the other threads do nothing) runs frominputs x y(buffers 0 and 1 holdxandy) without a fault, leaves buffers 0 and 1 as they were, and its buffer 2 holds at every cellk < n,k < Tthe operation onx_kandy_kdelivered at the site's number.map2_refines: for every kernel whose theorem is the conclusion ofmojo_kernel'smap2clause (Kernel.SoundforElem.Outwith the bytesElem.map2of a float operation at the oracle numbers of a steps, the result ofKernel.sound_map2), and every path list whose computed step for that operation iss, every observation of a complete run of the kernel (Kernel.Obs, every admitted address choice and oracleω) holds at the output cell of every threadk < n,k < Tthe value of cellkof buffer 2 of the run ofmap2 op nunder the translated oracleoracle paths ω. It covers every kernel of the clause; the kernel's oracle numbers enter only through the computed step.- The instances for
vector_addition,vector_multiplyandvector_subtraction(vadd_tile,vmul_tile,vsub_tileinMojoProofTest/Tile.lean) are generated by the test commandtile_map2, which states the theorem once and proves it bymap2_refinesapplied to the kernel theoremmojo_kernelgenerated, the step of the operation evaluated by the kernel; each covers that one kernel at every valid one-dimensional launch, alignment, memory, count, input and oracle of its theorem's premises. Each is also applied at two concrete launch sizes, 6 threads and 2 threads, with a memory and a pair of inputs. dot_run(MojoProof/Tile/Dot.lean): for every float format, contraction flagc, sizesm,p,KwithKat most the row lengthssAandsB, matricesA(m × sA) andB(p × sB), tile oracle, numberTof threads and placementrcof threads on cells with no two threads at one cell, the matrix productdot c K rc(threadtat cell(r, col)loads cells(r, j)of buffer 0 and(col, j)of buffer 1 forj < K, asks for thefmuland thefaddchoice of each term, foldsdotStepfrom+0and stores the result in cell(r, col)of buffer 2; cells of two coordinates are numbered byNat.pair) runs frommats A Bwithout a fault, leaves buffers 0 and 1 as they were, and holds at the cell of every placed threadt < TthedotLoopover the firstKpairs of rowrofAand rowcolofBat the thread's oracle numbers.dot_meets: every such cell meets TorchLean's product (Meets): when no round is fused, the word and entry(r, col)ofTensor.matmulof the firstKcolumns ofAand the transpose of the firstKcolumns ofBare the same word or both NaNs, and when the format is IEEE and the word is finite, it is withindotBoundof the real product, and when the format is IEEE it isGPUProof.ApproxatGPUProof.dotGradeof every exact row and column and grades at which the cell's inputs areGPUProof.Approx(meets_dotLoop, frommatmul_cols_nanEq,matmul_cols_errorandMojoProof.Sem.dotLoop_approx).mat_refines(MojoProof/Tile/Mat.lean): for every kernel that is sound (Kernel.Sound) forElem.Outat the cells(row * sC + column) * wwith the bytesmatBytes(the encoding of each in-bounds thread'sdotLoopat its oracle numberso k; the shape ofKernel.sound_mat's conclusion), every launch valid withzsides 1, layout, float type of stored sizew, flag, oracle numbers, sizes withp ≤ sCand matrices: in every observation of a complete run, the run ofdot c K (place L m p)(each in-bounds thread at its row and column) at the tile oraclepairOracle oends without a fault, and every cell in bounds and in the launch's window is computed by one thread whose output cell holds (Elem.word) the word at that cell of buffer 2, a word that meets the product. It covers every kernel with such a theorem.naive_tile(MojoProofTest/TileNaive.lean):mat_refinesat the theoremNaiveFam.soundof the naive matmul kernel's row-stride familyNaiveFam.prog sA sB sC, its bytes beingmatBytes's (bytes_eq). It covers the family at every row stride, every launch valid withzsides 1, every memory ofMat.Pre, sizes below2 ^ 31and everyK ≤ min sA sB; the compiled kernel is the family at the strides 768, 768 and 50257. The tile oracle ispairOracleof the kernel's oracle numbers at its loop's steps (NaiveSpec.o), which are taken from the kernel's specification, not computed from its paths assiteStep?does formap2.rowLoops_meetsAt,meets_holds,mat_holds(MojoProof/Tile/MatVec.lean): a matrix product whose second matrix has one row is a matrix-vector product. For every IEEE format, width and flag, the behaviorsrowLoops(outputiis adotLoopfrom+0over rowiand the vector) meetGPUProof.Spec.matVec GPUProof.matVecGradeat the uniform encodingmatVecEnc(GPUProof.MeetsAt); words that meet the product at the cells(i, 0)meet the spec's rule at the inputs read from the matrices (matIn); and so does every kernel ofmojo_kernel'smatclause whose second matrix has one row, at the cells(i, 0)of its tile run (docs/proofs/spec.md, step 9). Its instance isnaive_holds(MojoProofTest/TileNaive.lean): the naive matmul kernel with one column, at every launch valid withzsides 1 that covers the rows and column 0, every memory ofMat.Preand sizes below2 ^ 31.stride_approx,bfly_approx,groupDot_approx,groupDot_holds(MojoProof/Tile/MatVec.lean): for every IEEE format, flag and oracle sequences, loops over the pairs strided by the lane count and added along any tree over the lanes, every lane of a butterfly over2 ^ ksuch loops, and soMojoProof.Sem.groupDot k, areGPUProof.ApproxatGPUProof.laneGradeat the tree's depth (kfor the butterfly) and⌈n / W⌉pairs per lane;groupDot kper row meets the rule ofGPUProof.Spec.matVec (GPUProof.laneVecGrade (2 ^ k) k fun n => ⌈n / 2 ^ k⌉). They are about words, not about a kernel's run (docs/proofs/dot.md, statements 15-16).
4. Trust
The tile layer is not in the trusted base. Its programs and its interpreter are definitions
that a theorem relates to the LLVM semantics: a statement about a kernel holds of the kernel's
compiled code whether or not the tile program is the intended one, and the headline theorems
about kernels remain stated against Kernel.Obs and TorchLean tensors. A wrong tile program
makes map2_refines or mat_refines inapplicable (its premises fail), not a wrong theorem true.
5. Tests
MojoProofTest/Tile.lean: the three instances, each applied at 6 and at 2 threads; the
computed step of each kernel's operation (23, by decide +kernel); map2 .fadd run on
concrete inputs at two counts and two numbers of threads, its values and access log evaluated
by decide +kernel and its values by #eval; two
faults (a count past the inputs' length, and frem, which has no IEEE operation); a program
written with do; and the refusal of a wrong program: vector_addition's paths have no fsub
step (siteStep? is none), and the subtraction program's output at a concrete input differs
from the addition program's.
The matrix product: dot_meets at the 2 × 3 binary32 matrices tA and tB of
MojoProofTest/Matmul.lean with the placement by rows (byRows) at two sizes, all 3 columns
(meets_tA3) and the first 2 (meets_tA2); the run of dot false on them evaluated by
decide +kernel at both sizes (with all 3 columns the cells are TorchLean's tA tBᵀ) and by
#eval; a count past the matrices' columns, which faults; and the placement by rows.
MojoProofTest/TileNaive.lean: naive_tile at the compiled kernel's strides, one launch of
2 × 2 threads and memory at two sizes, K = 768 and K = 4, its graded clause at K = 768, and
that launch's placement evaluated; naive_holds, mat_holds at the family with one column. MojoProofTest/TileGrade.lean: the graded clause at the inputs' own values
(meets_exact), at sizes 3 × 4 and 5 × 7 over generated binary32 matrices whose entries'
finiteness is evaluated by the kernel, and a grade one ulp tighter refused at a cell of a run
(tile_mutant). MojoProofTest/TileMatVec.lean: the product by rows with one column meets the
matrix-vector spec's rule at sizes 3 × 4 and 5 × 7 (holds_3x4, holds_5x7), and the spec graded
one ulp tighter is not met (rowLoops_mutant). MojoProofTest/TileLanes.lean: the strided lanes
at 3 lanes and length 7 along a tree of depth 2, the butterfly at 4 lanes and length 10 (lane 0
and lane 3, whose trees are evaluated), their words computed by the kernel, and the grade at
depth 0 refused on four binary32 pairs (depth_mutant).
6. Scope and what is not done
- Element-wise programs of one binary float operation per thread and the matrix product of one
dot loop per cell are related to kernels. Phases across threads, shared memory and collectives
are not;
barrieris handled only as a phase number in the log. - The steps of a loop's float operations are not computed from a kernel's paths:
mat_refinestakes the kernel's oracle numbers per term (o) as they appear in its theorem, and only one kernel, the naive matmul, is an instance. - Cells are numbered within a buffer; coordinates through a layout of the layout model are not used yet.