HeadInfer: Memory-Efficient LLM Inference by Head-wise Offloading

Cheng Luo, Zefan Cai, Hanshi Sun, Jinqi Xiao, Bo Yuan, Wen Xiao, Junjie Hu, Jiawei Zhao, Beidi Chen, Anima Anandkumar

TL;DR

HeadInfer is a novel, lossless LLM inference framework that slashes GPU memory usage by offloading the KV cache to CPU RAM at the level of individual attention heads. By overlapping fine-grained, head-by-head data transfers with GPU computation, it enables a standard Llama-3-8B model to process up to 4 million tokens on a single consumer GPU (like an NVIDIA RTX 4090), achieving a 92% reduction in total GPU memory footprint.

What Problem Does This Paper Address?

Context windows in modern Large Language Models (LLMs) are expanding rapidly, with some models supporting millions of tokens. However, during the inference phase, storing the Key-Value (KV) cache for these massive sequences creates a colossal memory bottleneck. For instance, running a Llama-3-8B model with a 1-million-token context requires around 128 GB of VRAM just for the KV cache, bringing total memory requirements to over 207 GB. This makes long-context inference impossible on consumer-grade hardware like the NVIDIA RTX 4090, which maxes out at 24 GB of VRAM.

Existing solutions fall into two camps, both with major drawbacks:

  1. Lossy compression/token eviction: Methods like H2O or StreamingLLM drop "unimportant" tokens to save space, but this degrades accuracy and fails on tasks requiring precise retrieval of niche details.
  2. Coarse-grained offloading: Existing systems offload KV caches to the CPU layer-by-layer. However, because entire layers are transferred at once, the remaining activation memory and active KV cache chunks still easily overflow consumer GPU limits.

Methods

The authors introduce HeadInfer, an inference framework that shifts the granularity of KV cache offloading from entire layers to individual attention heads. Because attention heads compute their outputs independently, HeadInfer decomposes the attention step so that only a single head's (or a small group of heads') KV cache needs to reside on the GPU at any given moment, while the rest sit cheaply in CPU RAM.

Figure 2. Demonstrations of KV cache policies in inference. Full KV cache contains two main dimensions: layer and head. Layer-wise offloads KV cache in the layer’s dimension, with a cache budget of all heads per layer. HEADINFER further reduces GPU memory by adaptively reallocating cache budgets in the head’s dimension, with a cache budget of one head.

Key components of the architecture include:

  • Head-wise Offloading: Decomposes the attention mechanism head-by-head, drastically reducing the active GPU memory footprint while maintaining exact mathematical equivalence to standard attention.
  • Ping-Pong Asynchronous Transfer: A dual-buffer design that completely overlaps PCIe data transfers with GPU computation. While the GPU computes attention for head $h$, it concurrently prefetches the KV cache for head $h+1$ from the CPU and offloads head $h-1$ back to the CPU.
  • Adaptive Head Grouping: To avoid excessive kernel launch overheads for shorter context lengths, HeadInfer dynamically groups and fuses multiple attention heads together, gradually narrowing down to single-head granularity only as the context grows extremely long.
  • Chunked Prefill: Integrates sequence chunking during the prefill phase to keep activation memory low and stable.

Datasets

The framework was evaluated using several long-context benchmarks to verify both performance and accuracy:

  • LongBench v2: A dataset with up to 2M-word contexts requiring deep reasoning and understanding.
  • SCBench: A comprehensive suite of 9 long-context understanding tasks (averaging 227K tokens).
  • Needle-in-a-Haystack (NIAH): A classic retrieval test used to ensure no information is lost across long sequences.
  • Ruler: A synthetic benchmark evaluating effective context size across various tasks up to 128K tokens.

Key Insights and Ideas

  • Exploiting Head Independence: The core "aha" moment is realizing that the embarrassingly parallel nature of attention heads can be leveraged to stream data at an ultra-fine granularity, bypassing the physical memory limits of the GPU without modifying model weights.
  • Hiding PCIe Latency via the Roofline: Through roofline analysis, the authors show that during the prefill stage with long contexts ($\ge 10k$ tokens), attention computation becomes heavily compute-bound due to its quadratic growth relative to sequence length. This massive compute requirement completely hides the slower PCIe transfer times.
  • Lossless Retrieval: Unlike token eviction methods which permanently discard tokens and hallucinate when queries look for obscure details, HeadInfer retains the entire context in CPU memory, ensuring perfect retrieval accuracy.

Key Results

  • Massive Memory Reduction: For a Llama-3-8B model with 1M tokens, HeadInfer reduced the active KV cache on the GPU from 128 GB to just 1 GB, and total GPU memory from 207 GB to 17 GB—a 92% reduction, fitting comfortably within a 24GB GPU limit.
Figure 1. Estimated memory consumption of inference a Llama-3- 8B model with 1 million token on a single GPU.
  • Unprecedented Context Scaling: HeadInfer successfully scaled Llama-3-8B to 4 million tokens on a single RTX 4090. It also scaled a 70B model to 1 million tokens using pipeline parallelism across 8 RTX 4090 nodes.
  • Perfect Accuracy: HeadInfer achieved identical accuracy to standard inference on Needle-in-a-Haystack and Ruler, outperforming lossy token-eviction methods on complex retrieval tasks.
  • Throughput: The implementation achieved 516 tokens/second during the prefill stage for a 1-million-token input.

Limitations and Future Work

  • Decoding Bottleneck: While prefill latency is well-hidden by intense computation, the decoding phase (generating tokens one by one) remains strictly memory-bound. At a 1M token context, decoding throughput drops to 0.15 tokens/second due to constant round-trip PCIe transfers.
  • CPU RAM Constraints: The maximum context length is no longer limited by the GPU, but it is now bounded by host CPU RAM (e.g., a 4M token context requires 512GB of CPU RAM).
  • Future Directions: The authors suggest exploring offloading from CPU RAM to NVMe SSDs to scale context lengths even further without requiring massive system memory.

Summary updated

BibTeX
@misc{luo2025headinfer,
  author = {Cheng Luo and Zefan Cai and Hanshi Sun and Jinqi Xiao and Bo Yuan and Wen Xiao and Junjie Hu and Jiawei Zhao and Beidi Chen and Anima Anandkumar},
  title = {{HeadInfer: Memory-Efficient LLM Inference by Head-wise Offloading}},
  year = {2025},
  eprint = {2502.12574},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2502.12574},
}
Figure