Papers
Topics
Authors
Recent
Search
2000 character limit reached

Zigzag Attention in Transformers

Updated 28 January 2026
  • Zigzag Attention is a technique that restructures Transformer attention to enable efficient long-context inference while minimizing degradation in model quality.
  • It employs layer-exclusive retrieval and streaming modes to reduce computational complexity, achieving significant speedups and memory savings compared to full attention.
  • Blockwise Zigzag in LoZA uses structured sparse patterns for near-linear scaling, delivering improved latency and decoding efficiency in long-context scenarios.

Zigzag Attention encompasses two families of techniques that restructure attention mechanisms in Transformer architectures to enable efficient long-context inference while minimizing degradation in model quality. Both families—exemplified by ZigzagAttention with exclusive retrieval and streaming heads, and by blockwise ZigZag attention as instantiated in LongCat ZigZag Attention (LoZA)—directly address computational and memory bottlenecks observed in LLMs with context lengths extending beyond tens of thousands of tokens. These techniques achieve significant reductions in inference latency and memory footprint by replacing traditional full attention either via exclusive per-layer sparsity or through structured, blockwise sparse attention patterns that preserve essential connectivity for information retrieval and reasoning at scale (Liu et al., 17 Aug 2025, Zhang et al., 30 Dec 2025).

1. Motivation and Problem Statement

Standard Transformers exhibit O(n2)\mathcal{O}(n^2) attention complexity, with nn denoting input sequence length. For long-context LLMs, especially in autoregressive decoding, this manifests as rapidly increasing GPU memory use for the key–value (KV) cache, with size O(nd)\mathcal{O}(n \cdot d), where dd is the hidden dimension. As n1,000n \gg 1,000, the operational cost becomes prohibitive. Prior sparse attention patterns—such as sliding window, block-sparse, or strided variants—either inadequately preserve long-range dependency or still incur O(nw)\mathcal{O}(n w) cost with window size ww scaling with nn for quality retention. Zigzag Attention methods are explicitly designed to resolve these trade-offs by either assigning entire layers to exclusive “retrieval” or “streaming” attention modes (ZigzagAttention), or by introducing blockwise alternating global-local connectivity at the kernel level (LoZA), thereby achieving near-linear scaling and robust long-context performance (Liu et al., 17 Aug 2025, Zhang et al., 30 Dec 2025).

2. ZigzagAttention: Layer-Exclusive Retrieval and Streaming Heads

ZigzagAttention addresses the cost of attention under long-context settings by leveraging the observation that not all attention heads contribute equally to global retrieval. Each head (i,j)(i, j) (with i{1,,L}i\in\{1,\dots,L\} layers and nn0 heads per layer) is assigned a gating score nn1 learned by distillation on synthetic long-context data. Mixed per-head attention is realized as

nn2

Heads are sorted by nn3 and a user-defined sparsity quantile nn4 designates the top nn5 fraction as “retrieval” heads and the bottom nn6 fraction as “streaming” heads. Crucially, ZigzagAttention enforces an exclusivity constraint per layer:

nn7

where nn8 for retrieval and nn9 for streaming. Thus, every layer comprises either all retrieval or all streaming heads. Assignment is determined via a transport-based enumeration that minimizes perturbation from the original gating scores, weighted by a hyperparameter O(nd)\mathcal{O}(n \cdot d)0 controlling the penalty for promoting streaming layers back to retrieval mode. Only one attention pass (either full or streaming) is performed per layer, eliminating the two-pass overhead characteristic of prior methods (e.g., DuoAttention) (Liu et al., 17 Aug 2025).

3. Blockwise ZigZag Attention in LoZA

LongCat ZigZag Attention (LoZA) implements a blockwise sparse attention pattern optimized for long-context scaling. The input sequence of length O(nd)\mathcal{O}(n \cdot d)1 is divided into O(nd)\mathcal{O}(n \cdot d)2 contiguous blocks of size O(nd)\mathcal{O}(n \cdot d)3. For every “sparse” (streaming) layer O(nd)\mathcal{O}(n \cdot d)4, each block O(nd)\mathcal{O}(n \cdot d)5 attends to:

  • A local radius of blocks (O(nd)\mathcal{O}(n \cdot d)6 to O(nd)\mathcal{O}(n \cdot d)7)
  • A global “sink” block whose index O(nd)\mathcal{O}(n \cdot d)8 zigzags across layers

Mathematically, for block O(nd)\mathcal{O}(n \cdot d)9, the keys and values attended are

dd0

and the result is

dd1

Across successive layers, by varying dd2, the pattern guarantees any block can reach all others in dd3 hops, whereas per-token computational cost remains constant. In LoZA, typically 50% of attention layers are converted to this streaming sparse pattern, calibrated via a phase attaching trainable dd4 to each layer output, ranking by calibration, and then statically converting the bottom half to sparse after mid-training (Zhang et al., 30 Dec 2025).

4. Quantitative Results and Empirical Analysis

Decoding and Prefill Latency

  • ZigzagAttention achieves per-token decoding speedups of approximately 37% over LLaMA-3-8B and 10–15% over DuoAttention for output lengths from 1k to 32k tokens, without degradation in prefilling speed (Liu et al., 17 Aug 2025).
  • LoZA's blockwise kernel executes with less than 10% of the cost of a full-attention kernel at dd5k tokens, yielding over 50% speedup in prefill and approximately 30% decode cost saving at dd6k tokens (Zhang et al., 30 Dec 2025).

Model Quality

  • On LongBench (50% sparsity), ZigzagAttention's average score (38.44) is within 1.3 points of the baseline LM-3 (39.78) and marginally underperforms DuoAttention (39.45). Needle-in-a-Haystack evaluations (40k–280k context) show no loss in retrieval performance (Liu et al., 17 Aug 2025).
  • LoZA matches or surpasses the quality of LongCat-Flash-Base/Chat on general benchmarks (MMLU, GSM8K, code) and improves upon long-context tasks (LongEval 95.7→99.3, MRCR at up to one million context) (Zhang et al., 30 Dec 2025).

Ablation and Sensitivity

  • The penalty parameter dd7 in ZigzagAttention reveals a trade-off: dd8 gives the best LongBench average, with higher dd9 eroding long-context performance.
  • Naïve interleaving of sparse layers (every-other) in LoZA erases long-context quality (LongEval 95.7→54.1), while calibrated assignment preserves performance (89.6), with full recovery through continued sparse-training (Zhang et al., 30 Dec 2025).

5. Algorithmic Details and Complexity

ZigzagAttention Assignment Algorithm (Layer-exclusive Mode)

O(nw)\mathcal{O}(n w)9 (Liu et al., 17 Aug 2025)

Blockwise ZigZag Sparse Layer in LoZA

ww0 (Zhang et al., 30 Dec 2025)

Computational Complexity

Scheme Cost per layer Scaling with n1,000n \gg 1,0000
Full attention n1,000n \gg 1,0001 Quadratic
Zigzag sparse n1,000n \gg 1,0002 (n1,000n \gg 1,0003) Linear

Interleaving full and Zigzag layers in LoZA yields overall cost n1,000n \gg 1,0004, with n1,000n \gg 1,0005 ensuring the sparse layers’ cost is negligible. Practical speedups of up to n1,000n \gg 1,0006 in the attention-dominated regime are reported; kernel-level Zigzag achieves n1,000n \gg 1,0007 cost reduction for sparse layers (Zhang et al., 30 Dec 2025).

6. Integration, Practical Considerations, and Limitations

For ZigzagAttention, only a one-time “transport” assignment (7 minutes wall time) is needed to fix layer roles; no inference or architectural changes are required beyond the adjusted layer modes. KV cache memory scales down almost linearly with the streaming head fraction (e.g., n1,000n \gg 1,0008 streaming heads yields n1,000n \gg 1,0009 memory reduction) (Liu et al., 17 Aug 2025). LoZA is calibrated during mid-training with learnable O(nw)\mathcal{O}(n w)0, followed by sparsification and curriculum-based long-context retraining. Post-training, task-specific supervised, DPO, or reinforcement finetuning can be applied (Zhang et al., 30 Dec 2025).

Optimal performance requires careful choice of block size O(nw)\mathcal{O}(n w)1, local radius O(nw)\mathcal{O}(n w)2, sink count O(nw)\mathcal{O}(n w)3, and sparsity ratio. For LoZA, O(nw)\mathcal{O}(n w)4, O(nw)\mathcal{O}(n w)5, O(nw)\mathcal{O}(n w)6, and O(nw)\mathcal{O}(n w)7 sparsity have been found effective. For short contexts (O(nw)\mathcal{O}(n w)8k), ZigZag overhead may outweigh benefits, suggesting defaulting to full attention in these regimes. Layer-level sparsity ensures uniform GPU allocation, while per-head sparsity (as in prior work) may introduce computational imbalances (Zhang et al., 30 Dec 2025).

A plausible implication is that the design principles of Zigzag Attention can be generalized to other settings where trade-offs between memory, compute, and retrieval accuracy need to be balanced without major architectural overhaul or kernel reengineering.

7. Applications and Impact

ZigzagAttention and LoZA primarily target LLM deployment scenarios requiring efficient inference over very long contexts, such as retrieval-augmented generation, document-level question answering, and tool-based agentic reasoning, where context lengths often exceed 100k tokens. These methods enable scalable model serving by curbing both per-token latency and KV cache growth without sacrificing core LLM capabilities on established benchmarks. Their “drop-in” nature makes them relevant for production environments where model retraining costs or architecture changes are undesirable, and their impact is most pronounced in prefill-intensive and decode-intensive workloads with large input/output history (Liu et al., 17 Aug 2025, Zhang et al., 30 Dec 2025).

Key limitations involve minor accuracy loss on certain long-context benchmarks at very high sparsity ratios, and inefficiency or computational overhead at short context lengths due to metadata handling. These trade-offs can be tuned, and further research is ongoing regarding the optimal scheduling of sparse/full layers and dynamic adaptation mechanisms.

In summary, Zigzag Attention constitutes a principled, empirically validated family of sparse attention designs capable of preserving LLM utility at million-token scale while dramatically reducing the operational burdens associated with quadratic attention.

Definition Search Book Streamline Icon: https://streamlinehq.com
References (2)

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Zigzag Attention.