Out-of-Distribution Generalization of In-Context Learning: A Low-Dimensional Subspace Perspective

Soo Min Kwon, Alec S. Xu, Can Yaras, Laura Balzano, Qing Qu

TL;DR

When can Transformers generalize to completely new tasks during in-context learning (ICL)? This paper provides a elegant mathematical answer using a low-dimensional subspace perspective: if a model is trained on a union of diverse subspaces, it can perfectly generalize to any unseen task within their joint span—even in regions with zero training probability. However, if trained on a single subspace, it fails to generalize out-of-distribution (OOD), incurring an error proportional to the angle of the distribution shift.

What Problem Does This Paper Address?

In-context learning (ICL) is one of the most fascinating emergent properties of large language models (LLMs)—the ability to learn a new task from just a few examples in a prompt without any weight updates. Yet, the community is deeply divided on whether ICL can truly generalize out-of-distribution (OOD). Some empirical studies show remarkable robustness to distribution shifts, while others argue that Transformers merely interpolate within their training mixture.

The core challenge is the lack of a rigorous theoretical framework to explain when and why ICL generalizes OOD. This paper bridges the gap by modeling task distribution shifts geometrically as varying angles between low-dimensional subspaces.

Methods

The authors analyze a single-layer linear attention model tasked with solving linear regression problems. To study distribution shifts cleanly, they parameterize the task vectors (the true regression weights $\mathbf{w}$) using low-rank covariance matrices. This allows them to define distinct task "subspaces" and quantify distribution shifts using the principal angles ($\theta$) between them.

They contrast two training regimes:

  1. Single Subspace: Task vectors are drawn from a single Gaussian distribution.
  2. Union of Subspaces: Task vectors are drawn from a mixture of Gaussians spanning multiple orthogonal subspaces, representing pre-training task diversity.
Figure 1: An illustrative overview of the setup of this paper. We consider two models trained on different task vectors and analyze the conditions under which a model can generalize OOD. When task vectors are drawn from a union of subspaces, a transformer via ICL can generalize to subspaces with zero density in the training data, whereas a model trained on a single subspace cannot.

Beyond the mathematical proofs, they empirically validate their findings using a deep, nonlinear GPT-2 architecture and evaluate performance on nonlinear function classes (such as cosine bases and Hermite polynomials).

Datasets

Because this is a theoretical and mechanistic study, the paper relies entirely on synthetically generated datasets to maintain strict mathematical control:

  • Linear Regression Data: Features are sampled from an isotropic Gaussian $\mathbf{x} \sim \mathcal{N}(0, \mathbf{I}_d)$, and labels are generated with added Gaussian noise.
  • Nonlinear Function Spaces: Inputs are sampled uniformly, and labels are generated using orthogonal basis functions, specifically Cosine bases and Hermite polynomials, to simulate real-world signal structures.

Key Insights and Ideas

  • The "Span" Explains the Magic: The apparent ability of Transformers to generalize OOD isn't magic—it happens because the evaluation task lies within the linear span of the diverse training subspaces. If the test task is in the span, the model succeeds even if that specific region had zero probability density during pre-training.
  • Diversity Changes the Learned Algorithm: Under the hood, an optimized linear attention layer acts like a single step of preconditioned gradient descent. When trained on a union of subspaces, the model learns a projection matrix that encompasses the entire joint span, making it invariant to angular shifts within that span.
  • The Cost of Narrow Training: If a model is trained on a single subspace, its learned algorithm projects test data onto that specific "familiar" subspace. If the test task is shifted by an angle $\theta$, this projection introduces a structural error proportional to $\sin^2(\theta)$.
  • Linear Theory Predicts Nonlinear Reality: Remarkably, the simple, single-layer linear attention proofs perfectly predict the empirical behavior of deep, multi-layer nonlinear models like GPT-2 when learning complex, nonlinear function classes.

Key Results

  • Perfect OOD Generalization (Union of Subspaces): Theorems 1 and 2 prove that when the model is trained on a union of subspaces, the asymptotic test risk drops arbitrarily close to the optimal label noise variance ($\sigma^2$), completely independent of the shift angle $\theta$.
  • Quantifiable Failure (Single Subspace): Proposition 3 shows that for a model trained on a single subspace, the asymptotic test risk is exactly $r \sin^2(\theta) + \sigma^2$ (where $r$ is the rank). At a maximum shift ($\theta = \pi/2$), the error scales with the rank of the subspace.
  • LoRA as a Fix: The authors prove in Corollary 6 that if a model is trained on a single subspace, adding rank-$r$ LoRA adapters allows it to successfully adapt to a shifted subspace by learning the missing orthogonal directions.

Limitations and Future Work

While the paper provides a compelling geometric framework, it has a few limitations:

  • Linear Foundations: The formal proofs are restricted to linear regression and single-layer linear attention, leaving the mathematical formalization of deep, multi-layer softmax attention to future work.
  • Feature Shifts vs. Task Shifts: The authors note that while Transformers handle task shifts well (changes in the function mapping), they are highly sensitive to feature shifts (changes in the input distribution), which can cause the test risk to diverge by $\mathcal{O}(1/\epsilon^2)$.

Future directions include extending these subspace guarantees to fully nonlinear transformer layers and exploring the exact learning dynamics of how LoRA captures these subspace shifts in practice.

Summary updated

BibTeX
@inproceedings{kwon2025out,
  author = {Soo Min Kwon and Alec S. Xu and Can Yaras and Laura Balzano and Qing Qu},
  title = {{Out-of-Distribution Generalization of In-Context Learning: A Low-Dimensional Subspace Perspective}},
  booktitle = {International Conference on Artificial Intelligence and Statistics},
  year = {2025},
  url = {https://openreview.net/forum?id=xrmPHv8SNT},
}
Figure