DLLens: Testing Deep Learning Libraries via LLM-aided Synthesis

Meiziniu Li, Dongze Li, Jianmeng Liu, Jialun Cao, Yongqiang Tian, Shing-Chi Cheung · DOI

Abstract

Deep learning (DL) libraries like TensorFlow and PyTorch are foundational to modern AI but suffer from functional bugs that are difficult to detect without a ground truth oracle. This paper presents DLLens, a system leveraging Large Language Models (LLMs) to enhance differential testing. DLLens addresses the "counterpart gap" by synthesizing equivalent code snippets across libraries and overcomes the "sanity check wall" by using LLMs to infer complex input constraints from execution traces. Evaluated on TensorFlow and PyTorch, DLLens synthesized 1.84x more counterparts than existing methods and uncovered 71 bugs, 59 of which have been confirmed by developers.

Introduction

Deep learning libraries are the bedrock of modern AI, yet they are prone to "functional bugs"—cases where code executes without crashing but produces mathematically incorrect results. Detecting these is challenging because there is often no "ground truth" to compare against, a challenge known as the test oracle problem.

Differential testing addresses this by comparing different implementations of the same algorithm (e.g., TensorFlow’s argmax vs. PyTorch’s argmax). However, this method faces two major hurdles:

  1. The Counterpart Gap: Many APIs lack a direct 1:1 equivalent in another library. Existing tools typically cover only about 10% of APIs.
  2. The "Sanity Check" Wall: DL APIs are heavily guarded by sanity checks (e.g., ensuring tensors are non-empty). Standard static analysis often fails to understand these checks when code calls into external C++ libraries, resulting in incomplete constraints that prevent testers from reaching core logic.

Methodology

DLLens introduces a three-step workflow that leverages the domain knowledge embedded in LLMs to automate the most difficult steps of differential testing.

LLM-aided Counterpart Synthesis

Instead of searching for a single matching API, DLLens asks an LLM to synthesize a wrapper function in a different library that mimics the target API’s behavior. This allows for "composition over mapping," where multiple simple APIs are combined to simulate a complex one. To handle potential hallucinations, DLLens uses an iterative feedback loop, refining the code based on execution errors or output mismatches.

Comparison of counterpart collection approaches, highlighting DLLens's ability to synthesize complex API compositions.

LLM-enhanced Path Constraint Extraction

When static analysis encounters an external C++ function it cannot parse, DLLens provides the LLM with the execution trace and asks it to infer the necessary input constraints. This converts previously "unsolvable" code paths into logical constraints that a Z3 solver can process, allowing the generator to bypass sanity checks and explore deep execution branches.

Comparison of extracted constraints across different testing tools, showing DLLens's superior coverage of required constraints.

Test Input Generation

Using the combined constraints from both the target API and its synthesized counterpart, DLLens generates diverse, valid inputs designed to maximize code coverage and trigger potential discrepancies between the two implementations.

Key Results

The authors evaluated DLLens on the latest stable versions of TensorFlow (v2.10.0) and PyTorch (v2.1.0). Key findings include:

  • Expanded Testing Surface: DLLens synthesized valid counterparts for 1,401 APIs, a 1.84x improvement over previous state-of-the-art techniques.
  • Increased Coverage: On a 200-API sample set, DLLens covered 7.23% more code branches than the best baseline tool.
  • Bug Discovery: The tool detected 71 bugs in total, with 59 confirmed by developers and 46 being previously unknown. Ten of these bugs have already been fixed in recent library releases.
  • Efficiency: Under the same time budget, DLLens detected 1.88x more bugs than existing tools.

Conclusion and Future Work

DLLens demonstrates that LLMs can act as powerful "bridge analyzers," translating complex C++ logic into Python-level constraints and composing disparate APIs into equivalent test oracles. While the approach is highly effective, some synthesized counterparts are only "partially valid," leading to potential false positives. Future work will focus on automatically identifying the valid input ranges for these synthesized functions to further reduce manual verification effort.

Summary updated

BibTeX
@article{li2026dllens,
  author = {Meiziniu Li and Dongze Li and Jianmeng Liu and Jialun Cao and Yongqiang Tian and Shing-Chi Cheung},
  title = {{DLLens: Testing Deep Learning Libraries via LLM-aided Synthesis}},
  journal = {ACM Trans. Softw. Eng. Methodol.},
  year = {2026},
  doi = {10.1145/3735637},
}
Figure