MagicPIG: LSH Sampling for Efficient LLM Generation

Zhuoming Chen, Ranajoy Sadhukhan, Zihao Ye, Yang Zhou, Jianyu Zhang, Niklas Nolte, Yuandong Tian, Matthijs Douze, Leon Bottou, Zhihao Jia, Beidi Chen

TL;DR

MagicPIG (Magic samPlIng for Generation) is a new heterogeneous system that accelerates long-context LLM decoding by replacing biased "TopK" attention with theoretically grounded LSH-based sampling. By offloading the memory-heavy KV cache to CPU DRAM and using Locality Sensitive Hashing to find relevant tokens, it achieves up to 5× throughput gains and serves 96k-token contexts on consumer hardware like the RTX 4090.

What Problem Does This Paper Address?

As Large Language Models (LLMs) move toward massive context windows (100k+ tokens), they hit a "memory wall." During the decoding phase, the model must store and access a "KV cache"—a record of all previous tokens. This cache grows linearly with sequence length, quickly exhausting expensive GPU VRAM and making the process "memory-bound," where the GPU spends more time waiting for data than actually performing calculations.

To solve this, many researchers use TopK attention, which only looks at the "most important" tokens to approximate the attention output. However, the authors of MagicPIG identify a critical flaw: TopK is a biased estimator. While it works for "needle-in-a-haystack" tasks (finding one specific fact), it fails miserably on "aggregated" tasks (like word extraction or summarization) where information is distributed across the entire context. In these cases, the "long tail" of low-score tokens actually matters, and ignoring them leads to significant quality degradation.

Accuracy drop of TopK on aggregated tasks

Methods

MagicPIG shifts the paradigm from deterministic selection (TopK) to unbiased sampling.

  1. LSH-based Sampling: The system uses Locality Sensitive Hashing (specifically SimHash) to approximate the attention distribution. Instead of calculating every attention score, it uses random projections to group similar keys and queries into "buckets."
  2. Self-Normalized Importance Sampling: To ensure the approximation is accurate, MagicPIG uses importance sampling. This allows the model to estimate the full attention output by only looking at a small fraction of tokens, backed by theoretical guarantees that the estimation error remains low.
  3. Heterogeneous System Design: MagicPIG splits the workload between the GPU and CPU:
  4. GPU: Handles compute-heavy linear projections and hash function calculations.
  5. CPU: Stores the massive LSH hash tables and the KV cache in DRAM. It performs the sampling and the final attention computation.
  6. Key Centering: The authors discovered that "attention sinks" (initial tokens that hog attention scores) create a geometric bias that makes LSH difficult. By "centering" the keys (subtracting the mean), they redistribute the tokens more uniformly, making the hashing significantly more effective.

Key Insights and Ideas

  • TopK is the "Achilles' Heel": The paper proves that TopK attention is inherently biased. In tasks where information is "dense" or "aggregated," the cumulative weight of many low-score tokens outweighs the few high-score ones.
  • The Geometry of Attention: The authors found that keys and queries usually lie in narrow, nearly opposite cones. This "mismatch" is why standard nearest-neighbor searches often perform poorly for attention.
  • Sparsity is an Illusion: Much of the perceived "sparsity" in LLMs is just the "attention sink" phenomenon. Once you account for the sink, the remaining attention is often quite distributed, necessitating a sampling approach rather than a pruning approach.
Attention characteristics: long-tail, sinks, and geometry
  • CPU DRAM as an Asset: While CPU memory is slower than GPU VRAM, its massive capacity allows for much larger batch sizes and longer contexts. By reducing the amount of data needed via LSH, the CPU's lower bandwidth ceases to be the primary bottleneck.

Key Results

  • Throughput & Latency: MagicPIG improved decoding throughput by up to 5× on NVIDIA L20 GPUs. On a single consumer-grade RTX 4090, it achieved a decoding latency of 54ms for a Llama-3.1-8B model with a 96k context—a feat usually requiring much more expensive enterprise hardware.
  • Accuracy Preservation: Unlike TopK methods that see sharp performance drops in aggregated tasks (like cwe and fwe), MagicPIG maintained accuracy within 2% of the full-attention baseline while using only ~2-5% of the total computation.
  • Scalability: The system successfully scaled to 70B parameter models and context lengths up to 256k, demonstrating that the LSH sampling approach remains robust as models grow.

Limitations and Future Work

  • Prefilling Stage: Currently, MagicPIG is optimized for the decoding (generation) stage. The authors note that implementing this in the prefilling (initial prompt processing) stage is a necessary next step for a complete end-to-end system.
  • DRAM Requirements: While it saves GPU VRAM, it requires substantial CPU DRAM to store the hash tables and KV cache. Systems with limited system RAM may not see the same benefits.
  • Advanced Hashing: The authors suggest that more sophisticated LSH algorithms (like Cross-polytope or data-dependent hashing) could further improve the trade-off between memory overhead and estimation accuracy.

Summary updated

BibTeX
@inproceedings{chen2025magicpig,
  author = {Zhuoming Chen and Ranajoy Sadhukhan and Zihao Ye and Yang Zhou and Jianyu Zhang and Niklas Nolte and Yuandong Tian and Matthijs Douze and Leon Bottou and Zhihao Jia and Beidi Chen},
  title = {{MagicPIG: LSH Sampling for Efficient LLM Generation}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=ALzTQUgW8a},
}
Figure