MojoProof

Machine-checked proofs for compiled Mojo GPU kernels, in Lean.

Code on GitHub Roadmap How to contribute

How MojoProof worksmojo build --emit llvmmojo_importmojo_kernelMojo kernel: .mojoMojo kernel.mojoLLVM IR: .nvptx.llLLVM IR.nvptx.llLean program: LLVM ASTLean programLLVM ASTKernel theorem: Kernel.SoundKernel theoremKernel.SoundFloatLib: IEEE 754FloatLibIEEE 754Semantics: MojoProof.SemSemanticsMojoProof.SemSpec: TorchLeanSpecTorchLean
Figure 1. How MojoProof works. The Mojo compiler writes one LLVM IR file per GPU kernel; MojoProof imports it into Lean, where it has a semantics for threads, memory and floating point (IEEE 754 through FloatLib), and mojo_kernel proves the kernel theorem against a spec stated with TorchLean. The check mark: every proof is checked by Lean's kernel. Click a box to see its code.

MojoProof proves things about the code the Mojo compiler actually emits, for every input and every launch of the shape a theorem states (one-dimensional, for the elementwise kernels), with floating point modelled bit for bit. It lives outside the compiler, so Mojo itself doesn't need to change, and every theorem says how far it reaches: every program, a family of kernels, or one kernel.

116compiled kernels in the corpus123Lean modules in the library4kernels with a kernel theorem111roadmap cards25Mojo and MAX bugs filed

Why Mojo

I really like Mojo, for two reasons.

  1. One language, all the way down. The same language takes you from Python-style code to kernels on NVIDIA, AMD and Apple GPUs.
  2. One system instead of many. What makes PyTorch and JAX hard to verify is how heterogeneous they are. One model runs through Python, C++, CUDA or Triton kernels and a graph compiler, each with its own semantics. With Mojo there is one language and one open-source compiler, so there is one thing to give a semantics to.

Why verification

GPU kernels are easy to get quietly wrong, and testing can't cover them. A kernel that passes on the sizes you tried can still read out of bounds, race, or give a different answer at another size or under another thread schedule. A proof can cover every input, every launch size and every schedule at once. MojoProof proves things about the code the Mojo compiler actually emits, so the guarantee is about what runs, not about a hand-written model of it.

What is proved today

Schedule independence every program

From a memory whose pointers name only its own blocks, one run of a launch to its end with no fault, no data race and no broken invariant-load promise shows that every thread schedule of that launch is safe, race free, terminates and gives the same result.

Grid.confluent, Grid.start_sound

A sound symbolic checker every program

A block, a path or a path to the kernel's return that the checker accepts runs as its summary says, whenever the path's conditions hold at its entry.

Block.sound

Elementwise kernels a family

A kernel with two input buffers, an output buffer and a count meets the kernel theorem when each thread takes a checked path, and each finite output cell below the count and the thread count is the float operation applied to the inputs' cells.

Kernel.sound_map2, Kernel.map2_real

Three kernels, end to end three kernels

vector_addition from max/examples, a vector multiply and a vector subtraction, each proved by one mojo_kernel command from its spec, with no proof lines written by hand.

vadd.correct, vmul.correct, vsub.correct

A kernel with a loop one kernel

Modular's naive matrix product meets the kernel theorem for every valid launch, size and input that meets the matmul precondition: each output cell inside the matrix holds the row-by-column dot loop. Its threads' runs are proved by hand from the library's lemmas, not yet generated by mojo_kernel.

NaiveKernel.correct

Each result links to its Lean declaration. Every statement, with all its premises, is in What is proved, and what the proofs trust is in the trusted base.

The roadmap

The roadmap, one square per cardFloatsthe rounding of fadd, fsub, fmul and fdiv, to nearest even in the operation's format (L3)contract, the fast-math flag that lets fmul and fadd fuse into one rounding (L3)deliver, the result of a floating-point operation as a word, NaNs from the oracle (L3)ex2.approx.ftz.f32, the approximate base-2 exponential of Mojo's float32 exp (L1)llvm.maxnum and llvm.minnum, the floating-point maximum and minimum (L1)NaN payloads, which NaN a floating-point operation may return (L1, with evidence above its level)rsqrt.approx.ftz.f32, the approximate reciprocal square root (L1)The GPU modeladdress spaces, the generic, global, shared and local spaces of a pointer (L1)atomics, atomicrmw with a commutative operation and an unused result (L1)the block barrier, bar.sync over the threads of a block (L1)cp.async, asynchronous copies from global to shared memory and their groups (L1)threads and the launch, the grid and blocks of threads and their special registers (L3)ldmatrix, a warp's load of 8x8 matrices from shared memory (L1)mma, a warp's tensor-core multiply-accumulate (L1)shared memory, the static and dynamic shared blocks of a block of threads (L1)warp shuffles, shfl.sync of an i32 across the lanes of a warp (L1)Kernelsgemv_kernel, a matrix-vector product (TorchLean.Tensor.matVecLoop) (L0)layer_norm_gpu, the 34 row-wise layer norm specializations (TorchLean.Tensor.layerNorm) (L0)the layout tensor-core tests, mma_layout_tc, matmul_naive and c_fragment_kernel (TorchLean.Tensor.matmul) (L0)multistage_gemm, the tiled tensor-core GEMM with asynchronous copies (TorchLean.Tensor.matmul) (L0)matmul_kernel_naive, one thread per output cell of C = A B (TorchLean.Tensor.matmul) (L4)sum_kernel, a block sum in shared memory with one atomic add per block (TorchLean.Tensor.sum) (L0)rms_norm_gpu, root mean square normalization (TorchLean.Tensor.rmsNorm) (L0)softmax_kernel, the row-wise softmax (TorchLean.Tensor.softmax) (L0)matmul split-k reduce, the sum of the partial products of a split GEMM (TorchLean.Tensor.sum) (L0)tiled_matmul_kernel, a matmul by shared-memory tiles (TorchLean.Tensor.matmul) (L0)vector_addition, the elementwise sum of two float32 vectors (TorchLean.Tensor.add) (L4)vector_multiply, the elementwise product of two float32 vectors (TorchLean.Tensor.mul) (L4)vector_subtraction, the tutorial's elementwise difference of two float32 vectors (TorchLean.Tensor.sub) (L4)Layout lawsA layout whose extents are powers of two is linear over F2 (L1)`blocked_product`'s outer stride is documented as the block's cosize times the tiler's stride (L1)`coalesce` keeps the offsets and merges contiguous modes (L1)`composition(a, b)` maps a coordinate through `b`, then `a` (L1)`cosize` is the offset of the last coordinate plus one (L1)`crd2idx` is the dot product of a coordinate with the strides (L1)`idx2crd` maps an offset back to a coordinate, documented as the inverse of `crd2idx` (L1)With a narrow index type the caller must ensure products and sums fit (L1)Stepping by a stride is exact only for an affine layout (L1)`reverse` and `transpose` reverse the shape and the stride (L1)`TileTensor.as_span`, `reshape` and `coalesce` need row-major contiguous strides (L1)`Swizzle` is `x ^ ((x & mask) >> shift)` and needs `abs(shift) >= bits` (L1)`two_level_layout_from_swizzle` evaluates to `Swizzle(L(x))` (L1)`upcast` divides strides and shapes by the factor with `shape_div` (L1)`zipped_divide` splits a layout into tile and number-of-tiles modes (L1)LLVM IR constructsInline asm `cp.async.cg.shared.global` with 3 operands (L0)Inline asm `cp.async.cg.shared.global` with 4 operands (L0)The `add` instruction (L3)The `addrspacecast` instruction (L1)The `alloca` instruction (L1)The `and` instruction (L1)The `ashr` instruction (L1)The `atomicrmw` instruction (L1)The `bitcast` instruction (L1)The `call` instruction (L3)The `extractelement` instruction (L1)The `extractvalue` instruction (L3)The `fadd` instruction (L3)The `fdiv` instruction (L1)The `fmul` instruction (L3)The `fpext` instruction (L1)The `fptrunc` instruction (L1)The `fsub` instruction (L3)The `getelementptr` instruction (L3)The `icmp` instruction (L3)The `insertelement` instruction (L1)The `insertvalue` instruction (L1)The `load` instruction (L3)The `lshr` instruction (L1)The `mul` instruction (L3)The `or` instruction (L1)The `phi` instruction (L3)The `ptrtoint` instruction (L1)The `sdiv` instruction (L1)The `select` instruction (L3)The `sext` instruction (L3)The `shl` instruction (L1)The `shufflevector` instruction (L1)The `sitofp` instruction (L1)The `store` instruction (L3)The `sub` instruction (L1)The `trunc` instruction (L3)The `udiv` instruction (L1)The `urem` instruction (L1)The `xor` instruction (L1)The `zext` instruction (L3)The intrinsic family `Intrinsic.barrier` (L1)The intrinsic family `Intrinsic.cpAsyncCommit` (L1)The intrinsic family `Intrinsic.cpAsyncWait` (L1)The intrinsic family `Intrinsic.extract` (L1)The intrinsic family `Intrinsic.fminmax` (L1)The intrinsic family `Intrinsic.griddepLaunch` (L1)The intrinsic family `Intrinsic.griddepWait` (L1)The intrinsic family `Intrinsic.laneid` (L1)The intrinsic family `Intrinsic.ldmatrix` (L0)The intrinsic family `Intrinsic.lifetime` (L1)The intrinsic family `Intrinsic.minmax` (L3)The intrinsic family `Intrinsic.mma` (L0)The intrinsic family `Intrinsic.reduceAnd` (L1)The intrinsic family `Intrinsic.rsqrtApprox` (L1)The intrinsic family `Intrinsic.shfl` (L1)The intrinsic family `Intrinsic.sreg` (L3)The intrinsic family `Intrinsic.stepvector` (L1)Refused: inline asm `cvt.rn.f16x2.e4m3x2` with 2 operands (L0)Refused: inline asm `cvt.rn.satfinite.e4m3x2.f32` with 3 operands (L0)Refused: `load atomic` (L0)Refused: `store atomic` (L0)The `br` terminator (L3)The `ret` terminator (L3)std.math functions`std.math.max` on SIMD values: `llvm.maxnum` on floats, `llvm.smax` on signed integers (L1)`std.math.min` on SIMD values: `llvm.smin` on signed integers (L1)`std.math.rsqrt` on float32 on the GPU is `llvm.nvvm.rsqrt.approx.ftz.f` (L1)L0L1L2L3L4evidence above its level
Figure 2. The roadmap, one square per card, grouped by area and coloured by its level, from L0, a written card, to L4; the roadmap page says what each level needs, for a kernel and for a construct. A dot marks a card that already has evidence for a level above its own, for example a kernel theorem waiting for its recorded GPU run. Levels are computed from the code when the site is built. Hover a square for its title and click it to open the card.

Get involved

  1. Pick a card on the roadmap whose level can go up.
  2. Follow the guide, or hand AGENTS.md to your coding agent. Specs are always checked by a human.
  3. Open a pull request. CI checks it, and this site updates itself when it's merged.

I'd love help, and I'd love to hear which kernels you want proved. Come say hi on the Modular forum : )