TensorLean: Formal Verification of Production GPU Kernels
Wei Qiang, Ganxiang Yang, Yi Rong, Ronghui Gu
cs.PL
Dec 3, 2026 · v1
cs.DC cs.LG
TL;DR
Implements a GPU kernel verifier in Lean 4, mechanizing PTX semantics, the weak memory model, and soundness proofs, with tensor specifications built on Mathlib.
Abstract
GPU kernels are the computational core of modern AI, yet a single correctness bug can silently corrupt frontier models and ML frameworks, putting training and inference at risk. Formal verification promises correctness across all inputs, launch configurations, and schedules, but it has never reached production GPU kernels. We present TensorLean, the first framework to formally verify production GPU kernels. To reduce manual effort, TensorLean verifies the kernel's compiled PTX against tensor-level specifications that can be shared across kernels and their thousands of instances. Given the kernel's PTX, TensorLean builds a symbolic happens-before graph over formalized PTX semantics to capture all valid inputs, launch configurations, and concurrent schedules in a single run. On this graph, it first verifies data-race freedom and termination, thereby reducing concurrent executions to finite, sequential ones. It then uses a push/pull model to compute PTX memory updates and proves their functional correctness against the specification over reals with SMT solvers. Finally, TensorLean extends the correctness guarantee to the kernel's actual floating-point PTX via inductive round-off bound inference. For scalability, TensorLean further automatically infers tile-loop invariants and decomposes proof goals via user-provided assertion hints. We mechanize the PTX semantics and soundness proofs in Lean 4 and evaluate TensorLean on 2,078 kernel instances covering all 143 kernels in FlashAttention-2, -3, and -4, DeepSeek DeepGEMM, and LinkedIn Liger Kernel. It proves 1,180 instances correct and uncovers 66 unique, previously unknown correctness bugs in 888 of the remaining instances, all reported to developers.
Problem
Production GPU kernels such as FlashAttention and DeepGEMM contain subtle concurrency, memory, and floating-point bugs. Existing verifiers cannot handle symbolic shapes, asynchronous operations, or low-precision floating point at production scale.
Approach
TensorLean verifies compiled PTX against tensor-level specifications written in Lean on top of a Mathlib-based tensor library. It formalizes PTX operational semantics (108 instruction families) and the PTX weak memory model in Lean 4. It builds a symbolic happens-before graph to check data-race freedom and termination, and proves functional correctness over the reals using a push/pull model with SMT solvers. Floating-point correctness follows from inductive round-off bounds, supported by tile-loop invariant inference and assertion hints, with soundness theorems proved in Lean.
Results
Across 2,078 instances of all 143 kernels in FlashAttention-2/3/4, DeepGEMM, and Liger Kernel, TensorLean proved 1,180 instances correct. It found 66 previously unknown bugs affecting 888 instances. The implementation is 80.7K lines of Lean 4 and is open source.