Abstract
Large Language Models (LLMs) demonstrate a remarkable ability for In-Context Learning (ICL), yet the internal mechanisms driving this behavior remain poorly understood. This paper investigates the "add-k" arithmetic task to reverse-engineer the underlying circuits. We demonstrate that ICL for addition is localized to a minimal set of attention heads and that numerical information is represented within low-dimensional activation subspaces using periodic and magnitude-based components.
Introduction
In-Context Learning (ICL) allows LLMs to perform new tasks based solely on a few demonstrations in a prompt. While previous research has identified "function vectors" that encapsulate task logic, the internal geometry and construction of these vectors have remained elusive. This study focuses on the add-k task (e.g., $y = x + 12$), a structural arithmetic problem that requires the model to extract a specific rule $k$ from examples. Unlike semantic tasks, addition provides a clean environment to study how models represent and manipulate abstract rules.
Methods
The researchers employed a multi-step mechanistic interpretability pipeline to dissect the model's internal state:
- Sparse Optimization: A novel optimization method was developed to identify the smallest subset of attention heads capable of maximizing ICL performance.
- Activation Patching: The identified heads were validated by "patching" their outputs into zero-shot prompts to see if they could successfully inject the addition rule.
- Dimensionality Reduction (PCA): Principal Component Analysis was used to project the 128-dimensional activation space of these heads down to a 6-dimensional "task subspace."
- Trigonometric Decomposition: These 6 dimensions were fitted to periodic functions to determine if the model utilizes modular arithmetic representations.
- Subspace Tracing: A mathematical identity was derived to track information flow from demonstration "label" tokens to the final "aggregator" heads.
Key Insights
Extreme Localization
In Llama-3-8B, the addition logic is concentrated in just three attention heads out of 1024. Ablating these three specific heads causes ICL accuracy to drop from 87% to 43%, confirming their critical role in the arithmetic circuit.
The Geometry of a Number
The model represents the rule $k$ not as a single scalar, but within a 6D subspace. This subspace is further factored into:
- Unit-Digit Subspace (4D): Tracks the unit digit using trigonometric functions with periods of 2, 5, and 10. This effectively creates a modular arithmetic circuit.
- Ten-Digit Subspace (2D): Tracks the "tens" digit or overall magnitude using low-frequency components.
Built-in Self-Correction
A surprising finding is the model's self-correction mechanism. Rather than simply averaging demonstrations, the model tracks errors. If a previous demonstration provides a noisy or incorrect signal, subsequent demonstrations generate signals in the opposite direction to cancel out the error, ensuring the final output remains accurate.
Key Results
- Performance Recovery: Function vectors derived from only the three identified heads in Llama-3-8B-instruct recovered 79% accuracy, representing approximately 90% of the original ICL performance.
- Variance Explained: The identified 6D subspace accounts for 97% of the task-related variance in head activations.
- Causal Validation: Projecting activations onto the unit-digit subspace preserved the model's ability to predict the last digit, while projecting out of it destroyed that capability, proving the subspace's causal necessity.
Conclusion and Future Work
This research provides a rare look into the "black box" of LLM arithmetic. By localizing logic to specific heads and low-dimensional subspaces, we can see how models use periodic representations to handle modular tasks like addition. Future work will explore whether more complex, non-linear reasoning tasks exhibit similar localization and if these mechanistic motifs can be automatically reverse-engineered using AI agents.
Summary updated