Logarithmic Depth for Exact In-Context Gradient Descent with Linear Attention

Yingze Li, Dong Wang, Xianglong Liu, Zou Qingyun, Chunnan Wang, Zhiyu Liang, Hongzhi WangNeurIPS 2026

TL;DR

When Transformers learn from examples in a prompt—a phenomenon called in-context learning—researchers often model them as running gradient descent, step-by-step, with one optimization update per layer. This paper proves you don't actually need $k$ layers to get the exact result of $k$ gradient descent steps; by using linear attention to multiply matrices and "fast-forward" through updates, a Transformer only needs $\Theta(\log k)$ layers. It turns out Transformer depth doesn't just add computational steps—it compounds them exponentially.

What Problem Does This Paper Address?

Give a Transformer a handful of input-output examples in its prompt, and it can adapt on the fly to make predictions on a new query without updating a single model weight. A foundational way to understand this in-context learning (ICL) on linear regression tasks is that the Transformer is secretly running gradient descent (GD) inside its forward pass.

Prior theoretical constructions showed how a single linear self-attention layer can execute one step of GD, meaning $k$ updates take $k$ layers. That gives a clean linear upper bound on depth, $O(k)$, like climbing a staircase one step at a time.

The open question is whether $k$ layers are truly necessary. Because the final result of $k$ gradient descent steps on a quadratic objective has a closed-form mathematical expression, could a shallower network take a shortcut to compute the exact same endpoint? The authors ask: what is the true minimum depth required to compute $k$ steps of in-context gradient descent?

Sequential GD adds one step per layer, while linear attention triples the polynomial degree ceiling at every layer, opening up a massive algebraic capacity gap.

Methods

The authors study an attention-only, unnormalized linear self-attention Transformer (where the standard softmax is removed, leaving pure matrix multiplications and residual connections, with no MLPs or LayerNorm). They tackle the problem from two directions to prove a tight $\Theta(\log k)$ bound:

  1. The Lower Bound (Algebraic Complexity): After $k$ steps of gradient descent on a quadratic loss, the final answer is a degree-$k$ polynomial in the input's spectral parameters. Looking at the linear attention equations, computing an attention score multiplies Queries and Keys, and multiplying that score by Values brings a third factor into play. This "trilinear" update means each layer can at most triple the polynomial degree of whatever came before it ($D \to 3D$). Therefore, $L$ layers have a hard ceiling of degree $3^L$, proving you need at least $\lceil \log_3 k \rceil$ layers.
  2. The Upper Bound (Repeated Squaring via Attention): To prove logarithmic depth is also sufficient, the authors build an explicit weight construction. Instead of inching forward one step at a time, they store matrix columns in dedicated "workspace tokens" and use 2-head attention sums to multiply matrices together. By reading the binary digits of $k$, the network implements classic doubling and increment operations ($m \to 2m$ and $m \to m+1$) on matrix powers and geometric sums. They also show how an extra initial layer can construct empirical covariance matrices directly from raw regression demonstrations before running this binary chain.
Overview of the core proof strategy: matching the degree ceiling lower bound with a matrix-composition upper bound.

Datasets

Because this is primarily a theoretical work paired with controlled synthetic verification, no new real-world benchmarks are introduced:

  • Synthetic Polynomial & Regression Tasks: Controlled scalar polynomial tasks (3 demonstrations at Chebyshev roots on $[1/\kappa, 1]$) and 16-dimensional linear regression prompts (64 demonstrations sampled from Gaussian priors $x \sim \mathcal{N}(0, \Sigma)$ across various condition numbers $\kappa$).
  • Pretrained LLM Probes: Exploratory classification evaluations (using 1 demonstration per class and 200 queries per task) run on existing open-weight models (Qwen2.5 1.5B/3B and Pythia 2.8B/6.9B) across 8 standard classification tasks (specified in their appendix).

Key Insights and Ideas

  • Width and context length can't buy you polynomial degree: Because token width, extra attention heads, and longer context lengths only enter the linear attention equations as sums and fixed linear maps, they cannot raise the polynomial degree ceiling. Only depth multiplies degree ($3^L$), giving a lower bound completely independent of model width or context size.
  • Exact computation vs. "good enough" approximation: Computing the exact $k$-step endpoint requires $\Theta(\log k)$ depth. However, if you only care about approximating the answer up to a finite error $\varepsilon$, polynomial approximation theory (via Chebyshev polynomials) takes over. For instance, at $k=128$ and $\varepsilon = 10^{-12}$, an oracle polynomial only needs degree 26 or 27—much lower than 128! Still, before GD converges ($k \le \kappa$), uniform approximation hits a barrier requiring $\Omega(\log k)$ depth.
  • Non-polynomial kernels break the ceiling: Adding standard softmax, exponential, or ReLU kernels steps outside the world of pure polynomials, allowing shallow networks (2–6 layers) to empirically fit high-$k$ targets that linear attention struggles with at those same depths.

Key Results

  • Exact Theoretical Bounds: Exact $k$-step GD endpoint computation for $d$-dimensional positive-definite quadratics requires $L \ge \lceil \log_3 k \rceil$ layers, and can be achieved in $L \le 2\lceil \log_2 k \rceil + 1$ layers using $H=2$ heads and model dimension $6d$ (or $L = \ell_{\text{bin}}(k) + 3$, $H=3$, $d_{\text{model}} = 8d+3$ starting directly from raw regression prompts).
  • Kernel Comparisons: When training models to reach a normalized MSE $< 10^{-5}$ on the polynomial task as $k$ scales from 8 to 128, linear attention's required empirical depth climbs from 3 to 8 layers. Softmax climbs from 2 to 6, while ReLU and unnormalized exponential kernels stay flat at 2 to 3 layers.
  • Exploratory LLM Probes: Applying a logit lens to Qwen2.5-3B shows a strong linear fit ($R^2 = 0.91$) between the layer depth needed to reach 95% of final accuracy and $\log_2 C$ (where $C$ is class count), though results vary widely on Pythia ($R^2 = 0.20$ for 2.8B and a negative slope on 6.9B), keeping this strictly exploratory.

Limitations and Future Work

  • Architectural Idealizations: The exact $\Theta(\log k)$ guarantees apply to unnormalized, full (unmasked) linear self-attention with affine input encodings and dedicated workspace tokens. Extending these exact constructions to causal masking or standard softmax Transformers with LayerNorm remains open.
  • Fixed Horizon Weights: In the explicit construction, the network weights depend on the prescribed step count $k$ (since the layers encode the binary representation of $k$) and require model width proportional to task dimension $d$.
  • Black-Box Training Dynamics: Measuring endpoint accuracy in trained networks shows what accuracy they achieve, not how they do it. Identifying whether gradient-trained Transformers actually discover binary composition under the hood—and resolving numerical instabilities observed when training deeper float64 linear-attention models at high condition numbers ($\kappa = 3000$)—is left for future work.

Summary updated

Abstract

How much Transformer depth is needed to compute the result of k gradient-descent updates? Constructions assigning one update to each layer give a linear upper bound. We establish a tight Θ(log k) characterization for exact endpoint computation by attention-only, unnormalized linear self-attention with a fixed coordinate workspace. Each layer raises polynomial degree by at most a factor of three, giving a depth lower bound independent of width, heads and context length. A matching construction stores matrix columns in tokens and uses attention sums to implement doubling and increment operations on matrix powers and geometric sums. The construction covers arbitrary positive-definite quadratic objectives and extends to prediction from regression demonstrations at an unseen query. We also prove a barrier to uniform approximation before convergence and a lower bound for prediction risk in a scalar model with a supplied spectrum. Controlled experiments study polynomial approximation and spectral conditioning, while broader regression experiments examine endpoint accuracy across architectures and task families.
BibTeX
@inproceedings{li2026logarithmic,
  author = {Yingze Li and Dong Wang and Xianglong Liu and Zou Qingyun and Chunnan Wang and Zhiyu Liang and Hongzhi Wang},
  title = {{Logarithmic Depth for Exact In-Context Gradient Descent with Linear Attention}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=rUh3nJ2f7c},
}
Figure