Fast attention mechanisms: a tale of parallelism

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

Introduction

Standard Transformers have revolutionized machine learning, but they suffer from a well-known Achilles' heel: the quadratic time bottleneck ($O(N^2)$). As the input sequence length $N$ grows, the cost of computing dense attention—where every token attends to every other token—scales quadratically, making it difficult to process massive contexts. While many "efficient" attention alternatives exist, such as low-rank approximations and sparse attention, it has remained unclear whether these approximations sacrifice the fundamental representational power that makes Transformers so effective.

This paper introduces Approximate Nearest Neighbor Attention (ANNA), a sub-quadratic mechanism that leverages locality-sensitive hashing to find relevant keys. The authors demonstrate that ANNA-transformers are theoretically "tighter" than standard ones, capable of simulating massively parallel algorithms using far fewer resources while maintaining the ability to solve complex reasoning tasks.

The Massively Parallel Computation Gap

Prior theoretical work established a fascinating link between Transformers and the Massively Parallel Computation (MPC) model, a framework used to understand large-scale distributed computing systems like MapReduce. However, there was a glaring inefficiency in this theoretical bridge: simulating a standard Transformer within the MPC model required $N^2$ machines.

The authors ask a pivotal question: Is there an alternative attention mechanism that more tightly captures the power of MPC without the quadratic overhead?

Enter ANNA: Approximate Nearest Neighbor Attention

To bridge this efficiency-power gap, the authors propose ANNA. Instead of computing a dense softmax over all possible keys, ANNA focuses exclusively on "neighborly" queries and keys—those that are close to each other in the embedding space.

  • Locality-Sensitive Hashing (LSH): ANNA uses LSH to group similar vectors into "buckets." During the query phase, a token only attends to keys that fall into its corresponding hash bucket, drastically reducing the number of computations.
  • Theoretical Simulation: Using a special case called Exact-Match Attention (EMA), the authors prove that ANNA can simulate any $R$-round MPC algorithm using just $O(R)$ layers. Furthermore, a "simulation theorem" shows that constant-depth ANNA-transformers can simulate low-rank Transformers, positioning ANNA as a powerful, generalized framework for understanding efficient attention.
  • Distillation Training: Because the LSH mechanism is not naturally differentiable, training ANNA directly is challenging. The authors circumvent this by training a standard softmax Transformer as a surrogate and then "distilling" those weights into an ANNA-based model for inference.

Evaluating Representational Power

To test ANNA's capabilities, the paper focuses on synthetic reasoning benchmarks designed to isolate specific computational skills:

  • Match2: A task requiring the model to identify pairs of elements in a sequence that sum to a specific value (modulo $M$). This tests relational lookups.
  • $k$-hop (Induction Heads): A generalization of associative recall where the model must follow a chain of tokens. This serves as a proxy for the in-context learning capabilities seen in Large Language Models.

Key Insights

The results reveal that sub-quadratic attention is not merely a "faster version" of standard attention; it is a more natural fit for the MPC model. ANNA-transformers can simulate MPC algorithms using a near-linear number of machines ($N^{1-\delta}$), whereas standard Transformers require quadratic resources.

Furthermore, unlike Graph Neural Networks (GNNs) where communication pathways are fixed by an input graph, Transformers rely on dynamic communication. ANNA’s LSH mechanism effectively manages this dynamism by routing information based on content similarity. The paper also demonstrates that ANNA is strictly more powerful than low-rank attention for certain tasks; low-rank models struggle with $k$-hop reasoning without massive depth, while ANNA solves it in $O(\log k)$ layers.

Key Results

  • Sub-quadratic Complexity: The ANNA implementation achieves a runtime of $\tilde{O}(N^{1+3\rho})$, where $\rho$ is a small constant related to hash quality, effectively breaking the $N^2$ barrier.
  • Match2 Performance: A single-layer ANNA-transformer achieved zero error on the Match2 task using just 8 hash tables.
  • $k$-hop Success: On the 1-hop induction task, a two-layer ANNA-transformer achieved an error rate as low as 0.1, significantly outperforming random guessing (0.75 error) and proving that LSH approximations preserve critical "induction head" logic.

Limitations and Future Directions

While ANNA presents a compelling theoretical and empirical step forward, the authors note several limitations. The current reliance on a surrogate softmax model for training highlights the need for a principled, differentiable training method that can optimize ANNA directly. Additionally, while the theoretical results are robust, empirical tests were limited to small-scale synthetic datasets ($N=32$ to $100$). Scaling ANNA to large language modeling tasks remains a promising next step. Finally, while LSH is mathematically efficient, optimizing its performance on modern GPU hardware—which heavily favors dense matrix multiplication—remains a practical engineering challenge.

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