Linear attention
The problem with attention
Let’s start with a review of standard multi-head attention. For a single layer and head, we compute :
We can then compute the output as:
where is the causal mask.1
The big issue with vanilla attention is that it is quadratic in sequence length. The computational complexity (number of floating point operations) of the matrix multiplication is , which is quadratic in the sequence length. And the memory complexity of storing is , which is also quadratic in the sequence length.2 This becomes problematic when is large (e.g., think about what happens when sequence length is 1 million tokens).
A refresher on the computational complexity of matrix multiplication.
Suppose we have two matrices and .
The resulting matrix has entries. Each entry can be computed as the dot product of the row of and the column of , which consists of multiplications and additions.
Thus, the complexity of the matrix multiplication is floating point operations.
Linear attention
Let’s consider the standard attention formulation,3 but without the softmax operation:
Without the softmax operation, attention becomes a linear operation – it’s just a bunch of matrix multiplications. This means that we take advantage of the associative property of matrix multiplication, and write:
With this order of operations, the complexity is no longer quadratic in sequence length. The matrix can be computed using FLOPs, and can also be computed using FLOPs. So the total complexity is , which is linear in the sequence length.
One thing we’ve conveniently ignored is the causal mask .
When we write linear attention[1]Transformers are RNNs: Fast autoregressive transformers with linear attention [link]
Angelos Katharopoulos, Apoorv Vyas, Nikolaos Pappas, and François Fleuret. Proceedings of the 37th International Conference on Machine Learning. 2020. with the causal mask, associativity no longer helps us, as we must apply an element-wise masking operation to the attention scores:
In this parallel view (i.e., when we compute the output for all token positions at once), the causal mask therefore prevents us from computing the output with linear dependence on the sequence length, and we are still stuck with quadratic dependence on the sequence length. We will see, though, that in the recurrent view (i.e., when we compute one token at a time), linear attention is very nice. Luckily, there is a way to balance between the two views via something called the chunkwise parallel form of linear attention, which we’ll describe later.
The recurrent view
As we’ve written it above, linear attention can be viewed as a parallel computation – the output is computed for all token positions at once via matrix multiplications.
But with linear attention, it’s useful to think about it as a recurrent computation – as some computation that is performed sequentially, one token position at a time (as in autoregressive inference). In this recurrent view, we think of the as a memory matrix, that is built up as we read the sequence.
First, we note that can be written as a sum of outer products:
With this view, we can think of the memory matrix being updated with a single key-value pair for each token position .
Let’s now define to be the memory matrix at time (e.g., up to and including token position ):
It is easy to define a recurrence relation for :
That’s basically all there is to it. With the recurrent view, we maintain a constant-size memory matrix that gets updated with a new key-value pair at each time step. Note that, once the memory matrix has been built up, additional tokens can be generated with constant time complexity () and constant memory complexity (also ), without dependence on the sequence length.
Chunkwise parallel form of linear attention
As we’ve seen so far, when viewed as recurrent computation, linear attention is very nice; however, when viewed as parallel computation, we still have quadratic dependence on sequence length (due to the causal mask).
Chunkwise parallel form[2]Gated linear attention transformers with hardware-efficient training [link]
Songlin Yang, Bailin Wang, Yikang Shen, Rameswar Panda, and Yoon Kim. Proceedings of the 41st International Conference on Machine Learning. 2024. is a way to balance between the two views, and to make parallel computation not entirely quadratic in sequence length.
The idea is to break up the sequence into chunks, each of length . Within each chunk (“intra-chunk”), we use the parallel view and use quadratic attention, incurring a cost of . Between chunks (“inter-chunk”), we use the recurrent view and use linear attention, incurring a cost of .
Let’s first split the input sequence of length into non-overlapping chunks. We’ll use subscript to denote chunk-level quantities. For chunk , we have:
- : query, key, and value vectors for chunk ,
- : memory matrix after processing chunks .
Inter-chunk memory matrix recurrence
For the inter-chunk recurrence, we compute the current chunk’s memory matrix from the previous chunk’s memory matrix:
where is the sum of outer products for all tokens in chunk .
Computing the output for a chunk
For each chunk , we compute the output as:
where is the causal mask applied within the chunk.
The first term (inter-chunk) allows each token in chunk to attend to all tokens in previous chunks via the memory matrix . The second term (intra-chunk) allows each token to attend to previous tokens within the same chunk using standard attention.
Complexity of chunkwise parallel form
Let’s analyze the complexity for processing a single chunk:
- Inter-chunk memory matrix recurrence computation: we’re multiplying a matrix by a matrix, for a total cost of .
- Computing the output for a chunk:
- Inter-chunk output: we’re multiplying a matrix by a matrix, for a total cost of .
- Intra-chunk output: this is the standard quadratic attention computation, which is .
The total complexity per chunk is therefore .
For the entire sequence with chunks, the total complexity is:
The chunk size is effectively a knob to control the tradeoff between parallelism and recurrence:
- When , we have the fully parallel form (quadratic complexity, maximally parallel);
- When , we have the fully recurrent form (linear complexity, maximally sequential);
- Intermediate values balance parallelism and complexity.
Gated delta networks
The gated delta network[3]Gated delta networks: Improving Mamba2 with delta rule [link]
Songlin Yang, Jan Kautz, and Ali Hatamizadeh. International Conference on Learning Representations. 2025. is a variant of linear attention that makes two additions: (1) a decay gate, and (2) a delta rule.
Decay gate
The intuition behind the decay gate is to gradually forget old information as we move forward in the sequence. This can be implemented by multiplying the memory matrix by a (data-dependent) decay factor :
Delta rule
The intuition behind the delta rule[4]Linear transformers are secretly fast weight programmers [link]
Imanol Schlag, Kazuki Irie, and Jürgen Schmidhuber. Proceedings of the 38th International Conference on Machine Learning. 2021. is this: when we update the memory matrix with some key-value pair , there may already be some conflicting information corresponding to the same key .
In addition to naively adding to the memory matrix, we can “clear out” the old information, and update the entry to correspond to a mix of the old and new information.
Notice that we can retrieve the old conflicting value by searching our memory with key : .
We can then update the memory matrix to clear out the old information, and then update the entry to correspond to a mix of the old and new information:
where is a (data-dependent) “writing strength”.
Putting them together
The gated delta network basically just combines the decay gate and the delta rule.
Putting them together, we have:
where is a data-dependent decay factor, and is a data-dependent writing strength.
References
References cited in the text are listed first, in order of citation; additional references follow, ordered alphabetically.
- Transformers are RNNs: Fast autoregressive transformers with linear attention [link]
Angelos Katharopoulos, Apoorv Vyas, Nikolaos Pappas, and François Fleuret. Proceedings of the 37th International Conference on Machine Learning. 2020. - Gated linear attention transformers with hardware-efficient training [link]
Songlin Yang, Bailin Wang, Yikang Shen, Rameswar Panda, and Yoon Kim. Proceedings of the 41st International Conference on Machine Learning. 2024. - Gated delta networks: Improving Mamba2 with delta rule [link]
Songlin Yang, Jan Kautz, and Ali Hatamizadeh. International Conference on Learning Representations. 2025. - Linear transformers are secretly fast weight programmers [link]
Imanol Schlag, Kazuki Irie, and Jürgen Schmidhuber. Proceedings of the 38th International Conference on Machine Learning. 2021. - FlashAttention: Fast and memory-efficient exact attention with IO-awareness [link]
Tri Dao, Dan Fu, Stefano Ermon, Atri Rudra, and Christopher Ré. Advances in Neural Information Processing Systems. 2022. - DeltaNet explained (Part I) [link]
Songlin Yang. 2024.
Footnotes
-
Note that we omit the scaling factor for simplicity. ↩
-
Note that some hardware-aware implementations of attention, such as FlashAttention[5]FlashAttention: Fast and memory-efficient exact attention with IO-awareness [link]
Tri Dao, Dan Fu, Stefano Ermon, Atri Rudra, and Christopher Ré. Advances in Neural Information Processing Systems. 2022., can effectively compute attention without materializing the full matrix. ↩ -
We’ll temporarily ignore the causal mask , but will return to it later. It turns out to be very important. ↩