Repoformer: Selective Retrieval for Repository-Level Code Completion

Di Wu, Wasi Uddin Ahmad, Dejiao Zhang, Murali Krishna Ramanathan, Xiaofei Ma

TL;DR

REPOFORMER is a selective Retrieval-Augmented Generation (RAG) framework that teaches code language models to decide when to fetch external repository context and when to rely solely on local context. By training the model to self-evaluate its own needs, REPOFORMER achieves state-of-the-art accuracy while delivering up to a 70% speedup in inference by skipping unnecessary or harmful retrievals.

Repoformer framework illustrating selective retrieval decisions.

What Problem Does This Paper Address?

Modern code completion tools increasingly rely on Retrieval-Augmented Generation (RAG) to look up relevant snippets from other files in a repository. While this is highly effective for resolving cross-file dependencies (like using a custom API defined in another module), the "always-on" approach to retrieval has two major flaws:

  1. Efficiency: Retrieval is computationally expensive. In many real-world scenarios, the time spent searching the repository and processing the extra context is longer than the time spent generating the code itself.
  2. Robustness: More context isn't always better. The authors found that in up to 80% of cases, retrieved snippets either do not help or actively confuse the model, leading to worse code generation than if the model had just relied on the current file.

Existing systems blindly retrieve context for every query. This paper asks a critical question: Should we always perform retrieval augmentation? Their answer is a resounding "no," and they introduce a framework that empowers the model to make that choice itself.

Methods

The core contribution of the paper is a self-supervised approach to training a "selective" RAG model.

  • Self-Selective RAG: The authors modified the standard "Fill-in-the-Middle" (FIM) architecture. After processing the code before and after a cursor, the model is trained to generate a special token—<cc> (cross-file context)—if it determines that retrieval is necessary. If it doesn't output this token, it proceeds with standard, local-only generation.
Comparison of standard Fill-in-the-middle and Self-selective RAG formats.
  • Contrastive Data Labeling: To train this selective behavior, the researchers needed a dataset indicating when retrieval actually helps. They analyzed thousands of repositories, comparing a model's performance with and without retrieval. If retrieval improved the "Edit Similarity" of the output beyond a specific threshold, that instance was labeled as "Retrieval Needed."
  • Multi-task Learning: They fine-tuned StarCoderBase models (ranging from 1B to 16B parameters) using a dual objective:
  • Self-evaluation: Accurately predicting whether to trigger the <cc> token.
  • Generation: Producing the correct code, whether external context was used or not.
  • Plug-and-Play Policy: Interestingly, the authors demonstrated that a smaller model (like REPOFORMER-1B) can act as a lightweight "router" for much larger, black-box models (like ChatGPT), dictating when the larger model should bother processing retrieved context.

Datasets

The researchers evaluated their framework on three major benchmarks:

  • RepoEval: A standard benchmark for line, API, and function completion.
  • CrossCodeEval: A multilingual benchmark covering Python, Java, C#, and TypeScript.
  • CrossCodeLongEval: A newly introduced benchmark focusing on "long-form" completion (entire code chunks and function bodies) across 1,500 diverse Python repositories, providing a more rigorous test of repository-level understanding.

Key Insights and Ideas

  • The Retrieval Paradox: The study reveals that "always retrieving" is a sub-optimal strategy. For the majority of code completion tasks, the local file context is entirely sufficient. Adding external snippets often introduces "noise" that distracts the model.
  • Models Know What They Don't Know: By training on the delta between RAG and non-RAG performance, the model learns "self-knowledge." It recognizes patterns where it typically fails alone (e.g., complex inter-module dependencies) and proactively asks for help.
  • Thresholding for Latency: Users can tune a "retrieval threshold" during inference. In high-latency environments, the model can be made more "conservative" about retrieving, saving massive amounts of compute without a significant drop in accuracy.

Key Results

  • State-of-the-Art Performance: REPOFORMER-16B set new records across all tested benchmarks. Notably, the smaller REPOFORMER-3B model outperformed the much larger StarCoder-7B on most tasks.
  • Massive Speedups: In online serving simulations, selective retrieval provided up to a 70% inference speedup. Because the model skips the retrieval and processing of external snippets for the majority of queries, the system is significantly more responsive.
  • Robustness: Unlike standard models that get confused by irrelevant retrieved snippets, REPOFORMER learned to be more robust, effectively "ignoring" noisy context when it was forced to use it.

Limitations and Future Work

  • Heuristic Labeling: The training labels were generated using Edit Similarity (a lexical metric). The authors suggest that using execution-based metrics (checking if the code actually runs and passes tests) might create even better training signals for complex function completion.
  • Retrieval Granularity: Currently, the model makes a binary choice: retrieve or don't retrieve. Future work could explore more granular choices, such as where to retrieve from or how much context to fetch.
  • Personalization: The current policy is a "one size fits all" for every repository. The authors note that some repositories are naturally more "RAG-friendly" than others, suggesting a need for personalized retrieval policies adapted to specific codebases.

Summary updated

BibTeX
@inproceedings{wu2024repoformer,
  author = {Di Wu and Wasi Uddin Ahmad and Dejiao Zhang and Murali Krishna Ramanathan and Xiaofei Ma},
  title = {{Repoformer: Selective Retrieval for Repository-Level Code Completion}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=moyG54Okrj},
}
Figure