Retrieval-Based Neural Code Generation

Shirley Anugrah Hayati, Raphaël Olivier, Pravalika Avvaru, Pengcheng Yin, Anthony Tomasic, Graham Neubig

Introduction

Generating source code from natural language (NL) descriptions is a complex structured prediction task. While standard neural models have made significant strides in mapping text to sequences, they often struggle with the rigid, hierarchical nature of programming languages and the "long tail" of rare logic. This paper, authored by researchers at Carnegie Mellon University, introduces RECODE, a framework that leverages a "search and edit" intuition to improve the synthesis of complex code structures.

The Structured Prediction Challenge

Code is typically represented as an Abstract Syntax Tree (AST). Standard tree-based models often fail to capture long-range dependencies or memorize intricate structures required for complex programming tasks. Furthermore, these models attempt to learn the entire mapping from text to code within their neural weights, making them brittle when encountering rare phrases or variable names not frequently seen during training.

RECODE addresses these limitations by shifting the paradigm from pure generation to retrieval-augmented generation. The core hypothesis is that if a similar problem has been solved in the training data, that solution should serve as a template for the current query.

The RECODE Framework

RECODE operates as a retrieval-augmented pipeline integrated into a syntactic code generation model. The process consists of four primary stages:

  1. Retrieval: Using a dynamic programming-based edit distance metric, the system identifies the top $M$ most similar NL descriptions in the training set for a given input query.
  2. Action Subtree Extraction: Rather than retrieving raw text, RECODE extracts $n$-grams of action sequences—the specific steps used to construct the AST of the retrieved examples. These serve as structural templates.
  3. Word Substitution and Alignment: To make templates useful, RECODE must account for different variable names. It employs a sentence-to-sentence alignment method to identify correspondences (e.g., mapping a retrieved variable lst to the query's params) and performs the necessary swaps.
  4. Retrieval-Guided Decoding: During beam search, the model boosts the log-probability of actions that align with the retrieved $n$-gram templates, weighted by the similarity of the retrieved sentence to the query.
RECODE's retrieval-guided decoding process, illustrating the alignment of retrieved tokens (e.g., swapping 'lst' for 'params') within the AST construction flow.

Experimental Evaluation

The authors evaluated RECODE on two distinct benchmarks: Hearthstone (HS), which contains complex Python classes for card game logic, and Django, which consists of diverse Python snippets paired with English pseudo-code.

Key Results

RECODE consistently outperformed state-of-the-art baselines, such as the Yin and Neubig (2017) model, across both datasets. On the Hearthstone dataset, which is characterized by high structural complexity, RECODE achieved a 78.4 BLEU score (a +2.6 improvement) and a 19.6% exact match accuracy. On the Django dataset, it reached 84.7 BLEU and 72.8% accuracy.

Performance comparison of RECODE against state-of-the-art baselines on the Hearthstone (HS) and Django datasets.

Qualitative analysis revealed that RECODE is particularly effective at generating complex logic, such as ternary operators, by successfully retrieving and adapting similar conditional structures from the training set.

Discussion and Limitations

While RECODE represents a significant advancement, several challenges remain. The model occasionally suffers from variable copying errors, where it selects the incorrect variable from the input description. Additionally, it can struggle with specific type-casting logic if the retrieved template does not perfectly match the required data type. Finally, the current heuristic alignment based on edit distance may be too rigid for highly diverse natural language phrasing, suggesting a need for more robust neural-based alignment methods in future work.

Conclusion

RECODE demonstrates that treating a training set as a library of templates rather than just a source for weight optimization can significantly enhance neural code generation. By focusing on AST construction actions and structural $n$-grams, the framework ensures that retrieved components are syntactically valid and contextually relevant, providing a more reliable path toward automated programming.

Summary updated

BibTeX
@inproceedings{hayati2018retrieval,
  author = {Shirley Anugrah Hayati and Raphaël Olivier and Pravalika Avvaru and Pengcheng Yin and Anthony Tomasic and Graham Neubig},
  title = {{Retrieval-Based Neural Code Generation}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2018},
  doi = {10.18653/v1/d18-1111},
}
Figure