XC-Cache: Cross-Attending to Cached Context for Efficient LLM Inference

João Monteiro, Étienne Marcotte, Pierre-André Noël, Valentina Zantedeschi, David Vázquez, Nicolas Chapados, Christopher Pal, Perouz Taslakian

Introduction

Modern Large Language Models (LLMs) are increasingly used for tasks involving long contexts, such as In-Context Learning (ICL) and Retrieval-Augmented Generation (RAG). While standard decoder-only architectures like LLaMA-2 are highly effective, their inference efficiency is severely hampered by the memory requirements of Key-Value (KV) caching. This paper introduces XC-Cache (Cross-Context-Cache), a method that retrofits pre-trained decoder-only models into encoder-decoder architectures. By using a few cross-attention layers, XC-Cache reduces the context cache memory footprint by over 98% while maintaining, and often exceeding, the performance of standard prompting baselines on Question Answering (QA) tasks.

The Memory Bottleneck of KV Caching

Processing large chunks of reference text at inference time is computationally expensive due to the quadratic scaling of standard self-attention. To mitigate this, engineers use KV caching, which involves pre-processing the context and storing the Key and Value states for every layer of the transformer.

However, for a 7B parameter model like LLaMA-2, this requires approximately 512 KB per token. Caching a 10,000-token document would thus consume 5GB of memory per sequence. This massive footprint creates a significant bottleneck for batch sizes, increases latency due to memory transfer overhead, and makes caching large corpora offline financially impractical.

The XC-Cache Architecture

XC-Cache addresses this bottleneck by separating context processing from text generation. Instead of caching states across all layers of a decoder, the authors draw inspiration from legacy encoder-decoder architectures, where only the final hidden states of the encoder need to be cached.

Comparison of inference paradigms: (a) standard inference, (b) retrieval-based, (c) standard KV caching, and (d) the proposed XC-Cache approach.

To implement this without the cost of training a new model from scratch, the authors inject untrained cross-attention layers into a pre-trained LLaMA-2 model. They explore two primary encoding strategies:

  1. XC-LLaMA: Utilizes the frozen LLaMA-2 decoder itself as the encoder, extracting representations from its final layer.
  2. XC-LLaMAEnc: Employs a small, trainable bi-directional encoder (Longformer) to process the context.

By freezing the base LLaMA-2 weights and fine-tuning only the new cross-attention layers (and the Longformer where applicable), the model is converted into an efficient encoder-decoder system. A multi-task training strategy involving context repetition and infilling ensures the model learns to effectively utilize these new cached representations.

Experimental Setup and Results

The models were trained on a variety of standard QA datasets, including Natural Questions (NQ), HotpotQA, and SQuAD-v2. Evaluation was performed on these datasets as well as RepLiQA, a dataset of fictional scenarios designed to test true reading comprehension and avoid reliance on memorized facts.

Massive Memory Savings

The efficiency gains of XC-Cache are substantial. While standard KV caching for LLaMA-2 requires 512 KB/token, XC-LLaMA reduces this to 8 KB/token, and XC-LLaMAEnc further drops it to just 1.5 KB/token. This represents a reduction of up to 340x, or over 98%.

Performance vs. context cache memory footprint. XC-Cache variants achieve high BERTScore and F1 while drastically reducing memory requirements compared to standard ICL and KV caching.

Strong QA Performance

On QA benchmarks, XC-Cache variants significantly outperform standard zero-shot prompting with LLaMA 2-Chat and GPT-3.5 Turbo. For instance, on the NQ dataset, XC-LLaMAEnc achieved a 63.12 F1 score, compared to 41.26 for LLaMA 2-Chat and 57.80 for GPT-3.5. While the XC models slightly underperform a fully fine-tuned LLaMA-2 with full KV caching (67.38 F1 on NQ), they offer a superior Pareto trade-off by nearly eliminating cache memory costs.

Conclusion and Future Work

XC-Cache demonstrates that encoder-decoder structures are highly optimized for context caching. By retrofitting existing decoders with cross-attention, it is possible to achieve extreme parameter efficiency, training less than 10% of the total parameters to fundamentally change the model's architecture.

Future research will focus on addressing "question-blind" encoding—where the encoder cannot see the user's query beforehand—and improving generalization to out-of-distribution data. Despite these challenges, XC-Cache provides a scalable path forward for deploying LLMs in memory-constrained environments.

Summary updated

BibTeX
@inproceedings{monteiro2024xc,
  author = {João Monteiro and Étienne Marcotte and Pierre-André Noël and Valentina Zantedeschi and David Vázquez and Nicolas Chapados and Christopher Pal and Perouz Taslakian},
  title = {{XC-Cache: Cross-Attending to Cached Context for Efficient LLM Inference}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.findings-emnlp.896},
}
Figure