Fast attention mechanisms: a tale of parallelism

Jingwen Liu, Hantao Yu, Clayton Sanford, Alexandr Andoni, Daniel Hsu · OpenReview

Abstract

Transformers have the representational capacity to simulate Massively Parallel Computation (MPC) algorithms, but they suffer from quadratic time complexity, which severely limits their scalability. We introduce an efficient attention mechanism called Approximate Nearest Neighbor Attention (ANNA) with sub-quadratic time complexity. We prove that ANNA-transformers (1) retain the expressive power previously established for standard attention in terms of matching the capabilities of MPC algorithms, and (2) can solve key reasoning tasks such as Match2 and $k$-hop with near-optimal depth. Using the MPC framework, we further prove that constant-depth ANNA-transformers can simulate constant-depth low-rank transformers, thereby providing a unified way to reason about a broad class of efficient attention approximations.

Introduction

The success of the Transformer architecture is often attributed to its attention mechanism, which allows every token in a sequence to interact with every other token. However, this flexibility comes at a steep price: a quadratic ($O(N^2)$) computational complexity relative to the sequence length $N$. This "quadratic tax" creates a bottleneck for processing long sequences and scaling models to larger contexts.

While various "fast" attention alternatives—such as low-rank approximations, kernel-based methods, and hashing-based sparsification—have been proposed, a fundamental theoretical question remains: do these efficient variants retain the core representational power that makes standard Transformers so effective? This paper introduces Approximate Nearest Neighbor Attention (ANNA) and utilizes the Massively Parallel Computation (MPC) framework to prove that sub-quadratic attention can indeed match the computational capabilities of parallel algorithms.

The ANNA Architecture

ANNA-transformers replace the dense $O(N^2)$ attention matrix with a sparse interaction pattern determined by proximity in the embedding space. Instead of every query attending to every key, ANNA treats queries as search probes and keys as data points.

Key Components:

  • Locality-Sensitive Hashing (LSH): ANNA leverages LSH to efficiently identify "neighborly" keys—those within a specific distance $r$ of a query—in sub-quadratic time.
  • Sparse Interaction: By restricting attention to these neighbors, the model reduces the number of interactions per token, leading to a total runtime of approximately $O(N^{1+3\rho})$, where $\rho$ is a small constant (typically $< 1/3$).
  • Exact-Match Attention (EMA): A significant theoretical finding of this work is that even a simplified version of ANNA, where tokens only attend to identical keys, is sufficient to simulate complex routing and parallel logic.

Theoretical Framework: Transformers as Parallel Computers

Previous research established that standard Transformers can simulate MPC algorithms, which are models of distributed computing where multiple machines process data in parallel. However, the simulation of a standard Transformer on an MPC system was historically "coarse," requiring an unrealistic $N^2$ machines to handle the dense attention matrix.

This paper provides a "tighter" theoretical link. We prove that ANNA-transformers can simulate any $R$-round MPC algorithm using $O(R)$ layers, but crucially, an MPC system can simulate ANNA using significantly fewer machines—approaching linear scaling in $N$. This suggests that ANNA is a more natural architectural fit for the parallel algorithms that Transformers seem to execute.

Furthermore, we demonstrate that ANNA serves as a unifying architecture. We prove that constant-depth ANNA-transformers can simulate constant-depth low-rank transformers. This positions nearest-neighbor approaches as a "super-set" that can explain the capabilities of other efficient attention approximations.

Empirical Evaluation

To validate the theoretical power of ANNA, we evaluated the model on two synthetic reasoning benchmarks designed to test information retrieval and associative logic:

  1. Match2: A task requiring the model to identify pairs of elements satisfying a mathematical relation (e.g., $x_i + x_j = 0 \pmod M$). A single ANNA layer achieved zero error using 8 hash tables, demonstrating its ability to perform precise relational matching.
  2. $k$-hop Induction Heads: A multi-step associative recall task that mimics the in-context learning behavior of large language models. A two-layer ANNA-transformer achieved an error rate of 0.1, significantly outperforming the random guess baseline of 0.75.

Limitations and Future Work

While ANNA offers a robust theoretical foundation for efficient attention, several challenges remain for practical deployment:

  • Differentiability: The LSH mechanism is not naturally differentiable. In this study, the authors trained a standard softmax Transformer and distilled its knowledge into the ANNA model. Developing natively differentiable LSH-based layers is a priority for future research.
  • Hardware Optimization: Current GPUs are highly optimized for dense matrix multiplications (e.g., FlashAttention). Despite its lower theoretical complexity, LSH-based methods require specialized hardware-aware implementations to achieve wall-clock speedups over standard attention.
  • Scaling: Future work will focus on testing ANNA on large-scale, real-world language modeling tasks to determine if the theoretical benefits translate to performance gains in LLMs.

Conclusion

ANNA-transformers bridge the gap between computational efficiency and theoretical expressivity. By proving that sub-quadratic attention can simulate both MPC algorithms and low-rank Transformers, this work provides a unified framework for understanding how Transformers can remain powerful while breaking the quadratic scaling barrier.

Summary updated

BibTeX
@inproceedings{liu2025fast,
  author = {Jingwen Liu and Hantao Yu and Clayton Sanford and Alexandr Andoni and Daniel Hsu},
  title = {{Fast attention mechanisms: a tale of parallelism}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=o9iReV4FGm},
}
Figure