# Statistical Advantage of Softmax Attention: Insights from Single-Location Regression

Odilon Duranthon, Pierre Marion, Claire Boyer, Bruno Loureiro, Lenka Zdeborová

► To cite this version:

Odilon Duranthon, Pierre Marion, Claire Boyer, Bruno Loureiro, Lenka Zdeborová. Statistical Ad-
vantage of Softmax Attention: Insights from Single-Location Regression. 2025. hal-05295736

HAL Id: hal-05295736

https://hal.science/hal-05295736v1

Preprint submitted on 3 Oct 2025

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>
Statistical Advantage of Softmax Attention:
Insights from Single-Location Regression

Odilon Duranthon¹, Pierre Marion²*, Claire Boyer³, Bruno Loureiro⁴ and Lenka Zdeborová¹

¹Statistical Physics of Computation Laboratory, École Polytechnique Fédérale de Lausanne (EPFL)

²Inria, École Normale Supérieure, PSL Research University

³Laboratoire de Mathématiques d'Orsay, Université Paris Saclay and Institut Universitaire de France

⁴Departement d'Informatique, École Normale Supérieure, PSL & CNRS

Abstract

Large language models rely on attention mechanisms with a softmax activation. Yet the dominance of softmax over alternatives (e.g., component-wise or linear) remains poorly understood, and many theoretical works have focused on the easier-to-analyze linearized attention. In this work, we address this gap through a principled study of the single-location regression task, where the output depends on a linear transformation of a single input token at a random location. Building on ideas from statistical physics, we develop an analysis of attention-based predictors in the high-dimensional limit, where generalization performance is captured by a small set of order parameters. At the population level, we show that softmax achieves the Bayes risk, whereas linear attention fundamentally falls short. We then examine other activation functions to identify which properties are necessary for optimal performance. Finally, we analyze the finite-sample regime: we provide an asymptotic characterization of the test error and show that, while softmax is no longer Bayes-optimal, it consistently outperforms linear attention. We discuss the connection with optimization by gradient-based algorithms.

# 1 Introduction

Large language models (LLMs) have recently reshaped natural language processing, enabling applications ranging from conversational agents and code generation to knowledge-intensive reasoning. At the heart of these models lies the Transformer architecture (Vaswani et al., 2017), where the use of softmax in attention layers has proven remarkably effective. Despite its dominance, it has computational drawbacks due to its quadratic complexity in the sequence length, while being theoretically arduous to study due to the softmax normalization that couples tokens in a complex manner.

For these reasons, numerous alternatives have been proposed. Notably, kernelized attention approximates the softmax function with kernel feature maps (Wang et al., 2020; Luo et al., 2021; Choromanski et al., 2021; Qin et al., 2022), achieving linear complexity in sequence length. In parallel, state-space models (Peng et al., 2023; Poli et al., 2023; Gu and Dao, 2024) introduce linear recurrent dynamics with gating to tackle long-context information retrieval. On the other hand, while less studied empirically, linear attention, which is the linearization of softmax around the origin, has been extensively studied in theoretical works (e.g. Ahn et al., 2023; von Oswald et al., 2023; Bai et al., 2023; Mahankali et al., 2023; Zhang et al., 2024; Lu et al., 2025; Zhang et al., 2025).

The comparison of softmax with these alternatives has been investigated in particular in the framework of synthetic in-context retrieval tasks, such as Needle-in-a-Haystack (NIAH), where the model must retrieve a single fact hidden in a long block of irrelevant text (Kamradt, 2023; Machlab and Battle, 2024). Overall, the comparison is nuanced: while some alternatives, in particular SSMs, match or even outperform softmax in some of these synthetic tasks, they fail to do so in slight variations thereof or in actual language settings (Arora et al., 2024; Hsieh et al., 2024; Shen et al., 2024). All in all, the practical dominance of softmax in attention is still not fully understood.

<footnote>*Part of this work was done while the author was at the Institute of Mathematics, EPFL.</footnote>
**Contributions and organization.** In this paper, we take a more principled approach to the question, with the following contributions. Code to reproduce our simulations is provided.

* We propose a mathematical formalization of tasks where the output depends on a single input token as the single-location regression model (Section 3). This model subsumes previous theoretical studies (Marion et al., 2025), notably by introducing random sequence lengths, and gives a formalization of information retrieval tasks such as NIAH.

* By combining analytical and numerical results, we provide an analysis of the performance of various attention layers, in particular softmax and linear, in increasingly involved settings (approximation, statistical, computational). The analysis remains tractable despite the presence of the softmax nonlinearity by leveraging ideas from the sequence multi-index models (Cui et al., 2024; Arnaboldi et al., 2025; Cui, 2025; Troiani et al., 2025), where properly scaled random variables concentrate in the high-dimensional limit and the learning behavior can then be characterized by a small set of so-called order parameters.

* We prove a gap in approximation performance between linear and softmax attention (Section 4), with the latter reaching Bayes performance. We argue that this analysis captures the performance efficiently attainable with one-pass stochastic gradient descent. This gap arises from both the exponential nonlinearity and the normalization in softmax, as illustrated by our comparison with kernelized and element-wise attention (Fig. 1).

* We characterize the regularized empirical risk minimizer (ERM), in the high-dimensional limit with sample complexity linearly proportional to the dimension, as the solution to self-consistent equations (Section 5). By solving these equations, we show that the advantage of softmax over linear attention still holds, and we benchmark against the optimal test risk at finite sample complexity, which we will call Bayes-optimal performance. We show numerically that the predicted ERM is achieved by gradient-based optimization algorithms (see Fig. 2).

## 2 Further Related work

**Synthetic information retrieval tasks.** Notable tasks related to single-location include Needle-in-a-Haystack (NIAH) (Kamradt, 2023; Machlab and Battle, 2024), where the goal is to retrieve a fact ("needle") given in a sentence inserted within a large block of unrelated text ("haystack"). An abstraction of NIAH is the Associative Recall (AR) (Graves et al., 2014; Ba et al., 2016) task. In this task, the input contains a sequence of bigrams representing key-value pairs from a random dictionary followed by a query token. For example, the correct output for the input A 2 B 8 C 4 A 3 → B? is 8. This task has been extensively studied through the lens of information retrieval circuits in Transformers, specifically induction heads (Olsson et al., 2022). AR is extended in the Multi-Query Associated Recall (MQAR) task (Arora et al., 2024), featuring several queries for each input sequence.

**Alternatives to softmax attention.** Two main alternative directions feature a linear complexity in the sequence length. First, in kernelized attention (often referred to as linear attention in the literature), the original attention function $Att(Q, K) = \text{softmax}(QK^\top/\sqrt{d})$ is replaced by $Att(Q, K) = \varphi(Q)\varphi(K)^\top$, where $\varphi$ is a kernel function (see, among others, Wang et al., 2020; Choromanski et al., 2021; Qin et al., 2022). More recently, state-space models (SSMs) were specifically introduced to handle information retrieval in long contexts (Fu et al., 2023; Peng et al., 2023; Poli et al., 2023; Gu and Dao, 2024). They consist in alternating linear recurrent neural networks (which can be seen as 1d-convolutions along the sequence time) with nonlinear operations applied in parallel over each element of the sequence (sometimes referred to as gating). We refer to Arora et al. (2024) for a common mathematical framework encompassing many variants of SSMs.

While SSMs outperform attention-based models in AR thanks to their capacity to handle long contexts, this success is brittle since they lag behind Transformers on the slightly more involved MQAR task (Arora et al., 2024; Aksenov et al., 2024; Chou et al., 2024; Wang et al., 2025). Arora et al. (2024) propose a theoretical explanation based on the lack of expressivity of convolution-gating models. Transformers also outperform SSMs and kernelized attention in (a generalization of) NIAH (Hsieh et al., 2024; Shen et al.,
2024). In this work, we take a principled approach to understanding the benefits of softmax, by identifying an information retrieval framework amenable to theoretical study, and going beyond expressivity results to statistical and computational advantages.

**Methodology for sequence multi-index models.** While our data model is inspired by Marion et al. (2025), it departs significantly by generalizing to variable sequence length, introducing a generic weighting mechanism to encode single location, and renormalizing to obtain proper high-dimensional limits, thereby allowing theoretical analysis of the softmax nonlinearity. The data model belongs among the sequence multi-index models as introduced in Cui (2025). Special cases of single-index models we studied in Cui et al. (2024); Arnaboldi et al. (2025). Our model corresponds to a sequence two-index case, for which the Bayes-optimal estimator was studied in Troiani et al. (2025). A related classification problem where outputs only depend on a few tokens was concurrently studied in Barnfield et al. (2025) with analysis of steps-wise gradient descent training.

# 3 Task and data model

## 3.1 Overview of the single-location regression task

We consider a sequence regression task where the input is a sequence $X \in \mathbb{R}^{L \times D}$ of $L$ tokens, each of dimension $D$. The length $L$ of the sequence is allowed to vary, remaining upper bounded by $\bar{L} > 0$. Each sequence is labeled by a scalar $y \in \mathbb{R}$, with the particularity that it depends only on a single input token. We model this single-location dependency by setting a hidden index $\epsilon^* \in \{1, \dots, L\}$ selecting the relevant token of the input sequence $X$. We emphasize that the latent index $\epsilon^*$ is sample/context-dependent, hence its recovery is akin to a toy in-context learning task.

Without additional structure, there is little hope to retrieve the relevant token. We explore two ways to add such structure, which both involve learning a hidden direction $k^* \in \mathbb{R}^D$ to recover the relevant token. In spiked single-location regression (spiked-SLR), a spike is introduced in the direction of $k^*$ at the relevant token $X_{\epsilon^*}$. A closely related scheme, referred to as max-SLR (for maximal-correlation SLR), consists in setting $\epsilon^*$ as the index of the token with the largest scalar product with $k^*$.

Our goal is to theoretically study the learning properties of such tasks. For this purpose, we introduce a probabilistic data model that is amenable to analysis in the high-dimensional limit. This framework also unifies the two variants under consideration (spiked-SLR and max-SLR), as described next.

## 3.2 Probabilistic data model for SLR

Denote by $\mathcal{N}(\omega, V)$ a Gaussian law centered at $\omega$ with covariance $V$, and by $\mathcal{N}(x; \omega, V)$ its density evaluated at $x$. The probabilistic data model begins by drawing two hidden directions

$$k^* \sim \mathcal{N}(0, I_D), \quad v^* \sim \mathcal{N}(0, I_D) \qquad (1)$$

Then, each sample $(X, y)$ is drawn as follows. The length $L$ of the sequence is drawn from some discrete law $P_L$ over $\{1, \dots, \bar{L}\}$, such as uniform or truncated Poisson, while the index $\epsilon^*$ of the relevant token is taken, conditionally on $L$, uniformly at random over $\{1, \dots, L\}$. The label is then a (potentially random) function of the projection of the relevant token along $v^*$, that is,

$$y = \frac{1}{\sqrt{D}} X_{\epsilon^*} v^* + \Delta\xi, \qquad (2)$$

where $\xi$ is independent standard Gaussian noise and $\Delta \ge 0$. It remains to discuss the law of the sequence $X$. A flexible framework consists in taking $X$ as a reweighted Gaussian distribution. More precisely, conditionally on $L$, $\epsilon^*$ and $k^*$, the density of $X$ at a point $x \in \mathbb{R}^{L \times D}$ is given by

$$P(x|L, \epsilon^*, k^*) = g_{\nu}(\epsilon^*, \chi^*) \prod_{\ell}^{L} \mathcal{N}(x_{\ell}; 0, I_D), \quad \text{where } \chi^* = \frac{1}{\sqrt{D}} x k^* \in \mathbb{R}^L. \qquad (3)$$
Here, $g_ν : \{1, ..., L\} \times \mathbb{R}^L \to \mathbb{R}^+$ is a weight function, indexed by the signal strength $ν \ge 0$. Its goal is to give more weight to sequences with a large projection $\chi_{ε^*} = \frac{1}{\sqrt{D}}x_{ε^*}k^*$. Both the examples from Section 3.1 fall into this framework for specific values of $g_ν$.

**Spiked-SLR** corresponds to $g_ν(ε, χ) = e^{\sqrt{ν}χ_ε - \frac{1}{2}ν}$. Factorizing the density of $x_{ε^*}$ shows that this definition is equivalent to shifting the mean of $X_{ε^*}$ by $\sqrt{ν}k^*$, while the other tokens are centered, as studied by Marion et al. (2025).

**Maximum-correlation SLR (max-SLR)** is a special case of the sequence multi-index model of Cui (2025); Troiani et al. (2025); Arnaboldi et al. (2025), obtained with $g_ν(ε, χ) = L e^{νχ_ε} / \sum_ℓ^L e^{νχ_ℓ}$. To gain intuition, note that by Bayes' rule, this model is equivalent to first drawing the tokens as independent standard Gaussians, then picking $ε$ randomly with logits proportional to the scalar product of the tokens with $k^*$, that is, $P(ε^* = i | X) = e^{νX_i k^*} / \sum_j e^{νX_j k^*}$. When $ν \to \infty$, $ε^*$ corresponds to the index of the token with the largest scalar product with $k^*$.

Other weight functions can be considered as long as they are invariant by label permutation, are properly normalized so that $\int_{\mathbb{R}^D} dx P(x | L, ε^*, k^*) = 1$, and at zero signal $ν$ are uniform i.e. $g_0(ε, χ) = 1$.

## 3.3 Learning with attention

Since the input sequence may have arbitrary length, we adopt the formalism of working with $(\mathbb{R}^D)_0^N$, the set of sequences in $\mathbb{R}^D$ that are eventually zero. We consider the class of estimators $\mathcal{F}_σ = \{f_{σ,k,v} : (\mathbb{R}^D)_0^N \to \mathbb{R}\}_{(k,v) \in (\mathbb{R}^D)^2}$, where for a certain activation function $σ: \mathbb{R}_0^N \to \mathbb{R}_0^N$ and two vectors $k, v \in \mathbb{R}^D$, the function $f_{σ,k,v}$ is defined by

$$f_{σ,k,v}(X) = σ(χ)^T z, \quad χ = \frac{1}{\sqrt{D}} Xk \in \mathbb{R}_0^N, \quad z = \frac{1}{\sqrt{D}} Xv \in \mathbb{R}_0^N. \qquad (4)$$

We focus on four cases for the activation function $σ$. Note that we always assume that $σ$ returns 0 on the vanishing part of the input sequence, so we only define its value on the non-vanishing part.

**Softmax activation** corresponds to the standard choice in current large language models. For an input of length $L$, the softmax writes for $ℓ \in \{1, ..., L\}$ as $σ(χ)_ℓ = e^{χ_ℓ} / \sum_{l'=1}^L e^{χ_{l'}}$.

**Linear activation** writes $σ(χ)_ℓ = 1 + χ_ℓ$. We add a constant term to break the symmetry around $χ = z = 0$, as further discussed in Section 4.2. This corresponds to linearizing the softmax around 0 up to a rescaling. Notice that the activation with the constant term is strictly more expressive than the identity $σ(χ) = χ$, which can be retrieved by taking large $χ_ℓ$ and small $z_ℓ$ at constant $χ_ℓ z_ℓ$.

For **element-wise sigmoidal non-linearity**, we focus on $σ(χ)_ℓ = 1 + \text{erf}(c + χ_ℓ) = \frac{2}{\sqrt{π}} \int_{-∞}^{c+χ_ℓ} dx e^{-\frac{1}{2}x^2}$, that varies from 0 to 2, with $c \in \mathbb{R}$ a learnable bias. While for concision we omit this parameter in the mathematical presentation below, it is learned in the numerical experiments. Here also we add a constant term 1 to break the symmetry around $χ = z = c = 0$.

Finally, we investigate **softplus kernelized attention** $σ(χ)_ℓ = ϕ(χ_ℓ) / \sum_{l'} ϕ(χ_{l'})$ in the particular case of the kernel $ϕ(x) = \text{softplus}(x) = \log(1 + e^x)$.

A limitation of our setting, compared to practical models, is the absence of learnable query vectors, which are not necessary here due to the structure of the task. Still, we note that the output of a standard attention layer for a so-called [CLS] query token exactly corresponds to our model (see Marion et al. (2025) for details). The absence of query vector also means that, in our setting, softmax attention is a special case of kernelized attention by taking $ϕ = \text{exp}$. Our analysis shall still give insightful results on the choice of nonlinearity by comparing softmax to the softplus kernel.

# 4 Comparison of softmax with alternatives in population risk

In this section, we compare the *expressivity* of the softmax attention with alternatives on our task, by theoretically assessing the Bayes risk and the minimal population risk for various activation functions. We show that the softmax reaches the Bayes performance. We combine analytical arguments with numerical evidence to support the claim that this approach also captures the performance that is efficiently achievable by running one-pass SGD, which directly minimizes the population risk from random initialization. All proofs of the results in this section are deferred to Appendix A.
**Remark 4.1.** We consider in this section the case $\Delta = 0$ in the output channel (2). Because of independence of the output noise with the other random variables, results would be identical for a positive $\Delta$, up to increasing all errors by $\Delta^2$ corresponding to the irreducible noise.

## 4.1 Bayes risk and optimality of softmax

In what follows, we assess the performances of the different architectures with respect to the Bayes risk (or Bayes error) $\mathcal{E}_{\text{Bayes}} = \mathbb{E}[(y - \mathbb{E}(y|X, L, k^*, v^*))^2]$, i.e. the best achievable risk given the task defined in Section 3.2.

**Proposition 4.1.** Let $L \sim P_L$ and, conditionally on $L$, $\epsilon \sim \text{Unif}(\{1, \dots, L\})$ and $\chi \sim \mathcal{N}(0, I_L)$. Then the Bayes risk is given by

$$ \mathcal{E}_{\text{Bayes}} = 1 - \mathbb{E}_L \mathbb{E}_{\epsilon, \chi} \frac{g_{\nu}(\epsilon, \chi)^2}{\sum_{\epsilon'=1}^{L} g_{\nu}(\epsilon', \chi)}. \quad (5) $$

Two particular cases are of interest. First, if the function $g_\nu$ is identically equal to 1, then the position of the relevant token is uniformly distributed over $1, \dots, L$ and independent of the tokens $X$. In this case, the Bayes error equals $1 - \mathbb{E}_\ell[1/L] > 0$. This reflects the irreducible noise stemming from the randomness of the informative token. Conversely, if $g_\nu(\epsilon, \xi) = \mathbb{1}_{\epsilon=1}$, the first token always carries the information. We then return to standard noiseless regression, where the Bayes error is null.

We turn our attention to the best reachable theoretical risk over the class of estimators $\mathcal{F}_\sigma$ when $\sigma$ is the softmax function. Surprisingly, this estimator reaches the Bayes performance as shown next.

**Proposition 4.2.** Assume that, for all $L > 0$, $(\epsilon, \epsilon') \in \{1, \dots, L\}^2$, and $\chi \in \mathbb{R}^L$,

$$ \frac{g_{\nu}(\epsilon, \chi)}{g_{\nu}(\epsilon', \chi)} = e^{c_{\nu}(\chi_{\epsilon} - \chi_{\epsilon'})}, \quad (6) $$

for some constant $c_\nu \ge 0$. Then, for any $k^*, v^* \in \mathbb{R}^d$,

$$ \min_{f_{k,v} \in \mathcal{F}_{\text{softmax}}} \mathbb{E}_{L, \epsilon^*} \mathbb{E}_{(X,y)}((y - f_{k,v}(X))^2) = \mathcal{E}_{\text{Bayes}}. $$

Note that the requirement on $g_\nu$ defining the distribution of $X$ holds in particular for the spiked-SLR and the max-SLR. In consequence, the softmax architectures $\mathcal{F}_{\text{softmax}}$ reach the Bayes performance in both settings, and furthermore the proof reveals that the minimum is reached for $k = c_\nu k^*$ and $v = v^*$, corresponding to recovery of the hidden directions. In statistical physics terminology, the softmax attention is said to satisfy the *Nishimori condition*, as detailed in the proof. This is to be contrasted with the performance of other activation functions, which is characterized next.

## 4.2 Expression of the population risk for arbitrary activation functions

We now characterize the theoretical population risk of the attention $\mathcal{E}_\sigma(k, v) = \mathbb{E}[(y - f_{\sigma, k, v}(X))^2]$.

**Proposition 4.3.** The population risk of the attention $(k, v) \mapsto \mathcal{E}_\sigma(k, v)$ can be reparameterized as a function of the following 7 variables, referred to as order parameters:

$$ m_{kk^*} = \frac{1}{D} k^\top k^* \qquad m_{vv^*} = \frac{1}{D} v^\top v^* \qquad m_{kv^*} = \frac{1}{D} k^\top v^* \qquad m_{vk^*} = \frac{1}{D} v^\top k^* \quad (7) $$

$$ q_{kk} = \frac{1}{D} k^\top k \qquad q_{vv} = \frac{1}{D} v^\top v \qquad q_{vk} = \frac{1}{D} k^\top v. \quad (8) $$

The first two order parameters, $m_{kk^*}$ and $m_{vv^*}$, quantify the recovery of the hidden directions, while $q_{kk}$ and $q_{vv}$ are the squared norm of the parameters $k$ and $v$. Finally, the cross-correlation terms $m_{kv^*}$, $m_{vk^*}$, and $q_{vk}$ are nuisance parameters. We let $\tilde{\mathcal{E}}_\sigma : \mathbb{R}^7 \to \mathbb{R}$ be the reparameterized risk depending on the order parameters.
**Optimization dynamics and manifold assumption.** In the following, we consider the case where these three nuisance parameters are null, that is, we restrain our analysis to the manifold

$$ \mathcal{M} = \{(k, v) \in (\mathbb{R}^D)^2, m_{kv^*} = m_{vk^*} = q_{vk} = 0\}. $$

This simplification is supported by the following observations pertaining to the landscape of the population risk $\mathcal{E}_\sigma$. In practice, we do not have access to minimizers of the risk, but can instead optimize parameters $k$ and $v$ by one-pass SGD on $\mathcal{E}_\sigma$, which corresponds on average to running gradient descent (GD) on the reparameterized risk $\tilde{\mathcal{E}}_\sigma$. In the high-dimensional limit $D \to \infty$, random Gaussian initialization of the parameters lands on $\mathcal{M}$ because the cross-correlations vanish. Then, the manifold is invariant by GD, in the sense that iterates initialized on the manifold remain on it (see Appendix A.2 for details on this statement and the following). At finite $D$, random initialization lands in a neighborhood of $\mathcal{M}$. In this case, we numerically check that $\mathcal{M}$ is stable in the sense that parameters stay close throughout GD. For linear attention, we also show that the risk has only one (local) minimizer on $\mathcal{M}$, which is also a local minimizer of the risk over the whole parameter space, while for softmax attention we know that a global minimizer is on $\mathcal{M}$. Taken together, these facts support that GD on $\tilde{\mathcal{E}}_\sigma$ and thus one-pass SGD on $\mathcal{E}_\sigma$ converges to a minimizer of the risk on $\mathcal{M}$. A formal proof is delicate, as the landscape features other local minima outside of the manifold.

With a small abuse of notation, we let $\tilde{\mathcal{E}}_\sigma(m_{kk^*}, m_{vv^*}, R_{kk}, R_{vv})$ be the reparameterized risk restricted to $\mathcal{M}$, with $R_{kk}^2 = q_{kk} - m_{kk^*}^2$ and $R_{vv}^2 = q_{vv} - m_{vv^*}^2$ the orthogonal components. It turns out that this object is amenable to insightful theoretical analysis. We first write it explicitly.

**Proposition 4.4.** Let $L \sim P_L$ and, conditionally on $L$, $\epsilon \sim \text{Unif}(\{1, \dots, L\})$, $\chi \sim \mathcal{N}(0, I_L)$ and $\xi \sim \mathcal{N}(0, I_L)$. Then, for $m_{kk^*}, m_{vv^*}, R_{kk}, R_{vv} \in \mathbb{R}^4$,

$$ \tilde{\mathcal{E}}_{\sigma}(m_{kk^*}, m_{vv^*}, R_{kk}, R_{vv}) = \mathbb{E}_{L} \mathbb{E}_{\epsilon, \chi, \xi} g_{\nu}(\epsilon, \chi) [1 - 2m_{vv^*} \sigma(m_{kk^*} \chi + R_{kk} \xi)_{\epsilon} + (m_{vv^*}^2 + R_{vv}^2) \sigma(m_{kk^*} \chi + R_{kk} \xi)^{\top} \sigma(m_{kk^*} \chi + R_{kk} \xi)] . \quad (9) $$

A consequence is the characterization of the activation functions $\sigma$ that allow easy learning, in the sense that the gradients at initialization with respect to the recovery order parameters $m_{kk^*}$ and $m_{vv^*}$ are nonzero. If this quantity vanishes, moving away from the initial condition is harder and requires more iterations and thus samples (Ben Arous et al., 2021). At initialization, $m_{kk^*}$ and $m_{vv^*}$ concentrate around 0 while $R_{kk}$ and $R_{vv}$ concentrate around 1. At the first order, the gradient at initialization thus equals $\nabla \tilde{\mathcal{E}}_\sigma(0, 0, 1, 1)$. The next result characterizes when this quantity vanishes. In particular, $\sigma(\chi) = \chi$ and $\sigma(\chi) = \text{erf}(\chi)$ impede learning, which is why we add a constant bias.

**Corollary 4.1.** If the activation function $\sigma$ has a non-vanishing mean, i.e. if $\mathbb{E}_L \mathbb{E}_{\xi \sim \mathcal{N}(0, I_L)} \sigma(\xi)_{\ell} \neq 0$ for all $\ell$, then

$$ (\partial_{m_{kk^*}} \tilde{\mathcal{E}}_{\sigma}, \partial_{m_{vv^*}} \tilde{\mathcal{E}}_{\sigma})(0, 0, 1, 1) \neq (0, 0). \quad (10) $$

If the activation function $\sigma$ is symmetric, i.e. if $\sigma(-x)_{\ell} = -\sigma(x)_{\ell}$ for all $\ell$ and all $x \in \mathbb{R}^L$ then

$$ (\partial_{m_{kk^*}} \tilde{\mathcal{E}}_{\sigma}, \partial_{m_{vv^*}} \tilde{\mathcal{E}}_{\sigma})(0, 0, 1, 1) = (0, 0). \quad (11) $$

## 4.3 Comparison between linear and softmax attentions

Letting $\mathbb{E}_\sigma = \min_{\mathcal{M}} \mathcal{E}_\sigma = \min_{\mathbb{R}^4} \tilde{\mathcal{E}}_\sigma$ be the minimum of the population risk, we now compare the optimal population risk $\mathbb{E}_{\text{lin}}$ for linear attention $\sigma(\chi)_{\ell} = 1 + \chi_{\ell}$ to the one for softmax $\mathbb{E}_{\text{softmax}} = \mathcal{E}_{\text{Bayes}}$, encompassing asymptotic regimes of strong signals $\nu \to \infty$ or long sequences $L \to \infty$.

**Corollary 4.2.** Consider the spiked-SLR model with input sequences of deterministic length $L$. The minimal risks attained over the manifold $\mathcal{M}$ satisfy

$$ \mathbb{E}_{\text{lin}} = 1 - \frac{L + \nu(L - 1)}{L^2 + \nu(L - 1)} = \frac{L}{L - 1} \frac{1}{\nu} + o_{\nu \to \infty}(1) \quad \text{while} \quad \mathbb{E}_{\text{softmax}} = e^{-c_L \nu + o_{\nu \to \infty}(\nu)} \quad (12) $$
with $c_L > 0$ a constant that depends on $L$. Consider then the max-SLR model at $\nu \to +\infty$ where $g_\nu(\epsilon, \chi) = L \mathbb{1}_{\epsilon=\arg\max_\epsilon \chi_\epsilon}$ with deterministic $L$. The softmax attention is well-specified while the linear attention is not: the minimal risks attained over $\mathcal{M}$ satisfy

$$ \mathbb{E}_{\text{lin}} = 1 - \mathcal{O}_{L \to \infty} \left( \frac{\log L}{L} \right) \quad \text{while} \quad \mathbb{E}_{\text{softmax}} = 0. \qquad (13) $$

The first part of the corollary shows that on the spiked-SLR for strong signal $\nu \to +\infty$ the risk vanishes for both attentions. However, the softmax model has a better dependence on $\nu$, thereby establishing its superiority over linear attention in this setting. Turning to the dependency on the sequence length $L$, for the max-SLR, the second part of Corollary 4.2 shows a clear separation in the performance of the linear and the softmax attentions: as the sequence length increases the error of the linear attention converges to 1, which is the error of the trivial null predictor, while the softmax attention reaches perfect prediction for all $L$.

The following corollary illustrates the impact of varying sequence lengths on the linear attention.

**Corollary 4.3.** Consider the max-SLR model at $\nu \to +\infty$ where $g_\nu(\epsilon, \chi) = L \mathbb{1}_{\epsilon=\arg\max_\epsilon \chi_\epsilon}$. Let $f(L) = \mathbb{E}_{\chi \sim \mathcal{N}(0, I_L)} \max_{\ell=1}^L \chi_\ell$. Then, the minimal risk attained over $\mathcal{M}$ by linear attention satisfies

$$ \mathbb{E}_{\text{lin}} = 1 - \frac{1 + (\mathbb{E}_{L}f(L))^2}{\mathbb{E}_{L}L} \ge 1 - \frac{1 + (f(\mathbb{E}_{L}L))^2}{\mathbb{E}_{L}L}. \qquad (14) $$

Consider then SLR at $\nu = 0$ (i.e. $g_\nu(\epsilon, \chi) = 1$). We obtain that $\mathbb{E}_{\text{lin}} = 1 - 1/\mathbb{E}_{L}L$ and $\mathbb{E}_{\text{softmax}} = 1 - \mathbb{E}_{L}(1/L)$ and consequently $\mathbb{E}_{\text{lin}} \ge \mathbb{E}_{\text{softmax}}$, with equality when $\text{Var } L = 0$.

The first part of the result reveals that the variance in the sequence length $L$ hurts linear attention, since the risk for some distribution of $L$ is always worse than the risk associated to the mean value of this distribution. This illustrates a fundamental limitation of linear attention, arising from its poor normalization properties. In Fig. 1, we indeed observe that the performance gap between softmax and linear attention widens when $L \sim \text{Unif}(1, 2, 3)$ compared to $L = 2$, everything else being fixed. We characterize this phenomenon in the second part of Corollary 4.3, for the case of null signal $\nu = 0$, where the position of the relevant token is independent from the law of the tokens. Here again softmax performs better than linear attention whenever $L$ admits some variance.

## 4.4 Other activation functions

We now turn to two non-linear activation functions, element-wise erf and normalized softplus (see Section 3.3). Contrarily to linear attention, the expression (9) cannot be analytically minimized. Instead, we resort to numerical optimization of this expression, and report results in Fig. 1. Overall, we observe that the population risks for these two activation functions are between linear and softmax. In particular, on the max-SLR model the two activation functions are not well-specified. Importantly, the element-wise function suffers from variable sequence lengths while the normalized softplus does not, as can be seen by comparing results for $L = 2$ and $L \sim \text{Unif}\{1, 2, 3\}$. This highlights the importance for the activation function to perform a normalization operation involving all the tokens. Furthermore, the gap between normalized softplus and softmax widens for larger $L$. This can be expected since the softplus does not grow fast enough at $+\infty$ to dominate the noise stemming from the irrelevant tokens. This shows that, for kernelized attention, the kernel has to be well tuned to reach good performances, as is commonly known (see, e.g., Aksenov et al., 2024).

# 5 Performance of the attention at finite sample complexity

So far, our analysis has focused on the properties of the minimizer of the population risk. In practice, however, we only have access to a finite dataset $\mathcal{D} = \{(X_\mu, y_\mu) \in \mathbb{R}^{L(\mu) \times (D+1)} : \mu \in [N]\}$, and must therefore rely on an empirical estimator. In this section, we consider the regularized empirical risk defined for some regularization strengths $r_k \ge 0$ and $r_v \ge 0$ by

$$ \mathcal{L}(k, v) = \frac{1}{2} \sum_{\mu=1}^{N} (y_{\mu} - f_{\sigma, k, v}(X_{\mu}))^2 + \frac{r_k}{2} \sum_{i=1}^{D} k_i^2 + \frac{r_v}{2} \sum_{i=1}^{D} v_i^2. \qquad (15) $$
<figure><img src="image_2.png" /><figcaption>Figure 1: Minimal population risk $E_\sigma$ over $\mathcal{F}_\sigma$ for different attention activations $\sigma$ (colors), compared to the Bayes risk $\mathcal{E}_{\text{Bayes}}$ (5) (black), for the two tasks spiked-SLR (top) and max-SLR (bottom). Softmax is the only one achieving the Bayes risk. The markers on the lines are for readability only. Population risks are computed via numerical optimization of (9), starting from a random initialization. In all cases, we found that $R_{kk} = R_{vv} = 0$ was optimal, i.e. $k$ exactly aligns with $k^*$ and $v$ with $v^*$.</figcaption></figure>

Our goal is to assess the performance of an empirical risk minimizer (ERM) $(\hat{k}, \hat{v}) \in \arg\min_{\mathcal{L}}$. Our approach here is thus statistical in nature: it quantifies the impact of the finite sample size on the performance of attention. We defer computational questions (i.e. how to practically minimize $\mathcal{L}$) to the end of the section. The performance of an estimator $(\hat{k}, \hat{v})$ is evaluated through the test error

$$ \mathcal{E}_{\sigma}(\hat{k}, \hat{v}) = \mathbb{E} \left[ (y - f_{\sigma, \hat{k}, \hat{v}}(X))^2 \mid \mathcal{D}, k^*, v^* \right] \quad (16) $$

We investigate the high-dimensional proportional limit where $N, D \to \infty$ at finite $N/D \to \alpha = \Theta(1)$. Observe that letting $\alpha \to \infty$ recovers the population risk discussed in Section 4. For simplicity, we consider the noiseless task $\Delta = 0$ in the following.

While in Section 4 the benchmark was the Bayes risk, at finite sample complexity it is natural to consider instead the best achievable test risk conditionally on a finite batch of data $\mathcal{D}$, also known as the *Bayes-optimal risk*. In the proportional high-dimensional limit, the Bayes-optimal performance for the SLR task can be computed by extending the results of Troiani et al. (2025) to random sequence lengths $L \sim P_L$ and arbitrary $g_\nu$. Although this result is of independent interest, for conciseness we refer the reader to Appendix C for the details. The outcome is that the Bayes-optimal performance presents a rich phenomenology, with a hard phase where the best-known first-order method fails to achieve the information-theoretical performance, see Section B for a full discussion.

The performance (16) of an ERM is a random quantity that depends on the draw of $k^*, v^*$ and of the dataset. Our main result in this section is that, in the proportional high-dimensional limit and under a
concentration assumption known as the replica symmetry, see e.g. Vilucchio et al. (2025), this random variable converges to a deterministic quantity, which can be fully characterized in terms of a few real-valued variables that follow a self-consistent equation. An analogous result for a class of sequence multi-index models was derived in Cui (2025). However, the generality of the result in Cui (2025) did not provide any specific insight about the behavior of the single-location regression model studied here. We instead provide a numerical evaluation of this characterization for our setting.

**Result 5.1** (Test risk for attention-based predictors). In the proportional high-dimensional limit $N, D \to \infty$ with $\alpha = N/D = \Theta(1)$, under the replica symmetry assumption, the test risk (16) of a global minimizer $(\hat{k}, \hat{v})$ of the empirical risk (15) converges to a deterministic quantity

$$ \mathcal{E}_{\sigma}(\hat{k}, \hat{v}) \xrightarrow{\mathbb{P}} \mathbb{E}_{\sigma}(\alpha) = \min_{(m_k, m_v, q_k, q_v, V_k, V_v) \in \mathcal{S}} \mathbb{E}_L \mathbb{E}_{\xi, \zeta, \chi^*, y} \mathbb{E}_{\nu}(1, \chi^*)(y - \sigma(\gamma)^{\top} \omega)^2 \quad (17) $$

where $\gamma = m_k\chi^* + \sqrt{q_k - m_k^2}\xi \in \mathbb{R}^L$, $\omega_1 = m_v y + \sqrt{q_v - m_v^2}\zeta_1$ and, for $\ell > 1$, $\omega_\ell = \sqrt{q_\ell}\zeta_\ell$. The first expectation is over $L \sim P_L$ and the second conditionally on $L$ over $\xi, \zeta, \chi^* \sim \mathcal{N}(0, I_L)$ and $y \sim \mathcal{N}(0, 1)$ independently. Finally, the set $\mathcal{S} \subset \mathbb{R}^6$ is the set of fixed points of the following iterative self-consistent equations

$$ m_k^{t+1} = (r_k + \hat{V}_k^t)^{-1} \hat{m}_k^t, \quad q_k^{t+1} = (r_k + \hat{V}_k^t)^{-2} ((\hat{m}_k^t)^2 + \hat{q}_k^t), \quad V_k^{t+1} = (r_k + \hat{V}_k^t)^{-1} \quad (18) $$

$$ m_v^{t+1} = (r_v + \hat{V}_v^t)^{-1} \hat{m}_v^t, \quad q_v^{t+1} = (r_v + \hat{V}_v^t)^{-2} ((\hat{m}_v^t)^2 + \hat{q}_v^t), \quad V_v^{t+1} = (r_v + \hat{V}_v^t)^{-1} \quad (19) $$

$$ \begin{pmatrix} \hat{m}_k^t \\ \hat{m}_v^t \end{pmatrix} = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y, \chi^*} g_\nu(1, \chi^*) \begin{pmatrix} (V_k^t)^{-1} \sum_{\ell}^{L} (\chi_\ell^* \chi_\ell' - m_k^t (V_k^t)^{-1} \text{Cov}(\chi_\ell)) \\ (V_v^t)^{-1} (y z_1' - m_v^t (V_v^t)^{-1} \text{Cov}(z_1)) \end{pmatrix} \quad (20) $$

$$ \begin{pmatrix} \hat{q}_k^t \\ \hat{q}_v^t \end{pmatrix} = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y, \chi^*} g_\nu(1, \chi^*) \sum_{l=1}^{L} \begin{pmatrix} (V_k^t)^{-2} (\chi_l' - \gamma_l) \\ (V_v^t)^{-2} (z_l' - \omega_l)^2 \end{pmatrix} \quad (21) $$

$$ \begin{pmatrix} \hat{V}_k^t \\ \hat{V}_v^t \end{pmatrix} = \alpha \mathbb{E}_L L \begin{pmatrix} (V_k^t)^{-1} \\ (V_v^t)^{-1} \end{pmatrix} - \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y, \chi^*} g_\nu(1, \chi^*) \sum_{l=1}^{L} \begin{pmatrix} (V_k^t)^{-2} \text{Cov}(\chi_l) \\ (V_v^t)^{-2} \text{Cov}(z_l) \end{pmatrix} \quad (22) $$

where we define the potential $\psi_{\text{out}}$ over $(\mathbb{R}^L)^2$, its extremizers and its covariances as:

$$ \psi_{\text{out}}(\chi, z) = -\frac{1}{2}(y - \sigma(\chi)^{\top} z)^2 + \sum_{\ell}^{L} \log \mathcal{N}(\chi_{\ell}; \gamma_{\ell}, V_k) + \sum_{\ell}^{L} \log \mathcal{N}(z_{\ell}; \omega_{\ell}, V_v) \quad (23) $$

$$ \chi', z' = \arg \max_{\chi, z} \psi_{\text{out}}(\chi, z) \in (\mathbb{R}^L)^2 \quad (24) $$

$$ \text{Cov}(\chi_\ell) = - \left( (\nabla^2 \psi_{\text{out}}(\chi', z'))^{-1} \right)_{\chi_\ell} \in \mathbb{R}, \quad \text{Cov}(z_\ell) = - \left( (\nabla^2 \psi_{\text{out}}(\chi', z'))^{-1} \right)_{z_\ell} \in \mathbb{R}. \quad (25) $$

The derivation of this result is given in Appendix C. We simplified it assuming diagonal order parameters, which is analogous to the manifold assumption made in Section 4. Taking the limit $\alpha \to \infty$ we checked that we recover the expression of the population risk of Proposition 4.4, and in particular $\lim_{\alpha \to \infty} \mathbb{E}_{\sigma}(\alpha)$ corresponds to the value of $\mathbb{E}_{\sigma}$ derived in Section 4.3 for the population risk. While our derivation is based on the non-rigorous replica method, we expect that a formal proof could be established under the so-called replicon condition along the lines of recent progress in proof techniques from Vilucchio et al. (2025) that is able to deal with minimization of intrinsically non-convex objectives for single-index models. Extending this proof to multi-index models, such as (15), is a technical challenge left for future work.

As in the population result of Proposition 4.3, the high-dimensional analysis shows that the risk depends only on a few order parameters. Although the resulting expressions are cumbersome, it is important to emphasize that these quantities are deterministic and independent of the diverging dimensions $N$ and $D$, in contrast to the original risk (15). Thus the result provides an implementable formula for the performance of ERMs. In practice, the minimum over the set $\mathcal{S}$ is computed by running the fixed point method from several initializations, typically random (so-called *uninformed*) and *informed* ones corresponding to (partial) alignment of $k$ and $v$ with $k^*$ and $v^*$.

We conclude by a few computational remarks. Because the empirical risk (15) is non-convex, it is not guaranteed a priori that optimization algorithms can find global minima. To assess the effect of non-convexity, we rely on numerical simulations shown in Fig. 2. More precisely, we compare the prediction of Result
<figure><img src="image_3.png" /><figcaption>Figure 2: Test error of the trained attention (linear vs. softmax) across different tasks and signal strengths $v$, for $L = 3$. Linear attention is shown in red and softmax in blue. Solid lines indicate $E_\sigma(\alpha)$ at finite $\alpha$ (Result 5.1), while markers represent the test error of an ERM obtained via a local optimization method with $\sqrt{ND} = 10^4$. The regularizations $r_k$ and $r_v$ are tuned by grid search to minimize the test error. Dotted and dashed lines correspond to the value of $E_\sigma$ in the infinite-$\alpha$ limit (see closed-formed formulas in Proposition 4.1 for softmax and Appendix A.2.7 for linear). The Bayes-optimal risk is shown in black (see Section B for a discussion on its discontinuity). Appendices D-E include more experimental details and results.</figcaption></figure>

5.1 (here, uninformed and informed initializations give the same result) to the outcome of running a local optimization algorithm (specifically, a quasi-Newton method) on the risk (15), starting from a random initialization, for finite but large $N$ and $D$. We first note that the agreement between both is excellent. This suggests that potential bad local minima in the optimization landscape are avoided, at least for the appropriate regularization strength. Analyzing this landscape is an interesting open question. Furthermore, the softmax attention has lower error than the linear attention across all the tested hyperparameters, which shows that the benefits of softmax extend beyond the population risk analysis of Section 4 to statistical and computational advantages on the empirical risk. We finally note that in this case the softmax is no longer Bayes-optimal but the gap to the Bayes risk closes as $\alpha$ grows, as expected from our analysis in Section 4.

## Acknowledgments

We would like to thank Hugo Cui for the insightful discussions. This work was supported by the French government, managed by the National Research Agency (ANR), under the France 2030 program with the reference "ANR-23-IACL-0008" and the Choose France - CNRS AI Rising Talents program. P.M. is supported by a Google PhD Fellowship. This work was done in part while P.M. and L.Z. were visiting the Simons Institute for the Theory of Computing. This research was supported by the Swiss National Science Foundation (SNSF) under grant number 212049 (SMArtNet).

## References

K. Ahn, X. Cheng, H. Daneshmand, and S. Sra. Transformers learn to implement preconditioned gradient descent for in-context learning. In A. Oh, T. Naumann, A. Globerson, K. Saenko, M. Hardt, and S. Levine, editors, *Advances in Neural Information Processing Systems*, volume 36, pages 45614–45650. Curran Associates, Inc., 2023.

Y. Aksenov, N. Balagansky, S. Lo Cicero Vaina, B. Shaposhnikov, A. Gorbatovski, and D. Gavrilov. Linear transformers with learnable kernel functions are better in-context models. In *Proceedings of the 62nd
*Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)*. Association for Computational Linguistics, 2024.

L. Arnaboldi, B. Loureiro, L. Stephan, F. Krzakala, and L. Zdeborová. Asymptotics of SGD in sequence-single index models and single-layer attention networks. *Advances in Neural Information Processing Systems*, 2025. arXiv preprint arXiv:2506.02651.

S. Arora, S. Eyuboglu, A. Timalsina, I. Johnson, M. Poli, J. Zou, A. Rudra, and C. Ré. Zoology: Measuring and improving recall in efficient language models. In *The Twelfth International Conference on Learning Representations*, 2024.

B. Aubin, F. Krzakala, Y. M. Lu, and L. Zdeborová. Generalization error in high-dimensional perceptrons: Approaching Bayes error with convex optimization. In *Advances in Neural Information Processing Systems*, 2020.

J. Ba, G. E. Hinton, V. Mnih, J. Z. Leibo, and C. Ionescu. Using fast weights to attend to the recent past. In *Advances in Neural Information Processing Systems*, volume 29. Curran Associates, Inc., 2016.

Y. Bai, F. Chen, H. Wang, C. Xiong, and S. Mei. Transformers as statisticians: Provable in-context learning with in-context algorithm selection. *Advances in neural information processing systems*, 36:57125–57211, 2023.

A. S. Bandeira, A. E. Alaoui, S. B. Hopkins, T. Schramm, A. S. Wein, and I. Zadik. The franz-parisi criterion and computational trade-offs in high dimensional statistics. In *Advances in Neural Information Processing Systems*, 2022. 2205.09727.

N. Barnfield, H. Cui, and Y. M. Lu. High-dimensional analysis of single-layer attention for sparse-token classification. 2025. private communication.

G. Ben Arous, R. Gheissari, and A. Jagannath. Online stochastic gradient descent on non-convex losses from high-dimensional inference. *Journal of Machine Learning Research*, 22(106):1–51, 2021.

F. Caltagirone, M. Lelarge, and L. Miolane. Recovering asymmetric communities in the stochastic block model. In *54th Annual Allerton Conference on Communication, Control and Computing*, 2016. arXiv:1610.03680.

K. M. Choromanski, V. Likhosherstov, D. Dohan, X. Song, A. Gane, T. Sarlós, P. Hawkins, J. Q. Davis, A. Mohiuddin, L. Kaiser, D. B. Belanger, L. J. Colwell, and A. Weller. Rethinking attention with performers. In *9th International Conference on Learning Representations*, 2021.

Y. Chou, M. Yao, K. Wang, Y. Pan, R.-J. Zhu, J. Wu, Y. Zhong, Y. Qiao, B. Xu, and G. Li. Metala: Unified optimal linear approximation to softmax attention map. In *Advances in Neural Information Processing Systems*, volume 37. Curran Associates, Inc., 2024.

H. Cui. High-dimensional learning of narrow neural networks. *Journal of Statistical Mechanics: Theory and Experiment*, 2025(2):023402, 2025.

H. Cui, F. Behrens, F. Krzakala, and L. Zdeborová. A phase transition between positional and semantic learning in a solvable model of dot-product attention. *Advances in Neural Information Processing Systems*, 37:36342–36389, 2024.

M. Dia, N. Macris, F. Krzakala, T. Lesieur, L. Zdeborová, et al. Mutual information for symmetric rank-one matrix estimation: A proof of the replica formula. *Advances in Neural Information Processing Systems*, 29, 2016. arXiv:1606.04142.

D. Y. Fu, T. Dao, K. K. Saab, A. W. Thomas, A. Rudra, and C. Ré. Hungry hungry hippos: Towards language modeling with state space models. In *The Eleventh International Conference on Learning Representations*, 2023.

A. Graves, G. Wayne, and I. Danihelka. Neural Turing machines. *arXiv preprint arXiv:1410.5401*, 2014.
A. Gu and T. Dao. Mamba: Linear-time sequence modeling with selective state spaces. In *First Conference on Language Modeling*, 2024.

C.-P. Hsieh, S. Sun, S. Kriman, S. Acharya, D. Rekesh, F. Jia, and B. Ginsburg. RULER: What's the real context size of your long-context language models? In *First Conference on Language Modeling*, 2024.

G. Kamradt. Needle in a haystack - pressure testing LLMs. Github, 2023. URL https://github.com/gkamradt/LLMTest_NeedleInAHaystack.

F. Krzakala, M. Mézard, F. Sausset, Y. Sun, and L. Zdeborová. Probabilistic reconstruction in compressed sensing: algorithms, phase diagrams, and threshold achieving matrices. *Journal of Statistical Mechanics: Theory and Experiment*, 2012.

T. Lesieur, F. Krzakala, and L. Zdeborová. Constrained low-rank matrix estimation: Phase transitions, approximate message passing and applications. *Journal of Statistical Mechanics: Theory and Experiment*, 2017, 2017. arxiv:1701.00858.

Y. M. Lu, M. Letey, J. A. Zavatone-Veth, A. Maiti, and C. Pehlevan. Asymptotic theory of in-context learning by linear attention. *Proceedings of the National Academy of Sciences*, 122(28):e2502599122, 2025.

S. Luo, S. Li, T. Cai, D. He, D. Peng, S. Zheng, G. Ke, L. Wang, and T.-Y. Liu. Stable, fast and accurate: Kernelized attention with relative positional encoding. *Advances in Neural Information Processing Systems*, 34:22795-22807, 2021.

D. Machlab and R. Battle. LLM in-context recall is prompt dependent. arXiv preprint arXiv:2404.08865, 2024.

A. Mahankali, T. B. Hashimoto, and T. Ma. One step of gradient descent is provably the optimal in-context learner with one layer of linear self-attention. *ICLR 2024*, 2023. arXiv preprint arXiv:2307.03576.

P. Marion, R. Berthier, G. Biau, and C. Boyer. Attention layers provably solve single-location regression. In *The Thirteenth International Conference on Learning Representations*, 2025.

C. Olsson, N. Elhage, N. Nanda, N. Joseph, N. DasSarma, T. Henighan, B. Mann, A. Askell, Y. Bai, A. Chen, T. Conerly, D. Drain, D. Ganguli, Z. Hatfield-Dodds, D. Hernandez, S. Johnston, A. Jones, J. Kernion, L. Lovitt, K. Ndousse, D. Amodei, T. Brown, J. Clark, J. Kaplan, S. McCandlish, and C. Olah. In-context learning and induction heads. *Transformer Circuits Thread*, 2022. https://transformer-circuits.pub/2022/in-context-learning-and-induction-heads/index.html.

B. Peng, E. Alcaide, Q. Anthony, A. Albalak, S. Arcadinho, S. Biderman, H. Cao, X. Cheng, M. Chung, L. Derczynski, X. Du, M. Grella, K. Gv, X. He, H. Hou, P. Kazienko, J. Kocon, J. Kong, B. Koptyra, H. Lau, J. Lin, K. S. I. Mantri, F. Mom, A. Saito, G. Song, X. Tang, J. Wind, S. Woźniak, Z. Zhang, Q. Zhou, J. Zhu, and R.-J. Zhu. RWKV: Reinventing RNNs for the transformer era. In *Proceedings of the 2023 Conference on Empirical Methods in Natural Language Processing*. Association for Computational Linguistics, 2023.

M. Poli, S. Massaroli, E. Nguyen, D. Y. Fu, T. Dao, S. Baccus, Y. Bengio, S. Ermon, and C. Re. Hyena hierarchy: Towards larger convolutional language models. In *Proceedings of the 40th International Conference on Machine Learning*, volume 202 of *Proceedings of Machine Learning Research*. PMLR, 2023.

Z. Qin, W. Sun, H. Deng, D. Li, Y. Wei, B. Lv, J. Yan, L. Kong, and Y. Zhong. cosformer: Rethinking softmax in attention. In *The Tenth International Conference on Learning Representations*, 2022.

X. Shen, D. Li, R. Leng, Z. Qin, W. Sun, and Y. Zhong. Scaling laws for linear complexity language models. In *Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing*. Association for Computational Linguistics, 2024.

E. Troiani, H. Cui, Y. Dandi, F. Krzakala, and L. Zdeborová. Fundamental limits of learning in sequence multi-index models and deep attention networks: High-dimensional asymptotics and sharp thresholds. In *Proceedings of the 42th International Conference on Machine Learning*, 2025.
A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A. N. Gomez, Ł. Kaiser, and I. Polosukhin. Attention is all you need. In I. Guyon, U. von Luxburg, S. Bengio, H. Wallach, R. Fergus, S. Vishwanathan, and R. Garnett, editors, *Advances in Neural Information Processing Systems*, volume 30, pages 6000–6010. Curran Associates, Inc., 2017.

M. Vilucchio, Y. Dandi, C. Gerbelot, and F. Krzakala. Asymptotics of non-convex generalized linear models in high-dimensions: A proof of the replica formula. *arXiv preprint arXiv:2502.20003*, 2025.

J. von Oswald, E. Niklasson, E. Randazzo, J. Sacramento, A. Mordvintsev, A. Zhmoginov, and M. Vladymyrov. Transformers learn in-context by gradient descent. In A. Krause, E. Brunskill, K. Cho, B. Engelhardt, S. Sabato, and J. Scarlett, editors, *Proceedings of the 40th International Conference on Machine Learning*, volume 202 of *Proceedings of Machine Learning Research*, pages 35151–35174. PMLR, 2023.

S. Wang, B. Z. Li, M. Khabsa, H. Fang, and H. Ma. Linformer: Self-attention with linear complexity. *arXiv preprint arXiv:2006.04768*, 2020.

X. Wang, L. Ma, J. Huang, P. Lu, P. Parthasarathi, X.-W. Chang, B. Chen, and Y. Cui. Resona: Improving context copying in linear recurrence models with retrieval. In *Second Conference on Language Modeling*, 2025.

R. Zhang, S. Frei, and P. L. Bartlett. Trained transformers learn linear models in-context. *Journal of Machine Learning Research*, 25(49):1–55, 2024.

Y. Zhang, A. K. Singh, P. E. Latham, and A. Saxe. Training dynamics of in-context learning in linear attention. *ICML 2025*, 2025. arXiv preprint arXiv:2501.16265.
# A Population risk

In this part we prove the expressions for the Bayes risk and the optimal risk of the attention layer, on population loss, stated in Proposition 4.1 and 4.3 and 4.4.

## A.1 Proof of proposition 4.1: Bayes risk

We consider the Bayes risk $\mathcal{E}_{\text{Bayes}}$. We assume the estimator exactly knows $k^*$ and $v^*$. $\mathcal{E}_{\text{Bayes}}$ is not trivially null because the position $\epsilon^*$ of the relevant token is not known.

The best estimator $\hat{y}_{\text{Bayes}}$ minimizing the square error on a given sample $X \in \mathbb{R}^{L \times D}$ is the posterior mean of $y$ given $L$, $k^*$, $v^*$ and $X$:

$$ \hat{y}_{\text{Bayes}} = \sum_{\epsilon} P(\epsilon|L, k^*, X)y = \sum_{\epsilon} P(\epsilon|L, k^*, X) \frac{1}{\sqrt{D}} X_{\epsilon}^{\top} v^* \quad (26) $$

The conditional distribution of $\epsilon$ given $L$, $k^*$ and $X$ reads

$$ P(\epsilon|L, k^*, X) = P(\epsilon|\chi^*) = \frac{g(\epsilon, \chi^*)}{\sum_{\epsilon'=1}^{L} g(\epsilon', \chi^*)} \quad (27) $$

Conditionally on $L$, let $\chi^* = \frac{1}{\sqrt{D}}Xk^* \in \mathbb{R}^L$ and $z^* = \frac{1}{\sqrt{D}}Xv^* \in \mathbb{R}^L$ be the projections of $X$ onto the two relevant directions. They are distributed according to uncorrelated standard Gaussians with law $\mathcal{N}(0, I_L)$.

We express the empirical means over samples $\mu$ as expectations over one sample. The risk of $\hat{y}_{\text{Bayes}}$ is:

$$ \mathcal{E}_{\text{Bayes}} = \mathbb{E}_{L, k^*, v^*} \mathbb{E}_{\epsilon^*, X} (y - \hat{y}_{\text{Bayes}})^2 \quad (28) $$

$$ = \mathbb{E}_{L, k^*, v^*} \mathbb{E}_{X \sim \mathcal{N}(0, I_{L \times D})} \frac{1}{L} \sum_{\epsilon^*} g_{\nu}(\epsilon^*, \chi^*)(y - \hat{y}_{\text{Bayes}})^2 \quad (29) $$

$$ = 1 + \mathbb{E}_{L} \mathbb{E}_{\chi^*, z^*} \frac{1}{L} \sum_{\epsilon^*} g_{\nu}(\epsilon^*, \chi^*) \left[ -2z_{\epsilon^*}^* \sum_{\epsilon} P(\epsilon|\chi^*) z_{\epsilon}^* + \left( \sum_{\epsilon} P(\epsilon|\chi^*) z_{\epsilon}^* \right)^2 \right] \quad (30) $$

$$ = 1 + \mathbb{E}_{L} \mathbb{E}_{\chi^*} \frac{1}{L} \sum_{\epsilon^*} g_{\nu}(\epsilon^*, \chi^*) \left[ -2P(\epsilon^*|\chi^*) + \sum_{\epsilon} P(\epsilon|\chi^*)^2 \right] \quad (31) $$

$$ = 1 - \mathbb{E}_{L} \mathbb{E}_{\chi^*} \frac{1}{L} \sum_{\epsilon^*} \frac{g_{\nu}(\epsilon^*, \chi)^2}{\sum_{\epsilon} g_{\nu}(\epsilon, \chi)} \quad (32) $$

This gives the expression stated in result 4.1.

## A.2 Risk of the attention layer on population loss

We consider the risk of the trained attention. We work on population loss. We recall that $k \in \mathbb{R}^D$ and $v \in \mathbb{R}^D$ are the weights of the attention. They can be described by the following scalar order parameters (or summary statistics, or sufficient statistics):

$$ m_{kk^*} = \frac{1}{D} k^\top k^* \qquad m_{vv^*} = \frac{1}{D} v^\top v^* \qquad m_{kv^*} = \frac{1}{D} k^\top v^* \qquad m_{vk^*} = \frac{1}{D} v^\top k^* \quad (33) $$

$$ q_{kk} = \frac{1}{D} k^\top k \qquad q_{vv} = \frac{1}{D} v^\top v \qquad q_{vk} = \frac{1}{D} k^\top v \qquad (34) $$

Setting

$$ \begin{pmatrix} q_{kk} & q_{vk} \\ q_{vk} & q_{vv} \end{pmatrix} = \begin{pmatrix} m_{kk^*} \\ m_{vk^*} \end{pmatrix}^{\top 2} + \begin{pmatrix} m_{kv^*} \\ m_{vv^*} \end{pmatrix}^{\top 2} + \hat{q} \quad (35) $$

with $\hat{q}$ a positive $2 \times 2$ matrix, $k$ and $v$ can be expressed as

$$ k = m_{kk^*} k^* + m_{kv^*} v^* + (\tilde{q}^{1/2})_{kk} k^\perp + (\tilde{q}^{1/2})_{kv} v^\perp \quad (36) $$

$$ v = m_{vk^*} k^* + m_{vv^*} v^* + (\tilde{q}^{1/2})_{kv} k^\perp + (\tilde{q}^{1/2})_{vv} v^\perp \quad (37) $$
with $k^\perp \in \mathbb{R}^D$ and $v^\perp \in \mathbb{R}^D$ two vectors orthogonal to $k^*$, $v^*$ and between themselves. We introduce the shorthands

$$R_{kk} = (\tilde{q}^{1/2})_{kk} \qquad R_{kv} = (\tilde{q}^{1/2})_{kv} \qquad R_{vv} = (\tilde{q}^{1/2})_{vv} \qquad (38)$$

$R_{kk}$, $R_{kv}$ and $R_{vv}$ are related to the magnitude of the components in $k$ and $v$ that are orthogonal to $k^*$ and $v^*$ and bring no information. In the case where there is no overlap between $v$ and $k$ or $k^*$, and no overlap between $k$ and $v$ or $v^*$, i.e. when $m_{kv^*} = m_{vk^*} = q_{vk} = 0$, one can simply express the Rs as $R_{kk}^2 = q_{kk} - m_{kk^*}^2$, $R_{vv}^2 = q_{vv} - m_{vv^*}^2$ and $R_{kv} = 0$. Then $R_{kk} = R_{vv} = 0$ means that $k$ is perfectly aligned with $k^*$ and $v$ with $v^*$.

The loss depends on $k$ and $v$ only via their projections onto the tokens $X$. Conditionally on $L$, we introduce the projections

$$\chi^* = \frac{1}{\sqrt{D}} X k^* \qquad z^* = \frac{1}{\sqrt{D}} X v^* \qquad (39)$$

$$\xi = \frac{1}{\sqrt{D}} X k^{\perp} \qquad \zeta = \frac{1}{\sqrt{D}} X v^{\perp} \qquad (40)$$

By central limit theorem we have $\chi^* \sim \mathcal{N}(0, I_L)$, $z^* \sim \mathcal{N}(0, I_L)$, $\xi \sim \mathcal{N}(0, I_L)$, $\zeta \sim \mathcal{N}(0, I_L)$. For conciseness we introduce the projections of $k$ and $v$:

$$b = \frac{1}{\sqrt{D}} X k = m_{kk^*} \chi^* + m_{kv^*} z^* + R_{kk} \xi + R_{kv} \zeta \in \mathbb{R}^L \qquad (41)$$

$$a = \frac{1}{\sqrt{D}} X v = m_{vk^*} \chi^* + m_{vv^*} z^* + R_{kv} \xi + R_{vv} \zeta \in \mathbb{R}^L \qquad (42)$$

We introduce

$$\Theta = (m_{kk^*}, m_{kv^*}, m_{vk^*}, m_{vv^*}, R_{kk}, R_{kv}, R_{vv}) \in \mathbb{R}^7$$

the set of parameters over which the loss is effectively minimized. Then the risk reads

$$\mathcal{E}_{\sigma}(k, v) = \tilde{\mathcal{E}}_{\sigma}(\Theta) \qquad (43)$$

$$\tilde{\mathcal{E}}_{\sigma}(\Theta) = \mathbb{E}_{L, k^*, v^*} \mathbb{E}_{\epsilon^*, X} (y - \hat{y})^2 \qquad (44)$$

$$= \mathbb{E}_{L, k^*, v^*} \mathbb{E}_{X \sim \mathcal{N}(0, I_{L \otimes D}), \epsilon^*} g_{\nu}(\epsilon^*, \chi^*)(y - \hat{y})^2 \qquad (45)$$

$$= \mathbb{E}_{L} \mathbb{E}_{\epsilon^*, \chi^*, z^*, \xi, \zeta} g_{\nu}(\epsilon^*, \chi^*) (z_{\epsilon^*}^* - a^{\top} \sigma(b))^2 \qquad (46)$$

and the minimal risk is

$$\mathbb{E}_{\sigma} = \min_{(k,v) \in (\mathbb{R}^D)^2} \mathcal{E}_{\sigma}(k,v) \qquad (47)$$

$$= \min_{\Theta \in \mathbb{R}^7} \tilde{\mathcal{E}}_{\sigma}(\Theta) \qquad (48)$$

### A.2.1 Proof of proposition 4.2: the softmax is Bayes-optimal

We recall that the Bayes risk and the optimal risk of the attention estimators are

$$\mathcal{E}_{\text{Bayes}} = 1 - \mathbb{E}_{L} \mathbb{E}_{\chi^*} \frac{1}{L} \sum_{\epsilon^*} \frac{g_{\nu}(\epsilon^*, \chi)^2}{\sum_{\epsilon} g_{\nu}(\epsilon, \chi)} \qquad (49)$$

$$\mathbb{E}_{\sigma} \leq \min_{m_{kk^*}, m_{vv^*} \in \mathbb{R}^2} \mathbb{E}_{L} \mathbb{E}_{\epsilon, \chi} [1 - 2m_{vv^*} g_{\nu}(\epsilon, \chi) \sigma(m_{kk^*} \chi) \epsilon + m_{vv^*}^2 g_{\nu}(\epsilon, \chi) \sigma(m_{kk^*} \chi)^{\top} \sigma(m_{kk^*} \chi)] \qquad (50)$$

where for $\mathbb{E}_{\sigma}$ we have a upper-bound obtained by restraining the min of eq. (48) to $m_{vk^*}$, $m_{kv^*}$, $R_{vv}$, $R_{kk}$, $R_{kv} = 0, 0, 0, 0, 0$. By definition of the Bayes risk we have $\mathbb{E}_{\text{Bayes}} \leq \mathbb{E}_{\sigma}$.

We search for the optimal $\sigma$. We show that it has to match the Bayes-optimal estimator of $\epsilon$. We recall that, from the previous part A.1, the posterior distribution of $\epsilon$ given $\chi = \frac{1}{\sqrt{D}} X k^*$ is

$$P(\epsilon|\chi) = \frac{g(\epsilon, \chi)}{\sum_{\epsilon'=1}^{L} g(\epsilon', \chi)} \qquad (51)$$
If, for a certain $m_{kk^*}$, $\sigma(m_{kk^*}\chi)_\epsilon = P(\epsilon|\chi)$, then

$$ \mathbb{E}_L \mathbb{E}_{\epsilon, \chi} g_{\nu}(\epsilon, \chi) \sigma(m_{kk^*} \chi)^{\top} \sigma(m_{kk^*} \chi) = \mathbb{E}_L \mathbb{E}_{\chi} \frac{1}{L} \sum_{\epsilon}^{L} g_{\nu}(\epsilon, \chi) \sum_{\epsilon'} P(\epsilon'|\chi)^2 \quad (52) $$

$$ = \mathbb{E}_L \mathbb{E}_{\chi} \frac{1}{L} \sum_{\epsilon}^{L} \frac{g(\epsilon, \chi)^2}{\sum_{\epsilon'}^{L} g(\epsilon', \chi)} \quad (53) $$

$$ = \mathbb{E}_L \mathbb{E}_{\epsilon, \chi} g_{\nu}(\epsilon, \chi) \sigma(m_{kk^*} \chi)_\epsilon \quad (54) $$

One can notice the similarity with the derivation of the Bayes risk in part A.1. We proved the equality

$$ \mathbb{E}_L \mathbb{E}_{\epsilon, \chi} g_{\nu}(\epsilon, \chi) \sigma(m_{kk^*} \chi)_\epsilon = \mathbb{E}_L \mathbb{E}_{\epsilon, \chi} g_{\nu}(\epsilon, \chi) \sigma(m_{kk^*} \chi)^{\top} \sigma(m_{kk^*} \chi) . \quad (55) $$

In statistical physics this equality is called the Nishimori condition. It is satisfied when $\sigma(m_{kk^*}\chi)_\epsilon$ matches the posterior distribution of $\epsilon$. The optimal $m_{vv^*}$ is then $m_{vv^*} = 1$ and we obtain

$$ \mathbb{E}_{\sigma} = 1 - \mathbb{E}_{L} \mathbb{E}_{\epsilon, \chi} g_{\nu}(\epsilon, \chi) \sigma(m_{kk^*} \chi)_{\epsilon} \quad (56) $$

$$ = 1 - \mathbb{E}_L \mathbb{E}_{\chi} \frac{1}{L} \sum_{\epsilon}^{L} \frac{g(\epsilon, \chi)^2}{\sum_{\epsilon'}^{L} g(\epsilon', \chi)} \quad (57) $$

Consequently $\mathbb{E}_{\sigma} = \mathcal{E}_{\text{Bayes}}$.

It remains to show which $\sigma$ can satisfy the Nishimori condition. We assume that there is a constant $c_\nu$ such that for all $L$, all $\epsilon, \epsilon' \in \{1, \dots, L\}$ and all $\chi \in \mathbb{R}^L$

$$ \frac{g_{\nu}(\epsilon, \chi)}{g_{\nu}(\epsilon', \chi)} = e^{c_{\nu}(\chi_{\epsilon} - \chi_{\epsilon'})} . \quad (58) $$

Then

$$ \sigma(m_{kk^*} \chi)_\epsilon = P(\epsilon|\chi) \quad (59) $$

$$ = \frac{g(\epsilon, \chi)}{\sum_{\epsilon'}^{L} g(\epsilon', \chi)} \quad (60) $$

$$ = \frac{e^{c_{\nu} \chi_{\epsilon}}}{\sum_{\epsilon'}^{L} e^{c_{\nu} \chi_{\epsilon'}}} \quad (61) $$

that is to say $\sigma$ is a softmax with inverse temperature $m_{kk^*} = c_\nu$.

### A.2.2 Invariant manifold

In the main part sec. 4 we introduced the manifold

$$ \mathcal{M} = \{(k, v) \in (\mathbb{R}^D)^2, m_{kv^*} = m_{vk^*} = q_{vk} = 0\}. $$

We show that it is invariant by GD. We consider the space of the order parameters, parameterized by

$$ \Theta = (m_{vv^*}, m_{vk^*}, m_{kv^*}, m_{kk^*}, R_{vv}, R_{kk}, R_{kv}) \in \mathbb{R}^7. $$

We introduce the manifold

$$ \tilde{\mathcal{M}} = \{ \Theta \in \mathbb{R}^7, m_{kv^*} = m_{vk^*} = R_{kv} = 0 \}. \quad (62) $$

At random initialization we have that $\Theta = (0, 0, 0, 0, 1, 0, 1) \in \mathcal{M}$. We consider the dynamics given by the gradient flow over the loss $\tilde{\mathcal{E}}_\sigma$ defined in eq. (44):

$$ \dot{\Theta} = -\nabla_{\Theta} \tilde{\mathcal{E}}_{\sigma}(\Theta) \quad (63) $$
We show that $\tilde{\mathcal{M}}$ is invariant under this dynamics. The gradient in the three directions $(m_{kv^*}, m_{vk^*}, R_{kv})$ on $\tilde{\mathcal{M}}$ reads

$$ \partial_{m_{kv^*}} \tilde{\mathcal{E}}_{\sigma} = 2\mathbb{E}_L \mathbb{E}_{\epsilon^*, \chi^*, z^*, \xi, \zeta} g_{\nu}(\epsilon^*, \chi^*) (a^{\top}\sigma(b) - z_{\epsilon^*}^*) a^{\top}\nabla\sigma(b)z^* \quad (64) $$

$$ \partial_{m_{vk^*}} \tilde{\mathcal{E}}_{\sigma} = 2\mathbb{E}_L \mathbb{E}_{\epsilon^*, \chi^*, z^*, \xi, \zeta} g_{\nu}(\epsilon^*, \chi^*) (a^{\top}\sigma(b) - z_{\epsilon^*}^*) \sigma(b)^{\top}\chi^* \quad (65) $$

$$ \partial_{R_{kv}} \tilde{\mathcal{E}}_{\sigma} = 2\mathbb{E}_L \mathbb{E}_{\epsilon^*, \chi^*, z^*, \xi, \zeta} g_{\nu}(\epsilon^*, \chi^*) (a^{\top}\sigma(b) - z_{\epsilon^*}^*) (\sigma(b)^{\top}\xi + a^{\top}\nabla\sigma(b)\zeta) \quad (66) $$

At $m_{kv^*} = m_{vk^*} = R_{kv} = 0$ we have $a = m_{vv^*}z^* + R_{vv}\zeta$ and $b = m_{kk^*}\chi^* + R_{kk}\xi$ is independent of $z^*$ and $\zeta$. Then, because of the parity with respect to $z^*$ and $\zeta$, we obtain that the gradient vanishes.

### A.2.3 Stability of the manifold

We show numerically that the manifold

$$ \tilde{\mathcal{M}} = \{\Theta \in \mathbb{R}^7, m_{kv^*} = m_{vk^*} = R_{kv} = 0\} $$

is stable. We simulate the gradient flow

$$ \dot{\Theta} = -\nabla_{\Theta} \tilde{\mathcal{E}}_{\sigma}(\Theta) \quad (67) $$

starting from a perturbed random initial condition

$$ (m_{kk^*}, m_{kv^*}, m_{vk^*}, m_{vv^*}, R_{kk}, R_{kv}, R_{vv}) = (0, 0, 0, 0, 1, 0, 1) + \eta, $$

with $\eta \sim \text{Unif}([-\bar{\eta}, +\bar{\eta}]^7)$ some small noise. We consider the value of $\Theta$ reached after convergence, for several independent realizations of $\eta$, for all the configurations considered in the main part of the article. On Figure 3 we observe that all the trajectories converge to a point that belongs to $\tilde{\mathcal{M}}$, up to the numerical errors due to the integration. We additionally observe that $R_{kk} \approx R_{vv} \approx 0$.

### A.2.4 Orthogonal components on the manifold

We show that the minimization of $\tilde{\mathcal{E}}_{\sigma}$, on the previously defined manifold, leads to a vanishing orthogonal component $R_{vv} = 0$. We can show that the second orthogonal component $R_{kk}$ is null if we assume that the attention is linear.

We recall that on the manifold $a = m_{vv^*}z^* + R_{vv}\zeta$ and $b = m_{kk^*}\chi^* + R_{kk}\xi$. We compute the gradient of the loss with respect to $R_{vv}$:

$$ \partial_{R_{vv}} \tilde{\mathcal{E}}_{\sigma} = 2\mathbb{E}_L \mathbb{E}_{\epsilon^*, \chi^*, z^*, \xi, \zeta} g_{\nu}(\epsilon^*, \chi^*) (a^{\top}\sigma(b) - z_{\epsilon^*}^*) \sigma(b)^{\top}\zeta \quad (68) $$

$$ = 2R_{vv} \underbrace{\mathbb{E}_L \mathbb{E}_{\epsilon^*, \chi^*, \xi} g_{\nu}(\epsilon^*, \chi^*) \sigma(b)^{\top} \sigma(b)}_{>0} \quad (69) $$

where we used the parity with respect to $\zeta$ and assumed that the supports of $\sigma$ and $b$ overlap. As a consequence $R_{vv} = 0$ is the minimizer of $\tilde{\mathcal{E}}_{\sigma}$.

We turn to the second orthogonal component $R_{kk}$. In full generality $R_{kk} = 0$ cannot be straightforwardly derived by considering only the gradient $\partial_{R_{kk}} \tilde{\mathcal{E}}_{\sigma}$ because the gradient may not be monotonous. Instead we assume that $\sigma(\chi) = 1 + \chi$. Then the gradient is

$$ \partial_{R_{kk}} \tilde{\mathcal{E}}_{\sigma} = 2\mathbb{E}_L \mathbb{E}_{\epsilon^*, \chi^*, z^*, \xi, \zeta} g_{\nu}(\epsilon^*, \chi^*) (a^{\top}(1+b) - z_{\epsilon^*}^*) a^{\top}\xi \quad (70) $$

$$ = 2\mathbb{E}_L \mathbb{E}_{\epsilon^*, \chi^*, \xi} g_{\nu}(\epsilon^*, \chi^*) m_{vv^*}^2 (1+b)^{\top}\xi \quad (71) $$

$$ = 2R_{kk} m_{vv^*}^2 \quad (72) $$

and consequently $R_{kk} = 0$ is the minimum of the loss.
<figure><img src="image_4.png" /><figcaption>Figure 3: Values of the order parameters $m_{vk^*}$, $m_{vk^*}$, $m_{vv^*}$, $R_{kk}$, $R_{kv}$ and $R_{vv}$ obtained after convergence of the gradient flow. The mean, max and min are taken over the independent runs. The initial noise is $\bar{\eta} = 0.1$ and there are at least twenty independent runs.</figcaption></figure>

### A.2.5 Linear attention, stability of the fixed point

We consider the case of the linear attention $\sigma(\chi) = 1 + \chi$. For this activation function we can state a more precise result about the stability of the manifold. We show that the fixed point obtained by minimizing the loss on the manifold is stable, i.e. it is a local minimum in the whole space of the order parameters $\mathbb{R}^7$.

At the fixed point obtained by minimizing the loss on the manifold we have $m_{vk^*}$, $m_{vk^*}$, $R_{vv}$, $R_{kk}$, $R_{kv} = 0, 0, 0, 0, 0$. We compute the Hessian of $\tilde{\mathcal{E}}_\sigma$ with respect to $m_{vk^*}$, $m_{vk^*}$, $R_{vv}$, $R_{kk}$, $R_{kv}$ at this point. For this we expand $\tilde{\mathcal{E}}_\sigma$ to the second order with respect to these five parameters. We have

$$ \tilde{\mathcal{E}}_{\sigma} = \mathbb{E}_{L} \mathbb{E}_{\epsilon^{*}, \chi^{*}, z^{*}, \xi, \zeta} g_{\nu}(\epsilon^{*}, \chi^{*}) \quad (73) $$

$$ \begin{aligned} & (z_{\epsilon^*}^* - (m_{vk^*} \chi^* + m_{vv^*} z^* + R_{kv} \xi + R_{vv} \zeta)^\top \sigma (m_{kk^*} \chi^* + m_{kv^*} z^* + R_{kk} \xi + R_{kv} \zeta))^2 \\ & = 1 - 2m_{vv^*} - 2(m_{vv^*} m_{kk^*} + m_{kv^*} m_{vk^*}) \mathbb{E}_{L} \mathbb{E}_{\epsilon^*, \chi^*} g_{\nu}(\epsilon^*, \chi^*) \chi_{\epsilon^*}^* \end{aligned} \quad (74) $$

$$ \begin{aligned} & + \mathbb{E}_{L} \mathbb{E}_{\epsilon^*, \chi^*} g_{\nu}(\epsilon^*, \chi^*) \left[ (m_{vv^*} z^{*\top} (1 + m_{kk^*} \chi^*))^2 + L(R_{vv}^2 + R_{vk}^2) \right. \\ & \qquad \left. + m_{vv^*}^2 m_{kv^*}^2 (z^{*\top} z^*)^2 + m_{vk^*}^2 ((1 + m_{kk^*} \chi^*)^{\top} \chi^*)^2 \right. \\ & \qquad \left. + m_{vv^*}^2 (R_{vk}^2 + R_{kk}^2) z^{*\top} z^* + m_{kk^*}^2 (R_{vv}^2 + R_{vk}^2) \chi^{*\top} \chi^* \right. \\ & \qquad \left. + 2m_{vv^*} m_{kv^*} m_{vk^*} z^{*\top} z^* (1 + m_{kk^*} \chi^*)^{\top} \chi^* + 2m_{vv^*} m_{kv^*} m_{vk^*} (1 + m_{kk^*} \chi^*)^{\top} z^* z^{*\top} \chi^* \right] \end{aligned} $$

The Hessian is diagonal and positive with respect to $R_{vv}$, $R_{kk}$ and $R_{kv}$; and consequently $R_{vv}$, $R_{kk}$, $R_{kv} = 0, 0, 0$ is well stable. The Hessian in the two remaining directions $m_{vk^*}$ and $m_{kv^*}$ reads

$$ \partial_{m_{vk^*}, m_{vk^*}}^2 \tilde{\mathcal{E}}_{\sigma} = \mathbb{E} \left( (1 + m_{kk^*} \chi^*)^{\top} \chi^* \right)^2 \quad (75) $$

$$ \partial_{m_{kv^*}, m_{kv^*}}^2 \tilde{\mathcal{E}}_{\sigma} = \mathbb{E} m_{vv^*}^2 (z^{*\top} z^*)^2 \quad (76) $$

$$ \partial_{m_{kv^*}, m_{kv^*}}^2 \tilde{\mathcal{E}}_{\sigma} = 2\mathbb{E} \left[ -\chi_{\epsilon^*}^* + m_{vv^*} z^{*\top} z^* (1 + m_{kk^*} \chi^*)^{\top} \chi^* + m_{vv^*} (1 + m_{kk^*} \chi^*)^{\top} z^* z^{*\top} \chi^* \right] \quad (77) $$
where we used the shorthand $\mathbb{E}$ for $\mathbb{E}_L \mathbb{E}_{\epsilon^*, \chi^*, z^*} g_\nu(\epsilon^*, \chi^*)$. The trace of the Hessian is positive. The determinant of the Hessian is

$$ \det = m_{vv*}^2 \mathbb{E} \left( (1 + m_{kk*} \chi^*)^\top \chi^* \right)^2 \mathbb{E} \left( z^{*\top} z^* \right)^2 - \left( \mathbb{E} \left[ -\chi_{\epsilon^*}^* + m_{vv*} z^{*\top} z^* (1 + m_{kk*} \chi^*)^\top \chi^* + m_{vv*} (1 + m_{kk*} \chi^*)^{\top} z^* z^{*\top} \chi^* \right] \right)^2 \quad (78) $$

We evaluate this quantity at $m_{vv*}$, $m_{kk*}$ the minimizers of the loss on the manifold, for all the configurations considered in the article. We find that $\det > 0$ and consequently the Hessian at the fixed point is positive, that is to say the fixed point on the manifold is a local minimizer on $\mathbb{R}^7$.

### A.2.6 Other possible minima

We numerically show that if one does not restrict the space of the parameters to $\mathcal{M}$, $\mathcal{E}_\sigma$ can admit several minima. Yet, among them, the minimum reached on $\mathcal{M}$ is the best.

We perform a local minimization of $\tilde{\mathcal{E}}_\sigma$ starting from the initial condition

$$ (m_{kk*}, m_{kv*}, m_{vk*}, m_{vv*}, R_{kk}, R_{kv}, R_{vv}) = (0, 1, 1, 0, 0, 0, 0). $$

The intuition is that the attention can confuse and mix $k^*$ and $v^*$. For instance for an identity activation function $\sigma(\chi) = \chi$, $k$ and $v$ play symmetric roles, that we partially broke by adding a constant bias. We call *mismatched minimum* such a minimum where $m_{kv*} \neq 0, m_{vk*} \neq 0$ and $m_{kk*} = 0, m_{vv*} = 0$, and set $\mathbb{1}_\sigma$ its risk, by opposition with the *well matched minimum* on $\mathcal{M}$ where $m_{kk*} \neq 0, m_{vv*} \neq 0$ and $m_{kv*} = 0, m_{vk*} = 0$. On Figure 4 we show that at large enough sequence length $L = 10$ and at $\nu > 0$, on the two models, the attention always has a well matched and a mismatched minimum, and that the mismatched minimum has a larger population error $\mathbb{1}_\sigma > \mathcal{E}_\sigma$.

<figure><img src="image_5.png" /><figcaption>Figure 4: Minimal population risk $\mathcal{E}_\sigma$, reached at the well matched minimum, and population risk of the mismatched minimum $\mathbb{1}_\sigma$, for different attention activations $\sigma$ (colors), for the two tasks spiked-SLR and max-SLR at $L = 10$. The markers on the lines are for readability only. Population risks are computed by numerical optimization of $\tilde{\mathcal{E}}_\sigma$ from a random initialization (well matched minimum) or from the mismatched initialization described in section A.2.6 (mismatched minimum).</figcaption></figure>
### A.2.7 Minimizer for the linear attention

On the manifold $\mathcal{M}$ the loss can be analytically minimized. We state here the expression of the minimizer. The loss is

$$ 
\begin{aligned}
\tilde{\mathcal{E}}_{\sigma} &= \mathbb{E}_{L} \mathbb{E}_{\epsilon^{*}, \chi^{*}, z^{*}, \xi, \zeta} g_{\nu}(\epsilon^{*}, \chi^{*}) (z_{\epsilon^{*}}^{*} - (m_{vv^{*}} z^{*} + R_{vv} \zeta)^{\top} (1 + m_{kk^{*}} \chi^{*} + R_{kk} \xi))^{2} \\
&= 1 - 2m_{vv^{*}} - 2m_{vv^{*}} m_{kk^{*}} \mathbb{E}_{L} \mathbb{E}_{\epsilon^{*}, \chi^{*}} g_{\nu}(\epsilon^{*}, \chi^{*}) \chi_{\epsilon^{*}}^{*} \\
&\quad + \mathbb{E}_{L} \mathbb{E}_{\epsilon^{*}, \chi^{*}} g_{\nu}(\epsilon^{*}, \chi^{*}) [m_{vv^{*}}^{2} (1 + m_{kk^{*}} \chi^{*})^{\top} (1 + m_{kk^{*}} \chi^{*}) \\
&\quad\quad + LR_{vv}^{2} + Lm_{vv^{*}}^{2} R_{kk}^{2} + m_{kk^{*}}^{2} R_{vv}^{2} \chi^{*\top} \chi^{*}]
\end{aligned}
\quad (79) $$

At the minimizer $R_{vv} = 0$ and $R_{kk} = 0$. We perform the minimization over the remaining variables and obtain that the loss is

$$ \tilde{\mathcal{E}}_{\sigma} = 1 - \frac{(\mathbb{E}(1 + m_{kk^{*}} \chi^{*})_{\epsilon^{*}})^2}{\mathbb{E}(1 + m_{kk^{*}} \chi^{*})^{\top}(1 + m_{kk^{*}} \chi^{*})} \quad m_{kk^{*}} = \frac{\mathbb{E}L\mathbb{E}\chi_{\epsilon^{*}}^{*} - \mathbb{E}1^{\top}\chi^{*}}{\mathbb{E}\chi_{\epsilon^{*}}^{*\top}\chi^{*} - \mathbb{E}\chi_{\epsilon^{*}}^{*}\mathbb{E}1^{\top}\chi^{*}} \quad (80) $$

where we used the shorthand $\mathbb{E}$ for $\mathbb{E}_{L}\mathbb{E}_{\epsilon^{*}, \chi^{*}} g_{\nu}(\epsilon^{*}, \chi^{*})$. We evaluate these quantities for the two models, the spiked- and the max-SLR. We set $f(L, \nu) = \mathbb{E}_{\epsilon^{*}, \chi^{*}} g_{\nu}(\epsilon^{*}, \chi^{*})\chi_{\epsilon^{*}}^{*}$, which for the max-SLR has no simpler expression in general. We compute:

$$ 
\begin{array}{c} 
\begin{array}{ccc} 
\mathbb{E}\chi_{\epsilon^{*}} & \mathbb{E}1^{\top}\chi^{*} & \mathbb{E}\chi_{\epsilon^{*}}^{*\top}\chi^{*} \\ 
\text{spiked} - \text{SLR} & \sqrt{\nu} & \mathbb{E}L + \nu \\ 
\text{max} - \text{SLR} & \mathbb{E}f(L, \nu) & \mathbb{E}L 
\end{array} \\ 
\begin{array}{c} 
\mathbb{E}L + \nu(\mathbb{E}L - 1) \\ 
(\mathbb{E}L)^2 + \nu(\mathbb{E}L - 1) 
\end{array} 
\end{array} 
$$

The optimal risk for the spiked-SLR finally is

$$ \mathbb{E}_{\sigma} = 1 - \frac{\mathbb{E}L + \nu(\mathbb{E}L - 1)}{(\mathbb{E}L)^2 + \nu(\mathbb{E}L - 1)} \quad (81) $$

while for the max-SLR the optimal risk is

$$ \mathbb{E}_{\sigma} = 1 - \frac{1 + (\mathbb{E}f(L, \nu))^2}{\mathbb{E}L} \quad (82) $$

### A.2.8 Proof of corollary 4.2: asymptotic convergence rates

**Spiked-SLR at large signal $\nu \to \infty$ and constant length $L$.** The loss of the linear attention is derived in part A.2.7. We take the limit $\nu \to \infty$:

$$ \mathbb{E}_{\sigma} = 1 - \frac{L + \nu(L - 1)}{L^2 + \nu(L - 1)} = \frac{L}{L - 1} \frac{1}{\nu} + o_{\nu \to \infty}(1) \quad (83) $$

The loss of the softmax attention is derived in part A.2.1. For the spiked-SLR it reads

$$ \mathbb{E}_{\sigma} = 1 - \mathbb{E}_{\chi} \frac{1}{L} \sum_{\epsilon}^{L} \frac{g(\epsilon, \chi)^2}{\sum_{\epsilon'}^{L} g(\epsilon', \chi)} \quad (84) $$

$$ = 1 - \mathbb{E}_{\chi_1 \sim \mathcal{N}(\sqrt{\nu}, 1)} \prod_{l>1}^{L} [\mathbb{E}_{\chi_l \sim \mathcal{N}(0,1)}] \frac{e^{\sqrt{\nu} \chi_1}}{\sum_{l'=1}^{L} e^{\sqrt{\nu} \chi_{l'}}} \quad (85) $$

where we isolated the first index by symmetry and made a change of variable on $\chi_1$. At large $\nu$ we approximate the softmax by a hardmax and

$$ \mathbb{E}_{\sigma} \approx 1 - \mathbb{E}_{\chi_1 \sim \mathcal{N}(\sqrt{\nu}, 1)} \prod_{l>1}^{L} [\mathbb{E}_{\chi_l \sim \mathcal{N}(0,1)}] \delta_{\chi_1 > \max_{l>1} \chi_l} \quad (86) $$

$$ = \prod_{l>1}^{L} [\mathbb{E}_{\chi_l \sim \mathcal{N}(0,1)}] e^{-\frac{1}{2}(\nu - \max_{l>1} \chi_l) + o_{\nu \to \infty}(\nu)} \quad (87) $$

$$ = e^{-\frac{1}{2}\nu + o_{\nu \to \infty}(\nu)} \quad (88) $$
**Max-SLR at $\nu = \infty$ for growing lengths $L$.** We take the limit $\nu = \infty$ so $g_\nu(\epsilon, \chi) = L\delta_{\epsilon=\arg\max_l \chi_l}$ and $f(L, \nu) = \mathbb{E}_{\chi^* \sim (0, I_L)} \max_l \chi_l^*$. We assume constant length, i.e. $P_L$ is a delta. The loss of the linear attention is derived in part A.2.7:

$$ E_{\sigma} = 1 - \frac{1 + (\mathbb{E}f(L, \nu))^2}{\mathbb{E}L} . \qquad (89) $$

For large $L$ we have the asymptotic $f(L, \nu) = \mathcal{O}_{L \to \infty}(\sqrt{\log L})$, which gives

$$ E_{\sigma} = 1 - \mathcal{O}_{L \to \infty} \left( \frac{\log L}{L} \right) . \qquad (90) $$

The loss of the softmax attention is derived in part A.2.1. For the max-SLR it reads

$$ E_{\sigma} = 1 - \mathbb{E}_{\chi} \frac{1}{L} \sum_{\epsilon}^{L} \frac{g(\epsilon, \chi)^2}{\sum_{\epsilon'}^{L} g(\epsilon', \chi)} \qquad (91) $$

$$ = 1 - \mathbb{E}_{\chi} \frac{1}{L} \sum_{\epsilon}^{L} g(\epsilon, \chi) \qquad (92) $$

$$ = 0 \qquad (93) $$

The softmax attention reaches exact recovery for any $L$, and in particular for large $L$, the limit been taken after the limit $\nu = \infty$.

## B Bayes-optimal error at finite sample complexity $\alpha$

In this section we state the expression for the Bayes-optimal test error in the case where samples are limited, i.e. when $\alpha = N/D$ is finite, in the high-dimensional limit $N, D \to \infty$. The derivation of this expression is given in appendix C. We analyze behaviour of the BO performances and show that the SLR task presents a hard phase where best algorithmic performances cannot reach best information-theoretical performances. This hard phase is not present at $\alpha \to \infty$.

The Bayes-optimal performances can be expressed in function of two low-dimensional order parameters (or summary statistics, or sufficient statistics), $m_k \in \mathbb{R}$ and $m_v \in \mathbb{R}$. Writing $k_{BO} \in \mathbb{R}^D$ and $v_{BO} \in \mathbb{R}^D$ the BO estimators of $k^*$ and $v^*$, $m_k$ and $m_v$ are defined as

$$ m_k = \frac{1}{D} k_{BO}^{\top} k^* = \mathrm{angle}(k_{BO}, k^*)^2, \quad m_v = \frac{1}{D} v_{BO}^{\top} v^* = \mathrm{angle}(v_{BO}, v^*)^2. \qquad (94) $$

To state our asymptotic characterization result we introduce the following partition functions, for all $L$ and for $B, y \in \mathbb{R}$, $A, R, V \in \mathbb{R}^+$ and $\gamma, \omega \in \mathbb{R}^L$:

$$ Z_k(B, A) = \mathbb{E}_{k \sim \mathcal{N}(0,1)} e^{-\frac{1}{2}Ak^2 + Bk}, \quad Z_v(B, A) = \mathbb{E}_{v \sim \mathcal{N}(0,1)} e^{-\frac{1}{2}Av^2 + Bv} \qquad (95) $$

$$ Z_{\mathrm{out}}(y, \gamma, R, \omega, V) = \int_{\mathbb{R}^L} d\chi dz \frac{1}{L} \sum_{\epsilon}^{L} g_{\nu}(\epsilon, \chi) P_{\mathrm{out}}(y|z_{\epsilon}) \prod_{l}^{L} \mathcal{N}(\chi_l; \gamma_l, R) \mathcal{N}(z_l; \omega_l, V) \qquad (96) $$

where

$$ P_{\mathrm{out}}(y|z_{\epsilon}^*) = \frac{e^{-\frac{1}{2\Delta}(y-z_{\epsilon}^*)^2}}{\sqrt{2\pi\Delta}} $$

is the output channel that corresponds to an additive Gaussian noise. We set $h_\nu$ an effective distribution on $\epsilon \in \{1, \dots, L\}$ defined as

$$ h_{\nu}(\epsilon, \gamma, R) = \int_{\mathbb{R}^L} d\chi g_{\nu}(\epsilon, \chi) \prod_{l}^{L} \mathcal{N}(\chi_l; \gamma_l, R) . \qquad (97) $$

Last we set the function $x\log x : x \to x \log(x)$.
**Result B.1 (Bayes-optimal risk).** We consider the high-dimensional limit $N, D \to \infty$, with $\alpha = \Omega(1)$. Let $L \sim P_L$ and, conditionally on $L$, $\zeta \sim \mathcal{N}(0, 1)$, $\xi \sim \mathcal{N}(0, I_L)$ and $\zeta \sim \mathcal{N}(0, I_L)$. Fix $m_k$ and $m_v$ so they satisfy the following fixed-point condition:

$$m_k, m_v = \arg \max_{m_k, m_v} \phi_{\text{BO}}(m_k, m_v) \qquad (98)$$

$$\phi_{\text{BO}}(m_k, m_v) = \max_{\hat{m}_k \in \mathbb{R}, \hat{m}_v \in \mathbb{R}} \left[ -\frac{1}{2\alpha}(\hat{m}_k m_k + \hat{m}_v m_v) + \frac{1}{\alpha}\mathbb{E}_{\zeta} \mathrm{xlogx} Z_k \left(\sqrt{\hat{m}_k \zeta}, \hat{m}_k\right) \right. \\ \left. + \frac{1}{\alpha}\mathbb{E}_{\zeta} \mathrm{xlogx} Z_v \left(\sqrt{\hat{m}_v \zeta}, \hat{m}_v\right) + \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy \mathrm{xlogx} Z_{\text{out}}(y, \sqrt{m_k} \xi, 1-m_k, \sqrt{m_v} \zeta, 1-m_v) \right] \qquad (99)$$

$\phi_{\text{BO}}$ is the free entropy of the problem at given $m_k$ and $m_v$. Then the Bayes-optimal (BO) test error on inferring y is:

$$\mathcal{E}_{\text{BO}} = 1 - m_v \mathbb{E}_L \mathbb{E}_{\xi} \frac{1}{L} \left[ \sum_{\epsilon}^{L} h_{\nu}(\epsilon, \sqrt{m_k} \xi, 1-m_k) \right]^{-1} \sum_{\epsilon}^{L} h_{\nu}(\epsilon, \sqrt{m_k} \xi, 1-m_k)^2 . \qquad (100)$$

Our above result describes the information-theoretical (IT) best performance. In general it may not correspond to the algorithmic best performance. In fact, the SLR task presents an interesting phenomenology: in a whole region of the parameters $\alpha, \nu, L$ of the model, there is a gap between the IT and algorithmic achievable best performances. Such a discrepancy is related to the existence of several maxima in the free entropy $\phi_{\text{BO}}$ eq. (99) and has already been studied for other models in numerous previous works (Krzakala et al., 2012; Dia et al., 2016; Lesieur et al., 2017) and we refer to Bandeira et al. (2022) for a more rigorous treatment. The free entropy $\phi_{\text{BO}}$ plays a central role and it is related to the log-likelihood of the model; higher free entropy gives lower risk.

We can show that $\phi_{\text{BO}}$ admits one or two maxima. We introduce some notation to distinguish them. At given $\nu, L$ we call $\alpha_{\text{algo}}$ the algorithmic threshold (or spinodal) the smallest $\alpha$ such that for all $\alpha > \alpha_{\text{algo}}$, $\phi_{\text{BO}}$ has a unique maximum. In the region $\alpha < \alpha_{\text{algo}}$ where $\phi_{\text{BO}}$ admits two maxima, we set $\phi_{\text{BO}}^u$ the maximum whose basin of attraction includes a neighborhood of $m_k, m_v = 1, 1$ and call it informed maximum. We set $\phi_{\text{BO}}^u$ the maximum of $\phi_{\text{BO}}$ whose basin of attraction includes a neighborhood of $m_k, m_v = 0, 0$ and call it uninformed maximum; it describes the algorithmic best performances, in the sense that an algorithm locally optimizing $\phi_{\text{BO}}$ and starting without information on the solution will reach $\phi_{\text{BO}}^u$. In general $\phi_{\text{BO}}^u \neq \phi_{\text{BO}}^i$. We call $\alpha_{\text{IT}}$ the IT threshold the $\alpha$ such that $\phi_{\text{BO}}^i = \phi_{\text{BO}}^u$. We have $\alpha_{\text{IT}} \le \alpha_{\text{algo}}$ and we compute that for the SLR $\alpha_{\text{IT}} = 1$ for a noise-less output channel $\Delta = 0$.

To summarize, in the region $\alpha_{\text{IT}} < \alpha < \alpha_{\text{algo}}$ the free entropy $\phi_{\text{BO}}$ has two maxima; the global maximum describes the IT best performances but it cannot be reached from $m_k, m_v = 0, 0$ by local algorithms, that only reach a local maximum with higher risk. We depict this hard phase on Figure 5 for the spiked- and max-SLR.

Considering Figure 5 we can state that the extent of the hard phase $\alpha_{\text{algo}} - \alpha_{\text{IT}}$ decreases with the signal $\nu$ and grows with the sequence length $L$. At finite $L$ the upper limit of the hard phase $\alpha_{\text{algo}}$ is finite and consequently on population loss at $\alpha = +\infty$ the hard phase is not sensible. Notice that since we consider a noise-less setting $v^*$ can be exactly inferred at finite $\alpha$.

A possible explanation of why such a hard phase exists is that it is induced by the sparsity of the SLR. If one flatten each sequence into a single token $\tilde{X}$ of dimension $LD$ one would have a setting similar to compress sensing where $y = \tilde{X}^\top \tilde{v}^*$, where the regression vector $\tilde{v}^* \in \mathbb{R}^{LD}$ has a proportion $(L-1)/L$ of null entries. Compress sensing has been shown to present a similar hard phase (Krzakala et al., 2012). More generally sparsity induces hard phase, as shown for sparse PCA (Dia et al., 2016) or strongly unbalanced binary SBM (Caltagirone et al., 2016).

An interesting particular case is the 0-SLR when $\nu = 0$ and the tokens are iid Gaussians bringing no information alone. As shown on Figure 5 it is still possible to exactly recover $v^*$ and perform better than random. Indeed one can compare the train label $y$ to $X_l^\top v$ for each token $l$. If $v$ has some correlations with $v^*$ then $(y - X_l^\top v)^2$ is likely to be smaller for $l = \epsilon^*$. This gives some information on the probable relevant token. Then one can refine $v$ by focusing more on the right $X_l$. At inference time one cannot use the label to guess $\epsilon^*$ and the prediction is simply $\sum_l X_l^\top v$.
<figure><img src="image_6.png" /><figcaption>Figure 5: BO test error $\mathcal{E}_{BO}$, overlap $m_v$ with $v^*$ and overlap $m_k$ with $k^*$ for the spiked-SLR and the max-SLR models, for different signals $\nu$ and sequence lengths $L$, in the noise-less case $\Delta = 0$. The values are given by the eq. (100). The algorithmic best performances are given by the "uninformed" curve while for $\alpha_{IT} < \alpha < \alpha_{alg}$ the IT best performances are given by the "informed" curve. For $\alpha < \alpha_{IT}$ and $\alpha_{alg} < \alpha$ the two curves are equal.</figcaption></figure>

## C Asymptotic characterization at finite sampling ratio: replica

In this part we derive the asymptotic characterization of the Bayes-optimal performances and the performances of the trained attention, at finite sampling ratio $\alpha$. We jointly treat the two cases, the BO and the attention, and the two models, spiked-SLR and max-SLR, taking a variable sequence length. The derivation is done for a general output channel $P_{out}$ such that for each sample the label $y$ is distributed with density $P_{out}(y|z_{\epsilon^*})$. The additive Gaussian noise channel considered in the main part then corresponds to

$$P_{\text{out}}(y|z_{\epsilon^*}) = \frac{e^{-\frac{1}{2\Delta^2}(y-z_{\epsilon^*})^2}}{\sqrt{2\pi\Delta^2}}. \qquad (101)$$

Moreover the derivation is done for any convex loss function with strictly convex regularization.

### C.1 Introduction of the partition function

We consider data $X, y$ made of $N$ train samples, indexed by $\mu = 1, \dots, N$, together with $N' = \rho N$ test samples, with $\rho > 0$, indexed by $\mu = N + 1, \dots, N + N'$. They are distributed according to the generative model defined in the main part 3.1. We will take the limit $\rho \to 0$ at the end of the derivation, so no information can be extracted from the test samples $X_{N+1 \le \mu \le N+N'}$ in an unsupervised way by the Bayes-optimal estimator.
The expected test square error of the attention-based estimator is

$$ \mathbb{E}_{\sigma}(\alpha) = \mathbb{E}_{X,y} \frac{1}{N'} \sum_{\mu=N+1}^{N+N'} (y_{\mu} - f_{\sigma,k,v}(X_{\mu}))^2 \quad (102) $$

where

$$ f_{\sigma,k,v}(X_{\mu}) = \sigma \left( \frac{1}{\sqrt{D}} X_{\mu} k \right)^{\top} \frac{1}{\sqrt{D}} X_{\mu} v \quad (103) $$

$$ k, v = \arg \min_{k,v} \mathcal{L}(k, v) \quad (104) $$

$$ \mathcal{L}(k, v) = \sum_{\mu=1}^{N} \ell(y_{\mu}, f_{\sigma,k,v}(X_{\mu})) + r_k \sum_{i}^{D} \gamma(k_i) + r_v \sum_{i}^{D} \gamma(v_i) \quad (105) $$

with $\ell(x, x') = \frac{1}{2}(x - x')^2$ the square loss and $\gamma(x) = \frac{1}{2}x^2$ the $l_2$ regularization. Notice that the following derivation is done in full generality for any convex $\ell$ and strictly convex $\gamma$. We will specialize to ridge regression at the end of the derivation.

The expected test square error of the BO estimator is

$$ \mathcal{E}_{\text{BO}} = \mathbb{E}_{X,y} \frac{1}{N'} \sum_{\mu=N+1}^{N+N'} (y_{\mu} - \hat{y}_{\mu}^{\text{BO}})^2 = \mathbb{E}_{X,y} \frac{1}{N'} \sum_{\mu=N+1}^{N+N'} (y_{\mu}^2 - 2y_{\mu}\hat{y}_{\mu}^{\text{BO}} + (\hat{y}_{\mu}^{\text{BO}})^2) \quad (106) $$

where $\hat{y}_{\mu}^{\text{BO}}$ is the optimal estimator in terms of expected square error, that is, for a test sample $\mu' > N$:

$$ \hat{y}_{\mu'}^{\text{BO}} = \int dy_{\mu'} y_{\mu'} P(y_{\mu'} | X, y_{\mu \le N}) \quad (107) $$

Notice that, since $\hat{y}_{\mu'}^{\text{BO}}$ is sampled from the posterior distribution, we have the simplification $\mathbb{E}_{X,y}(\hat{y}_{\mu'}^{\text{BO}})^2 = \mathbb{E}_{X,y} y_{\mu'} \hat{y}_{\mu'}^{\text{BO}}$. We can expand the posterior distribution over the test labels as

$$ P(y_{\mu>N}|X, y_{\mu \le N}) = \int d\epsilon dk dv P(y_{\mu>N}, \epsilon, k, v|X, y_{\mu \le N}) \quad (108) $$

$$ \propto \int dP(\epsilon, k, v|X) P(y|X, \epsilon, k, v) \quad (109) $$

$$ \propto \int dP_k(k) dP_v(v) \prod_{\mu=1}^{N+N'} dP_{\epsilon}(\epsilon_{\mu}) g_{\nu} \left( \epsilon_{\mu}, \frac{1}{\sqrt{D}} X_{\mu} k \right) P \left( y_{\mu} \middle| \frac{1}{\sqrt{D}} (X_{\mu} v)_{\epsilon_{\mu}} \right) \quad (110) $$

with $P_k = P_v = \mathcal{N}(0, I_D)$ the prior distribution on $k$ and $v$ and, with an abuse of notation, $P_{\epsilon} = \text{Unif}\{(1, \dots, L(\mu))\}$ the prior on $\epsilon_{\mu}$. We used Bayes rule and $P(X|\epsilon, k, v) \propto g_{\nu}(\epsilon, Xk/\sqrt{D})P(\epsilon)$ to rewrite $P(\epsilon, k, v|X)$; and we used the independence of the samples $X_{\mu}, y_{\mu}$ conditional on $\epsilon_{\mu}, k$ and $v$ to factorize the expression.

We can express the two errors $\mathbb{E}_{\sigma}(\alpha)$ and $\mathcal{E}_{\text{BO}}$ under the same formalism thanks to a partition function and a free entropy over $k$ and $v$:

$$ Z(X, y) = \int d\tilde{P}_k(k) d\tilde{P}_v(v) \int \prod_{\mu=1}^{N} dP_{\epsilon}(\epsilon_{\mu}) \tilde{g}_{\nu} \left( \epsilon_{\mu}, \frac{1}{\sqrt{D}} X_{\mu} k \right) \tilde{P}(y_{\mu} | X_{\mu}, \epsilon_{\mu}, k, v) \quad (111) $$

$$ \int \prod_{\mu=N+1}^{N+N'} d\hat{y}_{\mu} dP_{\epsilon}(\epsilon_{\mu}) \tilde{g}_{\nu} \left( \epsilon_{\mu}, \frac{1}{\sqrt{D}} X_{\mu} k \right) \hat{P}(\hat{y}_{\mu} | X_{\mu}, \epsilon_{\mu}, k, v) e^{\sum_{\mu=N+1}^{N+N'} (sy_{\mu}(y_{\mu}-\hat{y}_{\mu})+t\beta(y_{\mu}-\hat{y}_{\mu})^2)} $$

$$ \phi = \frac{1}{N} \mathbb{E}_{X,y} \log Z(X, y) \quad (112) $$
where, depending on the estimator, the densities are taken according to

<table><thead><tr><th></th><th>Π&#x0302;<sub>k</sub>(k)</th><th>Π&#x0302;<sub>v</sub>(v)</th><th>g&#x0302;<sub>ν</sub></th><th>Π&#x0302;(y<sub>μ</sub>|X<sub>μ</sub>, ε<sub>μ</sub>, k, v)</th><th>Π&#x0302;(ŷ<sub>μ</sub>|X<sub>μ</sub>, ε<sub>μ</sub>, k, v)</th></tr></thead><tbody><tr><th>attention</th><td>e<sup>-βr<sub>k</sub> &sum;<sub>i</sub><sup>D</sup> &gamma;(k<sub>i</sub>)</sup></td><td>e<sup>-βr<sub>v</sub> &sum;<sub>i</sub><sup>D</sup> &gamma;(v<sub>i</sub>)</sup></td><td>1</td><td>e<sup>-βℓ(y<sub>μ</sub>, f<sub>σ,k,v</sub>(X<sub>μ</sub>))</sup></td><td>&delta;(ŷ<sub>μ</sub> - f<sub>σ,k,v</sub>(X<sub>μ</sub>))</td></tr><tr><th>BO</th><td>P<sub>k</sub>(k)</td><td>P<sub>v</sub>(v)</td><td>g<sub>ν</sub></td><td>P(y<sub>μ</sub>|X<sub>μ</sub>, ε<sub>μ</sub>, v)</td><td>P(ŷ<sub>μ</sub>|X<sub>μ</sub>, ε<sub>μ</sub>, v)</td></tr></tbody></table>

with $\beta \to \infty$, so for the attention $k$ and $v$ concentrate onto the minimizer of the empirical loss. We introduced tilting variables $s$ and $t$ to access the expected test errors, obtained according to

$$ \mathbb{E}_{\sigma}(\alpha) = \lim_{\beta \to \infty} \frac{\partial}{\partial t} \frac{1}{\rho \beta} \phi|_{s=0, t=0}, \qquad (113) $$

$$ \mathcal{E}_{\text{BO}} = \frac{\partial}{\partial s} \frac{1}{\rho} \phi|_{s=0, t=0}. \qquad (114) $$

## C.2 Replica, free entropy

We compute the free entropy $\phi$ in the high-dimensional limit $N, D \to \infty$. We use the replica trick:

$$ \mathbb{E}_{X,y} \log Z(X, y) = \partial_n (\mathbb{E}_{X,y} Z(X, y)^n)|_{n=0} \qquad (115) $$

We introduce $n$ replica, indexed by $a = 1, \dots, n$, and count the expectation over the data $X, y$ as an additional replica indexed by $a = 0$ corresponding to the ground truth. We equivalently use the superscript $*$ to denote the ground truth when it is clearer to do so. We use the shorthand $O_{\mu}^{a} = sy_{\mu}(y_{\mu} - \hat{y}_{\mu}^{a}) + t\beta(y_{\mu} - \hat{y}_{\mu}^{a})^2$ for the observables. This gives

$$ \mathbb{E}_{X,y} Z^n \propto \mathbb{E}_{X,y} \int \prod_{a=1}^{n} d\tilde{P}_k(k^a) d\tilde{P}_v(v^a) \prod_{\mu}^{N+N'} \prod_{a=1}^{n} dP_{\epsilon}(\epsilon_{\mu}^a) \tilde{g}_{\nu} \left( \epsilon_{\mu}^a, \frac{1}{\sqrt{D}} X_{\mu} k^a \right) \qquad (116) $$

$$ \prod_{\mu} \prod_{a=1}^{N} \tilde{P}^{a}(y_{\mu}|X_{\mu}, \epsilon_{\mu}^{a}, k^{a}, v^{a}) \prod_{\mu>N} \prod_{a=1}^{n} d\hat{y}_{\mu}^{a} \hat{P}(\hat{y}_{\mu}^{a}|X_{\mu}, \epsilon_{\mu}^{a}, k^{a}, v^{a}) e^{O_{\mu}^{a}} \\ \propto \mathbb{E}_{X \sim \mathcal{N}} \int \prod_{a=0}^{n} d\tilde{P}_{k}^{a}(k^{a}) d\tilde{P}_{v}^{a}(v^{a}) \prod_{\mu} \prod_{a=0}^{N+N'} dP_{\epsilon}(\epsilon_{\mu}^{a}) \tilde{g}_{\nu}^{a} \left( \epsilon_{\mu}^{a}, \frac{1}{\sqrt{D}} X_{\mu} k^{a} \right) \qquad (117) $$

$$ \prod_{\mu} \prod_{a=0}^{N} dy_{\mu} \prod_{\mu>N} \prod_{a=0}^{n} \tilde{P}^{a}(y_{\mu}|X_{\mu}, \epsilon_{\mu}^{a}, k^{a}, v^{a}) \prod_{\mu>N} dy_{\mu} P(y_{\mu}|X_{\mu}, \epsilon_{\mu}^{*}, v^{*}) \prod_{a=1}^{n} d\hat{y}_{\mu}^{a} \hat{P}(\hat{y}_{\mu}^{a}|X_{\mu}, \epsilon_{\mu}^{a}, k^{a}, v^{a}) e^{O_{\mu}^{a}} $$

where $\tilde{P}_k^a, \tilde{P}_v^a, \tilde{g}_\nu^a, \tilde{P}^a$ equals $P_k, P_v, g_\nu, P$ if $a = 0$ and $\tilde{P}_k, \tilde{P}_v, \tilde{g}_\nu, \tilde{P}$ otherwise. We now introduce the two projections

$$ \chi_{\mu}^{a} = \frac{1}{\sqrt{D}} X_{\mu} k^{a} \in \mathbb{R}^{L(\mu)}, \qquad z_{\mu}^{a} = \frac{1}{\sqrt{D}} X_{\mu} v^{a} \in \mathbb{R}^{L(\mu)} \qquad (118) $$
thanks to Dirac deltas. We integrate over Gaussian X. We pack the replica into vectors of size $n + 1$. This gives

$$ \begin{aligned} \mathbb{E}_{X,y} Z^n \propto & \mathbb{E}_{X \sim \mathcal{N}} \int \prod_{a=0}^{n} d\tilde{P}_k^a(k^a) d\tilde{P}_v^a(v^a) \prod_{\mu}^{N+N'} \prod_{a=0}^{n} d\chi_{\mu}^a d\hat{\chi}_{\mu}^a dz_{\mu}^a d\hat{z}_{\mu}^a dP_{\epsilon}(\epsilon_{\mu}^a) \tilde{g}_{\nu}^a (\epsilon_{\mu}^a, \chi_{\mu}^a) \\ & \prod_{\mu}^{N} dy_{\mu} \prod_{a=0}^{n} \tilde{P}^a(y_{\mu}|z_{\mu}^a, \chi_{\mu}^a, \epsilon_{\mu}^a) \prod_{\mu>N} dy_{\mu} P(y_{\mu}|z_{\mu}^*, \epsilon_{\mu}^*) \prod_{a=1}^{n} d\hat{y}_{\mu}^a \hat{P}(\hat{y}_{\mu}^a|z_{\mu}^a, \chi_{\mu}^a, \epsilon_{\mu}^a) e^{O_{\mu}^a} \\ & \prod_{a=0}^{n} \prod_{\mu}^{N+N'} e^{\sum_{l}^{L(\mu)} i \hat{\chi}_{\mu,l}^a (\chi_{\mu,l}^a - \frac{1}{\sqrt{D}} X_{\mu,l} k^a) + i \hat{z}_{\mu,l}^a (z_{\mu,l}^a - \frac{1}{\sqrt{D}} X_{\mu,l} v^a)} \end{aligned} \quad (119) $$

$$ \begin{aligned} & \propto \int \prod_{a=0}^{n} d\tilde{P}_k^a(k^a) d\tilde{P}_v^a(v^a) \prod_{\mu}^{N+N'} dP_L(L(\mu)) \prod_{a=0}^{n} d\chi_{\mu}^a dz_{\mu}^a dP_{\epsilon}(\epsilon_{\mu}^a) \tilde{g}_{\nu}^a (\epsilon_{\mu}^a, \chi_{\mu}^a) \\ & \prod_{\mu}^{N} dy_{\mu} \prod_{a=0}^{n} \tilde{P}^a(y_{\mu}|z_{\mu}^a, \chi_{\mu}^a, \epsilon_{\mu}^a) \prod_{\mu>N} dy_{\mu} P(y_{\mu}|z_{\mu}^*, \epsilon_{\mu}^*) \prod_{a=1}^{n} d\hat{y}_{\mu}^a \hat{P}(\hat{y}_{\mu}^a|z_{\mu}^a, \chi_{\mu}^a, \epsilon_{\mu}^a) e^{O_{\mu}^a} \\ & \prod_{\mu}^{N+N'} \prod_{l}^{L(\mu)} \mathcal{N} \left( \begin{pmatrix} \chi_{\mu,l} \\ \hat{z}_{\mu,l} \end{pmatrix} ; 0, \begin{pmatrix} 1 & \frac{\underline{m}_{kk}^*}{Q_{kk}} & 0 & \frac{\underline{m}_{vk}^*}{Q_{kv}} \\ \frac{\underline{m}_{kk}^*}{Q_{kk}} & \frac{\underline{m}_{kv}^*}{Q_{kv}} & 1 & \frac{\underline{m}_{vv}^*}{Q_{vv}} \\ 0 & \frac{\underline{m}_{kv}^*}{Q_{kv}} & \frac{\underline{m}_{vv}^*}{Q_{vv}} & 1 \end{pmatrix} \right) \end{aligned} \quad (120) $$

where we used that $(k^*)^\top k^*/D = (v^*)^\top v*/D = 1$, $(k^*)^\top v^*/D = 0$ and we introduced the overlaps $\underline{m}_{kk^*}$, $\underline{m}_{vk^*}$, $\underline{m}_{kv^*}$, $\underline{m}_{vv^*} \in \mathbb{R}^n$ and $\underline{Q}_{kk}$, $\underline{Q}_{kv}$, $\underline{Q}_{vv} \in \mathbb{R}^{n \times n}$ defined for $a, b > 0$ by

$$ (\underline{m}_{kk^*})_a = \frac{1}{D}(k^*)^\top k^a, \quad (\underline{m}_{vk^*})_a = \frac{1}{D}(k^*)^\top v^a, \quad (\underline{m}_{kv^*})_a = \frac{1}{D}(v^*)^\top k^a, \quad (\underline{m}_{vv^*})_a = \frac{1}{D}(v^*)^\top v^a \quad (121) $$

$$ (\underline{Q}_{kk})_{ab} = \frac{1}{D}(k^a)^\top k^b, \quad (\underline{Q}_{kv})_{ab} = \frac{1}{D}(v^a)^\top k^b, \quad (\underline{Q}_{vv})_{ab} = \frac{1}{D}(v^a)^\top v^b \quad (122) $$

We introduce these overlaps via new Dirac deltas. We leverage the replica-symmetric assumption: we assume that there are $m_{kk^*}$, $m_{vk^*}$, $m_{kv^*}$, $m_{vv^*}$, $q_{kk}$, $q_{kv}$, $q_{vv}$, $Q_{kk}$, $Q_{kv}$, $Q_{vv} \in \mathbb{R}$ such that for all $a$ and $b$

$$ (\underline{m}_{kk^*})_a = m_{kk^*}, \quad (\underline{m}_{vk^*})_a = m_{vk^*}, \quad (\underline{m}_{kv^*})_a = m_{kv^*}, \quad (\underline{m}_{vv^*})_a = m_{vv^*} \quad (123) $$

$$ (\underline{Q}_{kk})_{ab} = q_{kk} \delta_{a \neq b} + Q_{kk} \delta_{a=b}, \quad (\underline{Q}_{kv})_{ab} = q_{kv} \delta_{a \neq b} + Q_{kv} \delta_{a=b}, \quad (\underline{Q}_{vv})_{ab} = q_{vv} \delta_{a \neq b} + Q_{vv} \delta_{a=b} \quad (124) $$

We pack these values into matrices

$$ m = \begin{pmatrix} m_{kk^*} & m_{kv^*} \\ m_{vk^*} & m_{vv^*} \end{pmatrix} \in \mathbb{R}^{2 \times 2}, \quad q = \begin{pmatrix} q_{kk} & q_{kv} \\ q_{kv} & q_{vv} \end{pmatrix} \in \mathbb{R}^{2 \times 2}, \quad Q = \begin{pmatrix} Q_{kk} & Q_{kv} \\ Q_{kv} & Q_{vv} \end{pmatrix} \in \mathbb{R}^{2 \times 2} \quad (125) $$

We factorize the replica by introducing random Gaussian variables $\zeta \sim \mathcal{N}(0, I_2)$, $\xi \sim \mathcal{N}(0, I_L)$ and $\zeta \sim \mathcal{N}(0, I_L)$. After a standard computation (see e.g. Aubin et al. (2020)) we obtain that the free entropy can be expressed as an extremum over the order parameters $\Theta = (m, q, Q)$ and their conjugates $\hat{\Theta} = (\hat{m} \in \mathbb{R}^{2 \times 2}, \hat{q} \in \mathbb{R}^{2 \times 2}, \hat{Q} \in \mathbb{R}^{2 \times 2})$:

$$ \phi = \max_{\Theta, \hat{\Theta}} \phi(\Theta, \hat{\Theta}) \quad (126) $$

$$ \begin{aligned} \phi(\Theta, \hat{\Theta}) = & -\frac{1}{\alpha} \operatorname{Tr} \hat{m}^{\top} m + \frac{1}{2\alpha} (\operatorname{Tr} \hat{q}q + \operatorname{Tr} \hat{Q}Q) \\ & + \frac{1}{\alpha} \mathbb{E}_{\zeta} Z_{kv}^{*} (\hat{m}^{\top} \hat{q}^{-1/2} \zeta, \hat{m}^{\top} \hat{q}^{-1} \hat{m}) \log Z_{kv} (\hat{q}^{1/2} \zeta, \hat{q} + \hat{Q}) \\ & + \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy Z_{\text{out}}^{*} (y, mq^{-1/2} (\xi^{\top}), 1 - m^{\top}qm) \log Z_{\text{out}} (y, q^{1/2} (\xi^{\top}), Q - q) \\ & + \rho \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy Z_{\text{out}}^{*} (y, mq^{-1/2} (\xi^{\top}), 1 - m^{\top}qm) \log Z'_{\text{out}} (y, q^{1/2} (\xi^{\top}), Q - q) \end{aligned} \quad (127) $$
with the partition functions

$$Z_{kv}^*(B, A) = \int_{\mathbb{R}} dP_k(k^*) P_v(v^*) e^{-\frac{1}{2} (k_v^*)^\top A (k_v^*) + B^\top (k_v^*)}, \quad (128)$$

$$Z_{kv}(B, A) = \int_{\mathbb{R}} d\tilde{P}_k(k) \tilde{P}_v(v) e^{-\frac{1}{2} (k_v)^\top A (k_v)}, + B^\top (k_v) \quad (129)$$

$$Z_{\text{out}}^*(y, \omega, V) = \int_{\mathbb{R}^L} d\chi^* dz^* \int dP_\epsilon(\epsilon^*) g_\nu(\epsilon^*, \chi^*) P_{\text{out}}(y|z_{\epsilon^*}) \prod_l^L \mathcal{N}\left((\chi_l^*; \omega_l, V)\right) \quad (130)$$

$$Z_{\text{out}}(y, \omega, V) = \int_{\mathbb{R}^L} d\chi dz \int dP_\epsilon(\epsilon) \tilde{g}_\nu(\epsilon, \chi) \tilde{P}(y|z, \chi, \epsilon) \prod_l^L \mathcal{N}\left((\chi_l^*; \omega_l, V)\right) \quad (131)$$

$$Z'_{\text{out}}(\omega, V) = \int_{\mathbb{R}} d\hat{y} e^O \int_{\mathbb{R}^L} d\chi dz \int dP_\epsilon(\epsilon) \tilde{g}_\nu(\epsilon, \chi) \hat{P}(\hat{y}|z, \chi, \epsilon) \prod_l^L \mathcal{N}\left((\chi_l^*; \omega_l, V)\right) \quad (132)$$

and the observables $O = sy(y - \hat{y}) + t\beta(y - \hat{y})^2$.

This expression of the free entropy can be simplified assuming that the order parameters $m, q, Q$ are diagonal. We numerically extremized $\phi$ for the full order parameters from uninformed and informed initializations and we observed that the cross-terms $m_{kv^*}, m_{vk^*}, q_{kv}, Q_{kv}$ and their conjugates go to 0. This simplification is similar to the one discussed in the main part for the population loss, though there is no direct relation with gradient descent and that we have the additional order parameter $Q$. We simplify the notations setting

$$m_k = m_{kk^*} \qquad q_k = q_{kk^*} \qquad Q_k = Q_{kk^*} \quad (133)$$

$$m_v = m_{kk^*} \qquad q_v = q_{vv^*} \qquad Q_v = Q_{vv^*} \quad (134)$$

and similarly for the conjugates. The set of order parameters becomes

$$\Theta = (m_k, \hat{m}_k, m_v, \hat{m}_v, q_k, \hat{q}_k, q_v, \hat{q}_v, Q_k, \hat{Q}_k, Q_v, \hat{Q}_v) \in \mathbb{R}^{12}.$$

The free entropy is now

$$\begin{aligned} \phi(\Theta) = & -\frac{1}{\alpha}(\hat{m}_k m_k + \hat{m}_v m_v) + \frac{1}{2\alpha}(\hat{q}_k q_k + \hat{Q}_k Q_k + \hat{q}_v q_v + \hat{Q}_v Q_v) \\ & + \frac{1}{\alpha} \mathbb{E}_\zeta Z_k^* \left( \frac{\hat{m}_k}{\sqrt{\hat{q}_k}} \zeta, \frac{\hat{m}_k^2}{\hat{q}_k} \right) \log Z_k \left( \sqrt{\hat{q}_k} \zeta, \hat{q}_k + \hat{Q}_k \right) \\ & + \frac{1}{\alpha} \mathbb{E}_\zeta Z_v^* \left( \frac{\hat{m}_v}{\sqrt{\hat{q}_v}} \zeta, \frac{\hat{m}_v^2}{\hat{q}_v} \right) \log Z_v \left( \sqrt{\hat{q}_v} \zeta, \hat{q}_v + \hat{Q}_v \right) \\ & + \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy Z_{\text{out}}^* \left( y, \frac{m_k}{\sqrt{q_k}} \zeta, 1 - \frac{m_k^2}{q_k}, \frac{m_v}{\sqrt{q_v}} \zeta, 1 - \frac{m_v^2}{q_v} \right) \log Z_{\text{out}} \left( y, \sqrt{q_v} \zeta, Q_k - q_k, \sqrt{q_v} \zeta, Q_v - q_v \right) \\ & + \rho \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy Z_{\text{out}}^* \left( y, \frac{m_k}{\sqrt{q_k}} \zeta, 1 - \frac{m_k^2}{q_k}, \frac{m_v}{\sqrt{q_v}} \zeta, 1 - \frac{m_v^2}{q_v} \right) \log Z'_{\text{out}} \left( \sqrt{q_v} \zeta, Q_k - q_k, \sqrt{q_v} \zeta, Q_v - q_v \right) \end{aligned} \quad (135)$$

with $\zeta \sim \mathcal{N}(0, 1)$ and the partition functions

$$Z_k^*(B, A) = \int_{\mathbb{R}} dP_k(k^*) e^{-\frac{1}{2} A(k^*)^2 + Bk^*}, \qquad Z_k(B, A) = \int_{\mathbb{R}} d\tilde{P}_k(k) e^{-\frac{1}{2} Ak^2 + Bk} \quad (136)$$

$$Z_v^*(B, A) = \int_{\mathbb{R}} dP_v(v^*) e^{-\frac{1}{2} A(v^*)^2 + Bv^*}, \qquad Z_v(B, A) = \int_{\mathbb{R}} d\tilde{P}_v(v) e^{-\frac{1}{2} Av^2 + Bv} \quad (137)$$

$$Z_{\text{out}}^*(y, \gamma, R, \omega, V) = \int_{\mathbb{R}^L} d\chi^* dz^* \int dP_\epsilon(\epsilon^*) g_\nu(\epsilon^*, \chi^*) P_{\text{out}}(y|z_{\epsilon^*}) \prod_l^L \mathcal{N}(\chi_l^*; \gamma_l, R) \mathcal{N}(z_l^*; \omega_l, V) \quad (138)$$

$$Z_{\text{out}}(y, \gamma, R, \omega, V) = \int_{\mathbb{R}^L} d\chi dz \int dP_\epsilon(\epsilon) \tilde{g}_\nu(\epsilon, \chi) \tilde{P}(y|z, \chi, \epsilon) \prod_l^L \mathcal{N}(\chi_l; \gamma_l, R) \mathcal{N}(z_l; \omega_l, V) \quad (139)$$

$$Z'_{\text{out}}(\gamma, R, \omega, V) = \int_{\mathbb{R}} d\hat{y} e^O \int_{\mathbb{R}^L} d\chi dz \int dP_\epsilon(\epsilon) \tilde{g}_\nu(\epsilon, \chi) \hat{P}(\hat{y}|z, \chi, \epsilon) \prod_l^L \mathcal{N}(\chi_l; \gamma_l, R) \mathcal{N}(z_l; \omega_l, V) \quad (140)$$
Once $\phi$ is extremized over the order parameters the test errors are computed as

$$ \mathbb{E}_{\sigma}(\alpha) = \frac{1}{\beta} \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy (Z'_{\text{out}})^{-1} Z_{\text{out}} \partial_t Z'_{\text{out}}|_{s=0, t=0} \quad (141) $$

$$ \mathcal{E}_{\text{BO}} = \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy (Z'_{\text{out}})^{-1} Z_{\text{out}} \partial_s Z'_{\text{out}}|_{s=0, t=0} \quad (142) $$

### C.3 Specialization to the BO

We consider the BO setting, where $Z_k^* = Z_k$, $Z_v^* = Z_v$ and $Z_{\text{out}}^* = Z_{\text{out}}$. A main simplification, known as Nishimori condition, is given by the fact the BO estimator is sampling the posterior distribution: we have $m_k = q_k$, $\hat{m}_k = \hat{q}_k$, $Q_k = 1$, $m_v = q_v$, $\hat{m}_v = \hat{q}_v$ and $Q_v = 1$. We set the function xlog: $x \to x \log(x)$. The resulting free entropy is

$$ \begin{aligned} \phi = & -\frac{1}{2\alpha}(\hat{m}_k m_k + \hat{m}_v m_v) + \frac{1}{\alpha}\mathbb{E}_{\zeta} \mathrm{xlog} Z_k(\sqrt{\hat{m}_k \zeta}, \hat{m}_k) + \frac{1}{\alpha}\mathbb{E}_{\zeta} \mathrm{xlog} Z_v(\sqrt{\hat{m}_v \zeta}, \hat{m}_v) \quad (143) \\ & + \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy \mathrm{xlog} Z_{\text{out}}(y, \sqrt{m_k} \xi, 1-m_k, \sqrt{m_v} \zeta, 1-m_v) \\ & + \rho \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy Z_{\text{out}}(y, \sqrt{m_k} \xi, 1-m_k, \sqrt{m_v} \zeta, 1-m_v) \log Z'_{\text{out}}(\sqrt{m_k} \xi, 1-m_k, \sqrt{m_v} \zeta, 1-m_v) \end{aligned} $$

The extremality condition of $\phi$ over the order parameters is obtained by setting its gradient to zero. We take the limit $\rho = 0$. It gives the following fixed-point equations:

$$ m_k = \mathbb{E}_{\zeta} Z_k^{-1} (\partial_B Z_k)^2 \quad (144) $$

$$ m_v = \mathbb{E}_{\zeta} Z_v^{-1} (\partial_B Z_v)^2 \quad (145) $$

$$ \hat{m}_k = \alpha \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy Z_{\text{out}}^{-1} (\nabla_{\gamma} Z_{\text{out}})^{\top} (\nabla_{\gamma} Z_{\text{out}}) \quad (146) $$

$$ \hat{m}_v = \alpha \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy Z_{\text{out}}^{-1} (\nabla_{\omega} Z_{\text{out}})^{\top} (\nabla_{\omega} Z_{\text{out}}) \quad (147) $$

We explicit the fixed-point equations. We set

$$ h_{\nu}(\epsilon, \gamma, R) = \int_{\mathbb{R}^L} d\chi g_{\nu}(\epsilon, \chi) \prod_{l}^{L} \mathcal{N}(\chi_l; \gamma_l, R) \quad (148) $$

the effective distribution over $\epsilon$. We assume that $P_{\text{out}}$ is the identity channel. Then

$$ m_k = \frac{\hat{m}_k}{1 + \hat{m}_k} \quad (149) $$

$$ m_v = \frac{\hat{m}_v}{1 + \hat{m}_v} \quad (150) $$

$$ \hat{m}_k = \alpha \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy \left[ \sum_{\epsilon} h_{\nu}(\epsilon, \sqrt{m_k} \xi, 1-m_k) \frac{e^{-\frac{(y-\sqrt{m_v \zeta \epsilon})^2}{2(1-m_v)}}}{\sqrt{2\pi(1-m_v)}} \right]^{-1} \quad (151) $$

$$ \frac{1}{L} \sum_{l}^{L} \left[ \sum_{\epsilon} \partial_{\gamma_l} h_{\nu}(\epsilon, \sqrt{m_k} \xi, 1-m_k) \frac{e^{-\frac{(y-\sqrt{m_v \zeta \epsilon})^2}{2(1-m_v)}}}{\sqrt{2\pi(1-m_v)}} \right]^2 \quad (152) $$

$$ \hat{m}_v = \alpha \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int_{\mathbb{R}} dy \left[ \sum_{\epsilon} h_{\nu}(\epsilon, \sqrt{m_k} \xi, 1-m_k) \frac{e^{-\frac{(y-\sqrt{m_v \zeta \epsilon})^2}{2(1-m_v)}}}{\sqrt{2\pi(1-m_v)}} \right]^{-1} \quad (152) $$

$$ \frac{1}{L} \sum_{l}^{L} \left[ h_{\nu}(l, \sqrt{m_k} \xi, 1-m_k) \frac{y - \sqrt{m_v \zeta_l}}{1-m_v} \frac{e^{-\frac{(y-\sqrt{m_v \zeta_l})^2}{2(1-m_v)}}}{\sqrt{2\pi(1-m_v)}} \right]^2 \quad (152) $$
At the fixed-point the Bayes-optimal error is given by

$$ \mathcal{E}_{\text{BO}} = 1 - m_v \mathbb{E}_L \mathbb{E}_{\xi} \frac{1}{L} \left[ \sum_{\epsilon} h_{\nu}(\epsilon, \sqrt{m_k} \xi, 1 - m_k) \right]^{-1} \sum_{\epsilon} h_{\nu}(\epsilon, \sqrt{m_k} \xi, 1 - m_k)^2 \quad (153) $$

### C.3.1 Specialization to spiked-SLR

The spiked-SLR is defined by taking $g_{\nu}(\epsilon, \chi) = e^{\sqrt{\nu}\chi_{\epsilon} - \frac{\nu}{2}}$. We can integrate over $\chi$ and explicitly compute $h_{\nu}$:

$$ h_{\nu}(\epsilon, \gamma, R) = e^{\sqrt{\nu}\gamma_{\epsilon} + \frac{1}{2}\nu(R-1)}. \quad (154) $$

Thanks to the invariance by permutation of the tokens we isolate the index $l = 1$. We change the variables $\xi_1 \to \xi_1 + \sqrt{\nu m_k}$ and $\zeta_1 \to \sqrt{1 - m_v \zeta_1} + \sqrt{m_v y}$ to obtain expressions that are easier to compute numerically. This gives

$$ m_k = \frac{\hat{m}_k}{1 + \hat{m}_k}, \qquad m_v = \frac{\hat{m}_v}{1 + \hat{m}_v} \quad (155) $$

$$ \hat{m}_k = \alpha \nu \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int \frac{dy}{\sqrt{2\pi}} e^{-\frac{1}{2}y^2} \frac{1}{1 + \sum_{l>1}^L e^{-\nu m_k + \sqrt{\nu m_k}(\xi_l - \xi_1) - \frac{(y-\sqrt{\nu m_v \zeta_l})^2}{2(1-m_v)}} + \frac{1}{2}(\sqrt{1-m_v y} - \sqrt{m_v \zeta_1})^2} \quad (156) $$

$$ \hat{m}_v = \frac{\alpha}{1 - m_v} \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int \frac{dy}{\sqrt{2\pi}} e^{-\frac{1}{2}y^2} \frac{(\sqrt{1 - m_v y} - \sqrt{m_v \zeta_1})^2}{1 + \sum_{l>1}^L e^{-\nu m_k + \sqrt{\nu m_k}(\xi_l - \xi_1) - \frac{(y-\sqrt{m_v \zeta_l})^2}{2(1-m_v)}} + \frac{1}{2}(\sqrt{1-m_v y} - \sqrt{m_v \zeta_1})^2} \quad (157) $$

$$ \mathcal{E}_{\text{BO}} = 1 - m_v e^{-\frac{\nu m_k}{2}} \mathbb{E}_L \mathbb{E}_{\xi} \frac{e^{\sqrt{\nu m_k} \xi_1}}{1 + \sum_{l>1}^{L} e^{\sqrt{\nu m_k} (\xi_l - \xi_1)}} \quad (158) $$

### C.3.2 Specialization to the max-SLR model

The max-SLR model is defined by taking $g_{\nu}(\epsilon, \chi) = L e^{\nu \chi_{\epsilon}} / (\sum_l e^{\nu \chi_l})$. $\chi$ cannot be integrated out. We consider the limit $\nu = +\infty$ to simplify the expression of $h_{\nu}$:

$$ h_{\nu}(\epsilon, \gamma, R) = L \int_{\mathbb{R}} d\chi \mathcal{N}(\chi; \gamma_{\epsilon}, R) \prod_{l \neq \epsilon} \frac{1}{2} \left( 1 + \operatorname{erf} \frac{\chi - \gamma_l}{\sqrt{2R}} \right). \quad (159) $$

Thanks to the invariance by permutation of the tokens we isolate the indices $l = 1$ and $l = 2$. We can still change variables $\zeta_{\epsilon} \to \sqrt{1 - m_v \zeta_{\epsilon}} + \sqrt{m_v y}$ to obtain expressions that are easier to compute numerically. This
gives

$$m_k = \frac{\hat{m}_k}{1 + \hat{m}_k}, \quad m_v = \frac{\hat{m}_v}{1 + \hat{m}_v} \tag{160}$$

$$\hat{m}_k = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int \frac{dy}{\sqrt{2\pi}} e^{-\frac{1}{2}y^2} \left[ \left( \partial_{\gamma_1} h_{\nu}(1, \sqrt{m_k}\xi, 1-m_k) \right)^2 e^{-\frac{1}{2}\left(\sqrt{1-m_v}y - \sqrt{m_v}\zeta_1\right)^2} \right. \\ \left. + 2(L-1)\partial_{\gamma_1} h_{\nu}(1, \sqrt{m_k}\xi, 1-m_k) \partial_{\gamma_1} h_{\nu}(2, \sqrt{m_k}\xi, 1-m_k) e^{-\frac{\left(y-\sqrt{m_v}\zeta_2\right)^2}{2(1-m_v)}} \right] \tag{161}$$

$$\left[ h_{\nu}(1, \sqrt{m_k}\xi, 1-m_k) e^{-\frac{1}{2}\left(\sqrt{1-m_v}y - \sqrt{m_v}\zeta_1\right)^2} + \sum_{l>1}^{L} h_{\nu}(l, \sqrt{m_k}\xi, 1-m_k) e^{-\frac{\left(y-\sqrt{m_v}\zeta_l\right)^2}{2(1-m_v)}} \right]^{-1}$$

$$+ \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int \frac{dy}{\sqrt{2\pi}} e^{-\frac{1}{2}y^2} \left[ (L-1)(\partial_{\gamma_1} h_{\nu}(2, \sqrt{m_k}\xi, 1-m_k))^2 e^{-\frac{1}{2}\left(\sqrt{1-m_v}y - \sqrt{m_v}\zeta_2\right)^2} \right. \\ \left. + (L^2 - 3L + 2)\partial_{\gamma_1} h_{\nu}(2, \sqrt{m_k}\xi, 1-m_k) \partial_{\gamma_1} h_{\nu}(3, \sqrt{m_k}\xi, 1-m_k) e^{-\frac{\left(y-\sqrt{m_v}\zeta_3\right)^2}{2(1-m_v)}} \right]$$

$$\left[ h_{\nu}(2, \sqrt{m_k}\xi, 1-m_k) e^{-\frac{1}{2}\left(\sqrt{1-m_v}y - \sqrt{m_v}\zeta_2\right)^2} + \sum_{l \neq 2}^{L} h_{\nu}(l, \sqrt{m_k}\xi, 1-m_k) e^{-\frac{\left(y-\sqrt{m_v}\zeta_l\right)^2}{2(1-m_v)}} \right]^{-1}$$

$$\hat{m}_v = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int \frac{dy}{\sqrt{2\pi}} e^{-\frac{1}{2}y^2} \frac{\left(\sqrt{1-m_v}y - \sqrt{m_v}\zeta_1\right)^2}{1-m_v} h_{\nu}(1, \sqrt{m_k}\xi, 1-m_k) e^{-\frac{1}{2}\left(\sqrt{1-m_v}y - \sqrt{m_v}\zeta_1\right)^2}$$

$$\left[ h_{\nu}(1, \sqrt{m_k}\xi, 1-m_k) e^{-\frac{1}{2}\left(\sqrt{1-m_v}y - \sqrt{m_v}\zeta_1\right)^2} + \sum_{l>1}^{L} h_{\nu}(l, \sqrt{m_k}\xi, 1-m_k) e^{-\frac{\left(y-\sqrt{m_v}\zeta_l\right)^2}{2(1-m_v)}} \right]^{-1} \tag{162}$$

$$\mathcal{E}_{\text{BO}} = 1 - m_v \mathbb{E}_L \mathbb{E}_{\xi} \frac{1}{L} h_{\nu}(1, \sqrt{m_k}\xi, 1-m_k)^2 \tag{163}$$

## C.4 Specialization to the attention

We consider the attention case, where the distributions $\tilde{P}_k$, $\tilde{P}_v$ and $\tilde{P}(y|X, \epsilon, k, v)$ do not match the distributions of the model. In this case it is more convenient to work with the variables

$$V_k = Q_k - q_k, \quad \hat{V}_k = \hat{Q}_k + \hat{q}_k, \quad V_v = Q_v - q_v, \quad \hat{V}_v = \hat{Q}_v + \hat{q}_v. \tag{164}$$

We perform the changes $\zeta_k \to \zeta_k + k^* \hat{m}_k / \sqrt{\hat{q}_k}$, $\zeta_v \to \zeta_v + v^* \hat{m}_v / \sqrt{\hat{q}_v}$, $\xi_l \to \xi_l \sqrt{q_k - m_k^2 / \sqrt{q_k}} + \chi_l^* m_k / \sqrt{q_k}$ and $\zeta_l \to \zeta_l \sqrt{q_v - m_v^2 / \sqrt{q_v}} + z_l^* m_v / \sqrt{q_v}$. We deal with the limit $\beta \to \infty$ by rescaling the parameters according to $\hat{m}_k \to \beta \hat{m}_k$, $\hat{m}_v \to \beta \hat{m}_v$, $\hat{q}_k \to \beta^2 \hat{q}_k$, $\hat{q}_v \to \beta^2 \hat{q}_v$, $\hat{V}_k \to \beta \hat{V}_k$, $\hat{V}_v \to \beta \hat{V}_v$ and $V_k \to \beta^{-1} V_k$, $V_v \to \beta^{-1} V_v$.

We introduce the effective joint distribution of the data, for $y \in \mathbb{R}$, $\epsilon^* \in \{1, \dots, L\}$, $\chi^* \in \mathbb{R}^L$ and $z^* \in \mathbb{R}^L$:

$$P^*(y, \epsilon^*, \chi^*, z^*) = \frac{1}{L} g_{\nu}(\epsilon^*, \chi^*) P_{\text{out}}(y|z_{\epsilon^*}^*) \prod_{l}^{L} \mathcal{N}(\chi_l^*; 0, 1) \mathcal{N}(z_l^*; 0, 1). \tag{165}$$

The free entropy is then

$$\begin{aligned} \frac{1}{\beta} \phi(\Theta) = & -\frac{1}{\alpha}(\hat{m}_k m_k + \hat{m}_v m_v) + \frac{1}{2\alpha} \left( \frac{1}{\beta} V_k \hat{V}_k + q_k \hat{V}_k - V_k \hat{q}_k + \frac{1}{\beta} V_v \hat{V}_v + q_v \hat{V}_v - V_v \hat{q}_v \right) \\ & + \frac{1}{\alpha \beta} \mathbb{E}_{\zeta} \int_{\mathbb{R}} dP_k(k^*) \log \int_{\mathbb{R}} dk e^{\beta \psi_k(k)} + \frac{1}{\alpha \beta} \mathbb{E}_{\zeta} \int_{\mathbb{R}} dP_v(v^*) \log \int_{\mathbb{R}} dv e^{\beta \psi_v(v)} \\ & + \frac{1}{\beta} \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int dP^*(y, \epsilon^*, \chi^*, z^*) \log \int_{\mathbb{R}^L} d\chi dz \left( e^{\beta \psi_{\text{out}}(\chi, z; 1)} + \rho e^{\beta \psi_{\text{out}}(\chi, z; 0) + t\beta(y - \hat{y}_{\sigma}(z, \chi))^2} \right) \end{aligned} \tag{166}$$
with the potentials

$$ \psi_k(k) = -r_k \gamma(k) - \frac{1}{2} \hat{V}_k k^2 + (\hat{m}_k k^* + \sqrt{\hat{q}_k} \zeta) k \quad (167) $$

$$ \psi_v(v) = -r_v \gamma(v) - \frac{1}{2} \hat{V}_v v^2 + (\hat{m}_v v^* + \sqrt{\hat{q}_v} \zeta) v \quad (168) $$

$$ \psi_{\text{out}}(\chi, z; \bar{t}) = -\bar{t}\ell(y, \hat{y}_{\sigma}(z, \chi)) + \sum_{l}^{L} \log \mathcal{N}(\chi_l; \gamma_l, V_k) + \sum_{l}^{L} \log \mathcal{N}(z_l; \omega_l, V_v) \quad (169) $$

$$ \gamma = m_k \chi^* + \sqrt{q_k - m_k^2} \xi, \quad \omega = m_v z^* + \sqrt{q_v - m_v^2} \zeta \quad (170) $$

$\bar{t} \in \{0, 1\}$ controls whether the loss or the observable are active or not. We introduce the extremizers of these potentials:

$$ k' = \arg \max_k \psi_k(k), \qquad v' = \arg \max_v \psi_v(v) \quad (171) $$

$$ \chi', z' = \arg \max_{\chi, z} \psi_{\text{out}}(\chi, z; \bar{t} = 1), \qquad \chi'', z'' = \arg \max_{\chi, z} \psi_{\text{out}}(\chi, z; \bar{t} = 0) \quad (172) $$

We introduce the covariances under these potentials around there maxima, with $\nabla^2$ the Hessian.

$$ \text{Cov}(k) = - (\nabla^2 \psi_k(k'))^{-1}, \qquad \text{Cov}(v) = - (\nabla^2 \psi_v(v'))^{-1} \quad (173) $$

$$ \text{Cov}(\chi_l) = - \left( (\nabla^2 \psi_{\text{out}}(\chi', z'; \bar{t} = 1))^{-1} \right)_{\chi_l}, \qquad \text{Cov}(z_l) = - \left( (\nabla^2 \psi_{\text{out}}(\chi', z'; \bar{t} = 1))^{-1} \right)_{z_l} \quad (174) $$

The extremality condition of $\phi$ over the order parameters is obtained by setting its gradient to zero. We take the limit $\rho = 0$. It gives the following fixed-point equations:

$$ m_k = \mathbb{E}_\zeta \int_{\mathbb{R}} dP_k(k^*) k^* k' \qquad m_v = \mathbb{E}_\zeta \int_{\mathbb{R}} dP_v(v^*) v^* v' \quad (175) $$

$$ q_k = \mathbb{E}_\zeta \int_{\mathbb{R}} dP_k(k^*) (k')^2 \qquad q_v = \mathbb{E}_\zeta \int_{\mathbb{R}} dP_v(v^*) (v')^2 \quad (176) $$

$$ V_k = \mathbb{E}_\zeta \int_{\mathbb{R}} dP_k \text{Cov}(k) \qquad V_v = \mathbb{E}_\zeta \int_{\mathbb{R}} dP_v \text{Cov}(v) \quad (177) $$

and

$$ \hat{m}_k = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int dP^*(y, \epsilon^*, \chi^*, z^*) \frac{1}{V_k} \sum_l^L \left( \chi_l^* \chi_l' - \frac{m_k}{V_k} \text{Cov}(\chi_l) \right) \quad (178) $$

$$ \hat{m}_v = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int dP^*(y, \epsilon^*, \chi^*, z^*) \frac{1}{V_v} \left( z_{\epsilon^*}^* z_{\epsilon^*} - \frac{m_v}{V_v} \text{Cov}(z_{\epsilon^*}) \right) \quad (179) $$

$$ \hat{q}_k = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int dP^*(y, \epsilon^*, \chi^*, z^*) \frac{1}{V_k^2} \sum_l^L \left( \chi_l' - m_k \chi_l^* - \sqrt{q_k - m_k^2 \xi_l} \right)^2 \quad (180) $$

$$ \hat{q}_v = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int dP^*(y, \epsilon^*, \chi^*, z^*) \frac{1}{V_v^2} \sum_l^L \left( z_l' - m_v z_l^* - \sqrt{q_v - m_v^2 \zeta_l} \right)^2 \quad (181) $$

$$ \hat{V}_k = \mathbb{E}_L \frac{\alpha L}{V_k} - \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int dP^*(y, \epsilon^*, \chi^*, z^*) \frac{1}{V_k^2} \sum_l^L \text{Cov}(\chi_l) \quad (182) $$

$$ \hat{V}_v = \mathbb{E}_L \frac{\alpha L}{V_v} - \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta} \int dP^*(y, \epsilon^*, \chi^*, z^*) \frac{1}{V_v^2} \sum_l^L \text{Cov}(z_l) \quad (183) $$

At the fixed-point the test error is given by

$$ \mathbb{E}_{\sigma}(\alpha) = \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta} \int dP^*(y, \epsilon^*, \chi^*, z^*) (y - \hat{y}_{\sigma}(\chi'', z''))^2 . \quad (184) $$
### C.4.1 Specializations to spiked- and max-SLR models

We consider an attention trained with $l_2$ regularization $\gamma(x) = \frac{1}{2}x^2$. The first fixed-point equations can be explicit

$$m_k = \frac{\hat{m}_k}{r_k + \hat{V}_k} \qquad m_v = \frac{\hat{m}_v}{r_v + \hat{V}_v} \qquad (185)$$

$$q_k = \frac{\hat{m}_k^2 + \hat{q}_k}{(r_k + \hat{V}_k)^2} \qquad q_v = \frac{\hat{m}_v^2 + \hat{q}_v}{(r_v + \hat{V}_v)^2} \qquad (186)$$

$$V_k = \frac{1}{r_k + \hat{V}_k} \qquad V_v = \frac{1}{r_v + \hat{V}_v} \qquad (187)$$

We can simplify the rest of the equations by using the permutation invariance w.r.t. the tokens to fix $\epsilon^* = 1$. For the two models we have that, for $l \neq \epsilon^*$, $z_l^*$ is Gaussian under $P^*(y, \epsilon^*, \chi^*, z^*)$ and $m_v z_l^* + \sqrt{q_v - m_v^2} \zeta_l \sim \mathcal{N}(0, q_v)$, so we can integrate out $z_l^*$. We assume that $P_{\text{out}}$ is the identity channel so $z_{\epsilon^*} = y$. In the potential $\psi_{\text{out}}$ we have $\omega_l = m_v y \delta_{l,1} + \sqrt{q_v - m_v^2} \delta_{l,1} \zeta_l$ for all $l$. The joint distribution over data reduces to

$$P^*(y, \epsilon^*, \chi^*, z^*) = P^*(y, \chi^*) = \mathcal{N}(y; 0, 1) g_\nu(1, \chi^*) \prod_l^L \mathcal{N}(\chi_l^*; 0, 1). \qquad (188)$$

Taking $y \sim \mathcal{N}(0, 1)$ and $\chi^* \sim \mathcal{N}(0, I_L)$, the fixed-point equations and the error are

$$\hat{m}_k = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y, \chi^*} g_\nu(1, \chi^*) \frac{1}{V_k} \sum_l^L \left( \chi_l^* \chi_l' - \frac{m_k}{V_k} \text{Cov}(\chi_l) \right) \qquad (189)$$

$$\hat{m}_v = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y, \chi^*} g_\nu(1, \chi^*) \frac{1}{V_v} \left( y z_1' - \frac{m_v}{V_v} \text{Cov}(z_1) \right) \qquad (190)$$

$$\hat{q}_k = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y, \chi^*} g_\nu(1, \chi^*) \frac{1}{V_k^2} \sum_l^L \left( \chi_l' - m_k \chi_l^* - \sqrt{q_k - m_k^2} \xi_l \right)^2 \qquad (191)$$

$$\hat{q}_v = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y, \chi^*} g_\nu(1, \chi^*) \frac{1}{V_v^2} \left( \left( z_l' - m_v y - \sqrt{q_v - m_v^2} \zeta_l \right)^2 + \sum_{l>1} (z_l' - \sqrt{q_v} \zeta_l)^2 \right) \qquad (192)$$

$$\hat{V}_k = \mathbb{E}_L \frac{\alpha L}{V_k} - \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y, \chi^*} g_\nu(1, \chi^*) \frac{1}{V_k^2} \sum_l^L \text{Cov}(\chi_l) \qquad (193)$$

$$\hat{V}_v = \mathbb{E}_L \frac{\alpha L}{V_v} - \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y, \chi^*} g_\nu(1, \chi^*) \frac{1}{V_v^2} \sum_l^L \text{Cov}(z_l) \qquad (194)$$

$$\mathbb{E}_{\sigma}(\alpha) = \mathbb{E}_{L} \mathbb{E}_{\xi, \zeta, y, \chi^*} g_{\nu}(1, \chi^*) (y - \hat{y}_{\sigma}(\chi'', z''))^2 \qquad (195)$$

For the spiked-SLR model we additionally have that $\chi_l^*$ is Gaussian under $P^*(y, \chi^*);$ so $m_k \chi_l^* + \sqrt{q_k - m_k^2} \xi_l \sim \mathcal{N}(\sqrt{\nu m_k} \delta_{l,1}, q_k)$ and $\chi^*$ can be integrated out. In the potential $\psi_{\text{out}}$ we have $\gamma_l = \sqrt{\nu m_k} \delta_{l,1} + \sqrt{q_v} \xi_l$ for all $l$. We have the simplifications

$$\hat{m}_k = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y} \frac{1}{V_k} (\sqrt{\nu} \chi_1' - \nu m_k) \qquad (196)$$

$$\hat{q}_k = \alpha \mathbb{E}_L \mathbb{E}_{\xi, \zeta, y} \frac{1}{V_k^2} \left( (\chi_1' - \sqrt{\nu m_k} - \sqrt{q_k} \xi_1)^2 + \sum_{l>1} (\chi_l' - \sqrt{q_k} \xi_l)^2 \right) \qquad (197)$$
# D Details on the numerics

## D.1 Training of the attention

The attention eq. (4) is trained using LBFGS on the full batch until convergence. The parameters $v$ and $k$ are initialized randomly according to a standard Gaussian $N(0, I_D)$.

In Fig. 2 the regularizations $r_k$ and $r_v$ are tuned by grid search to minimize the test error, over $\{0.3, 1, 3, 10\}^2$ for the linear attention and $\{0.03, 0.1, 0.3, 1\}^2$ for the softmax. These values were determined according to Fig. 6.

For most of the parameters of the data model we explored the training converges to the global minimum of the training loss eq. (15), in the sense that the training loss of trained attention is equal to the training loss predicted by our asymptotic characterization.

In a few cases, in particular at low signal and low regularization, the training does not converge towards the global minimum of the loss, which results in a discrepancy between the simulated performances and the predicted ones. In these cases we train the attention starting from an informed initialization $v = v^*$ and $k = k^*$. Note that this initialization does not necessary correspond to a minimum of the loss. The performances of the trained attention then better matches our predictions, as depicted in Fig. 6 for the softmax on the max-SLR. We checked that the achieved training loss is well smaller than the one starting from a random initialization.

## D.2 Computation of the asymptotic characterization

### D.2.1 BO

We compute the BO performance stated in result B.1 by iterating the fixed point equations given by the condition $\nabla\phi_{\text{BO}}(m_k, m_v) = 0$. These equations are detailed in appendices C.3.1 and C.3.2 for the two models. The uninformed initialization corresponds to $m_k, m_v = 0.1, 0.1$ while the informed initialization corresponds to $m_k, m_v = 1 - 10^{-2}, 1 - 10^{-2}$. The expectation over the Gaussian random variables is computed over $10^6$ and $10^5$ Monte-Carlo samples respectively for the spiked-SLR and the max-SLR models.

### D.2.2 Attention

We compute the performance of the attention stated in result 5.1 by iterating the given equations. We use Steffensen's method to speed up the convergence. For the spiked-SLR a simplification of the equations is given in Appendix C.4.1. The expectation over the Gaussian random variables is computed over $10^5$ Monte-Carlo samples. For each sample at each iteration the extremizers of $\psi_{\text{out}}$ are computed using a quasi-Newton optimization scheme. The optimization is started at the extremizers computed at the previous step.

Notice that $\psi_{\text{out}}$ is not convex and may admit several maxima. In practice, for most of the values of $\gamma, \omega, V_k$ and $V_v, \psi_{\text{out}}$ admits a unique maximum. When it is not the case (in particular at low signal and low regularization) one should compare several different initializations of the optimization algorithm to find the global maximum. We tried on a few cases; it appeared that the final predictions do not change by a quantity greater than the fluctuations due to the randomness.

The minimum over the set $S$ of fixed points is computed by running the iterations of result 5.1 from several initializations. We considered informed initialization $(m_k, m_v, q_k, q_v, V_k, V_v) = (1, 1, 0, 0, \varepsilon, \varepsilon)$ for small $\varepsilon$, partially informed initializations $(m_k, m_v, q_k, q_v, V_k, V_v) = (1, 1, 1, 1, 1, 1)$ and uninformed initializations $(m_k, m_v, q_k, q_v, V_k, V_v) = (0, 0, 1, 1, 1, 1)$. We performed a few different runs, adding small randomness to the initial condition. For all the values of the parameters, the obtained fixed point did not depend on the choice of the initialization, up to some small numerical fluctuations. These fluctuations are larger at small regularizations and low signals, in which case we select the run which reaches the highest free entropy eq. (166), i.e. the lowest train loss, among those that converged before a certain amount of iterations.
E Additional figures

We provide an additional figure for the result 5.1 about the test risk of the attention at finite α. Fig. 6 gives the test risk E_σ for the same configurations as in Fig. 2 for several additional regularizations r_k and r_v. It justifies the ranges of the grid search for the linear and the softmax attention in Fig. 2, in the sense that E_σ seems to reach its minimal value for regularizations close to the ones considered in Fig. 6. Moreover it shows the excellent agreement between our theory result 5.1 and the simulations. We observe a discrepancy for the softmax at small regularization for the spiked-SLR at ν = 1 or the max-SLR at ν = +∞. For the spiked-SLR it may be caused by the replica-symmetry assumption being false or the numerical errors in the resolution of the fixed point equations. For the max-SLR at r_k = r_v = 0.03 we observe that initializing the simulations at the informed point k = k*, v = v* leads to an agreement with our theory. This shows that in this case the local optimization cannot recover the global minimum of the loss from random initialization.

<figure><img src="image_7.png" /><figcaption>Figure 6: Test error of the trained attention, linear (top) and softmax (bottom), across different tasks and signal strengths $v$, for $L = 3$. Solid lines indicate $E_\sigma(\alpha)$ at finite $\alpha$ (Result 5.1), while markers represent the test error of an ERM approximated via a local optimization method with $\sqrt{ND} = 10^4$ and averaged over ten instances. Random initialization means that the attention is initialized at random $k$ and $v$ while informed initialization means it is initialized at $k = k^*, v = v^*$. Dashed lines correspond to the value of $E_\sigma$ in the infinite-$\alpha$ limit (see closed-formed formulas in Proposition 4.1 for softmax and Appendix A.2.7 for linear). The Bayes-optimal risk is shown in black (see Section B for a discussion on its discontinuity). Appendix D includes more experimental details.</figcaption></figure>