How do Transformers decide which words in a long sentence actually matter for predicting the next one? While we know they excel at "In-Context Learning" (ICL), the mechanical algorithms they run under the hood often remain a mystery.
A new paper from researchers at EPFL, titled "Transformers Learn Latent Mixture Models In-Context via Mirror Descent," provides a fascinating answer. They demonstrate that Transformers can dynamically infer hidden causal structures by implementing a classic optimization algorithm—Mirror Descent—directly within their forward pass.
The Challenge of Dynamic Context
Most previous research on ICL has focused on static tasks, like linear regression or simple Markov chains, where the relationship between tokens is fixed (e.g., every even token depends on the previous odd one).
However, real language is highly dynamic. Consider the sentence: "The dog that saw the bird threw the ball, and then ran to fetch..." To predict the next word, the model must realize that "dog" and "ball" are causally relevant, while "bird" is a distractor.
This relevance is a latent variable—it’s not explicitly written down; it must be inferred from the context on the fly. The authors ask: What algorithm do Transformers use to infer these latent structures?
The Mixture of Transition Distributions (MTD) Framework
To study this, the authors utilize a framework called the Mixture of Transition Distributions (MTD). In an MTD model, the next token is generated by picking one of the previous $m$ tokens based on hidden weights ($\lambda$) and applying a transition matrix ($\pi$).
The paper’s core contributions revolve around how Transformers solve this MTD problem:
- Algorithmic Mapping: Estimating the hidden weights $\lambda$ is equivalent to optimizing a likelihood function over a probability simplex. The authors show that the ideal tool for this is Mirror Descent (specifically, the Exponentiated Gradient algorithm).
- The "Construction" Proof: They mathematically construct a 3-layer "disentangled" Transformer that exactly implements one step of Mirror Descent:
- Layer 1: Computes "posterior responsibilities" (how likely each past position was responsible for the transitions seen so far).
- Layer 2: Aggregates these responsibilities across the entire sequence.
- Layer 3: Uses the aggregated information to "read out" the final mixture weights and make a prediction.
- Bayesian Link: They prove that this one-step Mirror Descent estimator is a first-order approximation of the Bayes-optimal predictor.
Experimental Setup
The study relies on synthetic datasets generated from the MTD model with a vocabulary size of 5, MTD orders ($m$) of 3, 4, and 5, and sequence lengths varying from 64 to 1984 tokens. They trained both simplified "disentangled" Transformers and standard Transformers from scratch to see if they would naturally discover the Mirror Descent algorithm.
Key Insights and Results
- Attention as an Optimizer: The most striking insight is that the attention mechanism is perfectly architected to perform Mirror Descent. The softmax operation naturally handles the exponentiated gradient update, while the weighted sum handles the aggregation of evidence.
- Implicit Regularization: A single step of Mirror Descent often performs better than finding the perfect Maximum Likelihood Estimate (MLE). The one-step update stays closer to the uniform prior, acting as a form of regularization that mimics Bayesian inference.
- Theory Matches Practice: Transformers trained from scratch achieved nearly identical KL-divergence scores to the hand-constructed Mirror Descent model. Furthermore, their attention maps displayed the exact "banded" patterns and responsibility computations predicted by the theory.
- Depth Matters: While 3 layers implement one step of Mirror Descent, 5-layer models performed significantly better, matching the performance of 2-step Mirror Descent. This suggests deeper Transformers are essentially "unrolling" more iterations of the optimization algorithm.
Limitations and Future Work
While the MTD model is a powerful proxy, real language contains hierarchical and recursive structures that MTD doesn't fully capture. Additionally, extending the explicit mathematical construction from one step of Mirror Descent to a general $N$-step process remains an open challenge. Finally, the current work assumes the transition matrix $\pi$ is known; future work could explore learning both the transitions and the mixture weights simultaneously in-context.
By framing In-Context Learning as a form of internal optimization, this paper moves us significantly closer to opening the black box of how large language models process and understand prompt structures.
Summary updated