Formal Verification Reaches Production AI Kernels, Finds 38 Bugs

In August 2025, some Claude users noticed something strange. Ask a question in English and Thai text appeared in the answer. Ask for code and the output carried obvious syntax errors. To the people hitting it, the model looked out of control. Anthropic’s post-mortem explained that a runtime optimisation used for speed had affected some TPU servers. A simple performance optimisation had introduced a computation error that disturbed the model’s probability distribution. Tokens that should almost never have been selected got abnormally high probability, and the output turned chaotic and unpredictable.

Claude API incident timeline showing output anomaly
A 2025 optimisation bug distorted Claude’s output distribution (Source: 36Kr)

In chat and coding, the consequence is garbled text and a damaged experience. Move the scene to an autonomous driving system and the same distortion at a critical moment, with a wrong probability where a brake decision should be, could fail the system and endanger lives. When an algorithm loses trust and traditional testing cannot exhaust every possible error, researchers turn to a field developed over decades in maths and computer science. Formal verification.

Bringing that capability down to the underlying computation of modern AI has been far harder. GPU kernels involve complex parallel execution, floating-point arithmetic and hardware optimisation. Verifying deployed high-performance operators at scale has long been an open problem. That problem just moved.

TensorLean proves the code the hardware runs

On 20 November, researchers from CertiK and Columbia University published TensorLean. It is the first work worldwide to mathematically verify production-grade AI operators at the compiled GPU code level, PTX, while accounting for dynamic task allocation, asynchronous data transfer and floating-point error. It proves whether the actual computation matches the specification.

The team evaluated 149 compiled GPU kernel instances. Eighty-three passed their corresponding Lean specifications on the first attempt. The remaining 66 had defects, 44.3 per cent of the sample. The study covered different GPU generations and included DeepSeek’s DeepGEMM and the widely used FlashAttention operator libraries. Across 6 libraries the team found 38 correctness problems, 2 in DeepSeek’s DeepGEMM and 23 in FlashAttention. A single defect can affect multiple compiled instances.

A GPU kernel travels through several stages from source to execution. A developer writes source in a high-level language such as CUDA C++. A compiler turns it into an intermediate instruction set called PTX, Parallel Thread Execution. Nvidia’s assembler then converts PTX into machine code the hardware runs directly. Along the way the compiler performs heavy translation optimisation. It reorders instructions, merges repeated computation and rearranges data placement. These make programs faster but can introduce errors. The source can be right while the compiled executable is wrong. Verification that checks only source misses compiler-introduced faults. TensorLean checks the compiled PTX directly.

To do that, the team built a complete PTX semantics model in the Lean theorem prover. It describes GPU floating-point rules, Tensor Core behaviour, inter-thread memory synchronisation and asynchronous data copy in precise mathematical language, plus a parser of roughly 1,800 lines that converts compiled PTX into a form ready for mathematical reasoning. The proof is completed in Lean and machine-checkable.

GPU kernel formal verification pipeline
TensorLean verifies compiled PTX, not just source code (Source: 36Kr)

Three hard problems, three solutions

The first is concurrency. A GPU kernel runs hundreds of threads at once, each doing part of the computation, exchanging data and waiting on each other at specific points. Reasoning directly about every thread under every possible schedule is close to impossible. In FlashAttention-4, two groups of threads take turns using the same scratch space. If the next group writes even slightly early, the previous group reads overwritten data. The team ran a related test 17,760 times on an H100 and never observed the error TensorLean found.

TensorLean groups threads by role and analyses one representative thread per group, covering all threads in that group in one pass. For interaction between groups it builds an ordering graph recording which operations are guaranteed to finish before others. It can then automatically prove two things. No two threads read and write the same memory without synchronisation, so there is no data conflict. And all threads terminate, so there is no deadlock. Once those hold, the per-group results can be safely combined.

The second problem is dynamism. In production kernels, loop counts and which data each thread handles are decided at runtime and vary with tensor size and hardware configuration. Verifying one parameter set is too narrow. TensorLean uses loop invariants and mathematical induction to prove that a property holds after every iteration regardless of count, covering all parameter combinations in one proof.

The third is floating-point precision. Maths is exact, GPU floating point rounds. Floating-point addition is not associative. Comparing a kernel’s floating output directly against a formula explodes exponentially with the number of operations. TensorLean uses a two-step method. It introduces an ideal reference assuming exact real arithmetic and proves that reference matches the mathematical specification, ruling out algorithmic errors. It then computes an upper bound on accumulated rounding error between the real floating execution and the ideal. Together the two prove the actual output stays within an acceptable tolerance.

Writing specifications has always been the expensive part of formal verification. The seL4 microkernel has about 8,700 lines of C code and took 20 person-years and 200,000 lines of proof to verify. TensorLean separates mathematical logic from memory layout, letting researchers write specifications at a higher level. A kernel specification splits into four parts. Spec_math describes what the kernel should compute in pure tensor operations. Spec_machine describes what changes in GPU memory. Layout describes how each tensor maps to memory. Constraints adds conditions on inputs and outputs. The team wrote mathematical specifications for 72 open-source kernels, 151,285 lines of source against 10,278 lines of specification, a ratio of about 15 to 1.

FlashAttention and DeepSeek, under the microscope

The team formally verified 27 DeepSeek-related kernels. It also checked all 37 source kernels of FlashAttention-2 through FlashAttention-4, producing 102 compiled instances. FlashAttention went through four generations in four years, from Ampere to Blackwell, each the best attention implementation of its moment. Deployed across PyTorch, vLLM and SGLang, it underpins the training and inference of a large share of language models. Any correctness defect in it can touch every model running on those systems. The 37 kernels span Ampere, Hopper and Blackwell, 55,910 lines of source described with 6,242 lines of specification.

Correctness bug categories found by formal verification
TensorLean classified defects into eight categories (Source: 36Kr)

End-to-end verification from maths to compiled PTX caught a wide range of defects, sorted into eight categories. Algorithmic implementation errors, where the code departs from the intended algorithm. Numerical defects, correct in real arithmetic but wrong under finite precision. Uninitialised memory, read before writing or output positions never written. Memory access errors, wrong layout or missing bounds checks. Data races, two threads hitting the same address with at least one write and no barrier. Deadlocks, circular waits that hang the kernel. Compiler errors, where NVCC, Triton or CuTe DSL turns correct source into incorrect executable code. And undefined behaviour, operations outside the range the language or instruction guarantees.

Some defects surfaced while writing specifications, when a precise mathematical description of intended behaviour revealed the implementation did not match. Others were found during automated proof, when the prover could not complete a path and investigation traced the problem to the kernel itself. All reported defects have been fixed and disclosed to the relevant developers. Even widely trusted and deployed kernels carry correctness risks that testing has not fully caught. Testing validates against finite inputs and cannot exhaust every boundary condition and execution path. Formal verification gives a mathematical guarantee across all inputs within the scope of the specification, lifting reliability from empirical testing to proof.

Infrastructure enters the provable era

Formal verification is not new. Airbus used formal methods in flight-control software more than twenty years ago. Intel embedded it deeply in processor development after the Pentium FDIV defect. Researchers proved the seL4 microkernel fully correct at a cost of roughly 20 person-years, still a landmark case. These examples show where the method has already earned its place. For AI systems that keep growing, whose computation is increasingly complex and whose low-level errors amplify layer by layer, the room is larger.

A single frontier training run can cost hundreds of millions of dollars. Inference serves hundreds of millions of people daily. Underneath sit thousands of GPU kernels doing matrix multiplication, attention and normalisation. To squeeze out performance, engineers optimise those kernels hard for each GPU architecture. Data layout, thread scheduling, precision and memory access all get reworked. The code that finally runs on the GPU is several layers of optimisation away from the formula in the paper. A small error anywhere can be amplified at scale.

University of Michigan researchers recorded a thousand-GPU training incident. A vision-language model ran more than 3,000 steps before monitoring raised an alarm. Engineers spent five days swapping kernels without finding the cause. It turned out to be a data race in a low-level PyTorch routine that corrupted a few rows only under rare input combinations. That is exactly the class of fault TensorLean is built to reach.

For model teams, every optimisation carries correctness risk. Change layout, precision or thread configuration and code that passed tests may fail under new conditions. TensorLean offers a verification method that can be rerun as code changes. Write the mathematical specification, then re-verify after optimisation. When the specification holds, proof can be reused. When optimisation changes the premise, the specification is updated. Engineers get a stricter correctness guarantee while still chasing performance.

For compute providers, larger customer workloads mean low-level errors bring expensive rework, wasted compute and unreliable training or inference. If critical kernels ship with checkable mathematical proof, a platform can offer an auditable layer of correctness on top of performance and stability.

The line of research runs from operating system kernels to AI kernels across a decade. In 2016 Ronghui Gu and Zhong Shao at Yale built CertiKOS, the first complete formal verification of a concurrent operating system kernel. The next year they founded CertiK and applied the method to smart contracts. By 2022 CertiK was a unicorn valued at USD 2 billion. In 2026 Gu’s Columbia lab and CertiK turned to AI infrastructure. Co-first authors Qiang Wei and Yang Ganxiang, with Rong Yi at Columbia and Li Xupeng at CertiK, built TensorLean and used it to verify the GPU kernels DeepSeek and FlashAttention actually run. The methodology carries through.

Model capability is racing ahead. The infrastructure underneath needs an equally serious guarantee of reliability. TensorLean’s step brings production AI code inside the range of formal verification for the first time. For infrastructure still evolving fast, that is the start of a new phase.

Editor’s note: This is an adapted translation of the original 36Kr report. It has been trimmed and restructured for readability for an international business audience. The full original (in Chinese) is at https://36kr.com/p/4081655709406336.

The author’s views are their own and do not necessarily reflect the publisher’s position or constitute investment advice.

Leave a comment