NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Layer-Condensed KV Cache for Efficient Inference of Large Language Models

Haoyi Wu, Kewei Tu

Abstract

Huge memory consumption has been a major bottleneck for deploying high-throughput large language models in real-world applications. In addition to the large number of parameters, the key-value (KV) cache for the attention mechanism in the transformer architecture consumes a significant amount of memory, especially when the number of layers is large for deep language models. In this paper, we propose a novel method that only computes and caches the KVs of a small number of layers, thus significantly saving memory consumption and improving inference throughput. Our experiments on large language models show that our method achieves up to 26× higher throughput than standard transformers and competitive performance in language modeling and downstream tasks. In addition, our method is orthogonal to existing transformer memory-saving techniques, so it is straightforward to integrate them with our model, achieving further improvement in inference efficiency.

1. Introduction

The deployment of Large Language Models (LLMs) is often constrained by the memory requirements of the "KV cache." During text generation, models store the Keys and Values of every token across every layer to avoid redundant calculations. As sequence lengths increase and models grow deeper, this cache can consume over 30% of GPU memory, severely limiting throughput and maximum sequence length.

While previous research has focused on reducing the cache length (e.g., token eviction), this work addresses the depth of the cache. In a standard 32-layer model, 32 sets of KVs are stored per token. We propose Layer-Condensed KV Cache (LCKV), which demonstrates that caching KVs for only a small subset of layers is sufficient for maintaining high performance while drastically reducing memory overhead.

2. Methods

The core of LCKV is a transformer variant where queries from multiple layers attend to the KV pairs of a single "condensed" layer (typically the top layer).

2.1 Architecture and the Sandwich Configuration

To maintain accuracy, LCKV employs a "sandwich" configuration. Instead of dropping all intermediate caches, a few "warmup layers" at the bottom and top of the model retain standard attention. The middle layers are then modified to point to the top-layer KVs. This structure acknowledges that bottom layers primarily handle syntax while top layers handle semantics, both requiring local context to function effectively.

Comparison between the standard transformer (a) and the LCKV model (b).

2.2 Iterative Parallel Training

Standard Transformers are easily parallelizable during training. However, LCKV introduces a sequential dependency: layer $L$ of token $i$ depends on the top-layer KV of token $i-1$. To overcome this, we developed an iterative parallel training method. The model converges on correct representations over a small number of passes (typically $m=7$ iterations), making training feasible on modern hardware.

Inference (a) vs. Iterative Parallel Training (b) in the LCKV architecture.

2.3 Diagonal Masking

To resolve the cyclic dependency where a token requires its own top-layer KV to compute lower-layer representations, we mask the self-attention diagonal. Empirical results show this has a negligible impact on overall model performance.

3. Experimental Evaluation

We evaluated LCKV across various benchmarks, including pre-training on a 100B token subset of SlimPajama and zero-shot evaluation on commonsense reasoning tasks (HellaSwag, ARC, BoolQ, etc.).

3.1 Key Results

  • Throughput: LCKV achieved a 26x throughput increase on a 30B Llama model using CPU-offloading. On an A100 GPU, it enabled 8.5x larger batch sizes for a 7B model.
  • Performance: A 1.1B LCKV model achieved a perplexity of 9.265, nearly matching the 9.219 perplexity of a standard TinyLlama baseline.
  • Orthogonality: LCKV is compatible with sequence-shortening methods. Integration with StreamingLLM allowed for stable perplexity over 4 million tokens with even further reduced memory usage.

4. Discussion and Limitations

While LCKV significantly improves inference efficiency, it introduces a training overhead, with iterative training being approximately 3x slower than standard methods. Additionally, initial prompt encoding is slower due to the iterative nature of the model, though this is typically offset by massive gains during token generation. Future work will focus on developing optimized CUDA kernels to further enhance performance at scale.

5. Conclusion

Layer-Condensed KV Cache (LCKV) offers a powerful solution to the memory bottleneck in LLM inference. By exploiting depth redundancy in transformer layers, LCKV enables significantly higher throughput and larger batch sizes without sacrificing competitive performance.

Summary updated

BibTeX
@inproceedings{wu2024layer,
  author = {Haoyi Wu and Kewei Tu},
  title = {{Layer-Condensed KV Cache for Efficient Inference of Large Language Models}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=P9CNir6qao},
}
Figure