Towards Full-line Code Completion with Neural Language Models

Wenhan Wang, Sijie Shen, Ge Li, Zhi JinarXiv 2020

Abstract

Researchers from Peking University explore a significant evolution in developer tools: transitioning from predicting the next single token to predicting an entire functional line of code. This paper proposes "full-line code completion," a task where neural language models generate complete statements. Experimental results demonstrate that Transformer-based models operating on plain token sequences outperform both Recurrent Neural Networks (RNNs) and complex syntax-based approaches in terms of accuracy and inference speed.

The Challenge of Token-Level Completion

Most modern Integrated Development Environments (IDEs) offer code completion, but they typically operate one "token" at a time—predicting a single variable name, keyword, or operator. For instance, to write a statement like self.dropout = nn.Dropout(p=dropout), a developer using a traditional system might need to trigger and accept suggestions six or seven times.

Comparison between (a) traditional token-level completion and (b) proposed full-line completion.

The authors argue that this granularity is inefficient. Even if a "full-line" prediction is only partially correct, it is often faster for a developer to accept the line and make a minor edit than to manually select every individual component. However, predicting a sequence of tokens is significantly more complex than predicting a single one, and ensuring syntactic validity remains a non-trivial hurdle.

Methodology

The researchers compared two primary philosophies for generating code:

  1. Token-based Models: These treat code as a sequence of words, similar to natural language. The study evaluated Gated Recurrent Units (GRUs) and Transformers (specifically the GPT/GPT-2 architecture). They also utilized Byte-Pair Encoding (BPE) to handle the "Out-of-Vocabulary" problem by breaking rare identifiers into smaller sub-tokens.
  2. Syntax-based Models: To ensure structural correctness, the researchers employed the TranX framework. This approach converts code into a sequence of "actions" used to construct an Abstract Syntax Tree (AST). Instead of predicting words directly, the model predicts the structural steps required to build the code's logic.
An example of an Abstract Syntax Tree (AST) and the corresponding sequence of actions used by syntax-based models to generate code.

To facilitate full-line generation, the models were trained with a special <eol> (end-of-line) token, and string literals were masked with a generic <str> token to reduce noise from unpredictable text strings.

Experimental Setup and Datasets

The study utilized two major Python corpora:

  • Py150: A standard dataset containing 150,000 Python 2 files from GitHub.
  • PyCodeSuggest: A modern dataset consisting of 949 Python 3 projects, characterized by longer and more complex lines of code.

Key Insights and Results

Transformers vs. RNNs

Consistent with trends in Natural Language Processing, Transformer models significantly outperformed RNN-based models (GRUs) across all metrics. Their superior ability to capture long-range dependencies proved crucial for understanding code context.

The "Syntax Tax"

Surprisingly, syntax-based models using AST actions did not outperform simple token-based models. The authors identified a "syntax tax": converting code to AST actions significantly increases sequence length, which burdens the language model. Furthermore, large-scale neural models appear to learn Python's grammar implicitly from data. Manually enforcing AST rules provided little benefit while increasing inference time by nearly 20 times (~1.34s for syntax-based vs. 0.07s for token-based Transformers).

Performance Metrics

On the Py150 dataset, the TransformerLM+Token model achieved an 8.93% top-1 accuracy and a 15.98% top-5 accuracy. Even when the model failed to achieve an exact match, it maintained a high Edit Similarity (approximately 54%), suggesting that incorrect guesses are often close enough to the ground truth to be useful with minimal editing.

Conclusion and Future Work

While token-based Transformers show great promise for real-time full-line completion, limitations remain. Current syntax guidance only ensures basic parsability, not semantic correctness (e.g., checking if variables are defined). Future work aims to incorporate more robust static analysis for "hard" constraints on API usage and explore "copy mechanisms" to allow models to explicitly reuse variables defined earlier in the file context.

Summary updated

BibTeX
@misc{wang2020towards,
  author = {Wenhan Wang and Sijie Shen and Ge Li and Zhi Jin},
  title = {{Towards Full-line Code Completion with Neural Language Models}},
  year = {2020},
  eprint = {2009.08603},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2009.08603},
}
Figure