Free Lunch for Testing: Fuzzing Deep-Learning Libraries from Open Source

Anjiang Wei, Yinlin Deng, Chenyuan Yang, Lingming Zhang

Abstract

Deep learning (DL) libraries like PyTorch and TensorFlow are the bedrock of modern AI, yet testing them is challenging due to their complex, dynamically-typed Python APIs. We present FreeFuzz, an automated testing framework that mines existing open-source code, documentation, and models to generate high-quality test cases. By tracing real-world API usage and applying mutation-based fuzzing, FreeFuzz achieves significantly higher coverage than previous tools and has uncovered 49 new bugs in major DL libraries.

The Challenge of Testing DL Libraries

Deep learning models are increasingly deployed in safety-critical domains, from autonomous vehicles to medical diagnostics. While substantial effort has gone into testing the models themselves, the underlying libraries—the C++ and Python engines that power them—are often overlooked.

Testing these libraries is notoriously difficult. Their public APIs are primarily exposed in Python, a dynamically typed language. This makes it hard for automated tools to infer the specific input constraints (e.g., tensor shapes, data types, or valid parameter ranges) required by an API. Previous "fuzzing" attempts often relied on mutating entire models. This "system-level" approach is slow, covers only a fraction of available APIs, and frequently fails because random mutations break the strict mathematical constraints necessary for a model to execute.

The multi-layered architecture of deep learning libraries makes them difficult to test.

FreeFuzz: Mining the "Free Lunch"

The authors introduce FreeFuzz, an API-level fuzzer that leverages the "free lunch" provided by the vast ecosystem of existing open-source code. Instead of manually defining API specifications, FreeFuzz observes how developers already use these libraries.

The Four-Stage Pipeline

The FreeFuzz workflow consists of four main stages:

  1. Code Collection: FreeFuzz automatically scrapes three primary sources: code snippets from official documentation, developer-written library tests, and "models in the wild" from GitHub.
  2. Dynamic Instrumentation: Rather than static analysis, FreeFuzz runs the collected code. It instruments the library to record the exact types, values, and tensor shapes used for every API call, building a "value space" of valid inputs.
  3. Mutation-Based Fuzzing: New test cases are generated by mutating recorded inputs using three strategies:
  4. Type Mutation: Altering tensor dimensions or data types.
  5. Random Value Mutation: Swapping in random but valid primitives.
  6. Database Value Mutation: A novel approach where FreeFuzz borrows values from "similar" APIs (identified via name similarity) to expand the search space.
  7. Test Oracles: To detect failures, FreeFuzz employs differential testing (comparing CPU vs. GPU results or Aten vs. CuDNN backends) and metamorphic testing (e.g., verifying that float16 operations provide the expected speedup over float32).
The FreeFuzz workflow: from mining open-source code to automated bug detection.

Experimental Evaluation

FreeFuzz was evaluated on PyTorch 1.8 and TensorFlow 2.4, targeting over 2,500 APIs. The testing database was built from ~1,000 documentation snippets, ~2,900 developer tests, and over 200 GitHub models.

Key Results

  • Superior Coverage: FreeFuzz successfully fuzzed 1,158 popular APIs—9x more than the previous state-of-the-art tool, LEMON.
  • Bug Detection: The framework identified 49 new bugs (28 in PyTorch, 21 in TensorFlow). At the time of publication, 38 had been confirmed by developers and 21 were already fixed.
  • Efficiency: By focusing on the API level rather than full models, FreeFuzz achieved a 3.5x lower overhead than model-level mutation tools.
  • Performance Insights: One notable bug involved a performance regression where float16 was slower than float32 on specific hardware, highlighting the value of metamorphic testing for performance-critical software.

Discussion and Future Work

The "Free Lunch" concept demonstrates that we don't need manual specifications to test complex libraries; we can simply observe existing usage. However, FreeFuzz currently focuses on single APIs in isolation. Future work will address stateful errors that only emerge during specific sequences of API calls and more complex inter-parameter dependencies that simple mutations might miss.

By bridging the gap between unit testing and system-level fuzzing, FreeFuzz provides a scalable, automated path toward more robust and reliable deep learning infrastructure.

Summary updated

BibTeX
@inproceedings{wei2022free,
  author = {Anjiang Wei and Yinlin Deng and Chenyuan Yang and Lingming Zhang},
  title = {{Free Lunch for Testing: Fuzzing Deep-Learning Libraries from Open Source}},
  booktitle = {ICSE},
  year = {2022},
  doi = {10.1145/3510003.3510041},
}
Figure