Exact Learning Dynamics of In-Context Learning in Linear Transformers and Its Application to Non-Linear Transformers
Nischal Mainali, Lucas Teixeira
TL;DR
This work derives an exact, non-asymptotic SGD dynamics for a linear transformer performing in-context linear regression, revealing a clear separation of learning timescales along data-eigenmodes dictated by the input covariance spectrum. It shows that learning proceeds via mode-specific, nonlinear trajectories with a conserved quantity restricting dynamics, and the learned computation at convergence effectively preconditions inputs to recover the target linear map. The authors extend insights from solvable deep linear networks to propose macroscopic diagnostics (spectral rank dynamics, subspace stability, curvature-based loss analysis) and demonstrate qualitative parallels in non-linear, multi-layer transformers, including sudden ICL emergence and grokking phenomena. The results offer a principled analytic framework to interpret transformer training, with potential applications to interpretability, monitoring, and extension to nonlinear attention architectures and more complex tasks.
Abstract
Transformer models exhibit remarkable in-context learning (ICL), adapting to novel tasks from examples within their context, yet the underlying mechanisms remain largely mysterious. Here, we provide an exact analytical characterization of ICL emergence by deriving the closed-form stochastic gradient descent (SGD) dynamics for a simplified linear transformer performing regression tasks. Our analysis reveals key properties: (1) a natural separation of timescales directly governed by the input data's covariance structure, leading to staged learning; (2) an exact description of how ICL develops, including fixed points corresponding to learned algorithms and conservation laws constraining the dynamics; and (3) surprisingly nonlinear learning behavior despite the model's linearity. We hypothesize this phenomenology extends to non-linear models. To test this, we introduce theory-inspired macroscopic measures (spectral rank dynamics, subspace stability) and use them to provide mechanistic explanations for (1) the sudden emergence of ICL in attention-only networks and (2) delayed generalization (grokking) in modular arithmetic models. Our work offers an exact dynamical model for ICL and theoretically grounded tools for analyzing complex transformer training.
