Abstract
Deep-learning (DL) compilers such as TVM and TensorRT are increasingly being used to optimize deep neural network (DNN) models to meet performance, resource utilization and other requirements. Bugs in these compilers can result in models whose semantics differ from the original ones, producing incorrect results that corrupt the correctness of downstream applications. However, finding bugs in these compilers is challenging due to their complexity. In this work, we propose a new fuzz testing approach for finding bugs in deep-learning compilers. Our core approach consists of (i) generating diverse yet valid DNN test models that can exercise a large part of the compiler's transformation logic using light-weight operator specifications; (ii) performing gradient-based search to find model inputs that avoid any floating-point exceptional values during model execution, reducing the chance of missed bugs or false alarms; and (iii) using differential testing to identify bugs. We implemented this approach in NNSmith which has found 72 new bugs for TVM, TensorRT, ONNXRuntime, and PyTorch to date. Of these 58 have been confirmed and 51 have been fixed by their respective project maintainers.
TL;DR
NNSmith is a novel fuzz testing framework designed to find bugs in deep learning compilers like TVM and TensorRT. It generates highly diverse, structurally valid neural network graphs using SMT solvers and employs a clever gradient-guided search to find inputs that avoid floating-point exceptions. This approach successfully uncovered 72 new bugs across major DL compilers, significantly outperforming existing fuzzers in code coverage.
What Problem Does This Paper Address?
Deep learning (DL) compilers (e.g., TVM, TensorRT, ONNXRuntime) take trained neural networks and heavily optimize them for specific hardware. Because these compilers are incredibly complex, they contain bugs that can cause silent errors—producing incorrect predictions without crashing. These bugs typically arise during model conversion or internal graph transformations.
To find these bugs, researchers use differential testing: generate a random model, run it through the compiler, run it through a trusted reference (like PyTorch), and compare the outputs. However, this faces two massive hurdles:
- Generating Valid Graphs: You can't just wire random neural network layers together. A
Conv2dlayer's output shape must perfectly match the input requirements of the next layer. Prior fuzzers struggled with this, either limiting the types of layers they used or forcing unnatural padding, which severely limited the diversity of the test models. - The NaN/Inf Problem: Even if the graph is structurally valid, feeding it random weights and inputs almost always results in floating-point exceptions (NaNs or Infs) as values explode or collapse through the layers. If a model outputs NaN, you can't compare it against the reference to check for compiler bugs, rendering the test useless.
Methods
NNSmith tackles these challenges with a three-pronged approach integrated into a unified fuzzing loop.
- SMT-Driven Graph Generation (§3.1 & §3.2): NNSmith requires developers to write lightweight specifications for each operator (e.g., rules about how
Conv2dchanges tensor shapes). It then uses an SMT solver (Z3) to incrementally stitch operators together, mathematically guaranteeing that the output shape of one node satisfies the input constraints of the next. - Attribute Binning: SMT solvers are "lazy" and often pick boundary values. To force the solver to generate diverse attributes (necessary to trigger complex compiler optimizations), NNSmith randomly restricts attributes to exponential "bins" (e.g., forcing a kernel size to be between 8 and 15).
- Gradient-Guided Value Search (§3.3): To solve the NaN/Inf problem, NNSmith treats numerical stability as an optimization problem. It defines "safe" input domains for vulnerable operators. If a random input causes a NaN, NNSmith calculates a loss based on the domain violation and uses gradient descent to tweak the inputs/weights until the model executes cleanly.
Datasets
NNSmith does not use static datasets like ImageNet. Instead, it dynamically generates its own test cases—randomized, valid DNN computation graphs and corresponding input tensors—to evaluate the compilers. The systems under test include ONNXRuntime, Apache TVM, TensorRT, and PyTorch.
Key Insights and Ideas
- SMT Solvers need a nudge for fuzzing: While SMT solvers are perfect for ensuring graph validity, their deterministic nature is the enemy of fuzzing diversity. "Attribute Binning" is a highly effective hack to force exploration of the wider configuration space.
- Numerical validity is a massive blind spot: The authors found that 56.8% of randomly generated 20-node models produce NaNs or Infs. Prior fuzzing papers largely ignored this, leading to wasted compute and false alarms.
- Gradients aren't just for training: Using backpropagation to avoid floating-point exceptions is a brilliant inversion of traditional adversarial ML techniques, allowing NNSmith to "fix" model inputs in milliseconds.
Key Results
- Bug Discovery: NNSmith found 72 new bugs (58 confirmed, 51 fixed) across TVM, TensorRT, ONNXRuntime, and PyTorch, including both crash bugs and silent semantic bugs.
- Superior Coverage: NNSmith achieved 1.8x higher total branch coverage and 32.7x higher unique branch coverage on ONNXRuntime compared to state-of-the-art graph fuzzers like LEMON.
- Search Efficiency: The gradient-guided search successfully finds numerically valid inputs for 98% of generated models with negligible overhead (~4.2% of model generation time).
Limitations and Future Work
- False Alarms from Floating-Point Math: Valid compiler optimizations can legitimately change floating-point results slightly, sometimes triggering false positives that require high error tolerances.
- Oracle Dependency: NNSmith relies heavily on PyTorch as the ground truth. While this found 10 PyTorch bugs, it complicates fault localization when the reference itself is flawed.
- Coverage Ceilings: The fuzzer does not reach 100% coverage, as DL compilers contain massive amounts of code for hardware-specific backends and auto-tuning that graph-level fuzzing cannot easily trigger.
Summary updated