Transformers, parallel computation, and logarithmic depth

Clayton Sanford, Daniel Hsu, Matus Telgarsky

Abstract

This research establishes a formal link between Transformer architectures and Massively Parallel Computation (MPC), demonstrating that Transformers function fundamentally as parallel rather than serial processors. By employing a "pointer doubling" strategy, Transformers can solve sequential tasks—such as following a chain of $k$ links—in $O(\log k)$ layers. This logarithmic depth efficiency distinguishes Transformers from serial models like RNNs and modern State-Space Models (SSMs), which require depth proportional to $k$ for similar tasks.

Introduction

Despite the ubiquity of Transformers, theoretical understanding of their specific computational advantages over recurrent architectures has remained limited. While universality theorems suggest that sufficiently large models can compute any function, they fail to explain why Transformers are exceptionally efficient at certain algorithmic tasks. This paper addresses the representation gap by viewing Transformers through the lens of distributed computing and classical complexity theory.

Theoretical Framework: Transformers as MPC

The authors bridge deep learning and complexity theory by proving a bidirectional relationship between Transformers and the Massively Parallel Computation (MPC) model. They demonstrate that a constant number of self-attention layers can simulate a single round of MPC. In this framework, the attention mechanism acts as a "message routing" system, allowing every token to communicate with every other token simultaneously. This inherent parallelism allows the architecture to execute algorithms that are depth-efficient, even if the underlying task appears sequential.

The $k$-hop Induction Head Task

To empirically validate their theory, the researchers designed a synthetic "pointer-chasing" task. In this $k$-hop induction task, a model must follow a chain of $k$ bigram links to identify a target token. While a serial approach would require $k$ steps (finding link 1, then link 2, etc.), the authors hypothesized that a Transformer could use a "doubling" strategy—similar to binary lifting—to solve the problem in $\log_2(k)$ steps.

Testing was conducted on synthetic datasets with an alphabet size of 4 and context lengths of 100. The complexity was varied by adjusting the chain length $k$ from 0 to 16, testing both infinite-sample and finite-sample training regimes to observe generalization and the emergence of specific circuits.

Key Results: The Doubling Threshold

The empirical results provide striking confirmation of the logarithmic depth theory. As shown in Figure 1, there is a sharp threshold for error based on the number of layers $L$ relative to the hop distance $k$.

Error rates for the $k$-hop induction task across different model depths $L$. Each additional layer approximately doubles the maximum hop distance $k$ the model can successfully navigate, confirming the logarithmic depth theory.

Specifically:

  • A 2-layer model successfully handles 1-hop.
  • A 3-layer model handles up to 2-hops.
  • A 4-layer model handles up to 4-hops.
  • A 6-layer model handles up to 16-hops.

Each additional layer effectively doubles the "reasoning" capacity of the model for this task. Mechanistic interpretability analysis confirmed that the trained models actually discovered and implemented the pointer doubling circuits predicted by the mathematical construction.

The "Mamba" Gap and Architectural Trade-offs

A critical insight of this work is the sharp separation between Transformers and serial models. Recurrent models, including modern SSMs like Mamba, require depth proportional to $k$ to solve the same task that Transformers solve in $\log k$ depth.

Furthermore, the authors argue that the $O(N^2)$ quadratic cost of attention—often viewed as a computational burden—is precisely what enables this massive parallelism. "Efficient" sub-quadratic approximations (like Performer or Longformer) may inadvertently sacrifice the ability to perform these parallel shortcuts, making them depth-inefficient for complex algorithmic reasoning.

Conclusion and Future Work

While this paper proves that these parallel solutions exist and can be learned, several questions remain. The embedding dimension required to "compile" an MPC protocol into a Transformer can be quite large ($O(s^4 \log N)$), and the optimization dynamics that lead a model to discover these circuits from random initialization are not yet fully understood. Future research will focus on how these parallel shortcuts manifest in natural language tasks, such as long-range entity tracking and complex nested reference resolution.

Summary updated

BibTeX
@inproceedings{sanford2024transformers,
  author = {Clayton Sanford and Daniel Hsu and Matus Telgarsky},
  title = {{Transformers, parallel computation, and logarithmic depth}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=QCZabhKQhB},
}
Figure