Formal Verification of Production-Grade AI Operators: A Breakthrough at the GPU IR Level

    This article examines a production incident in which a runtime optimization altered low-level numerical computation paths and corrupted model outputs, and explains why formal verification is needed for GPU kernels. It covers the three main obstacles to verifying GPU kernels, the breakthrough of verifying at the compiled GPU intermediate representation using Lean, evaluation results across 149 compiled kernel instances from six libraries, and practical recommendations for engineering adoption.

    1. Starting From an “Output Anomaly”: Why Low-Level Computation Needs Mathematical Guarantees

    A cloud provider disclosed in a post-incident review that a runtime optimization intended to accelerate execution had affected some accelerator servers, causing anomalies such as mixed languages in model output and code syntax errors. The timeline was roughly: anomalies appeared at the end of one month, and the optimization was rolled back early the next month. The root cause was not the model architecture. The optimization had changed the underlying numerical computation path, giving tokens that originally had extremely low probability an abnormally high probability and distorting the output distribution.

    In chat and code-writing scenarios, this distortion shows up as garbled text. Put the same computation error into an autonomous driving, industrial control, or medical inference pipeline, and a single wrong probability value or threshold decision could have safety consequences. Traditional testing relies on test case coverage, but factors such as floating-point non-associativity, parallel reduction order, and asynchronous copy timing create a state space that is nearly impossible to exhaust. This is why formal verification is needed: use mathematical proof to show that an implementation satisfies a given specification under all inputs and all scheduling interleavings.

    2. Why It Is Hard: Three Obstacles to Formal Verification of GPU Kernels

    1. Parallelism and scheduling nondeterminism: interleaving of thread block and warp execution makes results depend on reduction order.
    2. Asynchronous data movement: multi-stage pipelines and barrier semantics turn “when data is ready” into a state problem.
    3. Floating-point error accumulation: integer equivalence cannot be substituted, so a provable error bound must be provided.

    Traditional approaches mostly verify at the source code level, but compiler optimizations, instruction selection, and scheduling introduce new behavior. Passing at the source level does not mean the compiled artifact passes.

    3. The Breakthrough: Moving Verification to the Post-Compilation Intermediate Representation

    This work is the first to model dynamic task allocation, asynchronous transfers, and floating-point error simultaneously on the compiled GPU intermediate representation of production-grade operators, and to provide machine-checkable proofs in Lean. There are three reasons for choosing this level:

    • Close to real execution: the verification target is the instruction sequence ultimately submitted to the hardware.
    • Optimizations cannot be bypassed: behavior after compiler transformations is fully included in the proof obligations.
    • Reusable specifications: different compilation instances of the same operator share a specification, so defects can be attributed across instances.

    A schematic verification skeleton is shown below (pseudocode, expressing the idea only):

    1. theorem kernel_correct (x : Tensor) :
    2. ‖ exec ptx (compile kernel) x - reference x ‖ ≤ ε := by
    3. -- 1) Enumerate scheduling interleavings; independence from reduction order
    4. -- 2) No read dependency before asynchronous copy completes
    5. -- 3) Floating-point error accumulates by level, converging to ε
    6. sorry

    4. What the Evaluation Results Show

    The research team evaluated 149 compiled kernel instances. 83 passed their corresponding specifications on the first attempt, while 66 were found to have defects, accounting for 44.3% of the sample. The evaluation covered different generations of accelerator architectures and involved six libraries, including an open-source matrix multiplication library and an attention operator library widely used in the industry. A total of 38 correctness issues were found: 2 in the matrix multiplication library and 23 in the attention library, with the same defect potentially affecting multiple compilation instances.

    The key takeaway from this data is that high performance and correctness are not naturally compatible. The defects were concentrated in synchronization semantics and boundary handling, not in the algorithms themselves.

    5. Recommendations for Engineering Adoption

    1. Establish a “specification first” process: write the reference implementation and error budget before writing the kernel.
    2. Layered verification: do logic verification at the source level and artifact verification at the intermediate representation level.
    3. Close the defect loop: turn counterexamples into regression test cases and incorporate them into continuous integration and monitoring alerts.
    4. Cloud support: use a container platform to pin the toolchain, use object storage to save proof artifacts, and use logging and monitoring alert services to track verification tasks.

    Formal verification does not replace testing. It hands the “impossible to exhaust” part to mathematics and leaves engineering efficiency to the team.