How Do Transformers Perform Two-Hop Reasoning in Context?

Tianyu Guo, Hanlin Zhu, Ruiqi Zhang, Jiantao Jiao, Song Mei, Michael I. Jordan, Stuart Russell · Submission to ICLR 2026 · Rejected

TL;DR

While large language models (LLMs) easily solve simple two-hop deduction problems in isolation, adding irrelevant distractor premises into the context causes pre-trained models to collapse into uniform random guessing among candidate answers. Fine-tuning on a modest set of just 1,000 examples fixes this failure mode and generalizes to contexts with more distractors. By reverse-engineering a minimal three-layer Transformer, the authors show this recovery is driven by an abrupt phase transition from unselective attention to a structured, multi-layer sequential retrieval circuit.

What Problem Does This Paper Address?

Consider the classical syllogism: “Socrates is human. All humans are mortal. Therefore, Socrates is mortal.” This requires two inferential hops: connecting a source entity ([SRC]) to a bridge entity ([BRG]), and then linking that bridge to an end entity ([END]).

While prior mechanistic interpretability work has studied in-weight multi-hop reasoning (retrieving facts memorized in model parameters), this paper focuses on in-context two-hop reasoning—extracting and linking unfamiliar facts directly from the prompt on the fly. Specifically, the authors test robustness against distractors: irrelevant premises mixed into the prompt. They show that standard pre-trained LLMs fail dramatically in this setting, collapsing to near-random guesses across candidate end entities.

Target Chain:     [Src-T] -> [Brg-T]  |  [Brg-T] -> [End-T]
Distractor Chain: [Src-NT] -> [Brg-NT] | [Brg-NT] -> [End-NT]
Query:            [Src-T] -> ???  (Target: [End-T])

Methods

To understand both the failure and the learned solution, the authors combine macroscopic evaluations with mechanistic reverse-engineering:

  • LLM Probing & Fine-Tuning: Testing base vs. supervised fine-tuned models (primarily OLMo-7B) on prompts containing varying numbers of distractor chains.
  • Minimal Proxy Model: Because single-layer and two-layer Transformers theoretically and empirically fail at this task, the authors train a minimal 3-layer, 1-head, attention-only Transformer from scratch on a symbolic abstraction of the task.
  • Mechanistic Dissection:
  • Attention Logits: Analyzing pre-softmax scalar values across layers to reveal what information is queried and copied without softmax compression artifacts.
  • Logit Lens: Projecting intermediate representations through the unembedding matrix directly to vocabulary space to interpret what each layer represents.
  • Analytical Toy Model: Constructing a 3-parameter attention-only mathematical model to reproduce the phase-transition dynamics observed during gradient descent.

Datasets

  1. Synthetic Natural Language Benchmark: Over 50,000 examples generated across 6 distinct semantic templates (e.g., familial relations, biology, geography) populated from predefined entity pools. Contexts vary in difficulty by mixing in $K-1$ distractor premise pairs.
  2. Fine-Tuning Dataset: A small set of 1,000 curated prompts containing exactly one target chain and one distractor ($K = 2$).
  3. Symbolic Reasoning Dataset: An abstract, synthetically generated dataset where entities are unique single tokens and relations are purely positional concatenated token pairs (e.g., [SRC] [BRG] and [BRG] [END]). Each sequence contains 1 target chain, 4 distractor chains, and concludes with [SRC-T]?.

Key Insights and Ideas

  • Why Pre-Trained Models Guess Randomly (The Slow Phase): In the early training stage, the model learns that end entities are child tokens, but cannot yet route queries selectively:

  • In Layer 2, child tokens attend uniformly to all preceding parent tokens.

  • In Layer 3, the query token attends uniformly to all child tokens.
  • The logit lens reveals that positive and negative activations for source and bridge tokens cancel out during linear readout, leaving only positive logits for all candidate [END] entities. Because attention was completely uniform across premises, each candidate end token receives an identical probability of $\approx 1/K$.
  • The Structured Circuit (After the Phase Transition): Around 800 training steps, the model undergoes a sharp transition into a sequential query pipeline:

  • Layer 1 (Intra-Premise Copying): Each child token strictly attends to its paired parent (e.g., [BRG-T] copies [SRC-T]; [END-T] copies [BRG-T]).

  • Layer 2 (Bridge Retrieval): The query token attends specifically to [BRG-T]. Because [BRG-T] already holds [SRC-T] from Layer 1, the query representation now contains both the source and bridge identities.
  • Layer 3 (End Entity Selection): The query attends directly to [END-T]. Because both the query and [END-T] now share [BRG-T] features, attention cleanly matches them, routing the correct [END-T] token to the output.
  • Architectural Lower Bound: Two-hop reasoning in-context requires at least 3 layers for an attention-only network (unlike single induction-head copy tasks, which require 2 layers).

Key Results

  • Collapse to $1/K$ in Base OLMo-7B: Without distractors ($K=1$), OLMo-7B assigns 0.72 average probability to the correct [END-T]. With distractors, the probability assigned to [END-T] drops to 0.37 ($K=2$), 0.25 ($K=3$), 0.18 ($K=4$), and 0.14 ($K=5$), mirroring uniform random choice across the $K$ available end entities.
  • Sample-Efficient Fine-Tuning: Fine-tuning on 1,000 examples with only one distractor ($K=2$) drives target accuracy to 1.00 at $K=1$ and $K=2$.
  • Length Generalization: Despite training strictly on $K=2$, the fine-tuned model generalizes to harder distractor regimes, achieving 0.66 at $K=3$, 0.57 at $K=4$, and 0.50 at $K=5$ (far exceeding the 0.20 random guessing baseline).

Limitations and Future Work

  • Premise Order Assumptions: In the synthetic setup, the first premise ([SRC] [BRG]) is always placed earlier in the sequence than the second premise ([BRG] [END]), leaving out-of-order premise reasoning unexplored.
  • Scaling to Arbitrary $k$-Hops: The mechanistic analysis is restricted to 2-hop tasks; scaling the reverse-engineering framework to arbitrary $k$-hop chains remains an open direction.
  • Toy vs. Production Architectures: While the findings explain the behavior of a 3-layer, 1-head attention-only model, validating whether identical weight-level circuits execute inside full multi-head, multi-billion parameter architectures with MLPs remains to be verified.

Summary updated

BibTeX
@misc{guo2025how,
  author = {Tianyu Guo and Hanlin Zhu and Ruiqi Zhang and Jiantao Jiao and Song Mei and Michael I. Jordan and Stuart Russell},
  title = {{How Do Transformers Perform Two-Hop Reasoning in Context?}},
  year = {2025},
  eprint = {2502.13913},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2502.13913},
}
Figure