Engineering note
From Standard Attention to Linear Attention Reducing memory footprint
Notes on differences between Linear and Standard attention
Standard Attention
In a standard decode step, we have an input vector of shape and project it to three vectors: of shape . Here, is the current token query and represent the historical context for all tokens in the sequence (“past and present”).
Each time we project into and , we store it in a KV cache so we don’t have to recompute these projections on subsequent decode iterations and compute attention for current token generation:
This type of attention allows for full context sharing between tokens that are very far apart, at a cost of increasing memory just for storing this cache. In Llama 2 (one of the last models with a multi-head attention architecture), a max context window of 4k tokens occupied about 3.12 GiB of VRAM just for the cache. For comparison, modern models like DeepSeek V4 support up to 1 Million tokens.
Many architectural improvements have scaled down this memory footprint. One was GQA (Grouped-Query Attention), where instead of maintaining , groups of query heads can share a single underlying KV head, reducing the cache footprint by a factor of . There are also sequence compression tricks (like DeepSeek V4’s hybrid layers), but a new paradigm has gained traction in recent models like the Qwen 3.5 family: Linear Attention (you can see it as a GatedDeltaNet layer).
Linear Attention
Linear attention still enables tokens to exchange information, but it drops the compute and memory footprint to . By removing the softmax step, we can apply the associative property of matrix multiplication. Instead of a growing sequence allocation (of max context), we compress history into a static state matrix (known as similarity matrix), . This acts as a “collapsed KV cache” updated once per token, removing the need for each query to explicitly attend to all previous tokens, shifting complexity from down to .
This matrix of shape holds all key-value relationships within a fixed-size footprint. It acts as an associative memory table, where we can query it by computing to see what value it associates with a given key vector. In early linear attention implementations, this matrix was built up as a simple running summation of outer products: .
However, a more precise way to update this state is to apply the delta rule, as implemented in the GatedDeltaNet blocks of the Qwen 3.5 family. The delta update generates a prediction first, then applies a hardware correction matrix based on the error vector, exactly like early perceptrons:
We then compute and add the delta correction matrix to . This yields an updated version of the memory state that actively accounts for the token at . This correction ensures that querying the system with that same key immediately afterward forces the output closer to the true expected target .
Geometrically, this operation commands the matrix: “When I query with a key close to , return .” The core bottleneck with this approach is that if a subsequent token arrives at step with a highly similar key but mapped to a completely different value , the delta update will overwrite the historical trace to favor the new value.
Conclusion
This extreme memory efficiency comes at the cost of overwriting long-term dependencies. To mitigate this, Qwen 3.5 uses a hybrid 3:1 layer ratio: for every 3 Linear Attention layers, 1 standard GQA layer is interleaved to preserve exact, long-range token relationships. Additionally, a local causal convolution is applied right before entering the linear attention block. This blends adjacent tokens together and embeds local context directly into the keys, yielding distinct representations like vs. . Because their geometric layouts no longer overlap exactly, the model prevents overwriting when adjusting .