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 · DOI

Introduction: The Memory Wall in Long-Context LLMs

As Large Language Models (LLMs) like Llama-3 and Claude evolve to support context windows ranging from 128K to 1M tokens, the memory requirements for the Key-Value (KV) cache have become a significant bottleneck. For instance, a Llama-3-8B model requires approximately 128 GB of memory to store the KV cache for 1 million tokens. This far exceeds the 24 GB VRAM available on high-end consumer GPUs like the NVIDIA RTX 4090, necessitating expensive data-center hardware for long-context inference.

Methodology: Head-wise Offloading

The core innovation of HeadInfer is shifting the granularity of memory management from entire layers to individual attention heads. Since attention heads are mathematically independent, HeadInfer serializes the computation: it keeps only the KV cache for the active head on the GPU while offloading the rest to CPU RAM.

Comparison of offloading strategies: Non-offload, Layer-offload, and the proposed Head-offload.

To maintain high performance, HeadInfer employs several key techniques:

  • Ping-Pong Buffering: Concurrently prefetches the next head's data while the current head is being processed, hiding PCIe transfer latency.
  • Adaptive Head Grouping: Dynamically adjusts the number of heads on-device based on context length to balance speed and memory usage.
  • Chunked Prefill: Processes long sequences in smaller increments to manage activation memory.

Key Results and Evaluation

HeadInfer was evaluated on benchmarks such as LongBench v2, SCBench, and Needle-in-a-Haystack. The framework demonstrates a dramatic reduction in GPU memory footprint, enabling 4-million-token inference on a single consumer GPU.

Memory footprint comparison of HeadInfer against baseline and other optimization techniques on an RTX 4090.

As shown in the comparison, HeadInfer reduces the total GPU memory usage for a 1-million-token context from 207 GB to just 17 GB—a 92% reduction. This allows the model to operate comfortably within the 24 GB limit of an RTX 4090. Unlike lossy methods like quantization or token eviction, HeadInfer is mathematically equivalent to standard inference, ensuring no loss in model accuracy.

Limitations and Future Work

While HeadInfer effectively addresses the memory wall during the prefill stage, the decoding phase remains memory-bound due to frequent data swaps over the PCIe bus, resulting in lower generation throughput (e.g., 0.15 tokens/sec for 1M context). Future research will focus on optimizing PCIe bandwidth utilization and exploring offloading to NVMe SSDs to further expand the context capacity beyond CPU RAM limits.

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