TF-Coder: Program Synthesis for Tensor Manipulations

Kensen Shi, David Bieber, Rishabh Singh · DOI

Abstract

The success and popularity of deep learning is on the rise, partially due to powerful deep learning frameworks such as TensorFlow and PyTorch that make it easier to develop deep learning models. However, these libraries also come with steep learning curves, since programming in these frameworks is quite different from traditional imperative programming with explicit loops and conditionals. In this work, we present a tool called TF-Coder for programming by example in TensorFlow. TF-Coder uses a bottom-up weighted enumerative search, with value-based pruning of equivalent expressions and flexible type- and value-based filtering to ensure that expressions adhere to various requirements imposed by the TensorFlow library. We train models to predict TensorFlow operations from features of the input and output tensors and natural language descriptions of tasks, to prioritize relevant operations during search. TF-Coder solves 63 of 70 real-world tasks within 5 minutes, sometimes finding simpler solutions in less time compared to experienced human programmers.

Introduction: The Challenge of Tensor Programming

Deep learning frameworks like TensorFlow have revolutionized AI development, but they present a notoriously steep learning curve. Unlike traditional imperative programming, which relies on explicit loops and conditionals, tensor programming requires developers to think in terms of "vectorization." This involves translating a mental model of data transformation into a sequence of specialized operations that must adhere to complex broadcasting rules, dimension reshapes, and data type constraints.

Developers often spend significant time searching documentation or forums like StackOverflow to find the right combination of functions for seemingly simple tasks, such as gathering specific columns from a 3D tensor. TF-Coder addresses this by allowing developers to specify their intent through input/output examples and optional natural language descriptions, automatically synthesizing the necessary TensorFlow code.

TF-Coder takes input/output examples and natural language to produce optimized TensorFlow code.

The core of TF-Coder is a bottom-up weighted enumerative search designed to navigate the massive space of possible TensorFlow programs. The system incorporates three primary innovations:

  1. Weighted Value Search: Rather than a simple breadth-first search, TF-Coder assigns weights to operations based on their complexity and general utility. This allows the search to prioritize common operations like tf.add over more niche functions.
  2. Two-Stage Filtering: To combat the exponential explosion of the search space, TF-Coder employs a sophisticated filtering system. Argument filters validate individual inputs (e.g., ensuring a tensor is numeric), while combination filters ensure multiple arguments are compatible (e.g., checking if shapes are broadcastable). This pruning strategy eliminates 98.6% of the potential program space before execution.
  3. ML-Guided Search: Two machine learning models guide the search process. One analyzes tensor features (shapes, statistics, and data patterns), while the other processes natural language descriptions. These models predict which operations are most likely to be relevant, dynamically lowering their weights to prioritize them during enumeration.
TF-Coder's search process uses weights and Cartesian products with filtering to efficiently explore the program space.

Datasets and Training

To evaluate and train TF-Coder, the authors utilized several data sources:

  • Real-World Benchmarks: A set of 70 tasks, with 50 sourced from StackOverflow and 20 from industrial use cases at Google.
  • Synthetic Training Data: To overcome the lack of a large-scale "TensorFlow query" dataset, the authors generated 40 million synthetic tasks. This was achieved by running the search algorithm on random inputs and collapsing the resulting code subtrees into new training examples.
  • Natural Language Data: The NL model was trained on TensorFlow documentation and approximately 14,000 functions scraped from GitHub.

Key Insights

  • Aggressive Pruning is Essential: The success of TF-Coder demonstrates that even massive search spaces are manageable when domain-specific constraints (like TensorFlow's internal rules) are used for aggressive pruning.
  • Machine-Generated Elegance: TF-Coder frequently discovered solutions that were more concise and efficient than those provided by human experts on StackOverflow. For example, it might combine tf.where and tf.sequence_mask in ways that are non-obvious to even experienced developers.
  • Multi-Modal Synergy: Combining data-driven signals from tensor features with intent-driven signals from natural language provides a significantly more effective search signal than using either modality in isolation.

Results and Performance

TF-Coder demonstrated remarkable efficiency and accuracy:

  • Success Rate: It solved 63 out of 70 real-world benchmarks.
  • Speed: For StackOverflow-derived tasks, the median solve time was just 1.6 seconds.
  • Human Comparison: In a controlled study, TF-Coder solved 100% of the tasks provided, whereas human volunteers (including professional engineers) achieved only a 50% success rate within a 5-minute limit.
  • Ablation Study: The inclusion of ML guidance resulted in a 1.78x average speedup over unguided search, highlighting the importance of the learning component.

Limitations and Future Work

While powerful, TF-Coder has limitations. It is currently optimized for programs with 4–6 operations; beyond this, the search space becomes prohibitively large. Its scope is also limited to standard tensor operations, excluding string manipulations, ragged tensors, or complex control flow like tf.while_loop. Future work includes expanding the domain scope and integrating the tool directly into IDEs to assist developers in real-time.

Summary updated

BibTeX
@article{shi2020tf,
  author = {Kensen Shi and David Bieber and Rishabh Singh},
  title = {{TF-Coder: Program Synthesis for Tensor Manipulations}},
  journal = {ACM Trans. Program. Lang. Syst.},
  year = {2020},
  doi = {10.1145/3517034},
}
Figure