TurboRAG: Accelerating Retrieval-Augmented Generation with Precomputed KV Caches for Chunked Text

Songshuo Lu, Hua Wang, Yutian Rong, Zhi Chen, Yaohua Tang · DOI

Abstract

Retrieval-Augmented Generation (RAG) significantly improves Large Language Model (LLM) accuracy but introduces substantial latency during the "prefill" stage. TurboRAG is a novel inference framework that addresses this bottleneck by precomputing Key-Value (KV) caches for document chunks offline. By stitching these precomputed caches at runtime, TurboRAG achieves up to a 9.4x speedup in Time-to-First-Token (TTFT) and a 98.46% reduction in computational resource utilization while preserving the accuracy of standard RAG systems.

Introduction

Retrieval-Augmented Generation (RAG) has become the standard for grounding LLMs in external knowledge. However, the "speed tax" associated with RAG is a significant barrier to real-time applications. In a standard RAG pipeline, retrieved documents are concatenated with the user query and processed by the LLM. This "prefill" step requires calculating the KV cache for every token, a process whose computational cost grows quadratically with sequence length. Furthermore, popular documents are often retrieved repeatedly, leading to redundant and expensive recomputation.

Methodology: The TurboRAG Framework

TurboRAG fundamentally redesigns the RAG inference pipeline by moving the heavy lifting of KV cache generation offline.

Comparison of Standard RAG and TurboRAG architectures across preparation and inference phases.

Offline KV Precomputation and Online Stitching

Unlike traditional systems that store raw text or embeddings, TurboRAG processes document chunks through the LLM offline to generate and store their KV caches. During inference, instead of recomputing these caches, the system retrieves the precomputed KV caches for the relevant chunks and "stitches" them together directly. This bypasses the online prefill computation for the retrieved context.

Handling Fragmented Contexts

Stitching independent KV caches introduces challenges regarding attention and positioning:

  1. Independent Attention: Standard causal masks assume a continuous sequence. TurboRAG implements an "Independent Attention" mechanism where document chunks only attend to themselves, while the query attends to all chunks.
  2. Reordered Positions: To maintain the model's understanding of relative token distances, TurboRAG assigns continuous position IDs across the concatenated chunks at runtime.
The Independent Attention mask used in TurboRAG, where document chunks attend only to themselves and the query attends to all previous chunks.

Model Adaptation

Since LLMs are typically trained on continuous causal sequences, the authors fine-tuned a base model (Qwen2-7B) on a specialized dataset. This ensures the model remains accurate when operating under the fragmented attention masks and jumped position IDs inherent to TurboRAG.

Evaluation and Results

The framework was evaluated using the RGB benchmark for bilingual document QA, LongBench for long-context understanding, and OpenCompass for general capability testing.

Performance Gains

TurboRAG demonstrates massive efficiency improvements:

  • Latency: TTFT was reduced by an average of 8.6x, reaching up to 9.4x on the LongBench dataset.
  • Compute: Online computational resource utilization (TFLOPs) dropped by 98.46%.

Accuracy and Generalization

Fine-tuning allowed TurboRAG to maintain high accuracy, performing within 1% of the "Naive RAG" baseline on the RGB benchmark. Regression testing via OpenCompass confirmed that the model's general reasoning, coding, and mathematical capabilities remained intact.

Key Insights

The success of TurboRAG rests on the observation that cross-chunk attention in standard RAG is often sparse. Masking cross-chunk communication (Independent Attention) has a negligible impact on reasoning. Additionally, the flexibility of Rotary Positional Embeddings (RoPE) allows for the runtime reordering of position IDs, making the stitched cache appear continuous to the model.

Limitations and Future Work

While TurboRAG offers significant speedups, it introduces a storage overhead, as KV caches are much larger than raw text. The speedup is also constrained by the bandwidth between storage and the GPU. Future research will explore combining TurboRAG with KV cache compression techniques to mitigate storage requirements while preserving latency benefits.

Summary updated

BibTeX
@misc{lu2024turborag,
  author = {Songshuo Lu and Hua Wang and Yutian Rong and Zhi Chen and Yaohua Tang},
  title = {{TurboRAG: Accelerating Retrieval-Augmented Generation with Precomputed KV Caches for Chunked Text}},
  year = {2024},
  eprint = {2410.07590},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.07590},
}
Figure