Large Language Models (LLMs) exhibit a remarkable capability known as In-Context Learning (ICL)—the ability to solve novel tasks simply by observing a few examples in a prompt, without any weight updates. While recent research has shown that Transformers can simulate simple algorithms like linear regression in-context, real-world tasks are rarely that straightforward. In practice, models must process complex, non-linear features rather than simple linear maps.
A recent paper by Guo et al. (ICLR 2024) takes a major step forward by asking: How do Transformers handle tasks where the underlying "features" are complex and fixed, but the "logic" applied to them changes from prompt to prompt?
The "Learning with Representations" Framework
Most existing ICL theory focuses on toy problems, such as learning a simple linear function $y = wx$. However, the relationship between an input (like a sentence) and a label is highly non-linear in reality. To bridge this gap, the authors propose a more realistic framework called Learning with Representations.
In this setup, the label $y$ depends on the input $x$ through a complex, fixed representation function $\Phi^*$ (such as a multi-layer perceptron) and a task-specific linear vector $w$:
$$y = \langle w, \Phi^*(x) \rangle$$
The challenge is twofold: the Transformer must "know" the complex $\Phi^*$ from its pre-training data, but it must figure out the specific $w$ for each new prompt on the fly. This mimics how an LLM uses its pre-trained knowledge of grammar and facts ($\Phi^*$) to solve a specific reasoning task ($w$) provided in a few-shot prompt.
Methodology: Probing and Pasting
To understand how Transformers solve these compositional tasks, the researchers combined theoretical construction with empirical "mechanistic interpretability" across synthetic datasets (including supervised learning and dynamical systems).
- Theoretical Construction: They mathematically proved that a Transformer with sufficient depth and attention heads can implement a two-stage algorithm: first computing the MLP representation, and then performing Ridge Regression on the transformed results.
- Linear Probing: To observe the model's internal behavior, they trained small linear models ("probes") to predict the hidden representations $\Phi^*(x)$ from the Transformer's internal activations at every layer.
- The "Pasting" Experiment: In a novel test of modularity, the authors took the upper layers of a Transformer trained on complex tasks and "pasted" them onto a simple linear input. If the upper layers truly act as an independent "linear ICL engine," they should be able to solve simple linear tasks despite being trained on complex ones.
Key Findings and Mechanisms
The study reveals that Transformers naturally develop a modular, dissected architecture to handle complex ICL tasks:
- The Functional Dissection: Lower layers (roughly layers 1-5 in a 12-layer model) act as a fixed feature extractor, focusing on computing the representation $\Phi^*$. Upper layers (layers 6-12) act as a flexible solver, focusing entirely on the linear ICL algorithm.
- In-Weights vs. In-Context: The fixed representation $\Phi^*$ is baked into the model's weights during pre-training (In-Weights Learning). Conversely, the task-specific vector $w$ is computed dynamically from the prompt (In-Context Learning).
- Near-Optimal Performance: Trained Transformers consistently match the performance of the "Bayes-optimal" ridge regressor. They don't just approximate a solution; they find the mathematically ideal one for these tasks.
- Probing Success: Probes confirmed the functional dissection. The error in recovering the representation $\Phi^*(x)$ drops sharply in the first few layers. Meanwhile, the error in predicting the final label $y$ only drops in the later layers, exactly where the linear ICL occurs.
- Pasting Proof: The pasting experiment was a success. The upper layers successfully performed linear ICL on raw inputs they had never seen before, confirming that the "ICL solver" module is robust and independent of the specific representation module.
Conclusion and Future Directions
By proving that Transformers split complex tasks into "in-weights" feature extraction and "in-context" linear inference, this research provides a crucial stepping stone toward understanding real-world LLM behavior.
While the study relies on synthetic data and idealized representations, it opens exciting avenues for future work. Next steps include investigating how these mechanisms scale to billion-parameter models, how Transformers handle representations that shift slightly across tasks, and how these mechanistic insights might eventually improve the efficiency of fine-tuning and prompting strategies.
Summary updated