Skip to main content
QUICK REVIEW

[Paper Review] Repoformer: Selective Retrieval for Repository-Level Code Completion

Di Wu, Wasi Uddin Ahmad|arXiv (Cornell University)|Mar 15, 2024
Web Data Mining and AnalysisComputer Science3 citations
TL;DR

This paper proposes Repoformer, a selective retrieval framework for repository-level code completion that uses a self-supervised code language model to dynamically decide when retrieval is necessary, improving both accuracy and inference efficiency. By avoiding unnecessary retrievals, it achieves up to 70% faster inference without sacrificing performance, outperforming invariable retrieval baselines across multiple benchmarks.

ABSTRACT

Recent advances in retrieval-augmented generation (RAG) have initiated a new era in repository-level code completion. However, the invariable use of retrieval in existing methods exposes issues in both efficiency and robustness, with a large proportion of the retrieved contexts proving unhelpful or harmful to code language models (code LMs). In this paper, we propose a selective RAG framework to avoid retrieval when unnecessary. To power this framework, we design a self-supervised learning approach to enable a code LM to accurately self-evaluate whether retrieval can improve its output quality and robustly leverage the potentially noisy retrieved contexts. Using this LM as both the selective RAG policy and the generation model, our framework achieves state-of-the-art repository-level code completion performance on diverse benchmarks including RepoEval, CrossCodeEval, and CrossCodeLongEval, a new long-form code completion benchmark. Meanwhile, our analyses show that selectively retrieving brings as much as 70% inference speedup in the online serving setting without harming the performance. We further demonstrate that our framework is able to accommodate different generation models, retrievers, and programming languages. These advancements position our framework as an important step towards more accurate and efficient repository-level code completion.

Motivation & Objective

  • To address the inefficiency and robustness issues caused by always performing retrieval in retrieval-augmented generation (RAG) for repository-level code completion.
  • To reduce computational waste from retrieving irrelevant or noisy cross-file contexts that degrade model performance.
  • To develop a self-evaluating code language model that can autonomously determine whether retrieval will improve completion quality.
  • To enable generalization across different code LMs, retrievers, programming languages, and completion granularities.
  • To create a plug-and-play selective retrieval policy that enhances both accuracy and latency in real-world coding environments.

Proposed method

  • Design a self-supervised RAG simulation procedure to generate training data by pairing code chunks with retrieved cross-file contexts.
  • Train a code LM (Repoformer) with a multi-task objective that jointly optimizes for code generation and selective retrieval decision-making.
  • Integrate a retrieval trigger token (<cc>) that the model predicts to determine whether to invoke retrieval based on context relevance.
  • Use a self-evaluation loss ($\mathcal{L}_{eval}$) to teach the model to assess whether retrieval would improve output quality.
  • Fine-tune the model on a combination of in-file completion and retrieval-augmented generation, with retrieval decisions conditioned on the model’s internal confidence.
  • Streamline the pipeline by using the same model as both the selective retrieval policy and the code generator, eliminating separate policy networks.
Figure 1 : (a) An overview of the proposed selective RAG framework. Given the current file context, a policy first assesses whether retrieval is required and triggers the retriever selectively. Then, the code LM makes a hypothesis with the optional retrieved context. With Repoformer , the two stages
Figure 1 : (a) An overview of the proposed selective RAG framework. Given the current file context, a policy first assesses whether retrieval is required and triggers the retriever selectively. Then, the code LM makes a hypothesis with the optional retrieved context. With Repoformer , the two stages

Experimental results

Research questions

  • RQ1Can a code language model be trained to self-evaluate whether retrieval will improve its code completion output quality?
  • RQ2Does selective retrieval reduce inference latency without degrading performance compared to always retrieving?
  • RQ3How robust is the model to noisy or irrelevant retrieved contexts when retrieval is triggered?
  • RQ4Can the same selective retrieval policy generalize across different code LMs, programming languages, and retrieval methods?
  • RQ5Is it possible to train a model to abstain from retrieval when it already knows the answer or when cross-file context is unnecessary?

Key findings

  • Repoformer achieves a 70% inference speedup by avoiding unnecessary retrievals, with no performance degradation.
  • On RepoEval and CrossCodeEval, Repoformer outperforms invariable retrieval baselines in both chunk and function completion tasks.
  • The ablation study shows that A2 (with $\mathcal{L}_{eval}$) achieves better selective decision-making than A1, which defaults to retrieval due to loss imbalance.
  • A3, which removes $CC$ from training, shows worse RAG performance, proving that training with retrieved contexts is essential for robustness.
  • A4, which places $CC$ inside the infilling span, performs poorly in function completion, indicating that it breaks the pre-trained fill-in-the-middle semantics.
  • Repoformer maintains strong performance across diverse programming languages and completion granularities, demonstrating generalizability.
Figure 2 : A comparison between fill-in-the-middle and self-selective RAG. We mark the end of the current file with a new token <eof> , which triggers the LM’s self-evaluation. $\rightarrow$ denotes the invocation of the LM. LM-generated parts are colored in red. fim_p , fim_s , and fim_m refer to t
Figure 2 : A comparison between fill-in-the-middle and self-selective RAG. We mark the end of the current file with a new token <eof> , which triggers the LM’s self-evaluation. $\rightarrow$ denotes the invocation of the LM. LM-generated parts are colored in red. fim_p , fim_s , and fim_m refer to t

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.