LARK: Lean-Assisted Reasoning for LLM-Generated GPU Kernels
Organizations: Department of Electrical and Electronic Engineering, Imperial College London, London, United Kingdom · Department of Computer Science and Technology, University of Cambridge, Cambridge, United Kingdom · Quettaflop AI · School of Informatics, University of Edinburgh, Edinburgh, United Kingdom ·
Abstract
Efficient GPU kernels are typically low-level and hardware-specific, and LLM agents can now generate them faster than experts can verify. Existing approaches rely on testing, which provides evidence but not a guarantee of correctness. We introduce LARK, an agentic framework that automatically and formally verifies GPU kernels against their PyTorch reference implementations. LARK proves equivalence in functional and memory behavior under exact arithmetic and establishes a new trust boundary: human effort is only needed to verify operator-level Lean semantics ahead of kernel generation time, instead of checking every single line of generated code. LARK formalizes the GPU machine model and the semantics of each operation in Lean 4 once per target language, and the Lean 4 semantics of any incoming kernel is derived from these pre-built definitions. LLM agents then automate the proof in two steps. First, an agent decomposes the whole kernel semantics into smaller segments and Lean 4 verifies this segmentation, which addresses the intractability of proving equivalence for large kernels directly. Second, agents write Lean proofs that each kernel segment matches its high-level PyTorch counterpart. Together, the two steps give whole-program equivalence. We demonstrate that LARK can be used 1) to ensure correctness in existing code-generation agents and 2) to automatically verify human-written kernel libraries. On the KernelCraft GPU tasks, combined with a simple ReAct agent, LARK enables GLM 5.3 to fully verify 10 of 12 Level-1 kernels and Opus 5.5 all 5 Level-2 kernels. Moreover, over 10 production kernels randomly sampled from popular open-source LLM serving frameworks, LARK verifies 9 and identifies a new data race bug in the tenth. We believe LARK represents a significant step toward verifiable code generation in the agentic era.
Figures & tables
| Kind (examples) | Guard | Update | |
| Scalar ( add , mul ) | tags, | ||
| Memory ( ld , st ) | tag, aligned, | ||
| Control ( bra , ret ) | if | , halt | |
| Barrier ( bar.sync ) | — | parks | warp, block, grid |
| Collective ( mma ) | tags on all lanes | all lanes’ | coll |
| Target Lang. | Metric | GLM 5.3 | GLM 5.3 Flash | DeepSeek V4.1 Flash | GPT-6 Luna |
| PTX | pass rate | 83% | 58% | 25% | 25% |
| input tokens (M) | 314.53 | 714.60 | 148.47 | 445.93 | |
| output tokens (M) | 8.05 | 10.54 | 1.93 | 3.26 | |
| CUDA | pass rate | 75% | 50% | 25% | 25% |
| input tokens (M) | 280.49 | 626.53 | 80.45 | 476.20 | |
| output tokens (M) | 6.18 | 9.09 | 1.12 | 3.42 |
| Model | Metric | FFN | SwiGLU | RoPE | FlashAttn | GQA | Total |
| Opus 5.5 | pass | ✓ | ✓ | ✓ | ✓ | ✓ | 100% |
| input token (M) | 78.20 | 66.60 | 15.50 | 172.50 | 203.90 | 107.34 | |
| output token (M) | 1.43 | 1.01 | 0.37 | 2.77 | 3.09 | 1.73 | |
| GLM 5.3 | pass | ✗ | ✗ | ✓ | ✗ | ✗ | 20% |
| input token (M) | (459.60) | (512.30) | 71.90 | (214.10) | (611.60) | 373.90 | |
| output token (M) | (9.13) | (15.96) | 2.70 | (4.49) | (14.16) | 9.29 |
| Rejected / tested | |||||
| ID | Rule | Change | CUDA | PTX | HIP |
| R1 | Arithmetic | One operator or immediate ( for , a constant) | 3/3 | 3/3 | 3/3 |
| R2 | Load/store | One address, buffer, or stored value | 3/3 | 3/3 | 3/3 |
| R3 | Barrier | One barrier removed or moved one statement | 2/2 | 2/2 | 2/2 |
| R4 | Register | One variable or source register replaced at a use | 3/3 | 3/3 | 3/3 |
| R5 | Loop range | One loop bound, increment, or stride | 3/3 | 3/3 | 3/3 |
| Metric | vllm-silu-and-mul | vllm-rms-norm | sglang-rmsnorm | vllm-rotary-embedding | llmc-softmax-forward |
| model used | GLM 5.3 | GLM 5.3 | GLM 5.3 | GLM 5.3 | GLM 5.3 |
| verified | ✓ | ✓ | ✓ | ✓ | ✗ |
| input tokens (M) | 192.90 | 294.00 | 529.00 | 152.80 | (0) |
| output tokens (M) | 4.56 | 8.12 | 12.23 | 4.44 | (0) |
| Metric | sglang-fused-add-rmsnorm | flashinfer-rmsnorm | vllm-fused-qknorm-rope | fam-flash-attn | vllm-awq-gemm |
| model used | Opus 5.5 | Opus 5.5 | Opus 5.5 | Opus 5.5 | Opus 5.5 |
Appendix figures & tables16 assets
Supplementary material from the paper’s appendix.
Appendix
| KernelCraft Level | Tasks |
| Level 1: Primitive Operations | silu , relu , gelu , softmax , layernorm , rmsnorm , gemv , gemm , batchmatmul , linear , conv2d , depthwise_conv |
| Level 2: Composite Operations | rope , ffn , swiglu , flash_attn , gqa |
| Rounds / ceiling ($) | ||||||
| Group | Workloads | DeepSeek V4.1 Flash | GPT-6 Luna | GLM 5.3 Flash | GLM 5.3 | Opus 5.5 |
| L1 | relu , silu , gelu , softmax , layernorm , rmsnorm , gemv , gemm , linear , batchmatmul , conv2d , depthwise_conv | 16 / 50 | 16 / 50 | 16 / 75 | 8 / 100 | — |
| L2 | rope | — | — | — | 16 / 100 | 10 / 100 |
| ffn , swiglu , flash_attn , gqa | — | — | — | 16 / 100 | 10 / 200 | |
| Library | vllm_silu_and_mul , sglang_rmsnorm , vllm_rms_norm , vllm_rotary_embedding | — | — | — | 8 / 100 | — |
| llmc_softmax_forward , sglang_fused_add_rmsnorm , flashinfer_rmsnorm , vllm_fused_qknorm_rope | — | — | — | — | 10 / 100 | |
| Highest cut | Core set | Library kernels |
| None | relu , silu , gelu , conv2d , rope | vllm_silu_and_mul , vllm_rotary_embedding |
| Warp | — | vllm_fused_qknorm_rope |
| Block | softmax , layernorm , rmsnorm , gemv , gemm , linear , batchmatmul , depthwise_conv , flash_attn , gqa | sglang_rmsnorm , vllm_rms_norm , flashinfer_rmsnorm , sglang_fused_add_rmsnorm , llmc_softmax_forward , fam_flash_attn , vllm_awq_gemm |
| Grid | ffn , swiglu | — |
| Task (parameters) | Configuration |
| Level 1 | |
| SiLU, ReLU, GELU | |
| Softmax, LayerNorm, RMSNorm | |
| GEMV | |
| GEMM | |
| BatchMatmul | |
| Flag | DeepSeek V4.1 Flash | GPT-6 Luna | GLM 5.3 Flash | GLM 5.3 | Opus 5.5 | What it controls |
| --model | deepseek-v4.1-flash-1017 | gpt-6-luna | glm-5.3-flash | glm-5.3 | opus-5.5 | Model of the verification agents |
| --isa | PTX, CUDA, HIP | PTX, CUDA, HIP | PTX, CUDA, HIP | L1: all three; L2: PTX; library: CUDA | L2: PTX; library: CUDA, PTX | Target language of the run |
| --kernel | L1 | L1 | L1 | L1, L2, production libraries | L2, production libraries | Task verified ( Table 6 ) |
| --backward-effort | xhigh | xhigh | default | default | xhigh | Reasoning effort of every verification agent |
| --proof-effort | xhigh | xhigh | default ( max ) | default ( max ) | xhigh | Effort of the two proof agents |
| --other-effort | default ( xhigh ) | default ( xhigh ) | default ( high ) | default ( high ) | default ( xhigh ) | Effort of the statement, review, and diagnosis agents |
| Model | Target Lang. | Metric | ReLU | SiLU | GELU | Softmax | LayerNorm | RMSNorm | GEMV | GEMM | Linear | BatchMatmul | Conv2D | DepthwiseConv | Total |
| GLM 5.3 | CUDA | pass/fail/budget | pass | pass | pass | pass | pass | pass | budget | pass | pass | budget | pass | budget | 75% |
| input tokens (M) | 44.67 | 61.61 | 87.60 | 332.71 | 291.45 | 481.23 | (700.51) | 389.51 | 430.82 | (549.06) | 404.79 | (562.16) | 280.49 | ||
| output tokens (M) | 1.78 | 1.78 | 2.38 | 7.97 | 6.32 | 5.93 | (15.60) | 8.95 | 11.25 | (13.53) | 9.23 | (13.47) | 6.18 | ||
| PTX | pass/fail/budget | pass | pass | pass | pass | pass | pass | pass | pass | pass | budget | pass | budget | 83% | |
| input tokens (M) | 119.20 | 77.30 | 56.50 | 391.20 | 483.20 | 288.60 | 481.00 | 277.70 | 508.70 | (752.70) | 461.90 | (616.80) | 314.53 | ||
| output tokens (M) | 2.66 | 2.77 | 1.91 | 10.63 | 11.50 | 6.68 | 13.79 | 8.09 | 10.54 | (15.33) | 11.94 | (15.93) | 8.05 |
| Set | Model | Segmentation proof | Equivalence proof | Diagnosis | Race-freedom fallback | Other steps |
| Level 1 (PTX, CUDA) | DeepSeek V4.1 Flash | 271.21 (68.7%) | 164.53 (36.2%; 15 of 24) | 14.85 (3.6%) | — | |
| GPT-6 Luna | 646.92 (93.6%) | 267.78 (38.5%; 9 of 24) | 35.95 (4.4%) | — | ||
| GLM 5.3 Flash | 719.16 (55.1%) | 454.39 (48.3%; 21 of 24) | 10.34 (0.7%) | — | ||
| GLM 5.3 | 116.39 (42.8%) | 186.60 (54.8%) | 4.12 (1.3%) | — | ||
| Level 2 (PTX) | GLM 5.3 | 103.96 (29.6%) | 388.21 (72.5%; 4 of 5) | 3.29 (1.0%) | — | |
| Opus 5.5 | 16.26 (27.2%) | 60.76 (69.4%) | 2.71 (1.4%; 2 of 5) | — |
| Model | Calls analyst / probe / 2nd op. | Share of cell input aggregate / median | Verdict stops | False positives refuted / issued | 2nd op. refuted |
| DeepSeek V4.1 Flash | 610 / 755 / 3 | 3.7% / 3.5% | 1 | 1 / 1 | 3 / 3 |
| GPT-6 Luna | 599 / 815 / 5 | 4.7% / 4.3% | 6 | 1 / 1 | 2 / 5 |
| GLM 5.3 Flash | 336 / 303 / 8 | 0.7% / 0.6% | 1 | 1 / 1 | 6 / 8 |
| GLM 5.3 | 154 / 176 / 4 | 1.1% / 1.1% | 2 | 0 / 0 | 0 / 4 |
| Opus 5.5 | 3 / 10 / 0 | 0.8% / 0.5% | 0 | 0 / 0 | 0 / 0 |
| PTX (H100) | CUDA (H100) | HIP (MI300X) | ||||||||||
| Kernel | Ours ( s) | vs eager | vs compile | vs vendor | Ours ( s) | vs eager | vs compile | vs vendor | Ours ( s) | vs eager | vs compile | vs vendor |
| relu | 2.05 | 3.53 | 0.96 | — | 2.01 | 3.60 | 0.98 | — | 1.73 | 4.35 | 1.07 | — |
| silu | 2.08 | 3.55 | 0.97 | — | 2.06 | 3.58 | 0.98 | — | 1.75 | 3.81 | 1.10 | — |
| gelu | 1.99 | 3.52 | 1.02 | 2.38 | 1.98 | 3.54 | 1.03 | 2.39 | 1.77 | 4.17 | 3.02 | 1.72 |
| softmax | 4.12 | 2.93 | 3.06 | 3.00 | 4.37 | 2.76 | 2.88 | 2.83 | 5.25 | 2.23 | 2.19 | 1.32 |
| layernorm | 5.03 | 6.63 | 1.78 | — | 4.78 | 6.98 | 1.87 | — | 5.05 | 3.79 | 1.69 | 0.85 |
| Kernel | Lang. | Mutants a | Static b | Race-free c | Kernel bug d | Premise e | Escaped f |
| ReLU | CUDA | 4 | 1 | 0 | 2 | 1 | 0 |
| ReLU | PTX | 4 | 0 | 1 | 2 | 1 | 0 |
| ReLU | HIP | 4 | 0 | 1 | 3 | 0 | 0 |
| Softmax | CUDA | 5 | 1 | 0 | 4 | 0 | 0 |
| Softmax | PTX | 5 | 1 | 0 | 4 | 0 | 0 |
| Softmax | HIP | 5 | 2 | 2 | 0 | 1 | 0 |
| Component | What it contains | Status | Per-language part |
| Machine model | |||
| State and memory hierarchy | One state for the launch: registers keyed by thread and name, global, shared, and local memory with type tags, coordinates and geometry | Shared | Coordinate reads and address spellings |
| Value representation | Typed values: exact reals with , exact integers with a width tag, predicates, packed halves; partial operations (faults) | Shared | |
| Cut hierarchy and folds | kinds, the folds of Equation 2 , convergence, fuels | Shared | Which operations define which cut; the warp width (32 or 64) |
| Race freedom | Definition over footprints, span lemmas, pair-lemma library, the reduction metatheory relied on | Shared | |
| Premise schemas | Placement, separation, parameters, alignment, geometry, fuel, entry (Appendix A.8 ) | Shared | |
| Stage / artifact | Produced by | Basis for acceptance | Status | If it fails |
| Offline, once per target language or per module | ||||
| Machine model | Humans, agents | Review; pinned by hash; its idealizations declared as scope | Trusted | (pre-checked) |
| Per-operation semantics | Generated from pinned tables | Measured against the hardware; pinned by hash | Trusted | Operation not in table: refuse |
| Torch module formalization | Agents, humans | Tested against the Torch module on random inputs | Trusted (tested) | Mismatch: fix before any run |
| Premise and extractor definitions | Pinned in | One definition each over a signature; pre-checked | Trusted | (pre-checked) |
| Reduction metatheory | Literature | Race-free phases one final state for every interleaving | Trusted | Reported |