TL;DR
REFRAG (REpresentation For RAG) is a novel decoding framework designed to accelerate Retrieval-Augmented Generation (RAG) by compressing retrieved text passages into compact chunk embeddings. By leveraging the inherent sparsity of RAG contexts and using a lightweight reinforcement learning policy to dynamically expand only the most critical chunks, REFRAG achieves up to a 30.85× acceleration in Time-to-First-Token (TTFT) and extends the effective context window by 16× with zero loss in accuracy.
What Problem Does This Paper Address?
While Large Language Models (LLMs) excel at using external knowledge via RAG, stuffing long retrieved contexts into a prompt introduces massive system overhead. Specifically, the Time-to-First-Token (TTFT) latency scales quadratically with prompt length, and Key-Value (KV) cache memory consumption scales linearly, severely bottlenecking throughput.
The authors observe that standard LLM serving treats all context tokens equally, which is highly inefficient for RAG. In typical RAG setups, prompts consist of numerous concatenated documents where only a tiny fraction is actually relevant to the user's query. Because these documents are distinct and often deduplicated, they exhibit low semantic similarity to one another, resulting in highly sparse, "block-diagonal" attention patterns. REFRAG exploits this structural sparsity to eliminate unnecessary computations over irrelevant context.
Methods
REFRAG introduces a "compress, sense, and expand" framework that shortens the input sequence length without modifying the underlying LLM architecture:
- Chunking & Encoding ("Compress Anywhere"): The raw context is split into fixed-size chunks of $k$ tokens. A lightweight encoder (e.g., RoBERTa) processes these chunks in parallel to generate compact chunk embeddings, which are then projected into the decoder's token embedding space. This reduces the sequence length seen by the decoder by a factor of $k$.
- Continual Pre-training (CPT) Recipe: To teach the decoder how to interpret these non-standard chunk embeddings, the authors use a two-step training strategy:
- Reconstruction Task: The decoder is frozen while the encoder and projection layers are trained to reconstruct the original tokens from the chunk embeddings.
- Curriculum Learning: Training begins with single-chunk reconstruction and gradually scales up to multi-chunk sequences to prevent optimization collapse.
- Selective Expansion via RL ("Sense & Expand"): Not all information can be compressed perfectly. REFRAG employs a lightweight reinforcement learning policy trained via Group Relative Policy Optimization (GRPO). This policy acts as a gatekeeper, analyzing the chunk embeddings and dynamically deciding which crucial chunks need to be fully expanded back into raw tokens for the decoder.
Datasets
- Continual Pre-training: A 20-billion-token subset sampled equally from the Book and ArXiv domains of the SlimPajama dataset.
- Evaluation (Perplexity & Generalization): Holdout sets from SlimPajama, PG19, and Proof-pile.
- RAG Fine-Tuning: 1.1 million data points spanning 5 domains, including OpenAssistant (Dialogue), CommonsenseQA/MathQA/MS MARCO (Open-Domain QA), and SQuADv2 (Reading Comprehension).
- Downstream Tasks: Evaluated on standard benchmarks like MMLU, BoolQ, KILT (HellaSwag, NQ), multi-turn datasets (TopiOCQA, ORConvQA, QReCC), and long-document summarization (ArXiv and PubMed).
Key Insights and Ideas
- RAG Attention is Uniquely Sparse: Unlike continuous text generation, RAG contexts are patchworks of independent documents. Cross-attention values between different retrieved passages are close to zero, meaning token-to-token computation across distinct chunks is largely redundant.
- Preserving Autoregressive Properties: Unlike prior context-compression methods that restrict compression to fixed prefixes, REFRAG supports compression and expansion at arbitrary positions. This flexibility is vital for multi-turn conversations and agentic workflows where queries and contexts alternate.
- Asymmetric Scaling Laws: The authors found that scaling up the decoder size (e.g., from 7B to 13B parameters) significantly improves performance, whereas scaling up the context encoder yields diminishing returns.
Key Results
- TTFT Acceleration: At a compression rate of $k=16$, REFRAG achieves a 16.53× TTFT speedup with caching. At $k=32$, the acceleration reaches 30.85× compared to a vanilla LLaMA-2-7B baseline, outperforming previous state-of-the-art methods (like CEPE) by 3.75×.
- Throughput Gains: REFRAG delivers up to a 6.78× improvement in token throughput because the decoder operates on drastically shortened sequences.
- Context Window Extension: By compressing tokens into dense embeddings, REFRAG effectively expands the LLM's context window by 16×, allowing models with a native 4k limit to handle much larger document pools successfully.
Limitations and Future Work
- Compression Limits: Pushing the compression rate too far results in performance degradation. While $k=16$ and $k=32$ maintain excellent accuracy, a compression rate of $k=64$ caused a noticeable regression in model capabilities.
- Caching Dependency: To realize the maximum 30× speedup, chunk embeddings must be pre-computed and cached in a vector database. Without caching, the encoder overhead reduces the TTFT acceleration to around 8.5×.
- Future Integration: The authors suggest that combining REFRAG's chunk-level embedding approach with token-level lexical pruning methods (like LLMLingua) could unlock even higher compression ratios and efficiency.
Summary updated