ZigzagAttention: Efficient Long-Context Inference with Exclusive Retrieval and Streaming Heads

Zhuorui Liu, Chen Zhang, Dawei SongarXiv 2025 · DOI

Abstract

As Large Language Models (LLMs) scale to handle massive context windows, the memory overhead of the Key-Value (KV) cache becomes a critical bottleneck. While previous methods like DuoAttention reduce memory by identifying "streaming" heads that do not require full history, they often suffer from increased latency due to mixed-head processing within layers. We present ZigzagAttention, an optimization strategy that reorganizes attention heads into layer-exclusive "retrieval" or "streaming" types. By formulating this reorganization as a transport optimization problem and enabling single-pass execution, ZigzagAttention achieves up to a 37% speedup in decoding latency while maintaining competitive performance on long-context benchmarks like LongBench and extending effective context windows up to 600k tokens.

Introduction

The KV cache—memory used to store keys and values of previous tokens—is a primary constraint in long-context LLM inference. In many scenarios, the cache size can exceed the model parameters themselves. Recent research, such as DuoAttention, identified that attention heads can be categorized into "retrieval heads" (essential for long-range recall) and "streaming heads" (focused on local context). While DuoAttention saves memory by waiving the KV cache for streaming heads, it introduces significant latency. Mixing these head types within a single layer necessitates dual attention passes and complex tensor indexing, creating a hardware-unfriendly execution pattern.

Methodology

ZigzagAttention addresses the latency bottleneck by moving from "intra-layer" diversity to "inter-layer" exclusivity.

Transport Optimization

The core of ZigzagAttention is the reorganization of heads so that each layer contains either 100% retrieval heads or 100% streaming heads. This is achieved by solving a transport optimization problem based on importance scores. The objective function minimizes the performance penalty of shifting heads while maximizing retrieval capabilities. A scaling factor ($\omega$) balances these shifts, ensuring critical retrieval paths are preserved.

Single-Pass Execution

By ensuring layer-level exclusivity, the model can perform a single, contiguous attention computation per layer. This eliminates the redundant memory access and indexing overhead inherent in mixed-head architectures, directly translating to lower latency.

Decoding latency comparison between DuoAttention and ZigzagAttention across various lengths.

Fine-Tuning

To recover performance lost during structural reorganization, a minimal distillation-driven fine-tuning process is applied. This allows the model to adapt to its new "zigzag" configuration, particularly for high-stakes retrieval tasks.

Evaluation

The authors evaluated ZigzagAttention using a long-context extension of LLaMA-3-8B.

  • Efficiency: Latency benchmarks were conducted across prefilling and decoding lengths from 1k to 32k tokens. ZigzagAttention demonstrated superior hardware efficiency, particularly in decoding, achieving up to 37% acceleration over DuoAttention.
  • Long-Context Performance: On the LongBench suite, ZigzagAttention achieved a score of 38.44, compared to DuoAttention's 39.45 and the full-cache baseline's 39.78. It maintained this performance while utilizing 50% less KV cache.
  • Context Extension: Using the Needle-in-a-Haystack (NIAH) test, the authors demonstrated that ZigzagAttention could extend the effective context window to 600k tokens with near-perfect retrieval accuracy.
Needle-in-a-Haystack results showing near-perfect retrieval up to 600k tokens.

Discussion and Key Insights

A central insight of this work is that hardware efficiency often outweighs theoretical FLOP reductions. While splitting heads might reduce calculations, the resulting memory fragmentation on GPUs can degrade overall speed. ZigzagAttention prioritizes contiguous memory patterns, proving that strategic placement of retrieval-heavy layers can sustain model "memory" throughout the transformer stack without requiring retrieval heads in every layer.

The optimization process itself is highly efficient, requiring only approximately 7 minutes for a LLaMA-3-8B model. While speedup gains slightly diminish at extreme decoding lengths, the method consistently outperforms existing baselines. Future work may explore scaling this exclusive-layer approach to larger models (70B+) and refining the criteria for head importance.

Conclusion

ZigzagAttention offers a hardware-aware solution to the KV cache bottleneck. By enforcing layer-exclusive attention types, it reconciles memory savings with high-speed inference, providing a practical path forward for deploying massive-context LLMs.

Summary updated

BibTeX
@misc{liu2025zigzagattention,
  author = {Zhuorui Liu and Chen Zhang and Dawei Song},
  title = {{ZigzagAttention: Efficient Long-Context Inference with Exclusive Retrieval and Streaming Heads}},
  year = {2025},
  eprint = {2508.12407},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2508.12407},
}
Figure