Transformers on Markov data: Constant depth suffices

Nived Rajaraman, Marco Bondaschi, Ashok Vardhan Makkuva, Kannan Ramchandran, Michael Gastpar

Introduction

Transformers have demonstrated a remarkable capacity for "in-context learning," the ability to adapt to new patterns provided within a prompt without further parameter updates. A fundamental benchmark for this capability is the learning of $k^{th}$-order Markov processes, where the probability of the next symbol depends strictly on the preceding $k$ symbols. Historically, theoretical models suggested that shallow transformers required a number of attention heads scaling linearly with the context length $k$ to capture these dependencies. This implied that modeling long-range dependencies would necessitate prohibitively wide architectures.

This paper challenges that paradigm, demonstrating that a standard transformer architecture with constant depth—specifically just three layers and a single attention head—is sufficient to represent $k^{th}$-order Markov processes for any $k$. The research highlights a critical trade-off: by leveraging non-linearities like Layer Normalization (LN) and Feed-Forward Networks (FFN), models can achieve high representational power through depth rather than width.

The Role of Architecture in Pattern Matching

The study distinguishes between "attention-only" models (consisting solely of softmax and linear layers) and "standard" transformers. The standard architecture, which includes Layer Normalization and Feed-Forward blocks, proves significantly more efficient.

The standard Transformer architecture, featuring Layer Normalization and Feed-Forward Networks, which the authors prove can represent k-gram models in constant depth.

A key insight is the "power multiplier" effect of Layer Normalization. Beyond its role in training stability, LN enables the model to perform $L_2$ normalization on embeddings. When paired with dot-product attention, this allows the transformer to implement $L_2$-norm-based attention, which is exceptionally effective at "zooming in" on exact matches of long sequences. This mechanism effectively creates "Higher-order Induction Heads" that can match an entire $k$-length prefix simultaneously.

In contrast, attention-only models are subject to a "doubling" constraint, where each layer can at most double the context window it observes. This results in a depth requirement that scales logarithmically with $k$ ($O(\log k)$). The addition of standard non-linearities collapses this requirement to a constant three layers.

Theoretical and Empirical Findings

The authors provide both a constructive proof and empirical validation for their claims. Theoretically, Theorem 5.1 establishes that a 3-layer, 1-head transformer can represent any $k$-gram model with an embedding dimension that scales only with the alphabet size, independent of context length $k$.

Empirically, the researchers trained "tiny" transformers on synthetic Markov data generated from Dirichlet priors. These experiments confirmed that minimal architectures could indeed learn complex dependencies that were previously thought to be beyond their reach.

Test loss gap from the optimal for a 3-layer, 1-head transformer across different Markov orders (k=2, 4, 8), showing successful convergence.

As shown in the experimental results, a 3-layer model with a single head successfully learned an 8th-order Markov process, significantly outperforming the theoretical limits predicted for such small models in earlier literature. Furthermore, the study utilized communication complexity tools to prove that 1-layer models are fundamentally limited; they require either the number of heads or the embedding dimension to scale almost linearly with the total sequence length $T$ to be effective.

Conclusion and Future Directions

This work redefines our understanding of transformer efficiency, proving that constant depth is sufficient for representing complex Markovian dependencies. While the paper focuses on representational power, it opens new questions regarding optimization. Specifically, while these 3-layer solutions exist, the mechanism by which Gradient Descent reliably discovers them during training remains an area for future investigation. Understanding these learning dynamics will be crucial for further optimizing transformer architectures for long-range sequence modeling.

Summary updated

BibTeX
@inproceedings{rajaraman2024transformers,
  author = {Nived Rajaraman and Marco Bondaschi and Ashok Vardhan Makkuva and Kannan Ramchandran and Michael Gastpar},
  title = {{Transformers on Markov data: Constant depth suffices}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=5uG9tp3v2q},
}
Figure