Transformers as Cross-Task Learners: Shared Structure Drives Sample Efficiency in In-Context Learning

Zhongjie Shi, Rongjie Lai, Alexander Cloninger, Wenjing LiaoarXiv 2026

TL;DR

How do Transformers learn entirely new tasks from just a few examples in a prompt? This paper provides a rigorous mathematical answer by showing that pretraining maps out a low-dimensional "task space." At inference time, the prompt simply pinpoints the unseen task within this pre-learned geometry, allowing the Transformer to achieve dimension-free sample efficiency and bypass the curse of dimensionality.

What Problem Does This Paper Address?

In-Context Learning (ICL)—the ability of a model to adapt to a new task at inference time using only a few prompt demonstrations without updating its weights—is one of the most powerful features of modern Transformers. However, our theoretical understanding of why it works so well remains limited.

Most existing mathematical analyses look at ICL through a single-task lens or restrict themselves to simple linear models. In reality, Transformers are pretrained on a massive variety of diverse, nonlinear tasks. This paper addresses a fundamental gap: how do Transformers exploit the shared, low-dimensional structure of nonlinear task families during pretraining to become highly sample-efficient during few-shot inference?

Methods

The authors introduce a geometric framework that uses covering numbers to quantify the complexity of a task space without forcing it into a rigid parametric formula. This allows them to define an "intrinsic task dimension."

Using this framework, they propose an oracle mechanism called a task-identification-and-evaluation procedure, which operates via a two-level Softmax partition-of-unity (POU) approximation scheme:

  1. Task Identification: The model uses the context examples to locate ("localize") the unseen task relative to a set of pre-learned "anchor functions."
  2. Function Evaluation: The model predicts the response to a new query by aggregating the evaluations of these anchor functions.

Crucially, the authors explicitly construct a standard three-block Transformer with Softmax attention that implements this exact mathematical procedure, proving that the Transformer's native architecture is perfectly aligned with this geometric localization process.

Figure 1: Task identification and evaluation from task-space and input-domain coverings. (a) The n context observations identify the unseen task f relative to representative anchor functions hl in a covering of task space Mf. (b) For each anchor function, the construction uses the anchor values hl(zm) associated with a covering of input domain Mx. Localizing the query xn+1 relative to the anchor points zm combines these values to approximate hl(xn+1). The task-identification weights then aggregate the resulting anchor-function evaluations to approximate f(xn+1).

Datasets

As a primarily theoretical paper, the authors validate their mathematical insights using a controlled synthetic setup: quadratic polynomials defined on a 5-dimensional hypercube ($[-1, 1]^5$). They compare a Transformer pretrained on the full 21-dimensional quadratic family against a Transformer pretrained on a hidden 2-dimensional subspace, alongside standard ridge regression.

Key Insights and Ideas

  • Shifting the Statistical Burden: Pretraining absorbs the heavy lifting of learning the overarching task geometry. At inference time, the prompt's job isn't to learn a function from scratch, but merely to identify which pre-learned task is being requested.
  • Dimension-Free Inference: The authors prove that once a Transformer has seen a sufficiently large number of pretraining tasks ($\Gamma$), its error rate at inference time becomes completely independent of the input and task dimensions. This mathematically explains the magic of few-shot learning.
  • Architectural Alignment: The mathematical concept of a partition-of-unity maps beautifully onto the native dot-product Softmax attention mechanism of Transformers, suggesting that the architecture possesses an inherent inductive bias for cross-task learning.

Key Results

  • The Generalization Bound: The authors establish an end-to-end generalization error bound that separates pretraining complexity from context complexity: $$\mathcal{O}\left(\max\left\{ \left(\frac{\log n}{n}\right)^{1/3}, \left(\frac{n^2 (\log n)^{d_x/\alpha+1}}{\Gamma}\right)^{\frac{2}{q+2}} \right\}\right)$$ When the number of pretraining tasks ($\Gamma$) is large, the second term vanishes, leaving a leading context-length ($n$) term that is entirely free from the curse of dimensionality.

  • Empirical Validation: In their quadratic polynomial experiments, when given a very short context length of just 4 examples ($n=4$), the Transformer pretrained on the hidden 2D subspace achieved a Test Mean Squared Error (MSE) of 0.0783. In comparison, the Transformer trained on the full 21D family scored 0.6666, and 21D ridge regression scored 0.6769.

Table 1: Test MSE on a common set of prompts from the hidden two-dimensional subspace. The two Transformers are pretrained on the full 21-dimensional quadratic family and the hidden subspace, respectively; ridge regression uses the full 21-dimensional polynomial basis and is fitted independently to each prompt.

Limitations and Future Work

  • Uniform Regularity Assumptions: The proofs rely on the assumption that the task functions exhibit uniform smoothness (specifically, $\alpha$-Hölder smoothness). Real-world data (like natural language) often contains highly discontinuous or non-uniform structures.
  • Fixed Context Lengths: The explicit Transformer construction assumes a uniform context length framework. The authors note that a promising future direction is developing a theory that can accommodate varying context lengths within a single, unified model.

Summary updated

BibTeX
@misc{shi2026transformers,
  author = {Zhongjie Shi and Rongjie Lai and Alexander Cloninger and Wenjing Liao},
  title = {{Transformers as Cross-Task Learners: Shared Structure Drives Sample Efficiency in In-Context Learning}},
  year = {2026},
  eprint = {2609.29060},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2609.29060},
}
Figure