linear attention and its gated descendants
August 7, 2026
a brief history on linear attention. from its original to the deltanet variants that are increasingly popular in todays open frontier.
Before we get into linear attention at all, lets start by realising some of the issues of standard softmax attention Transformers:
- During training we have quadratic time complexity in sequence length.
- During inference (memory bound) we have linear memory complexity as the KV cache memory storage grows linearly with each token leading to high memory burden hence increasing latency.
Standard Softmax Attention
For a sequence of length L with hidden dimension d, causal attention computes:
where:
- is the causal mask
The causal mask means token can only attend to tokens .
Complexity:
So total time is:
Memory during training is also sequence-quadratic because the attention matrix is .
Without Softmax and Masking
If attention did not have softmax and causal masking, we would like to reassociate:
instead of:
The latter forms an matrix; the former first forms a matrix.
But softmax attention is:
The softmax and causal mask operate on pairwise token-token scores, so we cannot freely move parentheses and turn the whole operation into .
Linear Attention as a Linear RNN
A conventional RNN usually has a nonlinear state transition:
A linear, or more exactly affine, RNN has a transition like:
The word "linear" refers to the dependence on the previous recurrent state. It does not mean the whole model is a linear function of the input tokens.
Linear attention is a linear RNN with a matrix-valued state. Using column vectors for individual tokens:
The state is:
So is an accumulated key-value memory. Each token writes a rank-1 matrix into the state, and the current query reads from that state.
This flips the usual RNN tradeoff:
standard RNN: O(d)-sized state, O(d^2) recurrent transform
linear attention: O(d^2)-sized state, O(d^2) rank-1 update/read
The important trick is that linear attention gets a state without using a dense transition matrix. A dense transition over a -sized state would cost . The outer-product update costs only:
One nuance: for vanilla linear attention, the additive states are prefix sums in principle:
So the recurrent presentation is not the only possible training implementation. But the token-by-token recurrent form is the natural decoding view, and the chunkwise form is the hardware-practical training/prefill view.
For batched chunk notation below, are row-major matrices in . With this convention, applying a state to all queries in a chunk is written as .
Recurrent Linear Attention for Inference
During autoregressive generation, softmax attention with a KV cache still has to scan all previous tokens at every step.
At step :
and the weighted value sum is also:
So per generated token:
Across L generated tokens:
The KV cache avoids recomputing old keys and values, but it does not avoid reading over them.
For recurrent linear attention, each new token updates:
and computes:
Per token:
So across L generated tokens:
Memory changes from storing the full KV cache:
to storing one state matrix:
Key inference win: no sequence-length scan during decoding. Each new token pays fixed cost with respect to context length.
Recurrent Form for Training
The recurrent form is natural for decoding, but awkward for training/prefill.
Naively, training would require:
with one update per token:
and one output per token:
The theoretical complexity is:
which can look good asymptotically. But the computation has two practical problems:
- Dependency chain length is
L, so parallelism is poor in the naive recurrent implementation. - The operations are many small rank-1 outer products () and matrix-vector products (), which are inefficient on GPUs compared with dense matmuls.
This is the main distinction: theoretical complexity and actual hardware performance are not the same thing.
Chunkwise Parallel Form
Chunkwise parallel form is the training/prefill compromise between two bad extremes.
The full parallel causal form has a good GPU shape, but it is still quadratic in sequence length because causal masking forces explicit token-token interactions:
The fully recurrent form is theoretically linear in L, but exposes a length-L dependency chain and many small rank-1 / matrix-vector operations:
The chunkwise idea: do not process one token at a time, and do not process the whole sequence as one masked attention matrix. Process blocks.
Break the sequence into chunks of size C, giving about:
chunks.
The algorithm has two steps:
- compute the recurrent state at chunk boundaries
- compute token outputs inside each chunk
Chunkwise does not remove recurrence. State computation is still sequential across chunks; it reduces the chain from L token steps to L/C chunk steps.
Step 1: Chunk-Boundary State
The state is computed only at chunk boundaries:
where .
This works because the linear attention state is additive:
and the chunk sum of rank-1 updates is one matmul:
Cost per chunk:
Across chunks:
This is the same total arithmetic order as token-wise updates, but packaged as larger matmuls.
Step 2: Output Compute
For each chunk, split the output into:
Every token in the current chunk can see all previous chunks. That history is summarized by the boundary state :
Cost per chunk:
Across all chunks:
Tokens inside the current chunk still need causal masking relative to each other. But the mask is only , not :
Cost per chunk:
Across all chunks:
So the chunkwise output is:
Total chunkwise cost:
If C is much smaller than L, this is subquadratic in sequence length and much better than:
The limiting cases are useful:
C = 1: recurrent form, maximally sequential,C = L: full parallel masked form,- intermediate
C: block matmuls plus chunk-level recurrence
That is the appeal of chunking: it converts a long stream of small operations into dense GEMMs plus state transfer between blocks. The algorithm is more intricate than standard attention, so real performance depends on kernel quality and benchmarking, not just big-O.
mental model
Softmax attention:
all tokens x all tokens
good GPU shape, but quadratic in sequence length
Recurrent linear attention:
token -> token -> token -> token
linear in sequence length, great for decoding, poor GPU utilization for naive training
Chunkwise linear attention:
chunk -> chunk -> chunk
inside each chunk: parallel dense matmuls
between chunks: compact recurrent state
In practice chunk size C is typically set to {64, 128, 256}. The chunkwise parallel algorithm can be generalized to linear attention with decay and delta rule. It is the defacto standard for training modern linear attention models including Mamba2, GLA, Lightning Attention, DeltaNet and others.
Hardware Performance
Songlin comments on the speedup achieved after implementing both the recurrent and chunkwise version in Triton:
chunkwise parallel approach consistently outperforms the recurrent baseline...this performance advantage grows more pronounced under two key conditions: as sequences get longer and as head dimensions increase.
Why? Songlin points to two issues (1) existing recurrent implementations process token-by-token, relying instead on both batch and head dimension to saturate GPU cores:
While this strategy worked well with moderate sequence lengths and larger batch sizes, it faces challenges in modern training scenarios. Today's models increasingly work with longer sequences or larger model parameters, often necessitating smaller batch sizes for memory efficiency. This shift was notably highlighted in the FlashAttention2 paper, which identified sequence-level parallelism as crucial for training.
when batch size x head dim is small, there isn't enough parallel work to keep modern GPUs fully utilized.
(2):
Tensor cores are designed around matmuls offering up to >16x speedup compared to other operations with equivalent FLOP counts. Recurrent implementations, despite requiring fewer total FLOPs, struggle to effectively leverage these hardware accelerators...Our chunkwise implementation, in contrast, restructures the computation to maximize use of tensor cores, achieving better real-world performance despite higher theoretical FLOP counts. This performance analysis illustrates a crucial principle in modern hardware-efficient deep learning: raw FLOP counts don't always translate directly to wall-clock time. The ability to leverage specialized hardware accelerators and maintain high GPU utilization often matters more than theoretical operation counts.
Linear Attention Takeaway
Linear attention's key move is replacing explicit token-token attention with an accumulated key-value state:
This gives excellent autoregressive inference behavior because the model no longer scans the full context for every generated token.
For training/prefill, the pure recurrent form is too sequential in its naive form. Chunkwise parallel form combines the recurrent and parallel views:
- recurrent across chunks
- parallel inside chunks
- dense matmuls instead of many tiny outer products
- complexity roughly
The practical lesson is that asymptotic complexity is only part of the story. Chunkwise linear attention is a hardware-aware algorithm: it accepts a bit of extra theoretical work to expose parallelism and tensor-core-friendly matmuls.
Gated variants of Linear Attention
Unfortunately, despite the aforementioned possibilities of performance efficiency improvements, in the end the language modeling capabilities of Linear Attention fall well short of their Transformer counterparts. There's no free lunch. Compressing history into a fixed size state matrix means we cannot perfectly preserve all historical context, by design. More formally, linear attention implements a key-value associative memory, which is the sum of outer products between keys and values . This space is inherently limited by . We don't have enough "room" to store all our prevous keys. We run into memory overload: in this key-value associative memory system, we can only add new key-value associations without the ability to erase existing information. As sequences grow longer, this leads to accumulating "retrieval errors" that degrade performance.
To address this limitation research moved to finding ways to incorporate forgetting mechanisms.
linear attention with data-independent decay
One of the most obvious things would be to realize that generally, distant tokens are less important than recent ones, so just add a decay factor to the state:
where . This works quite well in practice, was used by MiniMax in their Lightning Attention. This is a simple exponential moving average of the state.
linear attention with data-dependent decay
the previous variant uses a fixed decay rate, independent of the input data. however we can imagine that for different positions we want the model to have flexibility of adjusting the decay rate and that how you end up with data-dependent decay rates.
Here we say that is data dependent, meaning that it is a function of the input data . For example you could have a linear projection of to compute gamma, in the same way q,k,v are computed.
This type of decay is employed in Mamba2, mLSTM and more.
linear attention with fine-grained decay
We can imagine a more fine-grained decay mechanism. While was a simple decay scalar work like GLA uses a more fine-grained d x d matrix to decay the state
To summarize this section:
- Language modeling has strong recency bias
- Decay helps bridge perplexity gap between linear attention and softmax attention
- Fine-grained decay improves performance but faces scaling issues. Outer-product based decay structures are required to enable chunkwise forms as set up earlier
DeltaNet
Towards an even more expressive update rule.
The previous decay variants add forgetting, but they still do not directly solve the key-value memory problem. Vanilla linear attention is append-only:
Every new key-value association is added to memory, whether or not a similar key already exists. This muddies the memory as we accumulate overlapping associations.
The associative memory view makes this clear. Since:
retrieving with a key/query-like vector gives:
For example, with multiple entries:
Querying with gives:
If keys are normalized and roughly orthogonal:
so:
If keys were perfectly orthogonal, then querying with would isolate . In reality keys live in limited -dimensional space, so they interfere. The state matrix has finite capacity and memory quality degrades as more associations are added. The model has no way of solving this by itself, it is forced into "memory overload".
DeltaNet changes the update from unconditional addition to an error-correction rule:
Here is what the current memory already retrieves for key . This is the "prediction" in the delta rule sense. is the target value we want memory to return for this key.
So DeltaNet says: look up the old value for this key, compare it with the new value, and write the correction rather than blindly adding the full value again.
Let:
Then:
This can be interpreted as erase + write:
and:
So controls how strongly the new value overrides the old retrieved value:
- : keep old memory
- : fully replace the retrieved value with
- : blend old and new
The important conceptual move:
vanilla linear attention: append-only associative memory
DeltaNet: editable associative memory
This connects directly to the no-free-lunch problem. Since the state matrix has limited capacity, we need a better policy for using that capacity. DeltaNet makes the write operation depend on what is already stored, using the prediction error to update memory more carefully.
Gated DeltaNet
Gated DeltaNet is a simple extension of DeltaNet that adds a Mamba2-like decay gate to the state update.
Recall Mamba2-style gated linear attention:
where is a data-dependent gate that controls how much old state is preserved.
DeltaNet instead performs targeted memory editing:
Gated DeltaNet combines these:
Equivalently:
So the prediction is now made from the decayed previous memory:
Kimi Delta Attention
Kimi Delta Attention (KDA), used in Kimi Linear and Kimi K3, is best understood as a refinement of Gated DeltaNet.
The main conceptual change is:
Gated DeltaNet: one forget gate per head
KDA: one forget gate per key channel inside each head
Before writing the KDA update, it is useful to make the multi-head structure explicit. Linear attention variants still fit into the usual multi-head attention architecture:
x_t
-> project to q, k, v for each head
-> each head runs its own recurrent state update
-> concatenate head outputs
-> output projection
For one head:
and the recurrent state is:
This follows the Kimi paper convention, where the output is read as:
So each head has its own associative memory. If there are heads, the total state per layer is:
not:
This is important because it keeps the recurrent state manageable.
In Gated DeltaNet, the forget gate is a scalar per head:
and the update is:
This means the whole memory matrix for that head is decayed by the same amount.
KDA changes from a scalar into a vector:
and applies it as a diagonal matrix over the key/address dimension:
making KDA's recurrent form:
The clean way to read this is in two stages.
Decay Old Memory
This scales the rows of , meaning each key/address channel gets its own forgetting rate:
key channel 1: decay by alpha_1
key channel 2: decay by alpha_2
key channel 3: decay by alpha_3
...
Apply Delta-Rule Update
Then apply the usual delta-rule correction to the decayed memory:
So the prediction is not made from the raw old memory. It is made from the already-forgotten memory:
Then KDA writes the error correction:
This gives and separate roles:
- : what old memory survives into this step
- : how strongly the new association is written
where does this leave us?
A good layered understanding is:
- Linear Attention Memory is append-only:
- DeltaNet Memory becomes editable via prediction-error correction:
- Gated DeltaNet Memory gets a forget gate:
- KDA Forgetting becomes channel-wise:
The important structural shift is that from DeltaNet onward, the state update is no longer only additive. Vanilla linear attention has:
DeltaNet, Gated DeltaNet, and KDA have multiplicative state transitions:
where for KDA:
Now each token does not merely add something to the state. It also transforms the previous state before writing. Important to remember that the chunkwise parallel form remains integral to be able to implement these attention variants.