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

Tiberiu Mușat

TL;DR

This paper introduces the "retrieval problem"—a multi-step reasoning task—to study how deep Transformer models build complex internal circuits. The author proves that solving this task requires a minimum number of layers that grows logarithmically with the task's complexity, and demonstrates that these multi-layer "retrieval heads" emerge in a strict, sequential order driven by an implicit curriculum during training.

What Problem Does This Paper Address?

While the field of mechanistic interpretability has made great strides in understanding how single-layer and two-layer Transformers work (uncovering mechanisms like "induction heads"), understanding the internal "algorithms" of deeper, multi-layer models remains a major challenge.

The author tackles this by proposing the retrieval problem. Imagine a chain of assignments: $A=7$, $B=A$, $C=B$, $D=C$. To find the value of $D$, a model must "hop" through several steps of memory. This is a fundamental building block of reasoning, programming, and tracking concepts in text.

Examples of standard and conditional retrieval questions used to test language models.

The paper asks: How many layers does a Transformer actually need to solve this? How do the attention heads coordinate across layers? And how does the model learn to do this during training?

Methods and Datasets

The author uses a three-pronged approach to investigate this phenomenon:

  1. Theoretical Analysis: Using a simplified model of information flow, the author proves a lower bound on the number of layers ($t$) required to solve a retrieval problem of depth $D$: $t \ge \log_3(2D)$.
  2. Empirical Benchmarking: Large Language Models (LLMs) like GPT-4o and Claude 3.5 were tested on natural language versions of the retrieval problem to see if they could solve it zero-shot. These datasets included five specific benchmarks ranging from simple equations to logic puzzles like "Lives-with" relationships.
  3. Mechanistic Reverse-Engineering: The author trained "minimal" Transformers (with only one head per layer) on a synthetic dataset of $N$ retrieval chains with $D$ steps. By manually inspecting attention maps and performing ablations (selectively "turning off" parts of the model), the author mapped out the exact circuits the models developed.

Key Insights and Ideas

  • The Logarithmic Depth Requirement: You cannot solve a deep retrieval problem with just any model; there is a hard physical limit. Information can only travel so far through the "residual stream" in a single pass.
  • The Necessity of an Implicit Curriculum (IC): This is perhaps the most striking finding. Models failed to learn the task if they were only asked for the final answer (e.g., "What is D?"). They only succeeded when the training objective forced them to predict every step in the chain simultaneously. This "implicit curriculum" allows the model to learn the first hop, then the second, and so on.
  • Sequential Emergence: Attention heads do not all "click" at once. During training, the model first develops an induction-like head for the first step. Once that is stable, a head in a deeper layer emerges to handle the second step by "reusing" the output of the first, creating a literal stack of logic.
  • Circuit Diversity: Even on the same simple task, different training runs resulted in different internal "algorithms." While they all used the same number of heads, the specific layers and connections varied, showing there are multiple ways for a Transformer to "wire" itself.

Key Results

  • LLM Performance: Frontier models (GPT-4o, Claude 3.5, Gemini 1.5 Pro, Llama 3.1) solve the retrieval problem almost perfectly for up to $D=5$ steps, but accuracy drops sharply as the chain gets longer (around $D=8$ or $9$).
Accuracy of state-of-the-art language models on the retrieval task as the number of steps increases.
  • Training Success: In the synthetic experiments, 100% of models trained without the implicit curriculum failed to solve the task (loss > 0.7), whereas models with the curriculum succeeded.
  • Emergence Timing: In a 24-layer model, the first "hop" head emerged abruptly around epoch 450. Subsequent heads for deeper hops emerged sequentially at epochs 467, 485, and 522.

Limitations and Future Work

The author acknowledges that the "minimal formulation" used for reverse-engineering (one head per layer, no MLPs) is a simplification. Real-world LLMs are much noisier, with thousands of heads that might share or duplicate tasks.

Future research could investigate whether natural language itself provides the "implicit curriculum" necessary for these heads to emerge, or if we need to specifically design training data to encourage deeper reasoning circuits. Additionally, scaling the manual reverse-engineering process to automated "circuit discovery" tools remains a high priority for the field.

Summary updated

BibTeX
@inproceedings{musat2025mechanism,
  author = {Tiberiu Mușat},
  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