Abstract
In-context learning (ICL) is a central capability of Transformer models, but the structures in data that enable its emergence and govern its robustness remain poorly understood. In this work, we study how the structure of pretraining tasks governs generalization in ICL. Using a solvable model for ICL of linear regression by linear attention, we derive an exact expression for ICL generalization error in high dimensions under arbitrary pretraining-testing task covariance mismatch. This leads to a new alignment measure that quantifies how much information about the pretraining task distribution is useful for inference at test time. We show that this measure directly predicts ICL performance not only in the solvable model but also in nonlinear Transformers. Our analysis further reveals a tradeoff between specialization and generalization in ICL: depending on task distribution alignment, increasing pretraining task diversity can either improve or harm test performance. Together, these results identify train-test task alignment as a key determinant of generalization in ICL.
Introduction
In-Context Learning (ICL)—the ability of a model to learn a new task from just a few examples in its prompt—is a defining feature of modern Large Language Models (LLMs). Despite its success, the fundamental principles governing when ICL succeeds or fails remain elusive. Most theoretical studies assume that pretraining and testing distributions are identical, yet in practice, there is almost always a structural mismatch. This research introduces a mathematical framework to quantify this mismatch, demonstrating that ICL generalization depends on a specific structural "alignment" between task distributions rather than simply the volume of pretraining data.
Theoretical Framework: The Solvable Model
To analyze ICL dynamics, the researchers utilize a "solvable model" consisting of a linear attention module performing in-context linear regression. Using tools from high-dimensional statistics and Random Matrix Theory (RMT), they derive an exact analytical formula for ICL generalization error under arbitrary task covariance mismatch.
A central contribution of this work is the introduction of a new alignment measure, $e_{\text{misalign}} = \langle C_{\text{test}}, \mathcal{K} \rangle$. Here, $C_{\text{test}}$ represents the covariance of test tasks, and $\mathcal{K}$ is a matrix representing the "knowledge" or task-specific features extracted during pretraining. This measure quantifies how well the model's learned inductive biases match the requirements of the test-time distribution.
Key Insights and Results
The Diversity Trade-off
While common intuition suggests that more pretraining diversity is always beneficial, the authors identify a critical trade-off. If test tasks are low-dimensional or narrow, increasing pretraining diversity can actually harm performance. The model becomes a "jack of all trades, master of none," diluting its ability to solve specific tasks. Conversely, when training data is scarce, pretraining on a lower-dimensional version of the test distribution can create a helpful inductive bias, allowing the model to focus on the most important features.
Alignment as a Universal Predictor
Crucially, the alignment measure derived from the linear model proves to be a robust predictor for complex, nonlinear Transformers. When tested against models using softmax attention and MLP layers, the measure showed a near-perfect correlation with actual ICL error across various synthetic task distributions (e.g., power-law, low-rank, and spiked covariances).
Phase Transitions in Meta-Learning
The study identifies a clear "learning transition" based on task diversity ($\kappa$). Below a certain threshold of unique tasks, the model tends to memorize specific instances; above this threshold, it begins to "meta-learn" the underlying algorithm, enabling generalization to unseen tasks within the aligned distribution.
Conclusion and Future Work
This work identifies train-test task alignment as a fundamental determinant of ICL generalization. By providing an exact error formula and a universal alignment metric, the researchers offer a path toward more principled pretraining strategies. While the current study relies on Gaussian synthetic data and single-layer theoretical models, the high correlation with nonlinear Transformers suggests these geometric principles are broadly applicable. Future work will explore how these alignment principles apply to real-world language data and deeper architectural dynamics.
Summary updated