Abstract
Retrieval-Augmented Generation (RAG) is essential for grounding Large Language Models (LLMs) in external knowledge, yet it faces significant challenges regarding context window limits and computational overhead. We present CacheFocus, a training-free framework that enables LLMs to process extensive retrieved contexts efficiently. By dynamically re-positioning and pruning Key-Value (KV) caches using the mathematical properties of Rotary Position Embeddings (RoPE), CacheFocus mitigates the "lost in the middle" problem and extends effective context length. Experimental results show that CacheFocus reduces pre-fill latency by up to 81% and maintains robust performance even when inputs exceed the model's original training length.
Introduction
Large Language Models (LLMs) are notoriously constrained by their input length. Despite advancements like Rotary Position Embeddings (RoPE), models often experience "model collapse" or significant performance degradation when inputs exceed their training context. Furthermore, the high computational cost of processing long sequences—particularly the quadratic complexity of the attention mechanism—makes real-time RAG difficult. Existing solutions like Parallel Context Windows (PCW) attempt to process documents in parallel but often suffer from abnormal token distributions and performance drops as the number of documents increases. CacheFocus addresses these issues by introducing a dynamic management system for document caches that ensures the model focuses only on the most relevant information.
Methodology
The CacheFocus framework is built upon four primary pillars designed to optimize the handling of external document caches without requiring additional model training.
- Offline Query-Independent Caching: Documents are pre-processed into KV caches and stored in a persistent store. Since document content is independent of the user query, this stage is performed once, allowing the system to bypass the expensive "pre-filling" stage during live inference.
- Cache Re-Positioning: This core innovation leverages the reversibility of RoPE. By applying an inverse rotation to cached keys, the system can "reset" a token's position and then "re-rotate" it into a new position within the context window. This allows documents to be shifted in the positional encoding space without re-running the model.
- Layer-Adaptive Cache Pruning: To reduce noise and computational load, CacheFocus progressively discards low-relevance document caches at specific layers (e.g., every 4th layer) based on accumulated attention scores. This filtering mechanism ensures that only the most pertinent information reaches the deeper layers of the model.
- Adaptive Positional Allocation: Following pruning, the remaining caches are reassigned to new positions. The authors explored two strategies: Dynamic, which fills gaps left by pruned caches, and Attention-Guided, which sorts caches to place the most relevant documents positionally closer to the query.
Key Results
Evaluations on standard open-domain question-answering benchmarks, such as Natural Questions (NQ) and TriviaQA (TQA), demonstrate the effectiveness of CacheFocus.
Breaking the Length Barrier
On LLaMA-2-7B, which has a native 4k token limit, CacheFocus maintained high accuracy even as context length extended to over 6k tokens. In contrast, standard RAG performance dropped to near zero once the context exceeded the training limit.
Efficiency Gains
CacheFocus achieves massive speedups in inference. For an 8k token input, the framework reduced pre-fill time from 1.953 seconds (naive baseline) to just 0.095 seconds—a 95% reduction in that phase. Total inference time was lowered to approximately 81% of the baseline.
Scalability
Testing on Qwen2-7B showed that the model maintained stable performance even as the number of retrieved documents increased to 70. Baselines without pruning typically saw performance degradation after just 20 documents, highlighting the importance of the "noise filtering" provided by layer-adaptive pruning.
Discussion and Conclusion
CacheFocus demonstrates that positional embeddings are not permanent constraints and can be manipulated to improve model focus. By re-sorting relevant documents to be positionally closer to the query, the framework overcomes the "lost in the middle" phenomenon.
While the approach introduces storage overhead for KV caches and assumes relatively static documents, its training-free nature and significant latency reductions make it a compelling solution for long-context RAG applications. Future work may explore hierarchical document segmentation and the interaction between dynamic pruning and few-shot prompting strategies. In summary, CacheFocus provides a scalable, efficient, and plug-and-play method for teaching LLMs to focus on what matters in massive contexts.
Summary updated