arXiv(opens in a new tab)cs.LGSep 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2609.08981

Transformers as In-Context Samplers: From Closed-Form Diffusion to Estimation-Free Sampling

Arman Adibi, Alireza Jafari, Mohammad Ghavamzadeh, Hadi Daneshmand

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

A growing body of work establishes that large language models are not mere statistical memorizers, but are capable of in-context learning: performing inference at test time using only examples provided in the prompt, without any parameter updates. Prior theoretical work has shown that this capability extends to supervised learning tasks such as linear regression. We prove that in-context learning extends further to data generation: frozen transformers can simulate iterative generative samplers from in-context samples. We first show that transformers can realize closed-form and smoothed closed-form diffusion samplers. The construction identifies a concrete generative role for softmax attention: it computes responsibility weights and weighted empirical averages, while feedforward layers implement Euler updates.

To empirically relate these constructions to pretrained language models, we study semantic-topic sampling: prompts consisting of words drawn from a common semantic category, such as animals, foods, or cities. Across transformer layers, the normalized hidden states exhibit a two-stage geometry: they move toward a uniform spherical reference in intermediate layers and then return to structured, topic-dependent representations near the output. We further measure an interacting-particle energy on these hidden-state clouds and observe the same U-shaped pattern. We then prove that transformers can approximate an energy-based sampler, constructing the same U-shaped energy across the layers.

1 Introduction

"In-context learning" [1] has changed how we think about transformers. A transformer can adapt its behavior at inference time using only information provided in a prompt, without updating its parameters. This phenomenon was first made prominent by large language models, where a prompt containing a few demonstrations can induce the model to perform a new task [1–5]. It has since become a central paradigm for studying how models use demonstrations, instructions, and contextual information at inference time [6].

Existing studies have mostly focused on conditional data generation, specifically for supervised learning: the prompt contains examples from an unknown task, and the model predicts the output for a new query. A standard formalization studies prompts of the form

(x1,f(x1),…,xk,f(xk),xquery)⏟prompt↦f(xquery)⏟completion.\underbrace{(x_{1},f(x_{1}),\ldots,x_{k},f(x_{k}),x_{\mathrm{query}})}_{\mathrm{prompt}} \mapsto \underbrace{f(x_{\mathrm{query}})}_{\mathrm{completion}}.

For example, Garg et al. [7] ask whether transformers can learn simple function classes in context, such as linear functions, sparse linear functions, decision trees, and shallow neural networks. Subsequent

Preprint.

work studies whether transformers implement particular learning algorithms in context, including ridge regression, gradient descent, higher-order optimization, and algorithm selection [8–11]. These results suggest that transformers can do more than match surface patterns in the prompt: they can execute nontrivial computations inside their forward pass.

Transformers were originally proposed for conditional text generation [12], but have since become the backbone of powerful unconditional generative models, including latent diffusion models [13]. While recent studies have extensively investigated the mechanistic interpretation of in-context learning in conditional text generation, it remains an open question whether such capabilities extend to unconditional data generation and broader generative modeling applications. In this paper, we ask whether in-context learning extends to data generation:

References (43)