Mechanism and Emergence of Stacked Attention Heads in Multi-Layer Transformers

Tiberiu Musat · DOI

TL;DR

This paper introduces the "retrieval problem"—a reasoning task that requires a minimum number of transformer layers to solve. The author proves that the required depth grows logarithmically with the task's complexity and demonstrates that while large language models (LLMs) solve it naturally, training smaller models requires an "implicit curriculum" where the model learns intermediate steps before the final answer.

What Problem Does This Paper Address?

While the field of mechanistic interpretability has made great strides in understanding single-layer and two-layer transformers (uncovering structures like "induction heads"), deeper models remain a "black box." We know that depth matters, but we don't always know why a specific number of layers is required for certain reasoning tasks.

The author addresses this gap by defining the retrieval problem: a task where a model must follow a chain of associations (e.g., If A=7, B=A, C=B, what is C?). This is a fundamental building block of logic, programming, and linguistic reference.

Examples of retrieval and conditional retrieval tasks used to benchmark model reasoning.

The paper seeks to answer: How many layers are strictly necessary? How do these multi-layer circuits form during training? And do LLMs actually use these circuits?

Methods

The author employs a three-pronged approach:

  1. Theoretical Analysis: A formal proof establishes a lower bound on the number of layers ($t$) needed to solve a retrieval chain of depth $D$, showing that $t \ge \log_3(2D)$.
  2. LLM Benchmarking: The author tests state-of-the-art models (GPT-4o, Claude 3.5, Gemini 1.5, Llama 3.1) on various natural language formulations of the retrieval problem to see if they can solve it zero-shot.
  3. Mechanistic Interpretability: The author trains "minimal" transformers (1 to 24 layers) on a synthetic version of the task. By studying attention maps and performing "ablations," the author reverse-engineers the specific circuits the models developed.

Datasets

The paper uses five newly introduced synthetic benchmarks designed to test retrieval:

  • F1. Equations: (e.g., a=3, b=a, c=b, c=?)
  • F2. Lives-with: Social relations (e.g., Alice lives in X, Bob lives with Alice...)
  • F3. Kingdoms: Complex multi-step entity relations.
  • F4 & F5. Conditional Retrieval: Tasks where the retrieval path forms a Directed Acyclic Graph (DAG) rather than a simple linear chain, requiring the model to combine multiple previous steps.

Key Insights and Ideas

  • The Logarithmic Depth Requirement: The paper proves that information cannot "jump" across the sequence arbitrarily; it must be moved layer-by-layer. To solve a chain of depth $D$, the model needs a depth that scales logarithmically. This provides a concrete mathematical reason for why deeper models are inherently more capable of complex reasoning.
  • The Necessity of an Implicit Curriculum (IC): Small models failed to learn the task if they were only asked for the final answer. They only succeeded when the training data forced them to predict every intermediate step in the chain. This suggests that complex reasoning in LLMs might emerge because natural language (like Wikipedia or textbooks) inherently contains these intermediate "reasoning steps."
Validation loss and partial loss metrics showing that models only succeed when trained with an implicit curriculum (IC).
  • Sequential Emergence: During training, the model doesn't learn the whole chain at once. It first develops a simple "induction head" to solve a 1-step retrieval. Once that circuit is stable, it "reuses" it to build a 2-step circuit, and so on. Emergence is an additive, step-by-step process.
  • Induction Heads as Building Blocks: The multi-layer circuits discovered are essentially "stacked" induction heads. The first head connects a key to a value, and subsequent heads use that value to look up the next link in the chain.

Key Results

  • LLM Performance: Top-tier models like GPT-4o and Claude 3.5 solve retrieval chains almost perfectly up to a depth of $D=5$. Performance drops significantly as the chain length increases (approaching random guessing by $D=10$).
  • Training Success: In 100% of cases, models trained without the Implicit Curriculum (non-IC) failed to solve the task ($D=3$), regardless of depth. With IC, models successfully learned the task, with earlier steps in the chain always being mastered before later ones.
  • Circuit Discovery: The author successfully reverse-engineered 12-layer and 24-layer models, identifying the specific "retrieval heads" responsible for the information flow.

Limitations and Future Work

The author acknowledges that the analysis was performed on a minimal synthetic formulation, which may not perfectly capture the messy complexity of real-world natural language. Additionally, the reverse-engineering was done manually; while effective for these models, this process is difficult to scale to models with hundreds of billions of parameters.

Future research could investigate whether these specific "retrieval circuits" can be found in production LLMs using automated circuit discovery tools, and whether explicitly adding "curriculum-style" data can improve the reasoning depth of smaller models.

Summary updated

BibTeX
@inproceedings{musat2025mechanism,
  author = {Tiberiu Musat},
  title = {{Mechanism and Emergence of Stacked Attention Heads in Multi-Layer Transformers}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=rUC7tHecSQ},
}
Figure