Context. GPT-3 learns a task from a few input-output examples in its prompt with no gradient update, although nothing in next-token pretraining asks for this and a prompt of concatenated unrelated examples is an unnatural, low-probability document. Xie et al. ask what property of the pretraining distribution makes this happen, assuming the model has learned that distribution exactly; Raventós et al. ask, for transformers trained on regression prompts, when the learned predictor stops being the Bayes rule for its pretraining distribution. If every pretraining document is generated by a hidden concept that persists across the document, then predicting the next token already means inferring that concept, and a prompt works by supplying evidence about it. In-context learning is then the posterior predictive of the pretraining mixture, and it still works when the prompt is out of distribution, because the format mismatch lowers the log-likelihood by a bounded amount per example while the evidence grows with the example length. A trained transformer stays Bayesian for its pretraining distribution only while that distribution has few tasks: past a task-diversity threshold it abandons the Bayes estimator for the tasks it has seen, accepts a higher training loss, and converges to the Bayes estimator for the distribution that generated them, which is what generalizes.
Setting (Xie et al.). A concept with prior fixes the transition matrix of an HMM, and a pretraining document is a draw from the mixture . A prompt is unlike a document: its examples of length are independent, each restarted from a prompt start distribution, run under the prompt's concept and followed by a delimiter. The in-context predictor is under the pretraining distribution, compared with the best predictor , whose margin over the runner-up is . The assumptions bound the log-likelihood lost to the prompt format: delimiter states emit only the delimiter, transitions into them are below under and above under , their start probabilities lie in , start probabilities under exceed , and the prompt start distribution is within in total variation of the post-delimiter distribution. This gives two per-example bounds on that loss,
Theorems 1 to 3. If the evidence in one example exceeds the mismatch in one example, with the expected KL between the -th token conditionals of the prompt distribution and of concept , then the in-context predictor converges to the optimal one as , and its 0-1 risk to the Bayes risk; the condition does not involve . The proof treats as a point mass, so the theorem concerns a family in which has positive prior mass, such as a finite one. When some concepts violate the condition, the limiting excess 0-1 risk is at most , with a Fisher-information condition number and a calibration function of the logistic loss that is roughly linear on the useful range, so longer examples help like (Theorem 2, assuming the 0-1 and logistic risks have the same minimizers). Theorem 3 drops and the continuity assumption when the test input has uniformly random length.
The predictor is a posterior average whose weights are likelihood ratios to the true concept. Marginalize the concept and the test example's start state, and divide by : the weight on is which is 1 at , so it suffices that has a negative limit for every other . The argument resembles Bernstein-von Mises, but the standard theorem does not apply: under the model one HMM chain runs through the whole prompt, and the prompt comes from a different distribution.
The delimiters factor the likelihood into per-example terms, up to a constant per example. The delimiter token comes only from delimiter states, so they act as approximate renewal points: condition each example on the preceding delimiter state, and the bounds replace that conditional by a fresh start, where is the probability of one example as if it began a fresh document.
The average converges to a difference of KLs, which the condition makes negative. Under the prompt distribution the examples are i.i.d., so the law of large numbers gives ; the first is at most , since prompt and -examples differ only in the start state, and the chain rule splits the second over tokens: The evidence grows with because every token, inputs included, is evidence, while the mismatch is a fixed number of nats per example. This is misspecified Bayes (Berk; Kleijn and van der Vaart): the posterior goes to the concept closest in KL to the prompt distribution, and the condition makes that .
Once the concept is selected, the test example's start state must not change the argmax. With the model still mixes over the test example's start state , with post-delimiter weights where the optimal predictor uses the prompt start distribution. The argmax is unchanged if, for every delimiter state, the two start-state posteriors given are within in total variation, since each class probability then moves by less than . Closeness of the start distributions themselves is not enough, because conditioning on reweights start states by their likelihood: a state with tiny prior mass but high likelihood can dominate one posterior and not the other.
Without distinguishability, the bound on the summed KL still bounds the last-token KL. The posterior can stay spread over the concepts that violate the condition, so the limit predicts with a mixture over them; convexity of KL bounds its excess logistic risk by the worst single , and converts that to 0-1 risk. What remains is the last-token KL, knowing only that the sum over positions is at most , and the quadratic expansion of with Fisher information gives it in two steps: The condition number enters when moving from the average over positions to one position. With a uniformly random test length the test KL is that average itself, and no continuity is needed.
On a synthetic HMM mixture, trained models learn in context because of the concept structure. On GINC (5 concepts, factorial HMMs, about 10 million pretraining tokens), GPT-2-style transformers and an LSTM reach in-context accuracy that rises with both the number of examples and their length . Pretraining on random transitions, which shows every token transition but has no latent concept, gives flat curves, and prompts from concepts outside the pretraining family are not learned; this rules out "seeing the transitions" as the explanation, though it shows only that the outputs track the posterior and does not show that the network computes it. Some results are not explained by the theory: deeper transformers with the same validation loss learn better in context, the smaller LSTM does best, and accuracy varies by 10 to 40 points across orderings of the same examples. On GPT-3, five long LAMBADA examples beat five short ones (70.7% vs 69.8%), while duplicating the short ones to equal length does not help (69.6%).
Setting (Raventós et al.). A task is , a sequence has and , , , and a causal GPT-2 is trained to predict each from the preceding pairs. Pretraining tasks are uniform on a fixed set of vectors drawn once from ; is the task diversity. Two posterior means bracket the behavior: dMMSE, the Bayes estimator for the discrete prior on the seen tasks, which minimizes the pretraining loss, and ridge , the Bayes estimator for , optimal on new tasks. Defaults are , , .
More data per task pushes the model in opposite directions on the two sides of a threshold. For up to about the transformer matches dMMSE, optimal on seen tasks and poor on new ones. Beyond that it leaves dMMSE on seen tasks, so it no longer minimizes its own objective, and moves toward ridge on new tasks much faster than dMMSE itself does. A quick simulation (my check, not in the paper) shows how far dMMSE is from ridge: on new tasks dMMSE gives 0.91, 0.63 and 0.47 per dimension at , against 0.36 to 0.38 for ridge, so at the threshold the Bayes estimator for the pretraining prior is still about 25% worse than ridge, and a transformer that matches ridge there is not computing it. Raising sequences per task, by batch size or by steps with the same result, pulls the model toward dMMSE for and toward ridge for . The authors extrapolate a sharp phase transition as sequences per task go to infinity, a limit inferred from finite-size trends and not observed.
The switch is not underfitting or blurred memory, and it does not come from covering the task space. Below the threshold the distance to ridge on new tasks is minimized at an early-stopping time ; above it the distance falls monotonically through four times the base training, so the shape of the learning curve changes (a very late turn toward dMMSE cannot be excluded). A dMMSE with each point mass blurred into a Gaussian, the width tuned for new tasks, is still beaten from on, so storing the tasks imprecisely does not explain it. The threshold grows roughly linearly with dimension (about at ), whereas dMMSE approaches ridge only when the seen tasks cover the sphere where Gaussian tasks concentrate, which takes exponentially many; three values of the dimension are few for fitting a scaling law. Weight decay lowers the threshold, more capacity raises it for a small model, and with Laplace tasks the model again moves past a threshold to the Laplace-prior Bayes predictor (itself estimated by a model trained with unlimited tasks).
Consequences and limits. Xie et al. make "in-context learning is Bayesian inference" checkable: document-level latent structure in pretraining is enough for the exact next-token conditional to select the prompt's concept, as long as each example's evidence exceeds the log-likelihood lost to its format. Raventós et al. show where that reading stops holding for trained models: the choice is between two Bayes estimators, the one for the tasks actually seen, which the loss rewards, and the one for the population they came from, which generalizes. A statistician would recognize the difference between using the empirical distribution of past tasks as a prior and using a smooth prior fitted to it; the transformer does the second once is large, so data scale alone is not enough and diversity has to grow with it. Xie et al.'s theory is limited to the pretraining distribution itself, so it says nothing about how a finite network approximates it or why scale matters at fixed loss; it is asymptotic in with no rate, needs in the pretraining family, and is tested on five synthetic concepts. Raventós et al.'s evidence is empirical, for one model family on linear regression, and identifies neither the mechanism nor the implicit bias that selects ridge.
Terms
- Concept
- document-level latent variable: the HMM transition matrix shared by a whole document
- Prompt distribution
- independent examples from , each restarted from , joined by delimiters
- In-context predictor
- under the pretraining mixture (posterior predictive)
- per-example log-likelihood lost to the format at the delimiter and at the start state
- Distinguishability
- for all
- Task diversity
- number of distinct regression vectors in pretraining, drawn once from
- dMMSE
- posterior mean under the discrete prior on seen tasks: softmax of residual likelihoods over
- Ridge,
- posterior mean under ; optimal on new tasks
- Threshold
- diversity where more sequences per task switch from pulling toward dMMSE to toward ridge; - at
- Dimension scaling
- at : linear in rather than exponential
Quiz
- Why do longer examples (larger ) help in-context learning in this theory?The signal has terms, including those for input tokens, while the mismatch cost is a fixed number of nats per example. More tokens per example make the evidence for outgrow the mismatch.
- In the continuity version (Theorem 2), why does the Fisher condition number appear?The summed KL bounds through the smallest Fisher eigenvalue across positions, and the last-token KL is then bounded through the largest. Their ratio enters when moving from the average over positions to one position.
- Why does the smoothed-dMMSE comparison matter?A network that cannot resolve tasks might act like dMMSE with blurred point masses. With the blur tuned to be as good as possible on new tasks, the transformer is still better from on, so blurring alone does not explain it.
- Why is a threshold that grows linearly in evidence that the transformer is not approaching ridge the way dMMSE does?Gaussian tasks concentrate on a sphere of radius about , and a discrete prior matches the Gaussian posterior only when its atoms cover it finely, which takes exponentially many. A threshold proportional to means the transformer gets to ridge without that coverage.
Sources
- Sang Michael Xie et al. An Explanation of In-context Learning as Implicit Bayesian Inference. ICLR, 2022.
- Allan Raventós, Mansheej Paul, Feng Chen, and Surya Ganguli. Pretraining task diversity and the emergence of non-Bayesian in-context learning for regression. NeurIPS, 2023.