betaFeedback

WorksMathematics

Asymptotic Analysis of Discrete-Time Self-Attention Dynamics in Transformers

Abstract

In machine learning, a self-attention dynamics describes the attention mechanism of transformers, and is obtained treating the layer index of a transformer as time index. Even though naturally discrete-time, this dynamical system is most often studied in continuous-time. Scope of this paper is to provide a thorough investigation of the discrete-time version of the self-attention dynamics: a complete classification of its equilibria is given, and their stability properties are studied, in both the single-head and the multihead case. We show that in both cases the main attractor of the dynamics is a consensus equilibrium aligned with the principal eigenvector of the value matrix (in the single-head case, a corresponding matrix of weights in the multihead case), even though other locally asymptotically stable bipartite equilibria can be show to exist at least in the single-head situation. The behavior is confirmed by looking at the learned weight matrices of a real transformer.

Introduction

The core algorithm inside a large language model is a so-called attention-based transformer [24]. This architecture, introduced in 2017, has rapidly become the standard in many machine learning problems, also beyond natural language processing. A transformer is composed of multiple layers having identical structure, acting on “tokens”, that is, vectorial representations of the elementary units in which a set of data can be decomposed (words for language processing, patches of pixels for image processing, etc.). Inside a layer are two main subblocks: one is the attention mechanism and the other a feedforward neural network (FFN). The attention mechanism quantifies how much importance each token is giving to the other tokens, which justify why it is normally referred to as “self-attention” (the tokens’ attention is a function of the tokens themselves). It is implemented as a softmax function of an inner product involving the tokens multiplied by two learned weight matrices called query and key. The attention coefficients provide the weights of a convex combination of the tokens, which is in turn multiplied by a third matrix of weights called the value matrix. In a so-called multihead transformer, the attention mechanism inside a layer is “multiplexed” in such a way that different parts of a token can use different attention weights. The results at the various heads are then “concatenated” using a fourth matrix of weights called the output matrix. The FFN subblock of a layer is instead normally composed of a 2-layer “perceptron”, that is, a static map containing two learned weight matrices and bias terms with an activation function. Other ingredients of a transformer layer are residual connections, in which to the token value exiting one of the two subblocks above is added the value of the token before the subblock, and normalization operations, which we simplify here as L2L_2 normalizations of the token vectors, in place of the original “layernorm” operation (an affine, elementwise operation).

A popular approach to build mathematical models of transformers is to interpret the layer index as time axis, and to describe the changes that the tokens (the “state variables”) are subject to when passing through these layers as a dynamical system [10, 20, 16, 12, 13]. Even though a natural setting for such model is discrete-time and difference equations, most analyses carried out so far have opted for a continuous-time setting with ordinary differential equations [2, 3, 12, 13], in which the finite number of layers is replaced by a continuum of layers. The main reason is that most properties that one can investigate with a dynamical system are asymptotic properties, like for instance the existence and stability of the equilibria, which anyway requires the time axis (ie., the number of layers) to go to ∞\infty. Another reason is that continuous-time dynamics are more amenable to be investigated using e.g. mean-field methods, quite popular in this context [4, 5, 11].

Apart from continuous-time vs discrete-time, in the literature the setting of choice for a mathematical analysis of self-attention dynamics entails several simplifications: the initial embedding and positional encoding are disregarded; a single head is normally considered instead of a multihead; all layers are assumed to have the same weights (weight-sharing or “universal” transformers [7], which leads to a time-invariant dynamical system); the attention matrix is full (each token attend to all other tokens); the layernorm is replaced by an L2L_2 norm (which renders the tokens of unit length, that is, makes them evolve on the unit sphere); the weight matrices are restricted to special structure (sometimes equal to the identity [13], other times symmetric [3]); the FFN part is ignored.

For such simplified continuous-time model, it has been shown in [12] that the tokens tend to cluster into identical vectors. In particular, [1, 2] rephrased this feature as a consensus-like property, a familiar framework in the multiagent system literature [19]. Experimentally, this phenomenon is well-known under the names of oversmoothing, rank collapse, or token-uniformity [8, 9, 18, 17, 21, 22, 26]. Further analysis carried out in [3] provides a complete classification of the possible equilibria, and an analysis of their stability and convergence properties, showing that the eigenvectors of the value matrix play a prominent role: most token trajectories align in fact with the eigenvector associated to the principal

C. Altafini is with the Division of Automatic Control, Dept. of Electrical Engineering, Linköping University, SE-58183, Linköping, Sweden. email: [email protected]. Work supported in part by the Swedish Research Council (grant n. 2024-04772) and by the Wallenberg AI, Autonomous Systems and Software Program (WASP) funded by the Knut and Alice Wallenberg Foundation. eigenvalue of the value matrix, at least in the cases in which the value matrix is symmetric and with a positive strictly dominant eigenvalue.

While it is mentioned in several papers that the discrete-time model of a self-attention dynamics behaves like its continuous-time counterpart, an explicit, thorough stability analysis of the discrete-time case is still missing (an exception is [1] where discrete-time consensus is proven in some special cases). Such an analysis is the main contribution of this paper. What we obtain is that indeed the behavior of the discrete-time self-attention model reflects that of its continuous-time counterpart for what concerns the classification of the equilibria and their stability properties. Consensus (when all token vectors become identical), bipartite consensus (when some tokens converge to a point on the unit sphere and the others converge to the antipodal point), polygonal equilibria, and clustering (non-antipodal) equilibria are the four classes of possible equilibria. The first two classes can occur only at the eigenvectors of the value matrix, and in particular consensus can be asymptotically stable only when it is aligned with the principal eigenvector of the value matrix, while all other consensus points can be shown to be unstable. Bipartite consensus equilibria can be (sometimes) locally asymptotically stable, and coexist with the “main” consensus attractor, leading to a multistable system as in the continuous-time case [3]. Proving these asymptotic properties requires to compute explicitly the Jacobian of the system and its eigenvalues/eigenvectors, which is significantly more challenging than in the continuous-time case. These explicit calculations, which constitute the main technical content of the paper, yield several nontrivial results, most notably the fact that convergence to consensus is always associated to the principal (most positive) eigenvalue of the value matrix, rather than to the eigenvalue of largest absolute value (i.e., equal to the spectral radius).

As a further extension w.r.t. the existing literature, we can provide a stability analysis also for the multihead case. In particular, we can still show asymptotic stability of the consensus equilibrium at the principal eigenvector of a matrix which is the multihead equivalent of the value matrix (we call this the “layer matrix”, and it is the product of a sum of suitably zero-padded value matrices at the various heads with the output matrix), meaning that the dynamical behavior of the multihead system is similar to that of the single head case.

An advantage of considering a discrete-time self-attention dynamics is that the learned weight matrices of a real transformer can be used to test the model. While in most transformers these weight matrices change from layer to layer (leading to a time-varying dynamics), the special class of universal transformers [7] has the property of sharing weights across layers (leading to the time-invariant dynamics investigated in this paper). A simple example of such universal architecture is ALBERT (“A Lite BERT”) [14]. We show in the paper that under (some of) the simplifying assumptions mentioned above (L2L_2 regularization, no FFN), indeed the discrete-time self-attention dynamics obtained by choosing the ALBERT weight matrices converges to a consensus point aligned with the principal eigenvector of our layer matrix. This confirms that the theoretical analysis is sound, and can provide useful predictions also on the finite-time horizon given by the number of layers of a real transformer.

Model formulation

We consider a simplified model of a transformer consisting of TT layers, each characterized by a single head and by shared weight matrices (universal transformer), and in which we disregard the FFN part. A residual connection is included in the model, while as normalization we use L2L_2 normalization, which amounts to considering unit vectors, i.e., vectors that evolve on the unit sphere. To uniform ourselves with the standard dynamical systems notation, we treat tokens (i.e. state vectors) as columns vectors, see also [1], [13]. Consider nn tokens represented as unit length column vectors xi∈Sd−1⊂Rdx_i \in S^{d-1} \subset\mathbb{R}^d, i=1,…,ni = 1,\ldots,n. Denote FQ,FK∈Rm×dF_Q,F_K \in\mathbb{R}^{m \times d} the query and key matrices (for simplicity and without loss of generality we hereafter assume m=dm=d) and FV∈Rd×dF_V \in\mathbb{R}^{d \times d} the value matrix. Because of our column-token representation, these matrices correspond to the transposes of the weight matrices normally considered in the machine learning literature [24], [15], [6], [23] (more on this in § 5-A). Since we are dealing with a single head, a fourth matrix (denoted output matrix) is not required, and is therefore for now omitted.

The following discrete-time difference equation encodes the single-head self-attention dynamics we consider, including a residual connection and an L2L_2 layer normalization:

xi+=xi+ηFV∑j=1nAij(x)xj∥xi+ηFV∑j=1nAij(x)xj∥,i=1,…,n(1)x_i^+ = \frac{x_i + \eta F_V \sum_{j=1}^{n} A_{ij}(x)x_j}{\left\lVert x_i + \eta F_V \sum_{j=1}^{n} A_{ij}(x)x_j \right\rVert}, \qquad i=1,\ldots,n \tag*{(1)}

where Aij(x)=e(FQxi)T(FKxj)∑ℓ=1ne(FQxi)T(FKxℓ)A_{ij}(x) = \frac{e^{(F_Qx_i)^\mathsf{T}(F_Kx_j)}}{\sum_{\ell=1}^{n} e^{(F_Qx_i)^\mathsf{T}(F_Kx_\ell)}} is the attention that the token xix_i gives to the token xjx_j, computed through a softmax function (xx is the stack of x1,…,xnx_1,\ldots,x_n vectors) and where we have added an extra parameter η>0\eta> 0 (“step size”), which balances the importance of the self-attention part against the residual connection [18]. The (row stochastic) attention matrix A(x)A(x) is assumed full, i.e., each token attends to every other token. In (1) we are implicitly interpreting the tokens as layer-indexed state variables: xi=xi(t)x_i = x_i(t) and xi+=xi(t+1)x_i^+ = x_i(t+1), for some layer index t=1,…,Tt = 1,\ldots,T. We let ∥⋅∥\lVert\cdot\rVert denote the Euclidean vector norm and the operator norm for matrices. For the ii-th token, define the tangent projector on Sd−1S^{d-1} at xix_i by Πxi:=I−xixiT\Pi_{x_i} := I - x_ix_i^\mathsf{T}, which satisfies Πxixi=0\Pi_{x_i}x_i = 0 and ΠxiT=Πxi\Pi_{x_i}^\mathsf{T} = \Pi_{x_i}. We are interested in investigating the asymptotic behavior of (1) when the number of layers TT goes to ∞\infty, and in particular in computing the equilibria of (1) and studying their stability properties. For that, it is convenient to investigate first a simpler system, which, for analogy with the continuous-time case, we call the multiagent discrete-time Oja system [3]. This system has the same structure as (1) but lacks the attention matrix.

Multiagent discrete-time Oja system

When we consider uniform attention [18], i.e., we take A(x)=1n11TA(x) = \frac{1}{n}\mathbf{1}\mathbf{1}^\mathsf{T}, then instead of (1) we obtain the simpler system

xi+=xi+ηnFV∑j=1nxj∥xi+ηnFV∑j=1nxj∥.(2)x_i^+ = \frac{x_i + \frac{\eta}{n}F_V \sum_{j=1}^{n} x_j}{\left\lVert x_i + \frac{\eta}{n}F_V \sum_{j=1}^{n} x_j \right\rVert}. \tag*{(2)}

Let y=FVm(x)=1nFV∑j=1nxj∈Rdy = F_V m(x) = \frac{1}{n}F_V\sum_{j=1}^{n}x_j \in\mathbb{R}^{d} be the total influence of all agents on agent ii (which is the same for all agents). Splitting (2) into parts before/after the normalization we can also write:

x~i=xi+ηyandxi+=x~i∥x~i∥.(3)\widetilde{x}_i=x_i+\eta y \qquad\text{and}\qquad x_i^+=\frac{\widetilde{x}_i}{\lVert\widetilde{x}_i\rVert}. \tag*{(3)}

We make the following assumption which holds throughout the rest of this and next section.

Assumption 1 The value matrix FV∈Rd×dF_V\in\mathbb{R}^{d\times d} is nonsingular and symmetric, of eigenvalues λ1>λ2≥…≥λd\lambda_1>\lambda_2\geq\ldots\geq\lambda_d with λ1>0\lambda_1>0 simple and positive.

Let v1,…,vdv_1,\ldots,v_d be the associated eigenvectors, normalized s.t. ∥vk∥=1\lVert v_k\rVert=1.

Equilibria

Let us first give our definition of consensus and bipartite consensus equilibria, which we request to be collinear with the eigenvectors of FVF_V (the reason will become clear later on when we study the full model (1)). The points x1,…,xn∈Sd−1x_1,\ldots,x_n\in\mathbb{S}^{d-1} are said to be in a consensus equilibrium if xi=xj=vk ∀i,j=1,…,nx_i=x_j=v_k\ \forall i,j=1,\ldots,n (or xi=xj=−vk ∀i,j=1,…,nx_i=x_j=-v_k\ \forall i,j=1,\ldots,n) and for some k=1,…,dk=1,\ldots,d. They are said to be in a bipartite consensus equilibrium if xi=vkx_i=v_k for i∈V1⊂Vi\in\mathcal{V}_1\subset\mathcal{V} and xi=−vkx_i=-v_k for i∈V∖V1i\in\mathcal{V}\setminus\mathcal{V}_1, for some k=1,…,dk=1,\ldots,d.

Lemma 1 Let x=[(x1)T … (xn)T]Tx=[(x_1)^T\ \ldots\ (x_n)^T]^T be an equilibrium point of the system (2). Then xx belongs to one of the following classes:

  1. consensus: xi=vk ∀ix_i=v_k\ \forall i, and k=1,…,dk=1,\ldots,d;

  2. bipartite consensus: xi=±vk ∀ix_i=\pm v_k\ \forall i and k=1,…,dk=1,\ldots,d;

  3. polygonal equilibria: {xi∈Sd−1 s.t. FV∑j=1nxj=0}\{x_i\in\mathbb{S}^{d-1}\text{ s.t. }F_V\sum_{j=1}^{n}x_j=0\}.

References

  1. [1]A. R. Abella, J. P. Silvestre, and P. Tabuada. The asymptotic behavior of attention in transformers. arXiv preprint arXiv:2412.02682, 2024.
  2. [2]Á. R. Abella, J. P. Silvestre, and P. Tabuada. Consensus is all you get: The role of attention in transformers. In Forty-second International Conference on Machine Learning, 2025.
  3. [3]C. Altafini. Multistability of self-attention dynamics in transformers. IEEE Transactions on Automatic Control, 71(10):6582–6595, 2026.arxiv.org/abs/2511.11553
  4. [4]G. Bruno, F. Pasqualotto, and A. Agazzi. Emergence of meta-stable clustering in mean-field transformer models. In International Conference on Learning Representations, volume 2025, pages 7496–7526, 2025.arxiv.org/abs/2410.23228
  5. [5]M. Burger, S. Kabri, Y. Korolev, T. Roith, and L. Weigand. Analysis of mean-field models arising from self-attention dynamics in transformer architectures with layer normalization. Philosophical transactions. Series A, Mathematical, physical, and engineering sciences, 383(2298):20240233, 2025.arxiv.org/abs/2501.03096
  6. [6]S. P. Chowdhury, A. Solomou, A. Dubey, and M. Sachan. On learning the transformer kernel, 2022.
  7. [7]M. Dehghani, S. Gouws, O. Vinyals, J. Uszkoreit, and Ł. Kaiser. Universal transformers. arXiv preprint arXiv:1807.03819, 2018.arxiv.org/abs/1807.03819
  8. [8]Y. Dong, J.-B. Cordonnier, and A. Loukas. Attention is not all you need: Pure attention loses rank doubly exponentially with depth. In International conference on machine learning, pages 2793–2803. PMLR, 2021.arxiv.org/abs/2103.03404
  9. [9]G. J. Dovonon, M. M. Bronstein, and M. J. Kusner. Setting the record straight on transformer oversmoothing. arXiv preprint arXiv:2401.04301, 2024.arxiv.org/abs/2401.04301
  10. [10]S. Dutta, T. Gautam, S. Chakrabarti, and T. Chakraborty. Redesigning the transformer architecture with insights from multi-particle dynamical systems. Advances in Neural Information Processing Systems, 34:5531–5544, 2021.arxiv.org/abs/2109.15142
  11. [11]B. Geshkovski, H. Koubi, Y. Polyanskiy, and P. Rigollet. Dynamic metastability in the self-attention model. arXiv preprint arXiv:2410.06833, 2024.arxiv.org/abs/2410.06833
  12. [12]B. Geshkovski, C. Letrouit, Y. Polyanskiy, and P. Rigollet. The emergence of clusters in self-attention dynamics. Advances in Neural Information Processing Systems, 36:57026–57037, 2023.DOI
  13. [13]B. Geshkovski, C. Letrouit, Y. Polyanskiy, and P. Rigollet. A mathematical perspective on transformers. Bulletin of the American Mathematical Society, 62(3):427–479, 2025.arxiv.org/abs/2312.10794
  14. [14]Z. Lan, M. Chen, S. Goodman, K. Gimpel, P. Sharma, and R. Soricut. Albert: A lite bert for self-supervised learning of language representations, 2020.arxiv.org/abs/1909.11942
  15. [15]V. Likhosherstov, K. Choromanski, and A. Weller. On the expressive power of self-attention matrices. arXiv, abs/2106.03764, 2021.arxiv.org/abs/2106.03764
  16. [16]Y. Lu, Z. Li, D. He, Z. Sun, B. Dong, T. Qin, L. Wang, and T.-Y. Liu. Understanding and improving transformer from a multi-particle dynamic system point of view. arXiv preprint arXiv:1906.02762, 2019.arxiv.org/abs/1906.02762
  17. [17]T. Nguyen, T. Nguyen, and R. Banu. Mitigating over-smoothing in transformers via regularized nonlocal functionals. Advances in Neural Information Processing Systems, 36:80233–80256, 2023.arxiv.org/abs/2312.00751
  18. [18]L. Noci, S. Anagnostidis, L. Biggio, A. Orvieto, S. P. Singh, and A. Lucchi. Signal propagation in transformers: Theoretical perspectives and the role of rank collapse. Advances in Neural Information Processing Systems, 35:27198–27211, 2022.arxiv.org/abs/2206.03126
  19. [19]R. Olfati-Saber, J. A. Fax, and R. M. Murray. Consensus and cooperation in networked multi-agent systems. Proceedings of the IEEE, 95(1):215–233, 2007.
  20. [20]M. E. Sander, P. Ablin, M. Blondel, and G. Peyré. Sinkformers: Transformers with doubly stochastic attention. In International Conference on Artificial Intelligence and Statistics, pages 3515–3530. PMLR, 2022.arxiv.org/abs/2110.11773
  21. [21]M. Scholkemper, X. Wu, A. Jedbabaïe, and M. T. Schaub. Residual connections and normalization can provably prevent oversmoothing in gnns. arXiv preprint arXiv:2406.02997, 2024.arxiv.org/abs/2406.02997
  22. [22]H. Shi, J. Gao, H. Xu, X. Liang, Z. Li, L. Kong, S. Lee, and J. T. Kwok. Revisiting over-smoothing in bert from the perspective of graph. arXiv preprint arXiv:2202.08625, 2022.arxiv.org/abs/2202.08625
  23. [23]M. Ustaomeroglu and G. Qu. A theoretical study of (hyper) self-attention through the lens of interactions: Representation, training, generalization, 2025.arxiv.org/abs/2506.06179
  24. [24]A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A. N. Gomez, Ł. Kaiser, and I. Polosukhin. Attention is all you need. Advances in neural information processing systems, 30, 2017.arxiv.org/abs/1706.03762
  25. [26]S. Zhai, T. Likhamenko, E. Littwin, D. Busbridge, J. Ramapuram, Y. Zhang, J. Gu, and J. M. Susskind. Stabilizing transformer training by preventing attention entropy collapse. In International Conference on Machine Learning, pages 40770–40803. PMLR, 2023.arxiv.org/abs/2303.06296

Paper details

vlnc:W_t3kq7aey
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
Usage

views · 2

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

  • Mathematics

Not recorded for this paper

View statement
Looking for related papers…
Contents