Docs

mojo_kernel: the kernel theorem of an imported GPU kernel

mojo_kernel (MojoProof/Sym/KernelCmd.lean) takes a program imported from LLVM IR, one of its kernels, and a clause for each of the kernel's parameters. It checks the kernel's blocks and paths, and declares theorems about that one kernel. With the map2 clause, for a binary elementwise kernel such as out[i] = x[i] + y[i], the last of them, n.correct, says what the kernel computes at every valid one-dimensional launch (grid and block of side 1 in y and z) and every input that meets the precondition. With the mat clause, for a matmul kernel, it declares n.sound_mat, the kernel theorem at every launch with side 1 in z, whose premises are the memory's and the run of a thread in bounds, which you prove; the run of a thread out of bounds is generated. Nothing the command produces is trusted: every theorem it declares is checked by Lean's kernel like any other.

This guide covers what the command needs, its clauses, what it declares, what the theorems say and for which kernels, what it refuses and why, and one worked example, vector_addition (MojoProof/Kernels/Vadd.lean). For a first kernel, start with docs/new-kernel.md, which takes vector_subtraction (MojoProof/Kernels/Vsub.lean) from Mojo source to the kernel theorem step by step. The proofs behind the command are explained in docs/proofs/kernel.md. The section "The library behind the command" at the end lists the definitions and theorems the generated theorems are built from.

What the command needs

The command and its clauses

mojo_kernel n := P fn
  reads x := a0, y := a1
  writes out := a2
  scalars count := a3
  map2 t e bytes

n is the name under which the declarations are made, in the current namespace. P and fn are terms (written with parentheses if they are not single identifiers). Parameter a<i> is the kernel's parameter number i, counted from 0. The name on the left of := is the binder name that the generated theorems give the block number or the value of that parameter.

Each parameter must be named by exactly one clause.

The loop clause

mojo_kernel n := P fn
  loop h := pre inv trips post by tac

The clause comes after the others. h is the header's block number in fn.blocks (not the entry block 0); the clause names no SSA value. The four terms are:

by tac is optional; without it the tactic is decide +kernel, which closes few obligations. The command runs tac on each of four obligations, at every valuation V of pre:

Obligation Statement
n.entry the path into the loop has its premises and its last edge enters the header at the header's position (Edge)
n.init inv V 0 holds of the header's valuation after that path (Valuation.round)
n.step for every i < trips V and W of inv V i, the round from W has its premises and goes back to the header (Round), and inv V (i + 1) holds after it
n.exit for every W of inv V (trips V), the path out from W runs to ret with a memory of post V (Ret)

The tactic sees one obligation at a time and introduces its own names, so it typically starts with intro V hV and uses first to pick the lemma for each shape. If it leaves any obligation, the command fails, names each one it leaves (n.entry (the path into the loop), …) and adds nothing. MojoProofTest/KernelCmd.lean's cnt_open checks this message. The four terms are elaborated in the generated statements, each at its type (Valuation → Prop, Valuation → Nat → Valuation → Prop, Valuation → Nat, Valuation → Mem → Prop), so a term may use field notation on a binder without a type, as in fun V i W => inv V.ctx V.mem i W (MojoProofTest/KernelContract.lean's kfK).

Loops in sequence

mojo_kernel n := P fn
  loop h₁ := pre₁ inv₁ trips₁ post₁ by tac₁
  loop h₂ := pre₂ inv₂ trips₂ post₂ by tac₂

With a clause for each of several loops, in the order a thread runs them, the paths end at a branch into any of the headers. The command needs one path from the entry block into the first loop and, for each loop, one round and one path out, into the next clause's loop or, after the last, to ret; no loop is nested in another. n.chains holds one LoopSeg per loop. Each loop's obligations are those above, named with its header (n.entry4, n.init4, …) and proved by its clause's tactic, with two changes: a loop's path in is the previous loop's path out, and the exit of each loop but the last says that every valuation W of inv V (trips V) meets the next clause's precondition, and that every memory of the next clause's postcondition at W is one of post V. n.loop runs ChainWith.loop for each loop in turn, from the first clause's precondition to its postcondition, and n.sound_loop (on n.args) takes the first clause's precondition and postcondition. With one clause, the names, the statements and the messages are as above. MojoProofTest/KernelLoop.lean's cnt2k runs two clauses on SymLoop's cnt2, and cnt2_open checks the message for the obligations of the second loop that its tactic leaves.

Loops with fmul contract

mojo_kernel n := P fn
  contract
  loop h := pre inv trips post by tac

The flag contract comes before the loop clauses, if any, and puts fmul contract in the block checker's fragment: the summaries are summarizeC's, so a fadd contract that reads a recorded fmul contract product fuses with it or not by the thread's oracle. The checks are coversEntryWith true and coversChainsWith true, the obligations are over EdgeWith true, Valuation.roundWith true, RoundWith true and RetWith true (their names and order are those above), n.loop is a RetLsWith true, n.sound_of is Kernel.sound_of_chainsWith at true and n.sound_loop is Kernel.sound_entryWith at true. The per-thread premise of n.sound_loop also takes an admitted address choice and oracle and gives the thread's oracle (V.ctx.oracle = oracle k), since a fused operation's rounding depends on it. A loop-free kernel takes the flag with no loop clause: n.sound_of is then Kernel.sound_of_chainsWith at true, n.covers and no sequence of loops (its cover is rfl). With map2 the flag gives n.ret as a RetWith true (the mojo_ret proof of a Ret, moved by RetWith.exists_true_of_ret of MojoProof/Sym/RetFlag.lean, which holds when no summary of the paths records an fmul contract product, decided on them) and n.correct by Kernel.sound_map2With (contract := true) (MojoProofTest/KernelContract.lean's map2_contract). Without the flag, a kernel with fmul contract is refused at the block that has it. With mat the flag is optional ("The matmul clause mat" below). MojoProofTest/KernelContract.lean's kfK is the flag on its one-loop kernel kf, on a launch of six threads. Its pre (preF) is the start's facts of some thread with a memory of the invariant I (memF, the memory after that many threads from a memory with the caller's blocks), so every admitted thread's facts give it, and kernelK is kfK.sound_loop with every premise closed. Its twins are the clause without the flag, the flag without a loop clause (refused at the loop, which has no clause), and the message of a tactic that leaves two obligations; an open-goal test checks kfK.sound_loop's per-thread premise. The module's kgK is the flag on the loop-free kernel kg, with no loop clause; its twin is kg without the flag.

The matmul clause mat

mojo_kernel naive := naive.prog naive.fn_0 reads bA := a1, bB := a2 writes bC := a0
  scalars m := a3, n := a4, K := a5 contract mat [skipN] [chainN]

mat paths chains is for a kernel whose parameters are the output a0 and the inputs a1, a2, passed byval as one struct type of a pointer and a tail with a zero value, then the i32 sizes m, n, K (a3 to a5), with parameter j named %j: the naive matmul's shape. Here paths : List (List (Nat × List Cond × Summary)) are the kernel's loop-free paths from the entry block and chains : List LoopChain its sequences of loops, both terms; the command checks them with the path checker (coversEntryWith contract and coversChainsWith contract) by evaluation before it adds anything, so a wrong path or chain is refused. contract is optional and sets the flag of the checks and the runs. mat is not combined with map2 or loop. The clauses must be reads of a1, a2, writes of a0 and scalars of a3, a4, a5, and their names are the binder names of the blocks and sizes in the theorems, so none may be one of their other binder names (matBinders: L, t, x, k, c, f, h, Hin, …).

The clause declares four theorems and nothing else: n.names (parameter j has register name j, by decide +kernel), n.starts (the start on Mat.args z o a b m n K of MojoProof/Sem/Precond.lean succeeds, with z the zero tail), n.out and n.sound_mat.

n.out is the run of a thread out of bounds: from the facts the start gives the thread (Mat.Entry, MojoProof/Sym/KernelMat.lean), the sizes m, n, K below 2 ^ 31 and bytes bs that are none at every thread out of bounds, the thread takes some path of paths to ret (RetWith) with a memory of Elem after one more thread. Its proof tries each path in order: RetWith.of_first (the path's later blocks have no effects and its last returns, by decide +kernel), the walk's conditions, the branch out of bounds among them, by mojo_obl at the thread's row and column, and the memory by mojo_ret. If no path is discharged, the command is refused (mat: n.out does not elaborate: no path of the clause is discharged …).

n.sound_mat is Kernel.sound_mat (MojoProof/Sym/KernelMat.lean) at fn, the paths and the chains, with every premise that depends only on the program discharged: the coverage by decide +kernel, the parameters by rfl, the names by n.names, the struct's codec by Layout.Codec.ptrArg, the alignment by decide, withAttrs of the sizes by rfl, the start by n.starts, the run of a thread out of bounds, Hout, by n.out. Its premises are the launch's (valid, side 1 in z), the memory's (fields of Mat.Pre, which gives all of them; a ≠ b and K's bounds are not needed), n.out's two (the sizes below 2 ^ 31, the bytes none out of bounds) and one you prove: Hin, every thread in bounds runs a chain of chains to ret (RetLsWith) from the facts Mat.Entry, to Elem after one more thread at the 2-D cells (row * sC + column) * w. Its binders have the names of the clauses and of Kernel.sound_mat's, so named arguments such as (t := .fp .float), (w := 4) or (Hin := …) work. The conclusion is Kernel.Sound on Mat.args with Elem.Out at those cells.

The command does not find the run of a thread in bounds: a matmul thread's run needs a loop invariant over the dot product at every launch, which no tactic of the library finds. With the keyword inv after the clause's two terms (mat paths chains inv), for chains one chain of one loop with no nested loop, it proves the run from an invariant you give. It adds three more theorems. n.runInv is the run of one thread in bounds, from the facts the start gives it (Mat.Entry, the inputs other than the output, Elem and Elem.Only before it), its bounds and one premise H : LoopIterWith … at an invariant I for the trip count max K 0 (smax(K, 0), K the third size: one round for each index below K). n.sound_inv is n.sound_mat with Hin replaced by one premise H: LoopIterWith at each thread in bounds th, where I th may use the thread's facts. th : Mat.Thread … is a structure of those facts: the oracles, the index th.k, the context th.c, the memories th.m₀ and th.m', the frame th.f, the bounds th.inBounds, the oracle fact th.oracleEq, the entry facts th.entry, and Elem and Elem.Only before the thread (th.elem, th.only). n.kept gives the registers that the loop keeps (LoopSeg.kept) at the head of every round, each with the value that obl_have computes at the loop head from Mat.Entry, the launch's validity, the sizes and the thread's index facts, or, where obl_have gives that value only as a Classical.choose (at naive %40, the address of the thread's cell, which needs the bounds m * 50257 < 2 ^ 31 and n ≤ 50257 that Mat.Entry does not carry), with its value at the loop head itself. The three fields of LoopIterWith (MojoProof/Sym/KernelIter.lean) are about the heads of the rounds, the iterates R^[i] V₀ of the round from the loop head's entry valuation V₀ after the chain's path in: head (I at round 0 at V₀), round (at the head of every round i < max K 0, where Mat.Head and I i hold, the round's conditions, RoundWith, and I (i + 1) after it) and exit (at the head of round max K 0, where Mat.Head and I (max K 0) hold, the path out to ret with Elem after one more thread, RetWith). For a loop that runs another number of rounds (a tiled loop over K / t), H does not hold, so the clause's theorems do not apply to it. Mat.Head is what rounds that only read keep: the function, the buffers of Mat.Entry, Elem and Elem.Only. The command proves it at every head (mojo_inv at V₀, then Mat.Head.iterate at the registers through which the loop reads, LoopSeg.bases, their values at V₀ by n.kept, which the command adds before n.runInv), and the registers the loop keeps have their values at V₀ at every head (n.kept), so I carries neither. A proof of H is fun th => ⟨head, round, exit⟩, and it takes the thread's facts once. The edge into the loop is discharged by mojo_obl at the thread's row and column, as n.out's conditions are; if it is not, or Mat.Head is not proved at V₀, the command is refused and adds nothing. The proof is RetLsWith.of_iter (RetLsWith.of_inv, MojoProof/Sym/KernelMat.lean, at LoopIterWith.toInv) at each thread. The round's RoundWith is still yours (docs/proofs/kernel.md, "The heads of the rounds").

MojoProofTest/NaiveSound.lean runs the command on the naive matmul, with inv. Its naive_sound is naive.sound_mat with the run's premise (NaiveSpec.At) from the facts (NaiveSpec.At.of_entry); with the run NaiveIn.runIn it is NaiveKernel.correct. NaiveOut.runOut is naive.out at NaiveSpec.At. Open-goal tests there show the one premise of naive.sound_mat as case Hin and the premise of naive.sound_inv at a thread th as case head, case round and case exit, and thirteen refusals check the messages. An empty paths or chains is refused, since the checker's cover of an empty list is vacuous: with it the theorem would cover only launches with no thread out of, or in, bounds.

What it declares

Declaration What it is When
n.b<i> the summary of block i (Summary, from after the block's phis) always
n.paths the kernel's paths from the entry to ret, over the summaries always
n.covers coversEntry P fn n.paths = true, by decide +kernel always
n.sound_of the kernel theorem from the paths, with its premises left open always
n.args the arguments the clauses describe, from block numbers and scalars fn a constant, P without globals
n.starts the launch on n.args starts as n.args
n.sound n.sound_of on n.args, with the start discharged as n.args
n.argTy, n.argTail the buffers' struct type and the zero value of its fields after the pointer map2
n.ret each thread's path is a Ret to the elementwise invariant, by mojo_ret map2
n.correct the kernel theorem of the binary elementwise kernel map2
n.chains the kernel's one sequence of loops (LoopChain): the path into the first loop, and each loop's round and path out loop
n.coversChains coversChains P fn n.chains = true, by decide +kernel loop
n.entry, n.init, n.step, n.exit the loop's obligations, by the clause's tactic (with several clauses, n.entry<h>, … for each header h) loop
n.loop a thread of pre runs through the loops to ret with a memory of post (RetLs; the first clause's) loop
n.sound_loop the kernel theorem on n.args from Mem.Closed and an invariant, each thread by n.loop from the start's facts (Kernel.sound_entry) loop, as n.args
n.names parameter j of fn has register name j, by decide +kernel mat
n.starts the launch on Mat.args z o a b m n K starts mat
n.out a thread out of bounds takes a path of the clause to ret (RetWith.of_first, mojo_obl, mojo_ret) mat
n.sound_mat the matmul kernel theorem from the run of a thread in bounds Hin (Kernel.sound_mat, Hout by n.out) mat
n.kept the registers the loop keeps (LoopSeg.kept) at every head of a round, with their values at the loop head by obl_have (LoopSeg.kept_round) mat … inv
n.runInv a thread's run from its entry facts and LoopIterWith at an invariant, with Mat.Head at every head of a round (RetLsWith.of_iter, the edge into the loop by mojo_obl, Mat.Head by mojo_inv and Mat.Head.iterate at the registers LoopSeg.bases, their values at the loop head by n.kept) mat … inv
n.sound_inv n.sound_mat with Hin replaced by LoopIterWith with Mat.Head at each thread's Mat.Thread mat … inv

With mat, the command declares only the last six rows, the last two with inv (no summary, path, n.covers or n.args).

With the flag contract, n.covers, n.coversChains, the obligations, n.loop, n.sound_of and n.sound_loop are the With true forms of "Loops with fmul contract" above.

With the loop clause, n.paths holds only the paths that do not enter the loop (often none), and n.sound_of is Kernel.sound_of_chains at n.covers and n.coversChains. n.b<i> and n.paths are registered for equation lemmas, so simp only [n.b0] unfolds them in the module that runs the command too. In n.args, a buffer is its byval struct with a pointer to offset 0 of a block number and the zero value of every other field; a pointer parameter is the pointer .ptr ⟨b, 0⟩ to offset 0 of a block number b; a scalar is a bit vector. A pointer parameter with an attribute the start checks against its address (nonnull or align) makes n.starts an error: the start is proved by evaluation on every memory.

Sharing definitions. In a library module (one whose name starts with MojoProof.), and in any module where the option mojo_kernel.share is true, the command does not add a definition n.b<i>, n.paths, n.chains, n.args, n.argTy or n.argTail whose value an earlier definition of mojo_import, sym_summary or mojo_kernel already has, in the imports or the module. It uses that earlier constant instead, so the library has no two definitions of one value. For example, vector_multiply has the same blocks %4, %30 and %31 as vector_addition, so MojoProof/Kernels/Vmul.lean declares vmul.b1 and uses vadd.b0, vadd.b2 and vadd.b3, and so does MojoProof/Kernels/Vsub.lean with vsub.b1. mojo_import shares the same way: the blocks of vector_subtraction's file that are vector_addition's are vector_addition's constants. In a module outside the library without the option, the command does the same with the earlier definitions of the module itself, not with those of the imports, so no module has two definitions of one value: in MojoProofTest/KernelContract.lean, kgK.args is the module's kfK.args, without the option.

What the theorems say, and for which kernels

Every theorem the command declares is about the one kernel fn of P. The kernel theorems n.sound_of, n.sound and n.correct conclude Kernel.Sound P fn L args m adm obs R (MojoProof/Sem/Spec.lean): for every address choice and oracle that the admission adm admits, the launch starts, and its grid is sound (Grid.Sound). A sound grid has no undefined behaviour and no data race, keeps the promise of every !invariant.load, makes progress and terminates. Its final grids (the reachable grids in which every thread has returned) agree on the observation obs, which is defined on each of them and meets R.

n.correct covers one kernel, at every launch and input that meet its hypotheses. It says nothing about launches whose grid or block has a side other than 1 in y or z, about buffers that alias, or about other kernels. Its consequences for float maps, such as "a finite output float is the operation's result on the inputs' cells", follow from Kernel.map2_min, Kernel.map2_real, Kernel.map2_finEq and FinEq.eq_of_isFinite (MojoProof/Sym/Kernel.lean). Their instances for vector_addition, vector_multiply and vector_subtraction are in MojoProofTest/Kernel.lean.

What it refuses, and why

The checks of the clauses, the blocks, the paths, the loop clause's paths and terms, and the map2 prerequisites all come before anything is declared. A command refused by one of them therefore declares nothing. When the loop clause's tactic leaves an obligation, the command restores the environment it had before its first declaration, so it declares nothing either; an #eval in MojoProofTest/KernelCmd.lean checks both. With mat the checks come first in the same way, and if a generated theorem does not elaborate (mat: n.out does not elaborate, mat: n.sound_mat does not elaborate, mat: inv: n.runInv does not elaborate, mat: inv: n.kept does not elaborate, mat: inv: n.sound_inv does not elaborate) the environment is restored; for n.out, n.runInv and n.kept the failed attempt's own messages are dropped, so the refusal is the one message. Every message starts with mojo_kernel:. All but the first name the kernel term fn; the clause messages end with it in parentheses, which the table leaves out.

Message Why
p is not a parameter a<i> the right side p of a clause must have the form a<i>
clause x := a<i>: the kernel has k parameters the parameter does not exist
parameter a<i> is named by c clauses, not 1 the launch built from the clauses must pass one value per parameter; the start pairs arguments with parameters by position and does not compare their numbers
clause x := a<i>: a<i> is not byval of a buffer struct a reads or writes parameter must be a buffer struct passed byval
clause x := a<i>: a<i> is not an integer passed by value a scalars parameter must be an integer not passed byval
clause x := a<i>: a<i> is not a pointer without byval a pointers parameter must be a pointer not passed byval (a buffer struct passed byval is named by reads or writes)
block %l (block i of fn) is outside the checker's fragment the block checker has no summary for an instruction of the block
loop at block %l (block i of fn) with no loop clause; give its precondition, invariant, trip count and postcondition by loop i := pre inv trips post a branch goes back to a block on the current path, and the command has no loop clause; without one the kernel theorem covers threads that run one path without a cycle
the loop clause's header h is not a block of fn other than its entry block 0 (it has k blocks) the header is a block number, and the entry block is not a loop's header
loop at block %l (block i of fn), not at the loop clause's header … a second loop, or a loop nested in the clause's: the clause covers one loop
the loop clause needs one path into the loop at …, one round and one path out; it has … n.chains holds one sequence of loops with one round and one path out
loop at block %l (block i of fn), not at a loop clause's header (…); the clauses cover loops in sequence, with no loop in one with several clauses: a loop nested in one, or a loop without a clause
the loop clauses need one path from the entry block to a loop, into the first clause's loop, and for each loop one round and one path out, into the next clause's loop or, after the last, to a ret; … with several clauses: the clauses are not in the order the thread runs the loops, or a loop has more than one round or path out; the message ends with the entry block's paths or the loop it fails at
the path checker does not accept the paths of the loop at … (coversChains) n.coversChains would not hold (the loops at … with several clauses)
the loop clause's tactic does not close n.entry (…), … for the loop at …; the command adds nothing the tactic leaves the obligations named
the loop clauses' tactics do not close n.entry<h> (…), …; the command adds nothing with several clauses: the tactics leave the obligations named
map2 needs a kernel without a loop (fn) Kernel.sound_map2 is about kernels whose threads run one path
map2 needs the kernel as a constant; … does not elaborate to one n.correct names the kernel by its constant
map2 needs a program without globals; P has [@g] (fn) the command proves the start (n.starts) only for programs without globals
map2 needs reads a0, a1, writes a2, scalars a3, an i32 (fn) Kernel.sound_map2 is for kernels whose parameters are two inputs, an output and an i32 count, in that order
map2 needs a0, a1 and a2 byval of one type (fn), map2 needs a0, a1 and a2 byval (fn) the three buffers must be passed byval with one struct type
map2 needs a struct of a pointer and a tail (fn), map2: the struct's tail has no zero value (fn) n.args passes each buffer as a pointer followed by the zero value of the remaining fields
mat is not combined with map2 or loop (fn) n.sound_mat takes the paths and chains as given, and states no elementwise specification
mat needs the kernel as a constant; …, mat needs a program without globals; P has [@g] (fn) as for map2: the theorem names the kernel by its constant, and n.starts is proved only for programs without globals
mat needs a kernel of six parameters, three byval structs then three i32s; fn has k Kernel.sound_mat is for kernels of that shape
mat needs reads a1, a2, writes a0 and scalars a3, a4, a5 (fn), mat needs a3, a4 and a5 of type i32 (fn) the output first, then the inputs and the i32 sizes m, n, K, the order of Mat.args
mat needs a0, a1 and a2 byval of one type (fn), mat needs a0, a1 and a2 byval (fn), mat needs a struct of a pointer and a tail (fn), mat: the struct's tail has no zero value (fn) Mat.args passes each buffer as a pointer followed by the zero value of the remaining fields, and the codec is Layout.Codec.ptrArg's
mat needs parameter j named %j, for each j (fn) Kernel.sound_mat takes the registers %0 to %5 by number (n.names)
mat: the clause name x is a binder name of n.sound_mat or n.out (fn), mat: two clauses have one name (fn) the clause names are binder names of n.sound_mat and n.out, beside their own
mat needs a path; with no paths the theorem covers only launches with no thread out of bounds (fn), mat needs a chain; with no chains … no thread in bounds (fn) the checker's cover of an empty list is vacuous, so the theorem's Hout (or Hin) would hold only when no thread is out of (or in) bounds
mat: the path checker does not accept the paths (coversEntryWith c, fn), … the chains (coversChainsWith c, fn) the given paths or chains are not the kernel's at the flag c
mat: n.out does not elaborate: no path of the clause is discharged for a thread out of bounds by RetWith.of_first, mojo_obl and mojo_ret (fn): … no path of paths stores nothing after its entry block, returns and has its conditions proved by mojo_obl at a thread out of bounds, so Hout is not generated
mat: inv needs one chain; the chains do not reduce to a list of one (fn) n.runInv proves Hin with one invariant, for one chain
mat: inv needs a chain of one loop; its loops do not reduce to a list of one (fn), mat: inv needs a loop with no nested loop; its nested loops do not reduce to [] (fn) RetLsWith.of_inv is for a sequence of one loop with no nested loop
mat: the clause name x is a binder name of n.runInv, n.kept or n.sound_inv (fn) with inv, I, H, th, C, i, W and hW are binder names too
mat: inv: n.runInv does not elaborate: Mat.Head.iterate's premise LoopSeg.loadsFrom at LoopSeg.bases (%r, …) is false: block %l of the loop path stores; block %l' of the loop path loads through a pointer whose base is not a register the loop keeps (fn) evaluated before the attempt: a block of the loop path stores, or loads through a pointer whose base is no register the loop keeps (Summary.loadsIn at LoopSeg.bases fails), so the rounds may change the memory Mat.Head is about
mat: inv: n.runInv does not elaborate: Mat.Head.iterate's premise on the registers LoopSeg.bases fails at %r: obl_have gives it no value at the loop head (fn): ⊢ …, … fails at %r: its value at the loop head is not shown to name a block other than the output's by hne (a or b other than the output) (fn): ⊢ … register %r of LoopSeg.bases, which a load of the loop path reads through, has no value at the loop head by obl_have, or a value that a ≠ o and b ≠ o do not show to name a block other than the output's (at a pointer to the output, ⊢ o ≠ o)
mat: inv: n.runInv does not elaborate: the edge into the loop is not discharged by mojo_obl at the thread's 2-D index facts, or Mat.Head at the loop head is not proved by mojo_inv (fn): ⊢ … the chain's path into the loop has a condition mojo_obl does not prove at a thread in bounds, so Hin would need it as a further premise; or Mat.Head is not proved at the loop head. After the colon, in this row and the one above, the first error of the failed attempt from its first goal on, without the goal's hypotheses
mat: inv: n.kept does not elaborate: the function at the loop head is not proved by mojo_inv, or a register the loop keeps (LoopSeg.kept, numbers rs) has no value there by obl_have (fn): … obl_have computes no value, closed or Classical.choose, for a register of LoopSeg.kept at the loop head from the clause's facts, or mojo_inv does not prove the function there. After the colon, the first error of the failed attempt from its first goal on, without the goal's hypotheses

Two errors come from proofs, not checks. A scalar whose attributes do not accept its value by evaluation is an error at n.starts. If mojo_ret does not prove a thread's path, for instance because bytes is not what the kernel stores, the error is at n.ret.

Without map2, a kernel that is not a constant or a program with globals is not refused, but the command stops after n.sound_of.

Worked example: vector_addition

vector_addition (tests/corpus/vadd/) has four parameters: the inputs a0 and a1 and the output a2, each a buffer struct passed byval, and the i32 count a3. Thread k stores x[k] + y[k] at output cell k when k < n. MojoProof/Kernels/Vadd.lean writes the specification and runs the command:

def vadd.bytes (ω : Nat → Nat → Nat) {N : Nat} (x y : Tensor (Model FloatTy.float.fmt) [N])
    (n k : Nat) : Option (List MemByte) :=
  if h : k < n ∧ k < N then
    vadd.prog.layout.encode (.fp .float) (Val.ofFloat .float
      (deliver (Model.add (x (⟨k, h.2⟩, PUnit.unit)) (y (⟨k, h.2⟩, PUnit.unit)))
        (ω k (Nat.pair 23 0) / 3)))
  else none

mojo_kernel vadd := Kernels.vadd.prog Kernels.vadd.fn_0
  reads a := a0, b := a1
  writes out := a2
  scalars n := a3
  map2 (.fp .float) (Val.ofFloat .float) vadd.bytes

vadd.bytes is the binary32 sum of the inputs' cells k, delivered at the oracle number of the thread's fadd (which fixes the payload of a NaN result), encoded as 4 bytes. The 7 lines of the specification and the 5 lines of the command are all the kernel-specific text: there is no line of proof after the command, and the module sets no option.

The command declares the summaries vadd.b0 to vadd.b3 of blocks %4, %21, %30 and %31, and the two paths %4 → %21 → %31 (the thread is in range) and %4 → %30 → %31 (it is not). It then declares vadd.covers, vadd.sound_of, vadd.args, vadd.starts, vadd.sound, vadd.argTy, vadd.argTail, vadd.ret and vadd.correct. The statement of vadd.correct, written like the code above inside namespace MojoProof.Kernels, is

theorem vadd.correct {L : Launch} (hL : L.Valid) (hg : L.grid .y = 1 ∧ L.grid .z = 1)
    (hb : L.block .y = 1 ∧ L.block .z = 1) {al : Nat} (hal : 4 ∣ al) {a b out n N : Nat}
    {x y : Tensor (Model FloatTy.float.fmt) [N]} {m : Mem}
    (hP : Map2.Pre vadd.prog.layout (.fp .float) (Val.ofFloat .float) al vadd.argTail m
      a b out n x y) {ω : Nat → Nat → Nat} :
    Kernel.Sound vadd.prog vadd.fn_0 L (vadd.args a b out (BitVec.ofNat 32 n)) m
      (fun _ ω' => ω' = ω) (fun m => some m.blocks)
      (Elem.Out m out (fun i => 4 * i) (vadd.bytes ω x y n) (volume L.grid * volume L.block))

Read in words: on every valid one-dimensional launch (side 1 in y and z), for every memory meeting Map2.Pre, every address choice and every oracle, vector_addition starts, has no undefined behaviour and no data race, terminates with one result whatever the schedule, and leaves the output buffer as the caller's with each in-range thread's sum stored at its cell, every other block of the caller's memory unchanged.

To use it, discharge its hypotheses. In MojoProofTest/Kernel.lean, correct23 does so on a launch L23 of 2 blocks of 3 threads with count 5, where pre5 gives Map2.Pre by Map2.pre_mem on the memory Map2.mem builds:

theorem correct23 (ω : Nat → Nat → Nat) :
    Kernel.Sound Kernels.vadd.prog Kernels.vadd.fn_0 L23
      (Kernels.vadd.args 1 2 3 (BitVec.ofNat 32 5)) mem5 (fun _ ω' => ω' = ω)
      (fun m => some m.blocks)
      (Elem.Out mem5 3 (fun i => 4 * i) (Kernels.vadd.bytes ω x5 y5 5) 6) :=
  Kernels.vadd.correct valid23 ⟨rfl, rfl⟩ ⟨rfl, rfl⟩ (by decide) pre5

The same module derives what the theorem means for the floats, under the hypotheses of vadd.correct. The first three are about the observation of a complete run (Kernel.Obs) at output cell 4 k, for every thread k below the count, N and the number of threads T.

A second kernel, vector_multiply (MojoProof/Kernels/Vmul.lean, vector_addition with * for +), uses the same clauses with vmul.bytes (deliver (x_k * y_k) at the fmul's oracle number). Its blocks %4, %30 and %31, arguments, struct type and tail have the values of vadd.b0, vadd.b2, vadd.b3, vadd.args, vadd.argTy and vadd.argTail, which it uses. So it declares the definitions vmul.b1 and vmul.paths, and the theorems vmul.covers, vmul.sound_of, vmul.starts, vmul.sound, vmul.ret and vmul.correct. Its corollaries vmul_min, vmul_real, vmul_finEq and vmul_stable are in MojoProofTest/Kernel.lean; vmul_real is about each cell: where finite, the float at cell 4 k is the product of the inputs' cells k.

A third, vector_subtraction (MojoProof/Kernels/Vsub.lean, vector_addition with - for +), is the worked example of docs/new-kernel.md. Its module imports its file itself with mojo_import and runs the command with the same clauses and vsub.bytes (deliver (x_k - y_k) at the fsub's oracle number); like vector_multiply, it declares vsub.b1 and vsub.paths and uses vector_addition's other definitions. Its corollaries vsub_min, vsub_real (with TorchLean's x - y and the rounded real difference), vsub_finEq and vsub_stable are in MojoProofTest/Kernel.lean.

Worked example: a counting loop

The counting loop cnt of MojoProofTest/SymLoop.lean (for i in [0, n): p[i] = i, with %n in register 7 and %p in register 8) has four blocks: the entry %entry, the header %hdr (block 1, a phi and the comparison), the body %body and the exit %exit. MojoProofTest/KernelCmd.lean runs the command with the loop clause at header 1:

set_option mojo_kernel.share true in
mojo_kernel cntk := cntP cnt
  loop 1 := cntPre cntInv nOf cntPost by
    intro V hV
    obtain ⟨hP, hfn, hn, h7, h8, hm⟩ := hV
    first
      | obtain ⟨hW, hd, hpc, -⟩ := invN_entry (c := V.ctx) (k := V.k) (m := V.mem)
          (f := ⟨V.fn, 0, 0, V.regs, V.prods, [], none⟩) hfn h7 h8 hm
        rw [← hfn] at hpc
        exact ⟨hW, hd, hpc⟩
      | exact (invN_entry (c := V.ctx) (k := V.k) (m := V.mem)
          (f := ⟨V.fn, 0, 0, V.regs, V.prods, [], none⟩) hfn h7 h8 hm).2.2.2
      | (intro i hi W hW; exact invN_step hP hn hi hW)
      | (intro W hW; exact invN_exit hn hW)

cntPre V says V is in cntP and cnt, %n is nOf V ≤ 2 ^ 60, %p is at offset 0 of a block bOf V with room for nOf V cells; cntInv V is SymLoop's invariant invN at V's context, bound and block; the trip count is nOf V; cntPost V says cell j holds j for every j < nOf V. The tactic's four branches are the four obligations, by SymLoop's lemmas invN_entry, invN_step and invN_exit. With mojo_kernel.share (and SymLoop's sym_summary definitions recorded for it, which sym_summary does not do), the blocks' summaries are SymLoop's cntE, cntH, cntB and the library's Kernels.vadd.b3, so the test adds no second definition of one value. The command declares the empty cntk.paths, cntk.chains (equal to SymLoop's cntL.chain, cntk_chains), the obligations, cntk.loop, cntk.sound_of, and, since cnt is a constant and cntP has no globals, cntk.args (no arguments), cntk.starts, cntk.sound and cntk.sound_loop. cntk_three applies cntk.loop at SymLoop's start 3.

cnt has no parameters, so the frame a launch's start gives a thread has no %n and no %p, and the per-thread premise of cntk.sound_loop does not hold on any launch: cntk.sound_loop is the theorem's form for this kernel, not a kernel instance. MojoProofTest/KernelLoop.lean runs the command on cntK, cnt with the parameters %n (an i64) and %p (a plain pointer of address space 1):

set_option mojo_kernel.share true in
mojo_kernel loopK := cntKP cntK
  scalars n := a0
  pointers p := a1
  loop 1 := preB invV nOf postB by
    ...

Its invariant lemmas invB_entry, invB_step and invB_exit are SymLoop's for every function with cnt's blocks and every context whose program has cntP's layout. The invariant invB also puts the stores of the rounds so far on the trace, and the postcondition postB gives the end's trace, which the end's check of data races needs. Since cntK is a constant and cntKP has no globals, the command declares loopK.args n p = [.bits 64 n, .ptr ⟨p, 0⟩], loopK.starts and loopK.sound and loopK.sound_loop, whose per-thread premise has the registers %n and %p holding the arguments; its summaries, paths and chains are cntk's. kernelK is loopK.sound_loop on Launch.one with loopK.args 3 outC.2 and the memory outC (closed for them, outC_closed), at ch₀ and ω₀: the thread's precondition comes from those registers and outC's block, with nothing of the start evaluated. An example with #guard_msgs checks the per-thread goal loopK.sound_loop leaves.

MojoProofTest/KernelCmd.lean tests the command:

The library behind the command

Sequential schedules (MojoProof/Sym/Sched.lean)

Every result here is for every program, launch and contexts.

A kernel's proof then needs a per-thread run on every memory of the invariant, not one schedule's memories, and facts about the last memory's trace, not about a grid. On a launch whose grid and block have side 1 in y and z, thread n's ctaid.x * ntid.x + tid.x is n for every n below the number of threads (Launch.index_oneDim, MojoProof/Sem/Index.lean). So thread k of Seq.of_inv is the thread whose one-dimensional index is k. No collective call is involved: every grid step is Grid.advance, and no thread waits.

Tests: MojoProofTest/SymSched.lean and MojoProofTest/SymStart.lean.

A per-thread run on every memory needs a block's table evaluated on an abstract valuation, where decide +kernel refuses the goal (it has free variables). vals_getElem? (MojoProof/Sym/Term.lean) takes it one node at a time: entry j of vals V ns is node j's value on the values of the nodes before it. Tests in MojoProofTest/SymCall.lean:

The start of a launch (MojoProof/Sym/Start.lean)

Sym.Grid.start_ok gives Grid.start's .ok for every program, kernel, launch, arguments, memory, address choice and oracle. Its premises are ones a caller checks:

Its invariant is Mem.FreeAbove (Mem.Closed.above): on such a memory, every allocation of the start (Mem.allocNext) succeeds, whatever the address choice. Neither the launch nor the number of arguments is a premise: the start pairs arguments with parameters by List.zip.

Tests in MojoProofTest/SymLaunch.lean:

The kernel theorem (MojoProof/Sym/Kernel.lean)

These results turn a kernel's paths into Kernel.Sound; their proofs are in docs/proofs/kernel.md. Each holds for every program, kernel, launch, arguments, memory, address choice and oracle, under the premises given.

Tests in MojoProofTest/Kernel.lean. Each covers the launch and memory it names.

Tests in MojoProofTest/KernelContract.lean and MojoProofTest/KernelXY.lean. Each covers the launch, memory, address choice and oracle it names, except not_apart_xy, which holds for every valid launch.