Table of Contents
Fetching ...

The Bayesian Geometry of Transformer Attention

Naman Aggarwal, Siddhartha R. Dalal, Vishal Misra

TL;DR

This work demonstrates that transformers can implement exact Bayesian posterior inference in controlled wind-tunnel tasks where memorization is infeasible and the true posterior is known. By analyzing two settings—bijection elimination and HMM state tracking—the authors uncover a unified three-stage mechanism: layer-0 creates an orthogonal hypothesis frame, middle layers perform sequential Bayesian elimination via progressive QK sharpening, and late layers refine posterior precision along a low-dimensional value manifold while keeping routing stable. They show that Transformers achieve near-perfect calibration of predictive entropy with respect to Bayes, while capacity-matched MLPs fail to implement the necessary content-addressable routing and hierarchical refinement. The findings provide mechanistic interpretability for Bayesian reasoning in transformers, offer a rigorous lower bound for reasoning in LLMs, and propose Bayesian wind tunnels as a principled tool to connect small verifiable systems to more complex language models. This work lays the groundwork for extending such wind-tunnel diagnostics to natural-language tasks and larger architectures, enabling principled evaluation of inference capabilities beyond surface performance metrics.

Abstract

Transformers often appear to perform Bayesian reasoning in context, but verifying this rigorously has been impossible: natural data lack analytic posteriors, and large models conflate reasoning with memorization. We address this by constructing \emph{Bayesian wind tunnels} -- controlled environments where the true posterior is known in closed form and memorization is provably impossible. In these settings, small transformers reproduce Bayesian posteriors with $10^{-3}$-$10^{-4}$ bit accuracy, while capacity-matched MLPs fail by orders of magnitude, establishing a clear architectural separation. Across two tasks -- bijection elimination and Hidden Markov Model (HMM) state tracking -- we find that transformers implement Bayesian inference through a consistent geometric mechanism: residual streams serve as the belief substrate, feed-forward networks perform the posterior update, and attention provides content-addressable routing. Geometric diagnostics reveal orthogonal key bases, progressive query-key alignment, and a low-dimensional value manifold parameterized by posterior entropy. During training this manifold unfurls while attention patterns remain stable, a \emph{frame-precision dissociation} predicted by recent gradient analyses. Taken together, these results demonstrate that hierarchical attention realizes Bayesian inference by geometric design, explaining both the necessity of attention and the failure of flat architectures. Bayesian wind tunnels provide a foundation for mechanistically connecting small, verifiable systems to reasoning phenomena observed in large language models.

The Bayesian Geometry of Transformer Attention

TL;DR

This work demonstrates that transformers can implement exact Bayesian posterior inference in controlled wind-tunnel tasks where memorization is infeasible and the true posterior is known. By analyzing two settings—bijection elimination and HMM state tracking—the authors uncover a unified three-stage mechanism: layer-0 creates an orthogonal hypothesis frame, middle layers perform sequential Bayesian elimination via progressive QK sharpening, and late layers refine posterior precision along a low-dimensional value manifold while keeping routing stable. They show that Transformers achieve near-perfect calibration of predictive entropy with respect to Bayes, while capacity-matched MLPs fail to implement the necessary content-addressable routing and hierarchical refinement. The findings provide mechanistic interpretability for Bayesian reasoning in transformers, offer a rigorous lower bound for reasoning in LLMs, and propose Bayesian wind tunnels as a principled tool to connect small verifiable systems to more complex language models. This work lays the groundwork for extending such wind-tunnel diagnostics to natural-language tasks and larger architectures, enabling principled evaluation of inference capabilities beyond surface performance metrics.

Abstract

Transformers often appear to perform Bayesian reasoning in context, but verifying this rigorously has been impossible: natural data lack analytic posteriors, and large models conflate reasoning with memorization. We address this by constructing \emph{Bayesian wind tunnels} -- controlled environments where the true posterior is known in closed form and memorization is provably impossible. In these settings, small transformers reproduce Bayesian posteriors with - bit accuracy, while capacity-matched MLPs fail by orders of magnitude, establishing a clear architectural separation. Across two tasks -- bijection elimination and Hidden Markov Model (HMM) state tracking -- we find that transformers implement Bayesian inference through a consistent geometric mechanism: residual streams serve as the belief substrate, feed-forward networks perform the posterior update, and attention provides content-addressable routing. Geometric diagnostics reveal orthogonal key bases, progressive query-key alignment, and a low-dimensional value manifold parameterized by posterior entropy. During training this manifold unfurls while attention patterns remain stable, a \emph{frame-precision dissociation} predicted by recent gradient analyses. Taken together, these results demonstrate that hierarchical attention realizes Bayesian inference by geometric design, explaining both the necessity of attention and the failure of flat architectures. Bayesian wind tunnels provide a foundation for mechanistically connecting small, verifiable systems to reasoning phenomena observed in large language models.
Paper Structure (70 sections, 1 theorem, 19 equations, 18 figures, 1 table)

This paper contains 70 sections, 1 theorem, 19 equations, 18 figures, 1 table.

Key Result

Theorem 1

The minimizer of eq:ce is the Bayesian posterior predictive distribution where

Figures (18)

  • Figure 1: Bijection wind tunnel: transformer matches the Bayesian posterior; MLP cannot. Entropy trajectories at 150k training steps. The transformer lies essentially on top of the analytic Bayes curve across positions, while the capacity-matched MLP barely reduces uncertainty and fails to implement hypothesis elimination. This is the comparison summarized quantitatively in \ref{['tab:hmm_transformer_vs_mlp']} and discussed in \ref{['sec:results-bijection']}.
  • Figure 2: Bijection wind tunnel: per-sequence entropy dynamics. Eight randomly chosen bijections from the test set. Each panel shows transformer entropy (solid) and analytic Bayes entropy (dashed) as a function of position. The sawtooth pattern----discrete drops when mappings are revealed and collapses to (near) zero when previously seen inputs reappear----confirms that the transformer is performing stepwise hypothesis elimination, not merely matching the Bayes curve in aggregate.
  • Figure 3: Bijection wind tunnel: layer-wise ablation. Mean absolute entropy error (bits) when ablating each layer (attention+FFN) in turn, averaged over seeds. Removing any single layer increases calibration error by more than an order of magnitude, showing that the Bayesian computation is genuinely hierarchical and compositional rather than shallow or redundant.
  • Figure 4: Head-wise ablation. Change in mean absolute entropy error when ablating individual attention heads. A single Layer-0 "hypothesis-frame head" plays a uniquely important role, while many later heads are partially redundant. This supports the three-stage picture in \ref{['sec:discussion']}: foundational binding, progressive elimination, and value-manifold refinement.
  • Figure 5: HMM wind tunnel: calibration across sequence lengths. Transformer predictive entropy $H_{\text{model}}(t)$ (solid) versus analytic $H_{\text{Bayes}}(t)$ (dashed) at the training length $K=20$ and at $K=30$ and $K=50$. At $K=20$ the trajectories overlap almost perfectly; for longer sequences the error grows smoothly with position and shows no kink at the training boundary, indicating a position-independent recursive algorithm rather than finite-horizon memorization.
  • ...and 13 more figures

Theorems & Definitions (3)

  • Theorem 1: Population optimum of cross-entropy
  • proof
  • Remark 1