NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

LongCoder: A Long-Range Pre-trained Language Model for Code Completion

Daya Guo, Canwen Xu, Nan Duan, Jian Yin, Julian J. McAuley

The Context Bottleneck in Code Completion

Most modern code completion tools rely on Transformer architectures. While highly capable, standard Transformers suffer from a "quadratic bottleneck": their computational cost grows by the square of the input length ($O(n^2)$). Consequently, most models are constrained to a limited context window, typically 512 or 1,024 tokens.

In real-world software development, this is insufficient. A standard Python file on GitHub averages over 1,300 tokens. Critical contextual information—such as library imports, global variables, or class definitions—is frequently located at the top of the file, well outside the reach of a standard model's context window. When an AI "forgets" these distant details, it hallucinates incorrect function names or fails to recognize imported variables.

To address this, researchers introduce LongCoder, a sparse Transformer model designed to handle long-range dependencies in code completion efficiently.

The LongCoder Approach: Sparse Attention

LongCoder overcomes the quadratic bottleneck by replacing standard self-attention with a three-pronged sparse attention mechanism. This reduces the computational complexity to linear time ($O(n)$) while preserving access to distant, critical code snippets.

The architecture relies on three specific attention types:

  1. Window Attention: The model primarily attends to a local "sliding window" of code (e.g., the last 512 tokens). This handles the immediate, line-by-line logic of the program.
  2. Bridge Attention: To prevent information loss over long distances, LongCoder inserts "bridge tokens" at regular intervals. These tokens aggregate information from their local neighborhood, allowing other tokens to "hop" across the file and access distant context in just two steps without the massive memory overhead of attending to every character.
  3. Global Attention (Memory Tokens): This is LongCoder's core innovation. Using the tree-sitter parser, the model extracts the Abstract Syntax Tree (AST) of the code to identify high-value structural statements, such as import calls, class definitions, and function signatures. It designates the line breaks of these statements as "memory tokens" that are globally accessible to every other token in the file.

Unlike general-purpose long-range Transformers (like Longformer or BigBird) that use random or fixed global tokens, LongCoder relies on code-specific heuristics. By explicitly identifying imports and definitions, the model mimics how a human programmer naturally scrolls to the top of a file to verify a definition.

Introducing the LCC Benchmark

Because existing benchmarks like CodeXGLUE were too short to adequately test long-range modeling, the researchers introduced LCC (Long Code Completion).

LCC features Python, Java, and C# files curated specifically to require long-range context. On average, the examples in LCC are five times longer than those in previous datasets. LongCoder was pre-trained on the CodeSearchNet dataset and evaluated across LCC, CodeXGLUE (for short code), and RepoBench XF-R (for cross-file completion).

Key Results

Despite having only 150 million parameters, LongCoder punches well above its weight class by utilizing smart, code-aware attention:

  • LCC Performance: On the Long Code Completion benchmark, LongCoder achieved an Exact Match (EM) score of 17.88% in Python. This significantly outperformed standard Transformers (10.64%) and even general-purpose sparse models like BigBird (17.03%).
  • Short Context Reliability: On CodeXGLUE, LongCoder maintained state-of-the-art performance, proving that its long-range optimizations do not degrade its ability to handle short-range logic.
  • The Importance of Memory Tokens: An ablation study revealed that removing the AST-based "memory tokens" caused a noticeable drop in accuracy. This confirms that explicitly "remembering" imports and definitions is vital for accurate code completion.

Limitations and Future Directions

While LongCoder offers a highly efficient path for developers without access to massive compute clusters, the authors acknowledge a few hurdles:

  • Model Scale: At 150M parameters, LongCoder is relatively small compared to industry giants like the 12-billion parameter OpenAI Codex. Future research will explore how these sparse attention mechanisms scale to multi-billion parameter models.
  • Data Overlap: Because LCC and pre-training datasets are sourced from GitHub, there is an inherent risk of data leakage, where the model might have encountered test data during pre-training.
  • Beyond Single Files: Currently, LongCoder is optimized for single, large files. The authors suggest that future iterations could extend the "bridge and memory" token approach to handle entire repositories, enabling true cross-file completion.

Summary updated

BibTeX
@inproceedings{guo2023longcoder,
  author = {Daya Guo and Canwen Xu and Nan Duan and Jian Yin and Julian J. McAuley},
  title = {{LongCoder: A Long-Range Pre-trained Language Model for Code Completion}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=6XwCseSnww},
}
Figure