How can softmax attention be written as a kernel smoother? Why does replacing by a feature-map kernel reduce the cost from to ? How can causal linear attention be computed as an RNN, and what is lost?
Sequence models and transformers, Efficient attention
Level L4 Advanced. Length Medium. Nature Mat Mathematical.
What's being asked
Whether you can use the associativity of matrix products to avoid forming the matrix, and understand what this costs in expressiveness.
Key idea
A kernel smoother outputs a weighted average of values, with weights from a similarity function (the kernel). With a feature map , , with and . The sums and are computed once and shared by all queries.
Steps
- Kernel form of softmax attention: with . Computing all costs .
- Replace by , where maps to non-negative features. Pull out of the sums: .
- Cost, with the value dimension: takes , and each output , so in total. This is linear in ( when ). In matrix form, compute instead of .
- Causal case: use prefix sums. , , . This is an RNN with a matrix-valued state of fixed size , so generation needs constant memory per step.
- Choices of : (Katharopoulos et al., 2020), or positive random features that approximate without bias (Performer).
- What is lost: the attention matrix has rank at most and cannot be as sharply peaked as softmax, so exact recall from a long context gets worse. A fixed-size state cannot store an unbounded history. This motivates gated variants and state-space models.
Common mistakes
- Dropping the normaliser , so the weights no longer sum to 1.
- Using a that can be negative, which can make the denominator zero or negative.
- Claiming it is exactly softmax attention. Only an infinite-dimensional feature map recovers exactly.
- Forgetting that when the linear form is not cheaper.
How long your answer should be
Half a page; 5 minutes.
Last checked on 4 December 2026.
Read this
Previous in Sequence models and transformers: What is the sinusoidal positional encoding, and why is a linear function of ? How do rotary position embeddings (RoPE) make attention scores depend only on relative position?
Next in Sequence models and transformers: A transformer block can be written post-LN, , or pre-LN, . How do gradients flow back through layers in the two, and why does post-LN usually need learning-rate warm-up?