ZETA: Leveraging Z-order Curves for Efficient Top-k Attention

Qiuhao Zeng, Jerry Huang, Peng Lu, Gezheng Xu, Boxing Chen, Charles Ling, Boyu Wang · DOI · OpenReview

Abstract

Transformers have revolutionized deep learning but suffer from a quadratic $\mathcal{O}(N^2)$ complexity in sequence length. ZETA (Z-order curves for Efficient Top-$k$ Attention) addresses this by mapping compressed queries and keys into a 1D space using Morton codes. This enables parallelized nearest-neighbor searches that respect causal masking, reducing complexity to $\mathcal{O}(N \log N)$ and providing significant speedups on long sequences without compromising accuracy.

Introduction

The self-attention mechanism is the computational heart of the Transformer, yet its $\mathcal{O}(N^2)$ scaling makes processing long contexts—such as entire books or high-resolution images—prohibitively expensive. While top-$k$ attention has emerged as a promising solution to sparsify attention, it faces a critical challenge: causal masking. In autoregressive models, tokens cannot attend to future tokens. Existing top-$k$ methods often fail here, either by selecting "future" tokens that are then masked out (leaving queries with no context) or by resorting to sequential processing that negates GPU parallelism. ZETA introduces a novel approach to perform parallel top-$k$ attention while strictly adhering to causal constraints.

Methodology

ZETA achieves its efficiency through several key innovations:

1. Dimensionality Decoupling

In standard Transformers, the dimensions of Queries ($Q$), Keys ($K$), and Values ($V$) are identical. ZETA observes that while $V$ must remain high-dimensional to carry semantic information, $Q$ and $K$ only need enough capacity to represent relative distances. ZETA aggressively compresses $Q$ and $K$ (e.g., to $d_k = 3$) while maintaining a large $V$.

2. Z-order Curves (Morton Codes)

To find the top-$k$ matches efficiently, ZETA maps these low-dimensional vectors into 1D scalars using Z-order curves. By interleaving the bits of multi-dimensional coordinates, Z-order curves preserve spatial locality: points that are close in the original high-dimensional space remain close in the 1D mapping.

Once mapped to 1D, keys are sorted. To respect causal masking, the sorted keys are partitioned into chunks. Queries then perform a parallelized binary search for their nearest neighbors within valid historical chunks. This transformation turns a complex geometric search into a highly optimized sorting and searching problem.

4. Adaptive Cauchy-Softmax

Standard dot-product attention is unstable in very low dimensions. ZETA replaces it with a Euclidean distance metric passed through a trainable Cauchy kernel. The heavy-tailed nature of the Cauchy distribution ensures that even with aggressive pruning, the model retains a meaningful signal from relevant tokens.

Ablation studies on ZETA showing performance across model dimensions, key dimensions ($d_k$), kernels, and top-$k$ values.

Key Results

The authors evaluated ZETA on synthetic recall tasks, the Long Range Arena (LRA) benchmark, and language modeling on WikiText-103.

Performance and Scaling

ZETA demonstrates exceptional scaling. On sequences of length 65,536, ZETA is over 10x faster than FlashAttention (608.2 ms vs 7044.1 ms). On the LRA benchmark, ZETA achieved an average accuracy of 63.51%, outperforming the Vanilla Transformer (54.39%) and other efficient variants like Performer and Linformer.

Language Modeling Quality

Crucially, ZETA's efficiency does not come at the cost of modeling quality. On WikiText-103, ZETA achieved a test perplexity of 26.3, nearly identical to the Vanilla Transformer's 26.2, while using slightly fewer parameters.

Comparison of ZETA against other Transformer variants on the WikiText-103 language modeling task.

Conclusion

ZETA represents a significant step forward in efficient attention mechanisms. By leveraging the geometric properties of Z-order curves and decoupling key/value dimensions, it enables Transformers to handle massive contexts with $\mathcal{O}(N \log N)$ complexity. While hard top-$k$ cutoffs remain a potential source of information loss in extreme reasoning scenarios, ZETA's empirical results suggest it is a robust and highly scalable alternative to standard attention.

Summary updated

BibTeX
@inproceedings{zeng2025zeta,
  author = {Qiuhao Zeng and Jerry Huang and Peng Lu and Gezheng Xu and Boxing Chen and Charles Ling and Boyu Wang},
  title = {{ZETA: Leveraging Z-order Curves for Efficient Top-k Attention}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=j9VVzueEbG},
}
Figure