Non-autoregressive Model for Full-line Code Completion

Fang Liu, Zhiyi Fu, Ge Li, Zhi Jin, Hui Liu, Yiyang HaoarXiv 2022

Abstract

Code completion tools are frequently used by software developers to accelerate software development by suggesting the following code elements. Completing a sequence of code tokens (e.g., a full line of code) has been proved more efficient than predicting a single token at a time. To complete the code sequence, researchers are employing AutoRegressive (AR) decoders to generate tokens in a left-to-right, token-by-token fashion. Consequently, the prediction of the next token depends on all previously generated tokens, which leads to high latency in inference. To improve the efficiency and accuracy of full-line code completion, in this paper, we propose a Non-AutoRegressive (NAR) model for code completion boosted by a syntax-aware sampling strategy. Our experimental results on two widely used datasets suggest that our model outperforms both AR and NAR baselines on full-line code completion, and it is faster than the AR model with up to 9 times speed-up.

Introduction

Modern code completion tools like GitHub Copilot typically rely on autoregressive (AR) models. These models generate code one token at a time, from left to right, where each new token depends on the entire preceding sequence. While effective, this sequential nature introduces significant latency—generating a single line of 20 tokens requires 20 separate inference passes. In an IDE environment, where developers expect near-instantaneous feedback, this bottleneck is a major hurdle.

Furthermore, the strict left-to-right generation order of AR models doesn't always align with the logical structure of programming languages. For example, in a Python ternary expression, the first value depends on a condition located to its right. This paper introduces SANAR (Syntax-Aware Non-AutoRegressive model), which breaks this bottleneck by predicting an entire line of code in parallel, achieving up to a 9x speedup without sacrificing accuracy.

The SANAR Architecture

SANAR is built upon a Transformer encoder-decoder framework but introduces several innovations to enable efficient parallel generation.

The architecture of SANAR, featuring the Syntax-Aware Sampling (SAS) module and soft-copy mechanism.

Key components include:

  • Parallel Decoding: Unlike AR models, SANAR predicts all tokens in a target line simultaneously.
  • Length Prediction: To generate tokens in parallel, the model must first determine the target sequence length. It uses a dedicated [LENGTH] token in the encoder for this purpose.
  • Syntax-Aware Sampling (SAS): During training, SANAR employs a two-pass strategy. After an initial "rough draft" prediction, the model "glances" at specific ground-truth tokens to refine its learning. Unlike standard NAR models that pick tokens randomly, SAS prioritizes structural elements like keywords, identifiers, and operators.
  • Soft-Copy Mechanism: This mechanism initializes the decoder by "soft-copying" encoder hidden states based on the relative distance between source and target positions, providing a more informed starting point for generation.

Experimental Setup

The model was evaluated on two large-scale benchmarks: Py150 (150,000 Python files) and GitHub-Java (over 14,000 Java projects). The task was defined as predicting a "full line" of code given a 10-line preceding context.

Key Insights

The authors' research yielded several critical insights into the nature of code generation:

  • Reduced Dependency: Quantitative analysis showed that code tokens have weaker inter-dependencies than natural language tokens, making code a prime candidate for NAR generation.
  • Order Independence: Experiments showed that training an AR model to generate code right-to-left resulted in nearly identical performance to left-to-right generation, suggesting that the traditional order is an arbitrary constraint.
  • Structural Guidance: By focusing on key structural tokens during training, the SAS module helps the model learn the "skeleton" of code more effectively than random sampling.

Results and Performance

SANAR demonstrates superior performance compared to both traditional AR Transformers and existing NAR baselines like CMLM and GLAT.

Performance comparison on the Python dataset, showing SANAR's improvements in accuracy and speed.

As shown in the results for the Python dataset, SANAR achieves a BLEU score of 29.07, a significant jump from the AR Transformer's 21.93. In terms of efficiency, SANAR is 5–6x faster on average, with speedups reaching 9x for longer sequences (10+ tokens). It also maintains high Exact Match (EM) accuracy and exhibits low token repetition, a common issue in other NAR models.

Conclusion and Future Work

SANAR proves that non-autoregressive models can outperform autoregressive ones in both speed and accuracy for full-line code completion. While highly effective for single-line targets, future research will explore extending this approach to multi-line or full-function completion, where inter-line dependencies are more complex. Other potential improvements include more robust length-prediction methods and the integration of Abstract Syntax Trees (ASTs) to further leverage structural information.

Summary updated

BibTeX
@misc{liu2022non,
  author = {Fang Liu and Zhiyi Fu and Ge Li and Zhi Jin and Hui Liu and Yiyang Hao},
  title = {{Non-autoregressive Model for Full-line Code Completion}},
  year = {2022},
  eprint = {2204.09877},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2204.09877},
}
Figure