Abstract We present ShapeFlow, a dynamic abstract interpreter for TensorFlow which quickly catches tensor shape incompatibility errors, one of the most common bugs in deep learning code. ShapeFlow shares the same APIs as TensorFlow but only captures and emits tensor shapes, its abstract domain. ShapeFlow constructs a custom shape computational graph, similar to the computational graph used by TensorFlow. ShapeFlow requires no code annotation or code modification by the programmer, and therefore is convenient to use. We evaluate ShapeFlow on 52 programs collected by prior empirical studies to show how fast and accurately it can catch shape incompatibility errors compared to TensorFlow. We use two baselines: a worst-case training dataset size and a more realistic dataset size. ShapeFlow detects shape incompatibility errors highly accurately -- with no false positives and a single false negative -- and highly efficiently -- with an average speed-up of 499X and 24X for the first and second baseline, respectively. We believe ShapeFlow is a practical tool that benefits machine learning developers. We will open-source ShapeFlow on GitHub to make it publicly available to both the developer and research communities.
Introduction
In the domain of deep learning, tensors—multi-dimensional arrays—serve as the fundamental data structure. For these tensors to interact correctly within a model, their shapes must be compatible. For instance, attempting to multiply a $(3, 6)$ matrix by a $(4, 3)$ matrix results in a runtime error. While conceptually straightforward, these "shape incompatibility errors" are among the most pervasive and frustrating challenges in deep learning development.
Empirical studies highlight that shape incompatibility is the leading cause of bugs in deep learning software, accounting for approximately 45% of reported issues.
These errors are particularly problematic for three reasons:
- Delayed Detection: Errors often manifest deep within training pipelines. A developer might wait hours for preprocessing only to have the program crash during a late-stage epoch.
- Computational Waste: Frameworks perform intensive mathematical operations even when a shape mismatch makes failure inevitable, wasting significant GPU/CPU resources.
- High Developer Burden: Existing static analysis tools often require manual code annotations, which are tedious and error-prone to maintain.
Methodology
To address these issues, we introduce ShapeFlow, a dynamic abstract interpreter built directly into TensorFlow. ShapeFlow operates on the "abstract domain" of tensor shapes rather than the "concrete domain" of numerical data.
The core of ShapeFlow is the Shape Computational Graph. Instead of executing heavy floating-point math, ShapeFlow constructs a replica of the TensorFlow computational graph where edges carry only shape information.
Key technical innovations include:
- API Shadowing: We modified 118 core TensorFlow APIs to consume and emit shapes. For example, a "shape-abstracted" convolution calculates output dimensions based on input shape, filter size, and stride without performing the actual convolution.
- Dynamic Execution: By "running" the program logic dynamically, ShapeFlow can handle data-dependent shapes that static analysis tools typically miss.
- Seamless Integration: Built inside TensorFlow, ShapeFlow leverages the existing framework structure while bypassing expensive operations like backward passes (gradient calculations).
Experimental Evaluation
We evaluated ShapeFlow using a benchmark suite of 52 programs sourced from GitHub and StackOverflow. This suite included both buggy and fixed versions of models using standard datasets such as MNIST, CelebA, and Boston Housing.
We compared ShapeFlow against two baselines:
- Full Training Dataset: Representing the worst-case scenario for error detection latency.
- Sanity Check Dataset: A common developer practice of running code on a tiny subset of data to find immediate bugs.
Results and Discussion
ShapeFlow demonstrated exceptional accuracy and efficiency. It detected bugs with zero false positives and only one false negative (related to API misuse rather than shape mismatch). Compared to static tools like Ariadne and Pythia, ShapeFlow improved bug detection rates by 93% and 14%, respectively.
In terms of efficiency, ShapeFlow achieved:
- An average speedup of 499X compared to full training runs.
- A 24X speedup even when compared to the "sanity check" baseline.
- Extreme cases showed speedups as high as 23,532X.
By stripping away the mathematical computation and focusing solely on structural logic, ShapeFlow allows developers to "preview" model execution in seconds, effectively eliminating the wait time associated with shape-related crashes.
Conclusion and Future Work
ShapeFlow provides a practical, zero-annotation solution for catching one of the most common bugs in deep learning. While currently supporting 118 APIs, future work will focus on expanding API coverage and improving error localization to provide "root cause" analysis. Additionally, we envision automated repair mechanisms that suggest reshape or transpose operations to resolve detected conflicts automatically.
Summary updated