Finding Deep-Learning Compilation Bugs with NNSmith

Jiawei Liu, Jinkun Lin, Fabian Ruffy, Cheng Tan, Jinyang Li, Aurojit Panda, Lingming Zhang · DOI

Abstract

Deep-learning (DL) compilers such as TVM and TensorRT are increasingly being used to optimize deep neural network (DNN) models to meet performance and resource utilization 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. We propose NNSmith, a novel fuzz testing approach for finding bugs in DL compilers. NNSmith generates diverse yet valid DNN test models using light-weight operator specifications and performs gradient-based search to find model inputs that avoid floating-point exceptional values. To date, NNSmith has found 72 new bugs in TVM, TensorRT, ONNXRuntime, and PyTorch, with 51 already fixed by maintainers.

The Challenge of DL Compiler Reliability

Deep learning compilers are critical infrastructure; they translate high-level models (e.g., from PyTorch) into optimized code for specific hardware. However, their complexity makes them prone to bugs. A compiler bug might cause an immediate crash or, more insidiously, a "silent" semantic bug where the model executes but produces incorrect predictions. Such errors are particularly dangerous in safety-critical domains like autonomous driving.

Overview of DL compiler bug causes and symptoms.

Existing testing tools (fuzzers) face two primary hurdles:

  1. Validity vs. Diversity: Randomly connecting layers often leads to type or dimension mismatches. Previous tools either generated overly simple models or used "fix-up" layers that could mask the very bugs they were meant to find.
  2. Numerical Instability: Structurally valid models often produce NaNs (Not-a-Number) or Infinities with random inputs. These floating-point exceptions make differential testing impossible, as outputs cannot be reliably compared against a reference.

The NNSmith Pipeline

NNSmith addresses these challenges through a three-stage automated pipeline designed to maximize both the complexity and the numerical stability of generated test cases.

The NNSmith system architecture, including operator specification, model generation, and value searching.

1. SMT-Powered Graph Generation

NNSmith treats model generation as a constraint-satisfaction problem. By providing symbolic specifications for operators (e.g., how convolution output dimensions relate to stride and padding), the system uses the Z3 SMT solver to incrementally build mathematically valid graphs. To ensure diversity, NNSmith employs Attribute Binning, forcing the solver to explore a wide range of parameter values rather than defaulting to simple constants.

To solve the NaN/Inf problem, NNSmith applies machine learning techniques to the testing process itself. It treats finding valid inputs as an optimization problem, defining loss functions for vulnerable operators (like Sqrt or Log). It then uses backpropagation to "train" the input data and weights until the model produces stable numerical outputs, ensuring that differential testing is meaningful.

3. Differential Testing

Finally, the generated model is compiled by the target compiler (e.g., TensorRT) and its output is compared against a trusted reference (PyTorch). Any significant discrepancy in the results—or a compiler crash—is flagged as a potential bug.

Key Results and Impact

NNSmith was evaluated against four major DL ecosystems: Apache TVM, NVIDIA TensorRT, ONNXRuntime, and PyTorch.

  • Bug Discovery: The framework uncovered 72 new bugs. Of these, 58 have been confirmed and 51 have been fixed by project maintainers.
  • Unprecedented Coverage: NNSmith achieved significantly higher code coverage than previous state-of-the-art tools. For ONNXRuntime, it found 32.7x more unique branches than existing baselines, demonstrating its ability to exercise deep optimization logic.
  • Efficiency: The gradient-guided search is highly efficient, finding viable inputs for 98% of generated models in milliseconds, accounting for only 4% of the total generation time.

Conclusion

NNSmith demonstrates that combining formal constraint solving with gradient-based optimization can effectively navigate the complex search space of deep learning compilers. By ensuring both structural and numerical validity, NNSmith provides a robust framework for hardening the software stack that powers modern AI.

Summary updated

BibTeX
@inproceedings{liu2023finding,
  author = {Jiawei Liu and Jinkun Lin and Fabian Ruffy and Cheng Tan and Jinyang Li and Aurojit Panda and Lingming Zhang},
  title = {{Finding Deep-Learning Compilation Bugs with NNSmith}},
  booktitle = {ASPLOS},
  year = {2023},
  doi = {10.1145/3575693.3575707},
}
Figure