Retrieved article excerpt
Open article · Retrieved 2026-11-20T21:22:21.207229+00:00
GPU kernels serve as the computational core of modern AI. A single bug inside one kernel can silently compromise the entire model's correctness and put both training and inference at risk[[1Jonathan T. Barron et al.A Fix for Softmax and Cross-EntropyarXiv:2610.31642, 2026](https://tensorlean.ai/#ref-barron2026fix), [2Aaron Grattafiori et al.The Llama 3 Herd of ModelsarXiv:2407.21783, 2024](https://tensorlean.ai/#ref-grattafiori2024llama3herdmodels), [3Ziming Zhou et al.OpGuard: Bitwise Alignment for Precise and General Debugging of Production LLM TrainingOSDI 2026](https://tensorlean.ai/#ref-zhou2026opguard)]. Formal verification promises correctness over any configuration and GPU schedule, but it has never reached production kernels for modern AI. **We present TensorLean, the first framework to formally verify the correctness of production GPU kernels.** With TensorLean, we verified 149 real-world open-source kernel instances on their PTX assembly, including 102 from [FlashAttention](https://github.com/Dao-AILab/flash-attention) and those from [DeepSeek](https://github.com/deepseek-ai), [Liger Kernel](https://github.com/linkedin/Liger-Kernel), etc., and proved 83 of them correct. In verification, we also discovered 38 bugs of various kinds in frontier kernels that had eluded testing for months and years. We expect that, with TensorLean, formal verification will scale and become an essential step in developing correct and efficient kernels.
## Introduction
In February 2024, ChatGPT began producing text that made no sense [[4OpenAIUnexpected responses from ChatGPTOpenAI incident report, February 2024](https://tensorlean.ai/#ref-openai2024unexpected)].
The following year, a miscompilation disrupted Claude's token selection [[5AnthropicA postmortem of three recent issuesAnthropic Engineering, September 2025](https://tensorlean.ai/#ref-anthropic2025postmortem)].
Now, in October 2026, Google DeepMind showed that even standard softmax and cross-entropy in PyTorch and JAX contain numerical bugs that can corrupt gradients and collapse a model's accuracy during training [[1Jonathan T. Barron et al.A Fix for Softmax and Cross-EntropyarXiv:2610.31642, 2026](https://tensorlean.ai/#ref-barron2026fix)]. All of them were caused by silent correctness bugs in kernels, which are programs that implement the model's modules and algorithms, such as matrix multiplication and attention.
As AI models become infrastructure, we need to detect and fix these bugs, and prove their absence before they waste millions of GPU hours and dollars.
Formal verification is the only way to provide the exact guarantee.
In formal verification, developers translate their code into a mathematical model, then try to prove that the model satisfies a set of mathematical properties, known as specifications.
Once the proof is machine-checked,machine-checkedThe proof is written as a program in a formal language, e.g., Lean. A proof assistant then checks the proof's validity by type checking it. the code meets its specification on all allowed inputs and in every possible execution.
However, formal verification is often expensive: we must state precisely what the code should do, in a specification, and why it does so, in a proof. Both can cost orders of magnitude more than the development per se.
For example, the [seL4](https://github.com/seL4/seL4) microkernel took 2.5 person-years and 8,700 lines of C to build, but 20 person-years and 200,000 lines of proof to verify [[6Gerwin Klein et al.seL4: Formal Verification of an Operating-System KernelCommunications of the ACM, 2010](https://tensorlean.ai/#ref-klein2010sel4)].
Even with state-of-the-art verifiers, specifications and proofs are still several times larger than code [[7Andrea Lattuada et al.Verus: A Practical Foundation for Systems VerificationSOSP 2024](https://tensorlean.ai/#ref-lattuada2024verus)].
Moreover, GPU kernels are known to be complicated for their massive concurrency, distributed and memory layouts, and floating-point arithmetic, all challenging the scalability of formal verification.
To achieve formal guarantees on kernels at an affordable cost, we introduce TensorLean, the first to formally verify the correctness of production kernels.
In TensorLean, the efforts in specification and proofs are both reduced to a minimum.
For specification, TensorLean decouples mathematical computation from memory layouts, so specifications are as easy to write and audit as an ordinary tensor program and scale to production kernels at about a 15:1 code-to-spec ratio.
For verification, TensorLean proves that a kernel's compiled PTX meets its tensor-level specification on every input and thread schedule, within floating-point tolerance.
With TensorLean, we verified 149 kernel instances on their PTX, covering all 37 kernels of FlashAttention-2 to -4 and more from DeepGEMM and Liger Kernel: 83 are proved correct, and the other 66 contain bugs we found.
Along the way, we found 38 bugs that silently eluded the kernels' own tests.
K⊤VQSOsoftmax
*O* = softmax(*Q* · *K*⊤/√*d*) · *V*
TensorLean
```
theorem flash_attention_correct :
∀ inputs executions,
ptx ≈ attention := by
tensorlean
```
FlashAttention-12022
mma.sync.aligned.m16n8k16.row.col.f32.f16.f16.f32 …
**A100**
FlashAttention-22023
cp.async.cg.shared.global.L2::128B [%r93], [%rd324], 16;
**A100**
FlashAttention-32024
wgmma.mma\_async.sync.aligned.m64n224k32.f32.e4m3.e4m3 …
**H100**
FlashAttention-42026
tcgen05.mma.cta\_group::2.kind::f16 [tmem\_acc], …
**B200**
⋮
We will soon release our paper and [open-source TensorLean](https://github.com/TensorLean/TensorLean), including its PTX semantics and the specifications and proofs of the verified kernels, so that anyone can reuse them and verify other frontier kernels.
We expect that, with TensorLean, we can pace the formal verification of GPU kernels in step with their evolving development, with both experts and agents involved [[8Zihao Ye et al.CAKE: Compiler-Agent Co-Design for Frontier Kernel EvolutionarXiv:2608.12629, 2026](https://tensorlean.ai/#ref-ye2026cake)].
In the rest of this article, we cover TensorLean in more detail.
## Tensor-Level Specification
To prove that a kernel is correct, we must first state what a "correct behavior" is, known as its *specification* (spec).
A kernel is correct if, on every input it accepts and in every execution, it writes the same valuesthe same valuesDue to floating-point rounding, "the same" means equal within a bound, known as **tolerance**. as the operation it implements, and changes nothing else in GPU memory.
For example, an attention kernel with a KV cache is correct if, for every Q,K,VQ, K, VQ,K,V with head dimension ddd, it writes O=softmax(QK⊤/d)⋅VO = \mathrm{softmax}(Q K^\top/\sqrt{d})\cdot VO=softmax(QK⊤/d)⋅V, appends K,VK, VK,V to the cache, and leaves other bytes of GPU memory as they were.
The definition of kernel correctness is extensional:extensionalProperty PPP is extensional if (∀m. f(m)=g(m))⇒(P(f)⇔P(g))(\forall m.\ f(m) = g(m)) \Rightarrow (P(f) \Leftrightarrow P(g))(∀m. f(m)=g(m))⇒(P(f)⇔P(g)). For the kernel correctness case, mmm is GPU memory, and f,gf,gf,g are memory updates by two kernel executions, respectively. it constrains only the memory before and after the kernel finishes, in every possible execution.
A kernel's specification therefore describes the values the kernel reads and writes, and nothing of its execution, such as its warp specialization or synchronization.
**Kernel Specification at Scale.** The correctness specifications are notoriously costly to write, as they demand expertise in both verification and the target domain, and can be several times larger than code [[7Andrea Lattuada et al.Verus: A Practical Foundation for Systems VerificationSOSP 2024](https://tensorlean.ai/#ref-lattuada2024verus)]. The root cause is that a specification couples the mathematics of the computation with the representation of its data in the machine model's memory.
TensorLean makes the specification of GPU kernels scale: we achieve an average 15:1 code-to-spec ratio. For example, it only needs 2,291 lines of specification to define the correctness of 12 attention (forward) kernel variants, including the FlashAttention family and more, totaling 35,852 lines of source:
Code vs. Spec Size Comparison: Attention (Forward)
Kernel SourceTensorLean SpecificationShared Spec Library
05,00010,00015,00020,00025,00030,00035,00040,0000123456789101112Number of Kernel Variants (Ordered by First Release)Lines of Code35,852 lines2,291 lines156 lines05K10K15K20K25K30K35K40K024681012Kernel Variants, by First ReleaseLines of Code35,8522,291156
Figure 1. Kernel source vs. TensorLean specification size, accumulated over 12 attention (forward) variants.
**Decoupling Mathematics from Memory.** The key observation behind TensorLean's scalability is that the math and memory can be decoupled because a GPU kernel's correctness is extensional: the specification refers to memory only through the memory layout, which maps each tensor element to the bytes that hold it, while the mathematics refers to tensors alone.
Input tensors*Q*, *K*, *V*
`spec_math`
Output tensor*O* = softmax(*QK*⊤/√*d*) *V*
Specification
`spec_layout`
`spec_layout`
PTX memory before*Q**K**V*…
attention kernel
PTX memory after*Q**K**V**O*…
Implementation
SpecificationImplementation
Figure 2. Decoupling an attention kernel's mathematics from PTX memory.
With math and memory decoupled, a kernel’s specification can be written at the tensor level instead of the PTX level: the mathematics is stated over tensors, and the memory layout of each tensor is given separately. TensorLean then automatically lowers this tensor-level specification to a PTX-level one, in which each tensor element is read from the bytes that hold it, as given by the memory layout:
over tensors
```
Q[i, e]
```
[i, e] ↦ q\_ptr + 2\*(i\*D + e)layout of Q
over PTX memory
```
fp16(mem[q_ptr + 2*(i*D + e)])
```
Concretely, a TensorLean specification consists of five parts, over the tensors that the kernel only reads (`input`), writes without reading their old values (`output`), and reads and overwrites in place (`state`), such as a KV cache:
1. `spec_math`: The **mathematical computation** the kernel implements, as a tensor function from `state` and `input` to `output` tensors of real numbers.
2. `spec_mut`: The **GPU memory update** the kernel makes in place, as a function from `state` and `input` to `state'`, the `state` after execution.
3. `spec_layout`: The **memory layouts** of the tensors, as a function from each tensor's indices to GPU memory addresses.
4. `spec_tol`: The **tolerance** ε\varepsilonε of each `output` and `state'` tensor, by which its floating-point values in memory may differ from its exact real values.
5. `requires`: The **precondition** on `state` and `input`.
Read-only input*Q*, *K*, *V*
spec\_math
spec\_mut
spec\_math
spec\_mut
Return output|*O* − softmax(*QK*⊤/√*d*) *V*| ≤ *ε*
Mutable tensorKV cache: *C*
Mutable tensor*C*′ = append(*C*, *K*, *V*)
requires *C* has room for *K*, *V*
`q_ptr` `k_ptr` `v_ptr`
`c_ptr`
`o_ptr`
PTX memory
Figure 3. A sample specification of an attention kernel w/ KV cache in GPU global memory.
**Writing Specifications in Lean.** TensorLean provides a library of tensor operators to define the tensor-level specifications, built on Lean's [Mathlib](https://github.com/leanprover-community/mathlib4).
It lets developers declare tensors, with operators that follow PyTorch's names and semantics, e.g., `@` for `torch.matmul`, so a kernel's tensor-level specification takes no more effort to write, or to audit, than a PyTorch reference.
For example, an attention kernel's `spec_math` can be defined as below in TensorLean:
```
-- ℝ: real number, ℕ: natural number
-- L: sequence length, D: head dimension
structure Attention (L D : ℕ) where
Q : Tensor ℝ [L, D]
K : Tensor ℝ [L, D]
V : T