Source-linked AI summary
Transformers learn to implement preconditioned gradient descent for in-context learning
Kwangjun Ahn, Xiang Cheng, Hadi Daneshmand, Suvrit Sra
TL;DR
The paper asks whether transformers can learn algorithms through training on random problem instances, addressing a gap beyond showing that carefully configured transformers can express gradient descent. It analyzes the loss landscape of linear transformers trained on random linear-regression tasks and finds optimization-algorithm structure in global optima and selected critical points. The results establish one iteration for a single layer and L preconditioned iterations for certain L-layer critical points, while leaving the full multilayer critical-point landscape unresolved.
Problem
Whether transformers can learn to implement algorithms through training on random problem instances remains unclear despite their demonstrated algorithmic expressivity.
Method
The paper analyzes the loss landscape of linear transformers trained with in-context loss on random linear-regression instances.
Results
A single-layer global minimum implements one preconditioned gradient-descent step, while certain L-layer critical points implement L iterations with data-dependent preconditioning.
Takeaways & Limitations
Training transformers on random linear-regression instances can yield parameters interpretable as adaptive gradient-based algorithms.
Takeaways & Limitations
The analysis does not establish that all critical points of the multilayer non-convex objective correspond to similar optimization methods.
Abstract
from arXiv · showhide
Several recent works demonstrate that transformers can implement algorithms like gradient descent. By a careful construction of weights, these works show that multiple layers of transformers are expressive enough to simulate iterations of gradient descent. Going beyond the question of expressivity, we ask: Can transformers learn to implement such algorithms by training over random problem instances? To our knowledge, we make the first theoretical progress on this question via an analysis of the loss landscape for linear transformers trained over random instances of linear regression. For a single attention layer, we prove the global minimum of the training objective implements a single iteration of preconditioned gradient descent. Notably, the preconditioning matrix not only adapts to the input distribution but also to the variance induced by data inadequacy. For a transformer with $L$ attention layers, we prove certain critical points of the training objective implement $L$ iterations of preconditioned gradient descent. Our results call for future theoretical studies on learning algorithms by training transformers.
1 Introduction
The paper asks whether transformers can learn algorithmic procedures from random problem instances rather than merely express them through specially chosen parameters. It analyzes linear-transformer loss landscapes and finds gradient-based algorithms at global optima and selected critical points.
- Transformers can express algorithms such as gradient descent, but whether training over random instances learns those algorithms remains unclear.
- The paper theoretically studies gradient-based methods learned from in-context linear regression using a linear attention architecture without softmax.
- A single-layer linear transformer’s global optimum implements one step of preconditioned gradient descent.The preconditioner adapts to both the input distribution and variance caused by data inadequacy.
- For multilayer transformers, certain critical points implement gradient descent with data-dependent preconditioning.
- A further critical point yields an algorithm combining covariance-preconditioned gradient steps with a linear transformation that improves conditioning.With isotropic data covariance, this algorithm corresponds to GD++.
- Three-layer experiments confirm the predicted critical-point structure, while objective values close to 0 suggest these points may be global optima.
2 Setting: training linear transformers over random linear regression
The paper trains linear self-attention transformers on random linear-regression instances, where the query label is withheld and must be predicted from the context. It studies how the resulting non-convex objective connects transformer parameters to optimization algorithms.
- Data distribution: Random instances draw covariates x(i) independently from D_X and regression weights w⋆ from D_W.
- Data distribution: The input matrix contains covariates, observed responses, and a zero replacing the unknown response for the query covariate.
- Data distribution: The training target is the withheld response w⋆ᵀx(n+1), averaged over random covariates and regression weights.
- Architecture: The model uses stacked linear self-attention blocks that omit softmax and reparameterize value and key weights as P and Q.
- Architecture: For L layers, prediction reads the final representation’s (d+1,n+1)-th entry, with a minus sign for consistency with prior work.
- Landscape analysis: The objective is non-convex even for one layer, and attention’s cross-products make its parameter dependence highly nonlinear.
- Landscape analysis: Table 1 organizes analyses by transformer model, input distribution, sparsity conditions, and optional symmetry constraints.
3 The global optimum for a single-layer transformer
For Gaussian inputs, the single-layer transformer’s global optimum implements one step of preconditioned gradient descent. Its preconditioner adapts to input covariance and sample-induced variance, with isotropic data yielding a simpler form.
- Theorem 1 characterizes globally minimizing parameters for single-layer transformers under non-isotropic Gaussian inputs.The input covariance is Σ, and the target vector is sampled from N(0, Id).
- Up to rescaling, the optimal parameter configuration implements one step of gradient descent.This configuration is equivalent to parameters previously used to perform one gradient-descent step.
- For non-isotropic samples, the learned update becomes one step of preconditioned gradient descent.The preconditioning matrix is specified by the optimal parameters and depends on the input distribution.
- With many samples, the preconditioner’s first d × d submatrix approximates Σ−1, potentially accelerating convergence for ill-conditioned covariance.Σ−1 is also close to the Gram matrix formed from the in-context samples.
- The term involving the eigenvalues acts as a regularizer that becomes more important with fewer samples and higher input variance.Its sample-size adaptation resembles structural risk minimization.
4 Multi-layer transformers with sparse parameters
Under sparse parameter constraints, multilayer linear transformers can realize preconditioned gradient methods, with critical points tied to input covariance. Experiments support the theory by showing training converges near the analyzed parameters and near-zero loss.
- Sparse parameterization: A forward pass through an L-layer transformer with parameter configuration (8) equals L preconditioned gradient-descent steps using layer-specific matrices Aℓ.The scheme includes ordinary gradient descent and adaptive preconditioned variants, depending on the matrices Aℓ.
- Sparse parameterization: For isotropic Gaussian inputs, globally optimal two-layer parameters can use diagonal matrices implementing gradient descent with adaptive coordinate-wise stepsizes.These learned stepsizes depend on the underlying data distribution rather than on each individual problem instance.
- General covariance: For general covariance Σ, certain critical points use matrices proportional to Σ^-1, implementing a data-dependent preconditioned gradient algorithm.This resembles Newton-style or full-matrix adaptive methods and can improve conditioning for ill-conditioned problems.
- General covariance: The analyzed algorithm addresses distorted covariates by adapting to their covariance, while the corresponding preconditioner can make the regression problem better conditioned than vanilla gradient descent.The setup models covariates observed after a linear distortion and identifies Σ = WW⊤ as the relevant covariance.
- Experimental validation: Training a three-layer transformer produced almost-zero loss and parameters matching the stationary point predicted by Theorem 3 across experiments.The experiments used d = 5, n = 20, minibatches of size 20000, gradient clipping at 0.01, and averages over 5 runs.
- Experimental validation: Distances to the covariance-adjusted identity decreased during training, while distances to the ordinary identity remained constant, indicating preconditioned rather than plain gradient descent.The covariance-adjusted distance is Dist(Σ^1/2AiΣ^1/2, I), whereas the ordinary comparison is Dist(Ai, I).
5 Multi-layer transformers beyond standard optimization methods
With a relaxed parameterization, multilayer linear transformers can implement optimization algorithms beyond standard preconditioned gradient descent. Theorem 4 identifies critical points combining covariance-based gradient preconditioning with covariate transformations that improve conditioning, and experiments support convergence toward this structure.
- The relaxed parameterization permits learning algorithms beyond conventional preconditioned gradient descent.
- Theorem 4 considers an L-layer linear transformer trained on Gaussian linear-regression instances with covariance matrix Σ.
- Parameters in S have Ai proportional to Σ−1 and Bi proportional to the identity, defining the analyzed critical-point structure.
- These parameters implement distribution-dependent preconditioned gradient steps while transforming covariates to improve Gram-matrix conditioning across iterations.
- In experiments, loss appeared to converge to 0 while B0 and B1 approached identity and Ai approached Σ−1 up to scaling.
- The observed ordering ∥A0∥≤∥A1∥≤∥A2∥ corresponds to smaller gradient steps initially and larger steps later as conditioning improves.
6 Discussion
The paper analyzes whether transformers can learn gradient-based algorithms from random problem instances, extending expressivity results through loss-landscape analysis. It characterizes single-layer global minima and selected multilayer critical points, while identifying open questions involving nonlinear attention and the full multilayer critical-point set.
- Discussion: The paper’s main contribution is a loss-landscape analysis showing that transformers can learn gradient-based algorithms when trained on random linear-regression instances.For one layer, the global minimum implements preconditioned gradient descent; for multiple layers, certain critical points correspond to adaptive gradient-based algorithms.
- Discussion: A single-layer global minimum implements one preconditioned gradient-descent step, with preconditioning adapting to input distribution and data-inadequacy variance.This result is stated as Theorem 1.
- Beyond linear attention: For nonlinear attention, the paper characterizes a ReLU global minimizer whose structure is similar to the linear-attention global minimum for isotropic Gaussian data.The nonlinear-attention result is presented as an analog of Theorem 1.
- Refined landscape analysis for multilayer transformer: The multilayer analysis does not establish that all critical points implement similar optimization methods, and the loss can have multiple critical points.The paper proposes analyzing their algorithmic interpretations and suboptimality as future work.
A Proofs for the single layer case
The single-layer proof reduces the in-context loss to a smaller parameterization involving only selected rows and columns of the attention weights. It then expresses the loss through the attention output and analyzes the resulting objective.
- Loss formulation: The reduced loss is explicitly expanded after substituting the parameterization into the attention expression.The proof introduces this expansion before analyzing the objective’s minima.
- Attention expansion: Using the masked attention structure, the proof spells out the final-column output in terms of the prompt columns and the parameters b and A.The mask restricts the summation to the n labeled examples.
- Parameter reduction: The in-context loss depends only on the last row of P and the first d columns of Q.This observation motivates the reduced parameterization used throughout the proof.
- Parameter reduction: The proof reparameterizes the single-layer weights using b and A, with Q=[A 0].Under this parameterization, the loss is written as f(b,A).
A.2 Warm-up: proof for the isotropic data
The isotropic-data proof decomposes the reduced loss into components, characterizes each component’s global minimum, and combines them to obtain the single-layer optimum.
- Setup: The warm-up assumes x^(i) follows N(0,I_d) and analyzes the single-layer objective under isotropic data.This is the special case used before treating general covariance.
- Loss decomposition: The proof decomposes the loss into separate components by writing A=[a_1 ... a_d] and using independence of distinct isotropic coordinates.Each component can then be minimized separately.
- Component minima: Each component is convex in its matrix variable, so verifying a zero gradient establishes the stated global optimum.This is the central argument in Lemma 2.
- Gradient verification: The proof computes the required Gaussian expectations, including fourth-moment terms, to verify the gradient conditions.These calculations establish the componentwise stationary conditions used in the argument.
- Transition: The isotropic analysis is completed before moving to the non-isotropic covariance case.The proof explicitly treats the latter separately.
- Combining minima: Combining the componentwise minima yields a global minimum for f(b,A), with a coordinate-vector construction realizing the required parameters.The construction uses the vectors e_j and corresponding matrix entries.
A.3 Proof for the non-isotropic case
The non-isotropic proof first handles diagonal covariance and then reduces general Gaussian covariance to that case through an orthogonal coordinate transformation. Componentwise minima are again combined to obtain the global optimum.
- Diagonal covariance: The diagonal-covariance case assumes x^(i) follows N(0,Λ), with Λ=diag(λ_1,...,λ_d), while w⋆ follows N(0,I_d).This generalizes the isotropic warm-up.
- Diagonal covariance: The proof states componentwise global-minimum results for the diagonal covariance objective and verifies them through gradient calculations.Lemma 3 provides the diagonal-covariance analogue of the isotropic component analysis.
- Diagonal covariance: Covariance-dependent factors enter the calculations through generalized identities involving the eigenvalues λ_j.The proof explicitly identifies the factor of λ_j in one of these identities.
- Combining minima: Combining the diagonal-case identities and component minima produces the corresponding global minimum for the reduced loss.The proof repeats the component-combination step from the isotropic analysis.
- General covariance: For general covariance Σ=UΛU^⊤, the proof transforms inputs with U^⊤, obtaining diagonal covariance and reducing the problem to the previous case.The transformed inputs have covariance Λ, and rotational symmetry preserves the isotropic distribution of the transformed target.
A.4 Proof for non-linear attentions (Theorem 5)
The proof simplifies the nonlinear-attention loss under Gaussian inputs and identifies a global minimizer through symmetry, convexity, and probabilistic reductions.
- Gaussian reduction: The proof assumes isotropic Gaussian inputs and targets, then uses Stein’s lemma to simplify expectations involving ReLU.Symmetry gives E[σ′(x⊤Σx)]=1/2, which supports the subsequent loss calculations.
- Global minimization: The reduced lower-bound objective is optimized over diagonal parameters, yielding a minimizer with b1A proportional to the identity.The minimizer is obtained from convexity in b1A.
- Global minimization: Choosing b1=1, b0=0, and the specified A makes the lower bound equal the original objective, proving global optimality.The construction achieves equality between flower and f for the corresponding parameters.
- Symmetry reduction: Orthogonal and random-sign invariances reduce the optimization to diagonal matrices without increasing the objective.Jensen’s inequality is applied after averaging over random diagonal sign transformations.
- Structured parameter space: The multi-layer proof restricts parameters to a structured sparse set and compares arbitrary perturbations with perturbations that remain in that set.The argument uses constrained gradient flow and lower-boundedness of the loss to establish the desired stationary-point condition.
B.3 Proof of Theorem 4
The proof of Theorem 4 establishes stationarity within a structured parameter space by exploiting Gaussian symmetry and layer-wise perturbation reductions.
- Setup: The structured parameter space constrains each Ai to aiΣ−1 and each Bi to biI.This form is preserved under the transformations used in the proof.
- Stationarity: Lower-boundedness of the nonnegative loss rules out unbounded descent, establishing the theorem’s stationarity condition.The contradiction argument applies after the structured gradient-flow construction.
- Layer-wise reduction: To prove the stationarity condition, it suffices to analyze perturbations affecting one layer at a time.The proof separately considers perturbations to a single Aj or Bj.
- Layer-wise reduction: For a fixed layer, the proof makes the dependence of Xi and Yi on the transformed input X0 and perturbed Bj explicit.The notation Xi(X,C) and Yi(X,C) tracks the trajectory while other layer parameters remain fixed.
- Random transformations: Gaussian symmetry under Σ-weighted orthogonal transformations propagates through the transformer layers by induction.The identities rely on Ai=aiΣ−1 and Bi=biI.
- Stationarity: The transformed dynamics of G and its derivative are combined with affine dependence on perturbations to construct structured perturbations matching expected gradients.This completes the proof for perturbations to B; perturbations to A follow similarly.
C.1 Proof of Lemma 1 (Equivalence to Preconditioned Gradient Descent)
This section rewrites the transformer’s in-context prediction dynamics and shows that, under the specified sparsity pattern, they are equivalent to preconditioned gradient descent.
- Prediction dynamics: The mask makes the final query’s label unavailable while allowing the transformer to use the contextual examples.The architecture is defined through masked linear self-attention updates.
- Prediction dynamics: The prediction function preserves the query label additively and is linear in the query input.Thus g(x,y,k)=⟨θk,x⟩+y for some θk.
- Gradient-descent equivalence: The resulting update can be expressed using a matrix Ak that acts as a preconditioner for the least-squares gradient.The objective is defined from the contextual design matrix and target parameter.
- Gradient-descent equivalence: Under the parameter configuration, the in-context loss admits an equivalent reformulation in terms of the layerwise matrices.This reformulation is established as Lemma 5 and supports the subsequent optimization analysis.
- Prediction dynamics: The transformed context representation and prediction coordinates have simple dependence on the initial query label under the masked dynamics.The first two coordinates are independent of that label, while the final coordinate depends additively on it.
D Additional experimental results
Additional experiments compare learned linear-transformer predictors with gradient-based algorithms and visualize whether learned weights match the analyzed stationary structure.
- Weight visualization: The learned three-layer transformer’s weights match the stationary point analyzed in Theorem 4.The visualization covers the learned weight matrices for that theorem’s setting.
- Weight visualization: The experiments suggest a small initial gradient step and a large later step, attributed to improved conditioning after earlier layers.The authors specifically contrast A0 and A2 and relate the difference to B0 and B1.
- Predictor comparisons: Figure 6 plots test loss against the number of in-context examples for a learned three-layer transformer, three GD steps, three preconditioned-GD steps, and OLS.GD and preconditioned-GD stepsizes are selected by grid search, with preconditioning fixed to Σ−1.
- Predictor comparisons: Figure 7 plots test loss against layer count or optimization-step count for L=1, 2, 3, 4.It compares learned L-layer transformers with L steps of GD and preconditioned GD.