English (unofficial) translations of posts at kexue.fm
Source

Exploring Linear Attention: Does Attention Require a Softmax?

Translated by DeepSeek V4 Pro. Translations can be inaccurate, please refer to the original post for important stuff.

As is well known, although Transformer-like models based on the Attention mechanism possess excellent parallel performance, their spatial and temporal complexity are both of the order \mathcal{O}(n^2), where n is the sequence length. Consequently, when n is large, the computational load of Transformer models becomes unbearable. Recently, many efforts have been dedicated to reducing the computational complexity of Transformer models, such as model pruning, quantization, distillation, and other compression techniques, or modifying the Attention structure to reduce its complexity to \mathcal{O}(n\log n) or even \mathcal{O}(n).

A few days ago, I read the paper "Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention" and learned about the exploration of Linear Attention. Subsequently, I read some related literature and gained several insights, which I have summarized in this article.

Attention

The most popular Attention mechanism currently is Scaled-Dot Attention, which takes the form: Attention(\boldsymbol{Q},\boldsymbol{K},\boldsymbol{V}) = softmax\left(\boldsymbol{Q}\boldsymbol{K}^{\top}\right)\boldsymbol{V}\label{eq:std-att} Here \boldsymbol{Q}\in\mathbb{R}^{n\times d_k}, \boldsymbol{K}\in\mathbb{R}^{m\times d_k}, \boldsymbol{V}\in\mathbb{R}^{m\times d_v}. For simplicity, we have not explicitly written the scaling factor of the Attention. In this article, we are primarily concerned with the Self-Attention scenario, so for convenience of introduction, we set \boldsymbol{Q}, \boldsymbol{K}, \boldsymbol{V}\in\mathbb{R}^{n\times d}. Generally, n > d or even n \gg d (in BERT-base, d=64). For related interpretations, one can refer to my post "A Brief Reading of ’Attention is All You Need’ (Introduction + Code)", as well as some improvement works such as "Breaking the Bottleneck: Building a More Powerful Transformer" and "Google’s New Work Synthesizer: We Still Don’t Know Enough About Self-Attention". We will not go into further depth here.

Removing the Softmax

Readers might not expect that the key factor restricting Attention performance is actually the Softmax in its definition! In fact, a simple derivation leads to this conclusion. The step \boldsymbol{Q}\boldsymbol{K}^{\top} yields an n\times n matrix, and it is this step that determines the \mathcal{O}(n^2) complexity of Attention. If there were no Softmax, it would be a continuous multiplication of three matrices \boldsymbol{Q}\boldsymbol{K}^{\top}\boldsymbol{V}. Since matrix multiplication satisfies the associative law, we could first calculate \boldsymbol{K}^{\top}\boldsymbol{V} to get a d\times d matrix, and then left-multiply it by \boldsymbol{Q}. Since d \ll n, the approximate complexity of this calculation is only \mathcal{O}(n) (dominated by the left-multiplication of \boldsymbol{Q}).

In other words, the complexity of Attention without Softmax can be reduced to the ideal linear level \mathcal{O}(n)! This is clearly our ultimate pursuit: Linear Attention, an Attention mechanism with linear complexity. Therefore, the theme of this article is to explore Linear Attention after removing the Softmax.

General Definition

The question is, can it still be considered Attention if the Softmax is directly removed? Can it still achieve the effect of standard Attention? To answer this, let us first rewrite the definition of Scaled-Dot Attention [eq:std-att] equivalently as (vectors in this article are column vectors): Attention(\boldsymbol{Q},\boldsymbol{K},\boldsymbol{V})_i = \frac{\sum\limits_{j=1}^n e^{\boldsymbol{q}_i^{\top}\boldsymbol{k}_j}\boldsymbol{v}_j}{\sum\limits_{j=1}^n e^{\boldsymbol{q}_i^{\top}\boldsymbol{k}_j}}\label{eq:std-att-2} Thus, Scaled-Dot Attention is essentially a weighted average of \boldsymbol{v}_j using e^{\boldsymbol{q}_i^{\top}\boldsymbol{k}_j} as weights. Therefore, we can propose a generalized definition of Attention: Attention(\boldsymbol{Q},\boldsymbol{K},\boldsymbol{V})_i = \frac{\sum\limits_{j=1}^n \text{sim}(\boldsymbol{q}_i, \boldsymbol{k}_j)\boldsymbol{v}_j}{\sum\limits_{j=1}^n \text{sim}(\boldsymbol{q}_i, \boldsymbol{k}_j)}\label{eq:gen-att} This replaces e^{\boldsymbol{q}_i^{\top}\boldsymbol{k}_j} with a general function \text{sim}(\boldsymbol{q}_i, \boldsymbol{k}_j) of \boldsymbol{q}_i and \boldsymbol{k}_j. To preserve distribution characteristics similar to Attention, we require \text{sim}(\boldsymbol{q}_i, \boldsymbol{k}_j)\geq 0 to hold always. That is, if we want to define a new type of Attention, we should maintain the form of equation [eq:gen-att] and satisfy \text{sim}(\boldsymbol{q}_i, \boldsymbol{k}_j)\geq 0.

This general form of Attention is also known as a Non-Local Network in Computer Vision (CV), originating from the paper "Non-local Neural Networks".

Several Examples

If we directly remove the Softmax, then \text{sim}(\boldsymbol{q}_i, \boldsymbol{k}_j) = \boldsymbol{q}_i^{\top}\boldsymbol{k}_j. The problem is that the dot product cannot guarantee non-negativity, so this is not yet a reasonable choice. Below, we briefly introduce several viable schemes.

It is worth noting that among the Linear Attention methods introduced below, the first two come from the CV field, and the third is my own conception. Therefore, they have not yet been extensively tested on NLP tasks, providing experimental directions for NLPers working on model improvements (^_^). By the way, there are many improvement works on Attention in the CV field (besides those introduced below, there are also EMANet, etc.), and much of the content is worth reading for those of us in NLP.

Kernel Function Form

A natural idea is: if every element of \boldsymbol{q}_i and \boldsymbol{k}_j is non-negative, then the dot product will naturally be non-negative. To achieve this, we can apply activation functions \phi and \varphi to \boldsymbol{q}_i and \boldsymbol{k}_j respectively: \text{sim}(\boldsymbol{q}_i, \boldsymbol{k}_j) = \phi(\boldsymbol{q}_i)^{\top} \varphi(\boldsymbol{k}_j)\label{eq:gen-att-2} where \phi(\cdot) and \varphi(\cdot) are activation functions with non-negative ranges. The paper mentioned at the beginning, "Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention", chooses \phi(x)=\varphi(x)=\text{elu}(x)+1.

To frame it conceptually, equation [eq:gen-att-2] can be linked to "kernel methods". Especially when \phi=\varphi, \phi corresponds to a kernel function, and \langle \phi(\boldsymbol{q}_i), \phi(\boldsymbol{k}_j)\rangle is the dot product defined through the kernel function. For thoughts in this area, one can refer to the paper "Transformer dissection: An unified understanding for transformer’s attention via the lens of kernel". We will not extend further here.

Clever Use of Softmax

An earlier article, "Efficient Attention: Attention with Linear Complexities", provides a more interesting choice. It notes that in \boldsymbol{Q}\boldsymbol{K}^{\top}, where \boldsymbol{Q}, \boldsymbol{K} \in\mathbb{R}^{n\times d}, if "\boldsymbol{Q} is normalized along the d dimension and \boldsymbol{K} is normalized along the n dimension," then \boldsymbol{Q}\boldsymbol{K}^{\top} automatically satisfies normalization. Thus, the choice it provides is: Attention(\boldsymbol{Q},\boldsymbol{K},\boldsymbol{V}) = softmax_2\left(\boldsymbol{Q}\right)softmax_1(\boldsymbol{K})^{\top}\boldsymbol{V} where softmax_1 and softmax_2 refer to Softmax operations performed along the first (n) and second (d) dimensions, respectively. In other words, we apply Softmax to \boldsymbol{Q} and \boldsymbol{K} individually, rather than applying it after calculating \boldsymbol{Q}\boldsymbol{K}^{\top}.

If we directly take \phi(\boldsymbol{q}_i)=softmax(\boldsymbol{q}_i) and \varphi(\boldsymbol{k}_j)=softmax(\boldsymbol{k}_j), then this form is clearly a special case of equation [eq:gen-att-2]. Additionally, this design has appeared more than once in CV, for example, A^2-Nets also includes the same approach.

My Own Conception

Here, I present a conception of my own. The starting point for this idea is no longer equation [eq:gen-att-2], but rather an approximation of the original definition [eq:std-att-2]. From the Taylor expansion, we have: e^{\boldsymbol{q}_i^{\top}\boldsymbol{k}_j} \approx 1 + \boldsymbol{q}_i^{\top}\boldsymbol{k}_j If \boldsymbol{q}_i^{\top}\boldsymbol{k}_j\geq -1, then the non-negativity of the right side can be guaranteed, allowing us to let \text{sim}(\boldsymbol{q}_i, \boldsymbol{k}_j)=1 + \boldsymbol{q}_i^{\top}\boldsymbol{k}_j. At this point, readers might have already realized that to ensure \boldsymbol{q}_i^{\top}\boldsymbol{k}_j\geq -1, one only needs to perform l_2 normalization on \boldsymbol{q}_i and \boldsymbol{k}_j respectively. Therefore, the scheme I finally propose is: \text{sim}(\boldsymbol{q}_i, \boldsymbol{k}_j) = 1 + \left( \frac{\boldsymbol{q}_i}{\Vert \boldsymbol{q}_i\Vert}\right)^{\top}\left(\frac{\boldsymbol{k}_j}{\Vert \boldsymbol{k}_j\Vert}\right) This differs from the form in equation [eq:gen-att-2], but theoretically, it is closer to the original Scaled-Dot Attention.

Summary

This article introduced some works that modify the structure of Attention to reduce its computational complexity. The main idea is to remove the Softmax in standard Attention, which allows the complexity of Attention to degrade to the ideal \mathcal{O}(n) level (Linear Attention). Compared to other similar structural improvement works, this modification can reduce complexity to \mathcal{O}(n) while still retaining all "token-token" attention and preserving the possibility for autoregressive generation.

Reprinting: Please include the original address of this article: https://kexue.fm/archives/7546

For more detailed reprinting matters, please refer to: "Scientific Space FAQ"