Improving Neural Program Synthesis with Inferred Execution Traces

Richard Shin, Illia Polosukhin, Dawn SongNeurIPS 2018

Program synthesis—the task of automatically generating code that satisfies a given specification, such as a set of input/output pairs—remains a notoriously difficult challenge in artificial intelligence. While deep learning has driven massive progress in this area, most modern neural approaches attempt to map input/output (I/O) pairs directly to code using standard encoder-decoder architectures.

This paper introduces a highly effective two-step approach to neural program synthesis: first, inferring step-by-step execution traces from I/O examples, and then synthesizing the final code from those traces. By decomposing the problem, the authors achieved a new state-of-the-art accuracy on the Karel programming domain, significantly outperforming prior end-to-end models.

The Challenge of Sparse Specifications

The primary hurdle in program synthesis is that I/O pairs are "sparse" specifications. When mapping directly from I/O to code, a model must simultaneously learn two complex things: the semantics of the programming language (e.g., what a move() command actually does to a grid) and the complex logic of control flow (how if and while blocks should be structured to achieve the goal).

"Execution traces"—the step-by-step sequence of every action a program takes—provide a much richer roadmap. However, they are rarely available in real-world scenarios because asking a user to provide a full execution trace is almost as much work as writing the code itself. The authors bypass this limitation by training a model to guess the trace first.

A Two-Step Solution: Inferring Traces First

The authors propose a pipeline that splits the synthesis task into two distinct neural models:

1. I/O $\to$ TRACE

This model takes the input/output grids and predicts the sequence of atomic actions (e.g., move, turnLeft) that would successfully transform the input into the output.

Crucially, during trace generation, the model uses a Karel interpreter to track the state of the grid after each predicted action. This "current state" is fed back into the LSTM decoder. This offloads the burden of "remembering" the world state from the neural network to a reliable interpreter, allowing the network to focus purely on the next logical step.

Architecture of the I/O to Trace model, showing how intermediate grid states are fed back into the decoder.

2. TRACE $\to$ CODE

The second model takes the original I/O pairs plus the execution traces generated in step one. It uses a bidirectional LSTM to embed the trace (interleaving grid states and actions) and then applies an attention mechanism to generate the final structured code, complete with if, while, and repeat blocks.

Key Insights and Model Resilience

Breaking the problem down yields several structural advantages:

  • Semantic Offloading: By predicting traces first, the model doesn't have to "guess" what a program does in its head. The interpreter provides the ground truth of the world state at every step, allowing the synthesis model to focus purely on recovering high-level control flow.
  • Traces are Easier than Code: A trace is a linear sequence of actions without complex nesting. It is fundamentally easier for a Recurrent Neural Network (RNN) to predict a linear trace than a tree-structured program with loops and conditionals.
  • Resilience to Imperfect Traces: Interestingly, the I/O $\to$ TRACE model doesn't have to be perfect. Even if the inferred trace differs from the "gold" (ground-truth) trace—such as taking a slightly different path to the same result—the second model is trained on these inferred traces and learns to synthesize a correct, semantically equivalent program anyway.

State-of-the-Art Results on Karel

The study evaluates this approach on the Karel dataset, a popular benchmark where an agent moves around a grid-world, avoids walls, and manipulates markers. The training set consists of over 1.1 million entries.

The authors compared their two-step Maximum Likelihood Estimation (MLE) model against the previous state-of-the-art (Bunel et al., 2018), which relied on complex Reinforcement Learning (RL) techniques.

Results table comparing the two-step approach against prior baselines.
  • Generalization Accuracy: The two-step model reached 81.3%, surpassing the previous RL-based SOTA of 77.12%.
  • Top-50 Guided Search: When allowed to generate 50 candidate programs and check them against the I/O pairs, the accuracy climbed to an impressive 90.8%.
  • Scaling with Complexity: The approach showed the most dramatic improvements on long programs (30+ tokens), where it improved accuracy by over 12.4% compared to standard I/O-to-Code baselines.

Notably, these results were achieved using standard supervised learning, which is generally more stable and easier to tune than RL methods.

Limitations and Future Work

While highly successful in the Karel grid world, the authors note a few areas for future exploration. Currently, the model predicts a trace for each I/O pair independently, which can sometimes lead to traces that imply different underlying logic for the same program. Future work could condition trace generation on previously generated traces to ensure consistency.

Additionally, the two models are currently trained separately. Joint training using RL—where the reward is based on the final code's performance—could push accuracy even higher. Finally, it remains to be seen how well this "trace-first" approach scales to more complex, general-purpose languages like Python or C++, where state tracking is significantly more complex than a 2D grid.

Summary updated

BibTeX
@inproceedings{shin2018improving,
  author = {Richard Shin and Illia Polosukhin and Dawn Song},
  title = {{Improving Neural Program Synthesis with Inferred Execution Traces}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2018},
  url = {https://openreview.net/forum?id=HJNBvwbOWB},
}
Figure