Context. Transformers trained on random linear-regression prompts predict about as well as least squares (Garg et al.), which says nothing about mechanism. Three papers settle the one-layer case: von Oswald et al. build a linear attention layer that takes one gradient-descent step on the prompt and find it in trained weights, Ahn et al. show that the global minimum is such a step with a learned preconditioner, and Zhang et al. prove that gradient flow reaches it and compute its risk on any prompt. A linear attention layer adds , a sum of outer products of the data with itself, which is the form of the squared-loss gradient for a linear model, so a trained layer predicts with frozen at training time. That is a ridge-like smoother with shrinkage set by the training prompt length, and because its preconditioner is learned from the training covariates instead of computed from the prompt, it tolerates shifts in the task and the query but not in the covariates.
Setting. Each example is one token , and the query token starts as and holds the prediction in its -entry at the end. Linear self-attention (LSA) is attention with the softmax removed, and only the products and matter, up to the rescaling . Training draws a fresh task for each prompt, inputs with eigenvalues , and noiseless labels , and minimizes the squared error at the query over prompts of length ; a test prompt has length . Ahn et al. keep the query out of the keys and values and Zhang et al. let it in; the predictor is the same.
A gradient step is a change of targets, and one attention layer makes it (von Oswald et al.). Push the update from the weights into the data: for , so a layer that subtracts from every token's -entry, the query included, has taken the step without ever storing . With picking out , sending to and , the attention sum is exactly that update; starting the query at and negating its -entry at the end reads out . From the prediction is , Garg's averaging estimator up to .
Stacking gives more steps, and one more block adds curvature correction. After a layer the context tokens hold the residuals , the data the updated model faces, so layers are GD steps. GD++ also moves every input, the query included, by , which replaces the Hessian by The gain depends on : for the map is increasing on the spectrum and the condition number falls, larger maps the top of the spectrum to values below the peak at , which can help more, and near makes it worse. Repeated over layers it is an iterative preconditioner computed from the prompt.
An MLP supplies features, and an earlier head builds the tokens. After a residual MLP the same layer takes GD steps on kernel regression with . When inputs and targets sit in separate tokens, a head whose key-query product selects the successor's positional encoding copies each into its token, as an induction head does, and the next layer takes the GD step, as trained two-layer models do. Through a softmax the copy is only approximate (weight on the successor among tokens), so the key-query product must be scaled up.
Trained models match the construction (experiments). A trained single LSA layer matches one tuned GD step in loss, predictions and input sensitivities, its rescaled matrices visibly match the construction, and the midpoint of trained and constructed weights has the same loss. The two fail together: for inputs from with both blow up, because a step size tuned for overshoots the higher curvature. The match needs task diversity (it fails at 128 training tasks and is nearly perfect above 2048). Deeper models drift toward GD++, but only with fitted per layer, which is weaker evidence.
The one-layer loss is a sum of convex functions of rank-one matrices, and a convex relaxation solves it (Ahn et al.). For Gaussian inputs the global minimizer predicts , one step from zero of GD preconditioned by ; for , , plain GD with a tuned step, von Oswald's construction. The loss is nonconvex, so the proof relaxes it. With the query masked, the prediction is , where is the second-moment matrix of the context tokens, the last row of the value-projection product and the first columns of the key-query product, and for the loss splits over the columns of : Each is a convex quadratic in ; minimize it over all by Gaussian moments, and since the minimizers share one , they solve the original problem (general follows by rotating). The in comes from the fourth moment , the variance of from having only samples, and it shrinks directions with as ridge would. The minimal loss is , which is for (a short computation, not in the paper, checked by simulation), so one layer never interpolates.
With sparse layers the forward pass is preconditioned GD, and the loss is a matrix polynomial. Restrict each layer to a value product that writes only the label row and a key-query product . The inputs never change, the mask makes the query's label enter only additively, and every context label follows the same recursion as the query's, so by induction it holds the residual of the current model: with the context inputs: steps of GD with per-layer preconditioners. Putting the true label in the query slot turns the output into the prediction error, so the loss is an explicit matrix polynomial in the , built from with the mask; the remaining results analyze that polynomial.
For two layers some global minimizer is diagonal, by rotation and random sign flips. For , isotropic data and symmetric , some pair of diagonal matrices is a global minimizer, so the best two-layer model is GD with coordinatewise step sizes. Take a global minimizer, diagonalize by rotation invariance, and conjugate by a random diagonal sign matrix : the loss is unchanged (, the data are sign-invariant) and convex in , because the output is affine in . Since , Jensen gives
At symmetric points the gradient can be matched inside the symmetric set, so it contains critical points. With prior , Theorem 3 says the set , GD preconditioned by the Newton matrix, contains critical points, possibly approached only in the limit; Theorem 4 adds input updates , which give GD++ when ; the input update commutes with the symmetry used below only when it is a multiple of . The data law is invariant under , with Haar orthogonal, which fixes points of , so perturbing a layer in direction changes as perturbing it in direction , and by linearity in the direction one may average: Every directional derivative at a point of is thus matched by one inside , so a flow constrained to decreases at rate at least the full squared gradient norm, and since the gradient cannot stay away from zero. This shows existence and says nothing about optimality, but Adam finds these points: in a trained three-layer model converges to multiples of , and with input updates the whitened step sizes grow across layers (about ) as the conditioning improves.
With ReLU attention the minimizer is again a multiple of the identity, by sign symmetrization and Stein's lemma. With ReLU applied to the attention scores, isotropic data and the last row of the key-query product zero, the same rotation, sign-flip and Jensen moves bound the loss below by a function of , and Stein's lemma, , evaluates every term: a convex quadratic minimized at , where the bound is attained. ReLU changes only the constant.
The prediction is a rank-one factorization, and off the cross entries the loss reduces to two blocks (Zhang et al.). From a small balanced start, and with , gradient flow on the population loss converges to a global minimum whenever (Theorem 4.1). The prediction is an indefinite quadratic form in the vector of the weights that affect it, value weight times key-query weight, so the dynamics are those of a two-layer linear network. The cross entries stay at zero, since every term in their derivatives carries an odd power of the mean-zero task, and enters through the Wishart fourth moment of . Up to a constant the loss in the key-query block and the value entry is
Completing the square gives the minima, and conserved balance picks one. Since and commute, so the global minima are exactly the hyperbola , all giving the same predictor. Multiplying the two flow equations by and gives , the conservation law of two-layer linear networks, so balance holds for all time and pins the limit on the hyperbola at , .
A small start avoids the saddle at zero, and then a PL inequality gives convergence. The origin is a saddle with , and the condition on makes (von Neumann's trace inequality), so never reaches 0; with balance it stays above some . Keeping only the -gradient and comparing with the completed square, so the excess loss decays like , the flow has finite length, and the limit is a global minimum.
The error is a bias-variance split for a fixed linear smoother. For a test prompt of length from any distribution with , the best linear predictor, and , with vanishing cross terms. The last term is a variance , and gives a squared bias of order (Theorem 4.2). As for ridge, eigendirection keeps the fraction of the signal, a shrinkage level fixed at training, which does not vanish as grows. For noiseless tasks with and the condition number of , the error is at most . The term stays even for noiseless labels and , because the model applies a fixed where least squares would invert and be exact once .
The preconditioner fixed at training determines which distribution shifts the model tolerates. Any labels give the best linear predictor up to , so label noise and arbitrary are tolerated, and for small and isotropic a query orthogonal to all gets 0, as Garg et al. saw. Scaling the test inputs by gives , Garg et al.'s input-scale failure, while least squares is unaffected. Training on random diagonal covariances, one per prompt, does not fix it, since one layer cannot form and learns one fixed preconditioner: as it multiplies coordinate of by , which for Exponential(1) variances is , right only when .
Consequences and limits. For one linear attention layer the analysis is complete: the trained model is , a linear estimator whose risk follows from the spectrum of . Training length acts as regularization, so test error has a floor of order however long the test prompt, and matching least squares in distribution does not make the model least squares: the learned preconditioner matches Garg et al.'s fragility to input scale and covariance, and only computing from the prompt, which needs depth or nonlinearity, would remove it. The limits are linear attention, noiseless Gaussian training tasks, gradient flow on the population loss from one initialization, critical points without global optimality for depth, and softmax and LayerNorm variants that match less cleanly.
Terms
- LSA layer
- , softmax removed
- Change of targets
- is the loss of on targets
- layers
- GD steps, since tokens carry residuals from layer to layer
- GD++
- GD plus each layer; eigenvalues
- training-time preconditioner inverse; as
- Trained predictor
- , preconditioned one-step GD
- symmetric parameters (and ); contains critical points
- Balance
- is conserved by the flow; pins the limit
- Error split
- best linear error variance shrinkage bias
- Covariate scaling by
- prediction ; least squares is unaffected
Quiz
- GD++ transforms inputs by . Why does this speed up learning?Each eigenvalue is multiplied by , smallest for the largest , so for a suitable (for instance ) the ratio of extreme eigenvalues falls and GD converges faster. Iterated, it acts as a data-dependent preconditioner, short of exact whitening.
- In Theorem 4.2 the excess error has a term and a term. Which mechanism produces the term?, so the predictor is biased toward zero by order and the squared bias is order . The term is the variance of the test average.
- Why does the error not vanish for noiseless linear test prompts with much larger than , as it would for least squares?Least squares uses , which cancels the sampling error exactly once . The trained layer multiplies by a fixed matrix, so the fluctuation of passes through as a error, plus the shrinkage bias.
- Test inputs are scaled by while training used . What does the trained model predict for large ?The output is with fixed at training. With inputs scaled by , , and the target already carries the query's scale.
Sources
- Johannes von Oswald et al. Transformers learn in-context by gradient descent. ICML, 2023.
- Kwangjun Ahn, Xiang Cheng, Hadi Daneshmand, and Suvrit Sra. Transformers learn to implement preconditioned gradient descent for in-context learning. NeurIPS, 2023.
- Ruiqi Zhang, Spencer Frei, and Peter L. Bartlett. Trained Transformers Learn Linear Models In-Context. Journal of Machine Learning Research, 2024.
- Shivam Garg, Dimitris Tsipras, Percy Liang, and Gregory Valiant. What Can Transformers Learn In-Context? A Case Study of Simple Function Classes. NeurIPS, 2022.