Contextualize-then-Aggregate: Circuits for In-Context Learning in Gemma-2 2B

Aleksandra Bakalova, Yana Veitsman, Xinting Huang, Michael Hahn

TL;DR

This paper uncovers a two-step "contextualize-then-aggregate" mechanism in large language models (LLMs) for in-context learning (ICL). By using causal interventions, they show that LLMs first build contextually rich representations of individual examples and then aggregate this information to perform tasks, a process crucial for handling ambiguity.

What Problem Does This Paper Address?

In-context learning (ICL) is a remarkable capability of LLMs, allowing them to perform tasks based on examples provided in the prompt without explicit fine-tuning. While researchers have identified "function vectors" that encode task information, the precise circuit or mechanism by which LLMs assemble this information from multiple few-shot examples remains poorly understood. Existing work often focuses on the aggregation step, but it's unclear if this fully explains ICL performance, especially when examples are ambiguous or the task itself is complex. This paper aims to demystify this process by identifying the causal information flow within LLMs during ICL.

Methods

The authors employ causal interventions, specifically path patching, to dissect the information flow within the Gemma-2 2B model during ICL. This technique involves treating the LLM as a computation graph and systematically ablating (zeroing out) specific connections (edges) to observe the impact on performance. By replacing activations along an edge with those from a "counterfactual" input (a modified prompt designed to remove specific information), they can determine if that edge is causally responsible for transferring task-relevant information. They analyze this at both a position-level (treating entire tokens like "input" or "output" as nodes) and an activation-level (examining individual attention heads).

Datasets

The study uses five naturalistic ICL tasks:

  • Capitalization: Arbitrary lowercase words to arbitrary capitalized words.
  • Country-Capital: Countries to their capitals.
  • Present-Past: English verbs in present tense to past tense.
  • Person-Sport: Names of people to their associated sports.
  • Copying: Simple token repetition.

These datasets were sourced from prior work (Todd et al., 2024) and augmented for specific experiments (e.g., creating ambiguous examples for the Present-Past task).

Key Insights and Ideas

  • Contextualize-then-Aggregate is Key: The core finding is a two-stage process. Lower layers contextualize individual examples using preceding ones, while higher layers aggregate this contextualized information for the final prediction. This is a significant departure from solely focusing on aggregation.
  • Contextualization is Crucial for Ambiguity: The importance of the contextualization step dramatically increases when few-shot examples are ambiguous, helping the model disambiguate and avoid errors like simply copying.
  • Task-Dependent Circuits: The specific circuits identified are not monolithic; they vary in complexity and the importance of contextualization across different tasks.
  • Beyond Induction: The authors found that aggregation attention edges don't necessarily prioritize specific few-shot examples based on similarity to the query, challenging the notion that ICL is solely driven by classical "induction circuits."

Key Results

  • The proposed "contextualize-then-aggregate" circuits explain over 90% of the model's performance on ICL tasks.
  • An AGGREGATION-only circuit significantly underperforms the full model, especially on tasks like Capitalization and Country-Capital, dropping to below 60% accuracy in some cases.
  • Adding contextualization edges (e.g., yi → yj and xi → xi+1) recovers most of this performance gap, achieving over 90% accuracy.
  • In the presence of ambiguous examples (e.g., verbs with identical present/past forms), the AGGREGATION-only circuit's performance plummets (from 93% to 56%), while the full model maintains high accuracy (95%), highlighting the critical role of contextualization.
  • The yi → tN+1 edges (aggregation) causally transport task information, not token information.

Limitations and Future Work

  • Model and Task Specificity: The study primarily focuses on Gemma-2 2B and five specific tasks. While they mention evaluating larger variants (9B, 27B), the detailed circuit analysis is limited to the 2B model for tractability.
  • Computational Cost: The patching methodology is computationally intensive, limiting the scope of experiments.
  • MLP and Individual Head Interpretation: The current analysis focuses on attention edges and doesn't deeply interpret the role of MLPs or individual attention heads within the identified circuits.
  • Future Work: The authors suggest combining their findings with sparse feature circuits to understand contextualization at the level of individual representations. Further work is needed to comprehensively interpret components in larger models. They also note that future research could refine the stopping criteria for their pruning methods.

Summary updated

BibTeX
@inproceedings{bakalova2025contextualize,
  author = {Aleksandra Bakalova and Yana Veitsman and Xinting Huang and Michael Hahn},
  title = {{Contextualize-then-Aggregate: Circuits for In-Context Learning in Gemma-2 2B}},
  booktitle = {COLM},
  year = {2025},
  url = {https://openreview.net/forum?id=rGNAyHReSg},
}
Figure