Predictive synthesis of API-centric code

Daye Nam, Baishakhi Ray, Seohyun Kim, Xianshan Qu, Satish Chandra

Abstract

Program synthesis for API-heavy libraries like PyTorch often faces a search space explosion. We present a deep-learning approach that uses a Compositional Model to predict sequences of API calls directly. By representing intermediate program states in a latent space, the model bypasses exhaustive search, reducing synthesis time by an order of magnitude while maintaining high accuracy on real-world benchmarks.

Introduction

Modern data science relies on extensive libraries such as PyTorch, NumPy, and TensorFlow. Finding the exact sequence of functions to transform a specific input tensor into a desired output is a significant challenge for developers. Traditional enumerative synthesis—which exhaustively tests combinations of functions—suffers from exponential search space growth as program length increases. While previous machine learning approaches like DeepCoder prioritized likely functions, they still required the synthesizer to determine the correct sequence and structure through trial and error.

Methods

The proposed approach utilizes a Compositional Model based on Recurrent Neural Networks (RNNs) that treats API functions as semantic transformations rather than simple text tokens.

Model Architecture

The system consists of three main components: tensor encoding, latent state representation, and sequence prediction. Input and output tensors (including values, shapes, and types) are converted into fixed-length vectors. The RNN’s hidden state serves as a "neural proxy" for intermediate program values, learning to represent how functions like stack or transpose transform a tensor's meaning within this hidden vector space.

The end-to-end pipeline: Tensor values are encoded into vectors and processed by a Compositional Model (RNN) to predict the API sequence.

Prediction Modes

The model operates in two primary modes to guide the synthesizer:

  • Full-Seq: The model predicts the entire sequence of API calls in a single pass based on the initial input and final output.
  • First-Of-Sequence (FOS): The model predicts the first function, the synthesizer computes the actual intermediate value, and the model is queried again for the subsequent function.
Comparison of synthesis strategies: (a) Enumerative, (b) Prioritization, (c) Full-Seq, and (d) First-Of-Sequence (FOS).

By predicting the sequence, the synthesizer avoids guessing which functions to use and instead focuses on searching for the correct arguments (e.g., dimensions or indices) to complete the program.

Experimental Setup

The model was trained on a Synthetic Dataset of 5.5 million training pairs, generated by piping random tensors through 202 unique API sequences using 16 core PyTorch functions. To evaluate real-world performance, the authors used Stack Overflow (SO) Benchmarks, consisting of problems sourced from actual developer queries.

Results and Key Insights

The proposed model demonstrates significant performance gains over state-of-the-art baselines. On PyTorch benchmarks, average synthesis time dropped from 10.01 seconds to 1.04 seconds. In one complex scenario, the vanilla synthesizer required nearly 55 seconds, whereas the FOS model found a solution in 0.49 seconds. The model achieved approximately 79% top-1 accuracy on synthetic tests and 76% top-3 accuracy on Stack Overflow benchmarks.

A key insight is the model's ability to perform Composition without Intermediate Values. The RNN "imagines" intermediate states within its hidden layers, allowing it to predict multi-step sequences without explicitly seeing intermediate tensors. Furthermore, the model generalizes to unseen sequences because it learns the underlying behavior of individual functions.

Conclusion

This research demonstrates that neural networks can effectively learn the semantics of API transformations to accelerate program synthesis. While currently focused on a subset of PyTorch and numeric tensors, future work aims to scale the approach to larger libraries, automate argument prediction, and expand to more complex data structures like Pandas DataFrames.

Summary updated

BibTeX
@inproceedings{nam2022predictive,
  author = {Daye Nam and Baishakhi Ray and Seohyun Kim and Xianshan Qu and Satish Chandra},
  title = {{Predictive synthesis of API-centric code}},
  booktitle = {MAPS@PLDI},
  year = {2022},
  doi = {10.1145/3520312.3534866},
}
Figure