Pratham Patel
· 38 min read

Why Replace Attention? The Softmax Bottleneck and the Path to Linear Time

Why softmax is the bottleneck that every attention variant leaves untouched — and how rewriting attention as a kernel exposes an associativity trick that collapses the O(n²) cost to O(n) and turns the transformer into an RNN.

Every change made to attention so far has touched the periphery — which keys participate, how they are stored, what wraps the block — but left the formula

oi=jexp(qikj/dk)jexp(qikj/dk)vjo_i = \sum_{j} \frac{\exp(q_i^\top k_j / \sqrt{d_k})}{\sum_{j'} \exp(q_i^\top k_{j'} / \sqrt{d_k})} \, v_j

untouched. The softmax stayed.

This blog asks the next question: what if the softmax is the problem? The exponential and the denominator force every query to see every key before producing an output. That is what makes attention quadratic. Drop the softmax and the algebra changes — multiplication becomes associative, and a different grouping turns O(n2)O(n^2) into O(n)O(n) with constant memory per token. The transformer becomes an RNN.

The core paper for this blog is Katharopoulos et al. (2020), “Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention,” supplemented by the taxonomy from the Efficient Transformers survey (Tay et al., 2022).


The Running Example

We continue with the same model parameters from the series:

  • dmodel=512d_\text{model} = 512, h=8h = 8 heads, dk=dv=64d_k = d_v = 64, L=12L = 12 layers, fp16

For cost analysis, we use n=16n = 16 tokens (same sequence from the Sparse Factorization, Sliding Window, and DeepSeek Sparse Attention blogs) and scale up to n=128,000n = 128{,}000 to show how costs grow.

For the kernel and associativity derivations, we need to trace every matrix entry by hand. We use a tiny example:

  • n=4n = 4 tokens, dk=dv=2d_k = d_v = 2, single head

with concrete query, key, and value matrices:

Q=(10011120),K=(01101102),V=(10011120)Q = \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \\ 2 & 0 \end{pmatrix}, \quad K = \begin{pmatrix} 0 & 1 \\ 1 & 0 \\ 1 & 1 \\ 0 & 2 \end{pmatrix}, \quad V = \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \\ 2 & 0 \end{pmatrix}

Each row is one token. QQ has 4 rows (one per query), KK has 4 rows (one per key), VV has 4 rows (one per value). All matrices are 4×24 \times 2.


1. The Cost Ledger After Twelve Blogs

1.1 What we modified, axis by axis

Every blog in the series made a change to exactly one axis of the taxonomy from the Taxonomy blog. Here is the complete map:

BlogTopicAxisWhat changed
1–3Attention basicsSetup and motivation
4TaxonomyThe five-axis framework
5Residuals5Skip connections
6MQA / KV Bottleneck2Shared KV across heads
7GQA2Grouped KV sharing
8MLA2Low-rank KV compression
9Sparse Transformer3Fixed factorized patterns
10Sliding Window3Local windows + global tokens
11DeepSeek Sparse3Learned token selection
12Gated Attention5Gated residuals and SwiGLU FFN

Axes 2, 3, and 5 have been thoroughly explored. Axis 1 (number of heads) is implicitly handled by GQA. Axis 4 (KV cache and storage) was touched by MLA’s compressed latent.

1.2 What remains unchanged

Despite all these modifications, every blog preserved two things:

  1. The softmax normalization. Every variant computes exp(qikj/dk)\exp(q_i^\top k_j / \sqrt{d_k}) for the query-key pairs that participate, then normalizes by the sum of exponentials. This forces the model to evaluate all participating pairs before producing any output.

  2. The per-query dependence on all keys. Even in the sparse variants, the attention output for query ii depends on the full set of selected keys through the softmax denominator. You cannot compute the output for token ii without knowing the scores for all tokens in its connectivity set.

These two properties have a direct consequence: the attention operation cannot be decomposed into independent per-token computations. Every query must “see” its full key set before producing an output. This is the fundamental difference between attention and recurrence.


2. The Cost at Scale

2.1 Training: FLOPs per attention layer

For one head of standard causal attention on nn tokens with head dimension dkd_k, the dominant operations are:

Step 1: Compute QKQK^\top. This is an (n×dk)×(dk×n)(n \times d_k) \times (d_k \times n) matrix multiplication, producing an n×nn \times n matrix. The number of multiply-add operations is:

n×n×dk=n2dkn \times n \times d_k = n^2 d_k

Step 2: Apply softmax. This requires exponentiation and normalization over each row — O(n)O(n) per row, O(n2)O(n^2) total. Dominated by the matrix multiply.

Step 3: Compute AVAV. This is an (n×n)×(n×dv)(n \times n) \times (n \times d_v) matrix multiplication, producing an n×dvn \times d_v matrix:

n×dv×n=n2dvn \times d_v \times n = n^2 d_v

With dk=dvd_k = d_v, the total per-head cost is:

FLOPs per head=2n2dk\boxed{\text{FLOPs per head} = 2n^2 d_k}

Across hh heads:

FLOPs per layer=h×2n2dk=2n2(hdk)=2n2dmodel\text{FLOPs per layer} = h \times 2n^2 d_k = 2n^2 (h \, d_k) = 2n^2 d_\text{model}

The last step uses hdk=dmodelh \, d_k = d_\text{model}, the identity that has appeared throughout this series.

2.2 Numerical check

With n=16n = 16, dmodel=512d_\text{model} = 512:

2×162×512=2×256×512=262,144 FLOPs per layer2 \times 16^2 \times 512 = 2 \times 256 \times 512 = 262{,}144 \text{ FLOPs per layer}

With n=128,000n = 128{,}000:

2×128,0002×512=2×1.6384×1010×512=1.678×1013 FLOPs per layer2 \times 128{,}000^2 \times 512 = 2 \times 1.6384 \times 10^{10} \times 512 = 1.678 \times 10^{13} \text{ FLOPs per layer}

That is 16.78 trillion FLOPs per layer for a single forward pass on 128K tokens. Across L=12L = 12 layers:

12×1.678×1013=2.01×1014 FLOPs12 \times 1.678 \times 10^{13} = 2.01 \times 10^{14} \text{ FLOPs}

This is just the attention — the FFN adds a comparable amount (2×dmodel×dff×n=2×512×2048×128,0002.68×10112 \times d_\text{model} \times d_{ff} \times n = 2 \times 512 \times 2048 \times 128{,}000 \approx 2.68 \times 10^{11} per layer, much smaller than the attention cost at this sequence length). At n=128,000n = 128{,}000, attention dominates.

2.3 The crossover point

The FFN cost per layer is 2dmodeldffn2 \, d_\text{model} \, d_{ff} \, n, which is linear in nn. The attention cost is 2n2dmodel2 \, n^2 \, d_\text{model}, which is quadratic in nn. Setting them equal:

2n2dmodel=2dmodeldffn2 \, n^2 \, d_\text{model} = 2 \, d_\text{model} \, d_{ff} \, n

The 2dmodel2 \, d_\text{model} cancels from both sides:

n2=dffnn^2 = d_{ff} \, n

Divide both sides by nn (valid since n>0n > 0):

ncrossover=dff\boxed{n_\text{crossover} = d_{ff}}

With dff=2048d_{ff} = 2048, the crossover is at n=2,048n = 2{,}048 tokens. For sequences shorter than 2,048 tokens, the FFN costs more than attention. For sequences longer than 2,048 tokens, attention dominates — and it dominates more with every additional token.

2.4 Numerical check

At n=2,048n = 2{,}048:

Attention: 2×2,0482×512=2×4,194,304×512=4.295×109\text{Attention: } 2 \times 2{,}048^2 \times 512 = 2 \times 4{,}194{,}304 \times 512 = 4.295 \times 10^9 FFN: 2×512×2,048×2,048=4.295×109\text{FFN: } 2 \times 512 \times 2{,}048 \times 2{,}048 = 4.295 \times 10^9 \quad \checkmark

They match exactly at the crossover point.

At n=128,000n = 128{,}000: attention is 1.678×10131.678 \times 10^{13}, FFN is 2.68×10112.68 \times 10^{11}. The ratio is 1.678×1013/2.68×101162.6×1.678 \times 10^{13} / 2.68 \times 10^{11} \approx 62.6\times. Attention costs 63 times more than the FFN.

2.5 What sparse methods achieve

Our sparse methods from the Sparse Factorization, Sliding Window, and DeepSeek Sparse Attention blogs reduce the attention cost:

MethodAttention FLOPs per layerAt n=128,000n = 128{,}000
Full2n2dmodel2n^2 d_\text{model}1.678×10131.678 \times 10^{13}
Sparse Transformer (O(nn)O(n\sqrt{n}))2n3/2dmodelc2n^{3/2} d_\text{model} \cdot c4.7×1010\sim 4.7 \times 10^{10}
Sliding window (w=512w = 512)2nwdmodel2nw \, d_\text{model}6.71×1010\sim 6.71 \times 10^{10}
DSA (k=2,048k = 2{,}048)2nkdmodel2nk \, d_\text{model}2.68×1011\sim 2.68 \times 10^{11}

These are enormous improvements. But notice: even the cheapest method (Sparse Transformer) still has a cost that grows super-linearly with nn. And all three methods still require the softmax over the selected pairs — the O(St)O(|S_t|) normalization per query token. The cost is reduced but the mechanism is the same.

2.6 Interpretation

Sparse methods do not change the nature of the computation. They reduce how many pairs participate, but each participating pair still requires the full softmax pipeline: exponentiate, sum, normalize, multiply by values. The question this blog asks is: can we change the mechanism itself to eliminate the need for pairwise computation entirely?


3. The KV Cache Wall

3.1 Memory during autoregressive generation

During training, we pay the quadratic cost once for the full sequence. During inference (autoregressive generation), the cost is paid incrementally: each new token computes attention against all previous tokens.

For token tt (the tt-th generated token), the attention computation requires loading all previous key and value vectors from the KV cache. Each token stores:

Bytes per token=2×h×dk×2=4hdk=4dmodel bytes\text{Bytes per token} = 2 \times h \times d_k \times 2 = 4 h \, d_k = 4 \, d_\text{model} \text{ bytes}

The factor of 2 at the front accounts for both keys and values. The factor of 2 at the end is for fp16 (2 bytes per element). We simplify using hdk=dmodelh \, d_k = d_\text{model}.

Across LL layers:

Bytes per token, all layers=4dmodelL\text{Bytes per token, all layers} = 4 \, d_\text{model} \, L

3.2 Numerical check

With dmodel=512d_\text{model} = 512 and L=12L = 12:

4×512×12=24,576 bytes=24 KB per token4 \times 512 \times 12 = 24{,}576 \text{ bytes} = 24 \text{ KB per token}

After generating nn tokens, the KV cache occupies:

Total KV cache=4dmodelLn\text{Total KV cache} = 4 \, d_\text{model} \, L \, n

At n=128,000n = 128{,}000:

4×512×12×128,000=3.15×109 bytes3.15 GB4 \times 512 \times 12 \times 128{,}000 = 3.15 \times 10^9 \text{ bytes} \approx 3.15 \text{ GB}

For a model with dmodel=512d_\text{model} = 512, this is manageable. But real models are much larger. For a model with dmodel=8,192d_\text{model} = 8{,}192 (comparable to GPT-4 class models) and L=80L = 80 layers:

4×8,192×80×128,000=3.36×1011 bytes336 GB4 \times 8{,}192 \times 80 \times 128{,}000 = 3.36 \times 10^{11} \text{ bytes} \approx 336 \text{ GB}

That is 336 GB just for the KV cache — more than the memory of most GPUs. This is the KV cache wall: the point where the memory required to store past keys and values exceeds available GPU memory.

3.3 The per-token generation cost

When generating token tt, the attention computation for one head involves:

  1. Compute qtkjq_t^\top k_j for all j{1,,t}j \in \{1, \ldots, t\}: t×dkt \times d_k multiply-adds
  2. Apply softmax over tt scores: O(t)O(t) operations
  3. Compute weighted sum jαjvj\sum_j \alpha_j v_j: t×dvt \times d_v multiply-adds

Total per head: 2tdk2t \, d_k. Across all heads and layers:

FLOPs for token t=2tdk×h×L=2tdmodelL\text{FLOPs for token } t = 2t \, d_k \times h \times L = 2t \, d_\text{model} \, L

The cost to generate token tt grows linearly with tt — each new token is slower than the last.

3.4 Numerical check

Generating the 128,000th token:

2×128,000×512×12=1.57×109 FLOPs2 \times 128{,}000 \times 512 \times 12 = 1.57 \times 10^9 \text{ FLOPs}

Generating the 1st token:

2×1×512×12=12,288 FLOPs2 \times 1 \times 512 \times 12 = 12{,}288 \text{ FLOPs}

The last token costs 128,000×128{,}000\times more than the first. In wall-clock time, this means generation slows down as the sequence gets longer — each successive token takes more time to produce.

3.5 The total generation cost

To generate all nn tokens:

Total FLOPs=t=1n2tdmodelL=2dmodelLt=1nt=2dmodelLn(n+1)2=dmodelLn(n+1)\text{Total FLOPs} = \sum_{t=1}^{n} 2t \, d_\text{model} \, L = 2 \, d_\text{model} \, L \sum_{t=1}^{n} t = 2 \, d_\text{model} \, L \cdot \frac{n(n+1)}{2} = d_\text{model} \, L \, n(n+1)

The sum t=1nt=n(n+1)2\sum_{t=1}^n t = \frac{n(n+1)}{2} is Gauss’s summation formula. For large nn:

Total generation FLOPsdmodelLn2\boxed{\text{Total generation FLOPs} \approx d_\text{model} \, L \, n^2}

Quadratic in nn, again.

3.6 What an RNN gives you

Contrast this with a recurrent neural network. An RNN maintains a fixed-size hidden state htRdh_t \in \mathbb{R}^{d} and updates it at each step:

ht=f(ht1,xt)h_t = f(h_{t-1}, x_t)

The cost to generate each token is constant — O(d2)O(d^2) for the matrix-vector multiplication in ff, regardless of position tt. The total cost for nn tokens is O(nd2)O(n d^2) — linear in nn. The memory is O(d)O(d) — constant, regardless of how many tokens have been generated.

PropertyAttentionRNN
State size at step ttO(td)O(t \cdot d) — growsO(d2)O(d^2) — constant
Cost per token at step ttO(td)O(t \cdot d) — growsO(d2)O(d^2) — constant
Total cost for nn tokensO(n2d)O(n^2 d) — quadraticO(nd2)O(n d^2) — linear
Can access token 1 from token nn?Yes, directlyOnly through state

The last row is the tradeoff. Attention pays a growing cost because it maintains direct access to every past token. An RNN pays a constant cost because it compresses all past information into a fixed-size state — but that compression is lossy.

The question is: can we get the best of both worlds? Linear cost like an RNN, but with the expressivity of attention?

The answer begins with examining why the softmax makes the cost quadratic.


4. The Softmax Bottleneck

4.1 The attention output for one query

Let us write the attention output for a single query token ii in full generality. For a single head:

oi=j=1nexp ⁣(qikjdk)vjj=1nexp ⁣(qikjdk)o_i = \frac{\sum_{j=1}^{n} \exp\!\left(\frac{q_i^\top k_j}{\sqrt{d_k}}\right) v_j}{\sum_{j=1}^{n} \exp\!\left(\frac{q_i^\top k_j}{\sqrt{d_k}}\right)}

This is a weighted average of the value vectors vjv_j, where the weight on vjv_j is proportional to exp(qikj/dk)\exp(q_i^\top k_j / \sqrt{d_k}).

Let us define sim(q,k)\text{sim}(q, k) as the similarity function between a query and a key:

sim(q,k)=exp ⁣(qkdk)\text{sim}(q, k) = \exp\!\left(\frac{q^\top k}{\sqrt{d_k}}\right)

Then the attention output becomes:

oi=j=1nsim(qi,kj)vjj=1nsim(qi,kj)\boxed{o_i = \frac{\sum_{j=1}^{n} \text{sim}(q_i, k_j) \, v_j}{\sum_{j=1}^{n} \text{sim}(q_i, k_j)}}

This is the form we will work with for the rest of this blog. The specific choice of similarity function determines everything.

4.2 Why softmax forces pairwise computation

This is the part that is easy to gloss over but is the crux of the entire blog.

With the exponential similarity sim(q,k)=exp(qk/dk)\text{sim}(q, k) = \exp(q^\top k / \sqrt{d_k}), the weight for the pair (i,j)(i, j) depends on the specific combination of qiq_i and kjk_j. The exponential of a dot product cannot be factored:

exp(qikj/dk)f(qi)g(kj)\exp(q_i^\top k_j / \sqrt{d_k}) \neq f(q_i) \cdot g(k_j)

for any scalar functions ff and gg. The reason is that the dot product qikj=mqi,mkj,mq_i^\top k_j = \sum_m q_{i,m} \, k_{j,m} mixes the components of qiq_i and kjk_j inside the exponential, and exp(a+b)=exp(a)exp(b)\exp(a + b) = \exp(a) \exp(b) only factors when the argument is a sum — but the sum is over the dkd_k dimensions, not over the queries and keys.

To be precise: exp(qikj/dk)=exp ⁣(mqi,mkj,m/dk)=mexp(qi,mkj,m/dk)\exp(q_i^\top k_j / \sqrt{d_k}) = \exp\!\left(\sum_m q_{i,m} k_{j,m} / \sqrt{d_k}\right) = \prod_m \exp(q_{i,m} k_{j,m} / \sqrt{d_k}). Each factor in the product involves both qi,mq_{i,m} and kj,mk_{j,m} multiplicatively inside the exponential. There is no way to separate this into “a function of qiq_i only” times “a function of kjk_j only” using a finite number of terms.

This means we must compute sim(qi,kj)\text{sim}(q_i, k_j) for every pair (i,j)(i, j) separately. There are n2n^2 such pairs. No algebraic rearrangement can avoid this.

4.3 Numerical illustration

Let us verify this with our running example. Take query q1=(1,0)q_1 = (1, 0) and compute its similarity with all 4 keys (using dk=2\sqrt{d_k} = \sqrt{2}):

sim(q1,k1)=exp ⁣((1)(0)+(0)(1)2)=exp(0)=1.000\text{sim}(q_1, k_1) = \exp\!\left(\frac{(1)(0) + (0)(1)}{\sqrt{2}}\right) = \exp(0) = 1.000 sim(q1,k2)=exp ⁣((1)(1)+(0)(0)2)=exp(0.707)=2.028\text{sim}(q_1, k_2) = \exp\!\left(\frac{(1)(1) + (0)(0)}{\sqrt{2}}\right) = \exp(0.707) = 2.028 sim(q1,k3)=exp ⁣((1)(1)+(0)(1)2)=exp(0.707)=2.028\text{sim}(q_1, k_3) = \exp\!\left(\frac{(1)(1) + (0)(1)}{\sqrt{2}}\right) = \exp(0.707) = 2.028 sim(q1,k4)=exp ⁣((1)(0)+(0)(2)2)=exp(0)=1.000\text{sim}(q_1, k_4) = \exp\!\left(\frac{(1)(0) + (0)(2)}{\sqrt{2}}\right) = \exp(0) = 1.000

Each similarity score is different and depends on both q1q_1 and the specific key. We had to compute 4 exponentials — one per key. For all 4 queries, we would need 4×4=164 \times 4 = 16 exponentials. In general: n2n^2.

4.4 The denominator forces full evaluation

Even if we only cared about one value in the output oio_i, we would still need all nn similarity scores for query ii because of the denominator j=1nsim(qi,kj)\sum_{j=1}^n \text{sim}(q_i, k_j). The denominator is a sum over all keys — you cannot know the correct normalization without evaluating every key.

This is the fundamental constraint. The softmax denominator couples all keys together for each query. It prevents streaming or incremental computation of the output.

4.5 What if we could factor the similarity?

Suppose instead we had a similarity function that could be written as:

sim(q,k)=ϕ(q)ϕ(k)\text{sim}(q, k) = \phi(q)^\top \phi(k)

for some feature map ϕ:RdkRdϕ\phi : \mathbb{R}^{d_k} \to \mathbb{R}^{d_\phi} that maps each query or key independently into a new space of dimension dϕd_\phi. Then:

oi=j=1nϕ(qi)ϕ(kj)vjj=1nϕ(qi)ϕ(kj)o_i = \frac{\sum_{j=1}^{n} \phi(q_i)^\top \phi(k_j) \, v_j}{\sum_{j=1}^{n} \phi(q_i)^\top \phi(k_j)}

Now ϕ(qi)ϕ(kj)\phi(q_i)^\top \phi(k_j) is a scalar (a dot product in the feature space), so ϕ(qi)ϕ(kj)vj\phi(q_i)^\top \phi(k_j) \, v_j is a scalar times a vector. The sum jϕ(qi)ϕ(kj)vj\sum_j \phi(q_i)^\top \phi(k_j) \, v_j involves ϕ(qi)\phi(q_i) interacting with the ϕ(kj)\phi(k_j) vectors, and crucially, we can rearrange this sum. This rearrangement is the key to everything that follows.


5. Attention as a Kernel Function

5.1 The kernel interpretation

The formulation sim(q,k)=ϕ(q)ϕ(k)\text{sim}(q, k) = \phi(q)^\top \phi(k) is a kernel function from the theory of reproducing kernel Hilbert spaces. A kernel κ(x,y)\kappa(x, y) is any function that can be written as an inner product in some (possibly high-dimensional) feature space:

κ(x,y)=ϕ(x),ϕ(y)\kappa(x, y) = \langle \phi(x), \phi(y) \rangle

The function ϕ\phi is called the feature map. It transforms inputs from the original space into a feature space where inner products correspond to similarities.

This is the connection to kernel methods in machine learning — the same mathematical framework behind support vector machines and Gaussian processes. The survey by Tay et al. (2022) categorizes Linear Transformers (Katharopoulos et al., 2020) and Performers (Choromanski et al., 2020) under the “Low Rank / Kernels” class of efficient transformers, precisely because they exploit this kernel structure.

5.2 Can softmax attention be written as a kernel?

The softmax similarity sim(q,k)=exp(qk/dk)\text{sim}(q, k) = \exp(q^\top k / \sqrt{d_k}) is indeed a valid kernel. It can be written as an inner product in a feature space — but that feature space is infinite-dimensional. By the Taylor expansion of the exponential function:

exp(qk/dk)=m=0(qk/dk)mm!\exp(q^\top k / \sqrt{d_k}) = \sum_{m=0}^{\infty} \frac{(q^\top k / \sqrt{d_k})^m}{m!}

Each term (qk)m(q^\top k)^m can be expanded as a sum of products of components of qq and kk, which corresponds to a feature map that includes all monomials of degree mm in the components of qq (and similarly for kk). The full feature map includes monomials of all degrees — an infinite-dimensional vector.

So the softmax kernel has a feature map ϕ\phi, but ϕ(q)\phi(q) is an infinite-dimensional vector. We cannot compute ϕ(q)ϕ(k)\phi(q)^\top \phi(k) by first computing ϕ(q)\phi(q) and ϕ(k)\phi(k) separately — the vectors have infinitely many entries. We are forced to compute the kernel value exp(qk/dk)\exp(q^\top k / \sqrt{d_k}) directly, which brings us back to pairwise computation.

5.3 The key idea: use a different kernel

Katharopoulos et al. (2020) propose a simple solution: replace the softmax kernel with a kernel that has a finite-dimensional feature map. Instead of sim(q,k)=exp(qk/dk)\text{sim}(q, k) = \exp(q^\top k / \sqrt{d_k}), use:

sim(q,k)=ϕ(q)ϕ(k)\text{sim}(q, k) = \phi(q)^\top \phi(k)

where ϕ:RdkRdϕ\phi : \mathbb{R}^{d_k} \to \mathbb{R}^{d_\phi} is a finite-dimensional feature map. Their specific choice is:

ϕ(x)=elu(x)+1\boxed{\phi(x) = \text{elu}(x) + 1}

where elu is the exponential linear unit (Clevert et al., 2015):

elu(x)={xif x>0ex1if x0\text{elu}(x) = \begin{cases} x & \text{if } x > 0 \\ e^x - 1 & \text{if } x \leq 0 \end{cases}

Applied element-wise to a vector xRdkx \in \mathbb{R}^{d_k}, this gives ϕ(x)Rdk\phi(x) \in \mathbb{R}^{d_k} — the feature map has the same dimension as the input (dϕ=dkd_\phi = d_k). The +1+1 ensures that all components of ϕ(x)\phi(x) are non-negative, which guarantees that the similarity ϕ(q)ϕ(k)0\phi(q)^\top \phi(k) \geq 0 — a necessary property since attention weights must be non-negative.

5.4 Why non-negativity matters

In softmax attention, exp(qk/dk)>0\exp(q^\top k / \sqrt{d_k}) > 0 always — the exponential function is strictly positive. This guarantees positive attention weights, which means the output is a proper weighted average of the value vectors.

If we replace the similarity with ϕ(q)ϕ(k)\phi(q)^\top \phi(k) and ϕ\phi maps to non-negative outputs, then the dot product of two non-negative vectors is non-negative: ϕ(q)ϕ(k)=mϕ(q)mϕ(k)m0\phi(q)^\top \phi(k) = \sum_m \phi(q)_m \phi(k)_m \geq 0 since every term is a product of non-negative numbers. This preserves the weighted-average interpretation.

5.5 Numerical example: computing the feature map

Let us apply ϕ(x)=elu(x)+1\phi(x) = \text{elu}(x) + 1 to the queries and keys from our running example.

Queries:

ϕ(q1)=ϕ ⁣(10)=(elu(1)+1elu(0)+1)=(1+10+1)=(21)\phi(q_1) = \phi\!\begin{pmatrix} 1 \\ 0 \end{pmatrix} = \begin{pmatrix} \text{elu}(1) + 1 \\ \text{elu}(0) + 1 \end{pmatrix} = \begin{pmatrix} 1 + 1 \\ 0 + 1 \end{pmatrix} = \begin{pmatrix} 2 \\ 1 \end{pmatrix}

For x>0x > 0, elu(x)=x\text{elu}(x) = x, so ϕ(x)=x+1\phi(x) = x + 1. For x=0x = 0, elu(0)=0\text{elu}(0) = 0, so ϕ(0)=0+1=1\phi(0) = 0 + 1 = 1.

ϕ(q2)=ϕ ⁣(01)=(12)\phi(q_2) = \phi\!\begin{pmatrix} 0 \\ 1 \end{pmatrix} = \begin{pmatrix} 1 \\ 2 \end{pmatrix} ϕ(q3)=ϕ ⁣(11)=(22)\phi(q_3) = \phi\!\begin{pmatrix} 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 2 \\ 2 \end{pmatrix} ϕ(q4)=ϕ ⁣(20)=(31)\phi(q_4) = \phi\!\begin{pmatrix} 2 \\ 0 \end{pmatrix} = \begin{pmatrix} 3 \\ 1 \end{pmatrix}

Keys:

ϕ(k1)=ϕ ⁣(01)=(12)\phi(k_1) = \phi\!\begin{pmatrix} 0 \\ 1 \end{pmatrix} = \begin{pmatrix} 1 \\ 2 \end{pmatrix} ϕ(k2)=ϕ ⁣(10)=(21)\phi(k_2) = \phi\!\begin{pmatrix} 1 \\ 0 \end{pmatrix} = \begin{pmatrix} 2 \\ 1 \end{pmatrix} ϕ(k3)=ϕ ⁣(11)=(22)\phi(k_3) = \phi\!\begin{pmatrix} 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 2 \\ 2 \end{pmatrix} ϕ(k4)=ϕ ⁣(02)=(13)\phi(k_4) = \phi\!\begin{pmatrix} 0 \\ 2 \end{pmatrix} = \begin{pmatrix} 1 \\ 3 \end{pmatrix}

5.6 Numerical check: kernel similarity vs dot product

Let us verify that ϕ(qi)ϕ(kj)\phi(q_i)^\top \phi(k_j) gives a reasonable similarity measure. Compare with the softmax similarity from Section 4.3 for q1q_1:

Pairϕ(q1)ϕ(kj)\phi(q_1)^\top \phi(k_j)exp(q1kj/2)\exp(q_1^\top k_j / \sqrt{2})
(1,1)(1, 1)(2)(1)+(1)(2)=4(2)(1) + (1)(2) = 41.0001.000
(1,2)(1, 2)(2)(2)+(1)(1)=5(2)(2) + (1)(1) = 52.0282.028
(1,3)(1, 3)(2)(2)+(1)(2)=6(2)(2) + (1)(2) = 62.0282.028
(1,4)(1, 4)(2)(1)+(1)(3)=5(2)(1) + (1)(3) = 51.0001.000

The rankings differ: the kernel similarity ranks key 3 highest (score 6) while softmax gives keys 2 and 3 equal scores (both 2.028). The two similarity functions are not identical — they define different attention distributions. The question is whether the kernel version, despite being different, can still produce useful representations. Katharopoulos et al. (2020) show empirically that it can, with competitive performance on language modeling and speech recognition tasks.

5.7 The generalized attention formula

With the kernel similarity, the attention output for query ii becomes:

oi=j=1nϕ(qi)ϕ(kj)vjj=1nϕ(qi)ϕ(kj)o_i = \frac{\sum_{j=1}^{n} \phi(q_i)^\top \phi(k_j) \, v_j}{\sum_{j=1}^{n} \phi(q_i)^\top \phi(k_j)}

This looks identical to the softmax version, just with a different similarity function. The cost appears to be the same: n2n^2 dot products in the feature space. But there is a crucial algebraic difference that we have not yet exploited.


6. The Associativity Trick

6.1 The rearrangement

This is the core mathematical insight of the entire blog. It is a single algebraic step, but it changes the complexity from quadratic to linear.

Consider the numerator for query ii:

numi=j=1nϕ(qi)ϕ(kj)vj\text{num}_i = \sum_{j=1}^{n} \phi(q_i)^\top \phi(k_j) \, v_j

The term ϕ(qi)ϕ(kj)\phi(q_i)^\top \phi(k_j) is a scalar (a dot product of two dϕd_\phi-dimensional vectors). We can write it as:

ϕ(qi)ϕ(kj)=m=1dϕϕ(qi)mϕ(kj)m\phi(q_i)^\top \phi(k_j) = \sum_{m=1}^{d_\phi} \phi(q_i)_m \, \phi(k_j)_m

Substituting into the numerator:

numi=j=1n(m=1dϕϕ(qi)mϕ(kj)m)vj\text{num}_i = \sum_{j=1}^{n} \left(\sum_{m=1}^{d_\phi} \phi(q_i)_m \, \phi(k_j)_m\right) v_j

Now we swap the order of summation. The sum over jj and the sum over mm are both finite, so we can exchange them by Fubini’s theorem (interchanging finite sums):

numi=m=1dϕϕ(qi)m(j=1nϕ(kj)mvj)\text{num}_i = \sum_{m=1}^{d_\phi} \phi(q_i)_m \left(\sum_{j=1}^{n} \phi(k_j)_m \, v_j\right)

The inner sum j=1nϕ(kj)mvj\sum_{j=1}^n \phi(k_j)_m \, v_j does not depend on ii at all — it is a fixed vector that combines all keys and values, independent of which query we are computing.

6.2 The matrix form

To see this more cleanly, let us write the numerator in matrix notation. The value vjv_j is a row vector in Rdv\mathbb{R}^{d_v}, so ϕ(kj)vj\phi(k_j) \, v_j^\top is an outer product: a dϕ×dvd_\phi \times d_v matrix. Define:

S=j=1nϕ(kj)vjRdϕ×dv\boxed{S = \sum_{j=1}^{n} \phi(k_j) \, v_j^\top \in \mathbb{R}^{d_\phi \times d_v}}

Then the numerator becomes:

numi=ϕ(qi)SR1×dv\text{num}_i = \phi(q_i)^\top S \in \mathbb{R}^{1 \times d_v}

This is a single matrix-vector product: the dϕd_\phi-dimensional vector ϕ(qi)\phi(q_i) multiplied by the dϕ×dvd_\phi \times d_v matrix SS.

Similarly, the denominator:

deni=j=1nϕ(qi)ϕ(kj)=ϕ(qi)(j=1nϕ(kj))\text{den}_i = \sum_{j=1}^{n} \phi(q_i)^\top \phi(k_j) = \phi(q_i)^\top \left(\sum_{j=1}^{n} \phi(k_j)\right)

Define:

z=j=1nϕ(kj)Rdϕ\boxed{z = \sum_{j=1}^{n} \phi(k_j) \in \mathbb{R}^{d_\phi}}

Then:

deni=ϕ(qi)z\text{den}_i = \phi(q_i)^\top z

And the full attention output is:

oi=ϕ(qi)Sϕ(qi)z\boxed{o_i = \frac{\phi(q_i)^\top S}{\phi(q_i)^\top z}}

6.3 Why this changes the complexity

This is the moment the complexity drops.

The standard way (softmax attention): Compute the n×nn \times n matrix AA where Aij=sim(qi,kj)A_{ij} = \text{sim}(q_i, k_j), then multiply A×VA \times V. The bottleneck is the n×nn \times n matrix: O(n2dk)O(n^2 d_k) to compute QKQK^\top, then O(n2dv)O(n^2 d_v) to multiply by VV. Total: O(n2dk)O(n^2 d_k).

The new way (linear attention): First compute S=jϕ(kj)vjS = \sum_j \phi(k_j) v_j^\top and z=jϕ(kj)z = \sum_j \phi(k_j). Then for each query ii, compute ϕ(qi)S\phi(q_i)^\top S and ϕ(qi)z\phi(q_i)^\top z.

The costs:

  • Computing SS: sum nn outer products, each dϕ×dvd_\phi \times d_v. Cost: O(ndϕdv)O(n \, d_\phi \, d_v).
  • Computing zz: sum nn vectors of dimension dϕd_\phi. Cost: O(ndϕ)O(n \, d_\phi).
  • Computing all nn outputs: for each query, one matrix-vector product ϕ(qi)S\phi(q_i)^\top S of cost O(dϕdv)O(d_\phi \, d_v), and one dot product ϕ(qi)z\phi(q_i)^\top z of cost O(dϕ)O(d_\phi). Total: O(ndϕdv)O(n \, d_\phi \, d_v).

Grand total:

O(ndϕdv)\boxed{O(n \, d_\phi \, d_v)}

With dϕ=dkd_\phi = d_k (as in the elu+1 feature map), this is O(ndkdv)=O(ndk2)O(n \, d_k \, d_v) = O(n \, d_k^2). Compare to the standard O(n2dk)O(n^2 \, d_k).

When is the new way cheaper? When ndk2<n2dkn \, d_k^2 < n^2 \, d_k, which simplifies to dk<nd_k < n. Since dk=64d_k = 64 and nn can be 128,000128{,}000, this condition is overwhelmingly satisfied for long sequences.

6.4 What happened algebraically

The trick is a change of association in matrix multiplication. The standard attention computes:

Standard: (ϕ(Q)ϕ(K))V\text{Standard: } (\phi(Q) \, \phi(K)^\top) \, V

The parentheses indicate that we first multiply ϕ(Q)×ϕ(K)\phi(Q) \times \phi(K)^\top to get the n×nn \times n attention matrix, then multiply by VV.

The linear attention computes:

Linear: ϕ(Q)(ϕ(K)V)\text{Linear: } \phi(Q) \, (\phi(K)^\top V)

We first multiply ϕ(K)×V\phi(K)^\top \times V, which is a dϕ×nd_\phi \times n times n×dvn \times d_v product, giving a dϕ×dvd_\phi \times d_v matrix — this is our SS. Then we multiply each row of ϕ(Q)\phi(Q) by SS.

Matrix multiplication is associative: (AB)C=A(BC)(AB)C = A(BC) for any compatible matrices AA, BB, CC. This is a fundamental property of matrix multiplication. The two computations produce exactly the same result. But the intermediate matrix has different size:

  • Standard: intermediate is n×nn \times n (the attention matrix)
  • Linear: intermediate is dϕ×dvd_\phi \times d_v (the state matrix SS)

Since dϕ,dvnd_\phi, d_v \ll n for long sequences, the linear version avoids ever materializing the n×nn \times n matrix.

6.5 Numerical verification

Let us verify both computations produce the same result for our running example (unmasked attention, n=4n = 4, dk=dv=2d_k = d_v = 2).

Step 1: Compute ϕ(Q)\phi(Q) and ϕ(K)\phi(K).

From Section 5.5:

ϕ(Q)=(21122231),ϕ(K)=(12212213)\phi(Q) = \begin{pmatrix} 2 & 1 \\ 1 & 2 \\ 2 & 2 \\ 3 & 1 \end{pmatrix}, \quad \phi(K) = \begin{pmatrix} 1 & 2 \\ 2 & 1 \\ 2 & 2 \\ 1 & 3 \end{pmatrix}

Step 2 (Standard way): Compute ϕ(Q)ϕ(K)\phi(Q) \, \phi(K)^\top — the 4×44 \times 4 similarity matrix.

ϕ(Q)ϕ(K)=(21122231)(12212123)\phi(Q) \, \phi(K)^\top = \begin{pmatrix} 2 & 1 \\ 1 & 2 \\ 2 & 2 \\ 3 & 1 \end{pmatrix} \begin{pmatrix} 1 & 2 & 2 & 1 \\ 2 & 1 & 2 & 3 \end{pmatrix}

Row 1: (21+12,  22+11,  22+12,  21+13)=(4,5,6,5)(2 \cdot 1 + 1 \cdot 2, \; 2 \cdot 2 + 1 \cdot 1, \; 2 \cdot 2 + 1 \cdot 2, \; 2 \cdot 1 + 1 \cdot 3) = (4, 5, 6, 5)

Row 2: (11+22,  12+21,  12+22,  11+23)=(5,4,6,7)(1 \cdot 1 + 2 \cdot 2, \; 1 \cdot 2 + 2 \cdot 1, \; 1 \cdot 2 + 2 \cdot 2, \; 1 \cdot 1 + 2 \cdot 3) = (5, 4, 6, 7)

Row 3: (21+22,  22+21,  22+22,  21+23)=(6,6,8,8)(2 \cdot 1 + 2 \cdot 2, \; 2 \cdot 2 + 2 \cdot 1, \; 2 \cdot 2 + 2 \cdot 2, \; 2 \cdot 1 + 2 \cdot 3) = (6, 6, 8, 8)

Row 4: (31+12,  32+11,  32+12,  31+13)=(5,7,8,6)(3 \cdot 1 + 1 \cdot 2, \; 3 \cdot 2 + 1 \cdot 1, \; 3 \cdot 2 + 1 \cdot 2, \; 3 \cdot 1 + 1 \cdot 3) = (5, 7, 8, 6)

ϕ(Q)ϕ(K)=(4565546766885786)\phi(Q) \, \phi(K)^\top = \begin{pmatrix} 4 & 5 & 6 & 5 \\ 5 & 4 & 6 & 7 \\ 6 & 6 & 8 & 8 \\ 5 & 7 & 8 & 6 \end{pmatrix}

Now normalize each row (divide by row sum) and multiply by VV:

Row 1 sum: 4+5+6+5=204 + 5 + 6 + 5 = 20. Normalized: (0.20,0.25,0.30,0.25)(0.20, 0.25, 0.30, 0.25).

o1=0.20(10)+0.25(01)+0.30(11)+0.25(20)=(0.20+0+0.30+0.500+0.25+0.30+0)=(1.000.55)o_1 = 0.20 \begin{pmatrix} 1 \\ 0 \end{pmatrix} + 0.25 \begin{pmatrix} 0 \\ 1 \end{pmatrix} + 0.30 \begin{pmatrix} 1 \\ 1 \end{pmatrix} + 0.25 \begin{pmatrix} 2 \\ 0 \end{pmatrix} = \begin{pmatrix} 0.20 + 0 + 0.30 + 0.50 \\ 0 + 0.25 + 0.30 + 0 \end{pmatrix} = \begin{pmatrix} 1.00 \\ 0.55 \end{pmatrix}

Step 3 (Linear way): Compute S=ϕ(K)VS = \phi(K)^\top V — the 2×22 \times 2 state matrix.

S=ϕ(K)V=(12212123)(10011120)S = \phi(K)^\top V = \begin{pmatrix} 1 & 2 & 2 & 1 \\ 2 & 1 & 2 & 3 \end{pmatrix} \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \\ 2 & 0 \end{pmatrix}

Row 1 of SS: (11+20+21+12,  10+21+21+10)=(5,4)(1 \cdot 1 + 2 \cdot 0 + 2 \cdot 1 + 1 \cdot 2, \; 1 \cdot 0 + 2 \cdot 1 + 2 \cdot 1 + 1 \cdot 0) = (5, 4)

Row 2 of SS: (21+10+21+32,  20+11+21+30)=(10,3)(2 \cdot 1 + 1 \cdot 0 + 2 \cdot 1 + 3 \cdot 2, \; 2 \cdot 0 + 1 \cdot 1 + 2 \cdot 1 + 3 \cdot 0) = (10, 3)

S=(54103)S = \begin{pmatrix} 5 & 4 \\ 10 & 3 \end{pmatrix}

Also compute z=jϕ(kj)z = \sum_j \phi(k_j):

z=(12)+(21)+(22)+(13)=(68)z = \begin{pmatrix} 1 \\ 2 \end{pmatrix} + \begin{pmatrix} 2 \\ 1 \end{pmatrix} + \begin{pmatrix} 2 \\ 2 \end{pmatrix} + \begin{pmatrix} 1 \\ 3 \end{pmatrix} = \begin{pmatrix} 6 \\ 8 \end{pmatrix}

Now compute o1o_1:

ϕ(q1)S=(21)(54103)=(25+110,24+13)=(2011)\phi(q_1)^\top S = \begin{pmatrix} 2 & 1 \end{pmatrix} \begin{pmatrix} 5 & 4 \\ 10 & 3 \end{pmatrix} = \begin{pmatrix} 2 \cdot 5 + 1 \cdot 10, & 2 \cdot 4 + 1 \cdot 3 \end{pmatrix} = \begin{pmatrix} 20 & 11 \end{pmatrix} ϕ(q1)z=(21)(68)=26+18=20\phi(q_1)^\top z = \begin{pmatrix} 2 & 1 \end{pmatrix} \begin{pmatrix} 6 \\ 8 \end{pmatrix} = 2 \cdot 6 + 1 \cdot 8 = 20 o1=(20,11)20=(1.00,0.55)o_1 = \frac{(20, 11)}{20} = (1.00, 0.55)

Both methods give o1=(1.00,0.55)o_1 = (1.00, 0.55). \checkmark

The standard way computed a 4×44 \times 4 intermediate matrix. The linear way computed a 2×22 \times 2 intermediate matrix SS. Both produced the same output. But the 2×22 \times 2 matrix is fixed-size — it does not grow with nn.


7. The Recurrent Form

7.1 Causal masking

Everything in Section 6 assumed unmasked (bidirectional) attention — each query attends to all keys. For autoregressive (causal) language modeling, query ii can only attend to keys jij \leq i. The attention output becomes:

oi=j=1iϕ(qi)ϕ(kj)vjj=1iϕ(qi)ϕ(kj)o_i = \frac{\sum_{j=1}^{i} \phi(q_i)^\top \phi(k_j) \, v_j}{\sum_{j=1}^{i} \phi(q_i)^\top \phi(k_j)}

Applying the same associativity trick, define the causal versions of the state matrix and normalizer:

Si=j=1iϕ(kj)vj,zi=j=1iϕ(kj)S_i = \sum_{j=1}^{i} \phi(k_j) \, v_j^\top, \qquad z_i = \sum_{j=1}^{i} \phi(k_j)

Then:

oi=ϕ(qi)Siϕ(qi)zio_i = \frac{\phi(q_i)^\top S_i}{\phi(q_i)^\top z_i}

7.2 The recurrence

This is where the connection to RNNs becomes explicit. The causal sums SiS_i and ziz_i satisfy:

Si=Si1+ϕ(ki)vi\boxed{S_i = S_{i-1} + \phi(k_i) \, v_i^\top} zi=zi1+ϕ(ki)\boxed{z_i = z_{i-1} + \phi(k_i)}

with initial conditions S0=0S_0 = 0 (the zero matrix) and z0=0z_0 = 0 (the zero vector).

At each step ii, we:

  1. Compute ϕ(ki)\phi(k_i) and viv_i from the current token’s hidden state
  2. Update SiS_i by adding the outer product ϕ(ki)vi\phi(k_i) v_i^\top (a rank-1 update)
  3. Update ziz_i by adding ϕ(ki)\phi(k_i)
  4. Compute the output oi=ϕ(qi)Si/ϕ(qi)zio_i = \phi(q_i)^\top S_i \,/\, \phi(q_i)^\top z_i

This is a recurrent neural network. The hidden state is the pair (Si,zi)(S_i, z_i). The update rule is additive — each new token contributes an outer product to SS and a vector to zz. The output is a function of the current query and the accumulated state.

7.3 Tracing the recurrence

Let us trace the recurrence for our running example with causal masking.

Step i=1i = 1: Token 1 can only attend to itself.

S1=S0+ϕ(k1)v1=0+(12)(10)=(1020)S_1 = S_0 + \phi(k_1) v_1^\top = 0 + \begin{pmatrix} 1 \\ 2 \end{pmatrix} \begin{pmatrix} 1 & 0 \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 2 & 0 \end{pmatrix} z1=z0+ϕ(k1)=0+(12)=(12)z_1 = z_0 + \phi(k_1) = 0 + \begin{pmatrix} 1 \\ 2 \end{pmatrix} = \begin{pmatrix} 1 \\ 2 \end{pmatrix} o1=ϕ(q1)S1ϕ(q1)z1=(2,1)(1020)(2,1)(12)=(21+12,  20+10)21+12=(4,0)4=(1.00,0.00)o_1 = \frac{\phi(q_1)^\top S_1}{\phi(q_1)^\top z_1} = \frac{(2, 1) \begin{pmatrix} 1 & 0 \\ 2 & 0 \end{pmatrix}}{(2, 1) \begin{pmatrix} 1 \\ 2 \end{pmatrix}} = \frac{(2 \cdot 1 + 1 \cdot 2, \; 2 \cdot 0 + 1 \cdot 0)}{2 \cdot 1 + 1 \cdot 2} = \frac{(4, 0)}{4} = (1.00, 0.00)

Numerical check: with causal masking, query 1 only attends to key 1. The attention weight is 1 (only one key), so o1=v1=(1,0)o_1 = v_1 = (1, 0). \checkmark

Step i=2i = 2: Token 2 attends to tokens 1 and 2.

S2=S1+ϕ(k2)v2=(1020)+(21)(01)=(1020)+(0201)=(1221)S_2 = S_1 + \phi(k_2) v_2^\top = \begin{pmatrix} 1 & 0 \\ 2 & 0 \end{pmatrix} + \begin{pmatrix} 2 \\ 1 \end{pmatrix} \begin{pmatrix} 0 & 1 \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 2 & 0 \end{pmatrix} + \begin{pmatrix} 0 & 2 \\ 0 & 1 \end{pmatrix} = \begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix} z2=z1+ϕ(k2)=(12)+(21)=(33)z_2 = z_1 + \phi(k_2) = \begin{pmatrix} 1 \\ 2 \end{pmatrix} + \begin{pmatrix} 2 \\ 1 \end{pmatrix} = \begin{pmatrix} 3 \\ 3 \end{pmatrix} o2=ϕ(q2)S2ϕ(q2)z2=(1,2)(1221)(1,2)(33)=(1+4,  2+2)3+6=(5,4)9o_2 = \frac{\phi(q_2)^\top S_2}{\phi(q_2)^\top z_2} = \frac{(1, 2) \begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix}}{(1, 2) \begin{pmatrix} 3 \\ 3 \end{pmatrix}} = \frac{(1 + 4, \; 2 + 2)}{3 + 6} = \frac{(5, 4)}{9} o2=(0.556,  0.444)o_2 = (0.556, \; 0.444)

Numerical check: query 2 attends to keys 1 and 2 with similarities ϕ(q2)ϕ(k1)=(1)(1)+(2)(2)=5\phi(q_2)^\top \phi(k_1) = (1)(1) + (2)(2) = 5 and ϕ(q2)ϕ(k2)=(1)(2)+(2)(1)=4\phi(q_2)^\top \phi(k_2) = (1)(2) + (2)(1) = 4. Total: 5+4=95 + 4 = 9. Weights: 5/90.5565/9 \approx 0.556 and 4/90.4444/9 \approx 0.444.

o2=59(10)+49(01)=(0.5560.444)o_2 = \frac{5}{9} \begin{pmatrix} 1 \\ 0 \end{pmatrix} + \frac{4}{9} \begin{pmatrix} 0 \\ 1 \end{pmatrix} = \begin{pmatrix} 0.556 \\ 0.444 \end{pmatrix} \quad \checkmark

Step i=3i = 3: Token 3 attends to tokens 1, 2, and 3.

S3=S2+ϕ(k3)v3=(1221)+(22)(11)=(1221)+(2222)=(3443)S_3 = S_2 + \phi(k_3) v_3^\top = \begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix} + \begin{pmatrix} 2 \\ 2 \end{pmatrix} \begin{pmatrix} 1 & 1 \end{pmatrix} = \begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix} + \begin{pmatrix} 2 & 2 \\ 2 & 2 \end{pmatrix} = \begin{pmatrix} 3 & 4 \\ 4 & 3 \end{pmatrix} z3=z2+ϕ(k3)=(33)+(22)=(55)z_3 = z_2 + \phi(k_3) = \begin{pmatrix} 3 \\ 3 \end{pmatrix} + \begin{pmatrix} 2 \\ 2 \end{pmatrix} = \begin{pmatrix} 5 \\ 5 \end{pmatrix} o3=(2,2)(3443)(2,2)(55)=(6+8,  8+6)10+10=(14,14)20=(0.700,0.700)o_3 = \frac{(2, 2) \begin{pmatrix} 3 & 4 \\ 4 & 3 \end{pmatrix}}{(2, 2) \begin{pmatrix} 5 \\ 5 \end{pmatrix}} = \frac{(6 + 8, \; 8 + 6)}{10 + 10} = \frac{(14, 14)}{20} = (0.700, 0.700)

Step i=4i = 4: Token 4 attends to all tokens 1 through 4.

S4=S3+ϕ(k4)v4=(3443)+(13)(20)=(3443)+(2060)=(54103)S_4 = S_3 + \phi(k_4) v_4^\top = \begin{pmatrix} 3 & 4 \\ 4 & 3 \end{pmatrix} + \begin{pmatrix} 1 \\ 3 \end{pmatrix} \begin{pmatrix} 2 & 0 \end{pmatrix} = \begin{pmatrix} 3 & 4 \\ 4 & 3 \end{pmatrix} + \begin{pmatrix} 2 & 0 \\ 6 & 0 \end{pmatrix} = \begin{pmatrix} 5 & 4 \\ 10 & 3 \end{pmatrix} z4=z3+ϕ(k4)=(55)+(13)=(68)z_4 = z_3 + \phi(k_4) = \begin{pmatrix} 5 \\ 5 \end{pmatrix} + \begin{pmatrix} 1 \\ 3 \end{pmatrix} = \begin{pmatrix} 6 \\ 8 \end{pmatrix} o4=(3,1)(54103)(3,1)(68)=(15+10,  12+3)18+8=(25,15)26=(0.962,0.577)o_4 = \frac{(3, 1) \begin{pmatrix} 5 & 4 \\ 10 & 3 \end{pmatrix}}{(3, 1) \begin{pmatrix} 6 \\ 8 \end{pmatrix}} = \frac{(15 + 10, \; 12 + 3)}{18 + 8} = \frac{(25, 15)}{26} = (0.962, 0.577)

Notice: S4S_4 is exactly the same SS matrix we computed in Section 6.5 for unmasked attention! This must be the case — when the causal sum reaches the last token, it includes all tokens, so Sn=SS_n = S.

7.4 Memory and compute per step

At each step of the recurrence:

State size: SiS_i is dϕ×dvd_\phi \times d_v and ziz_i is dϕd_\phi. Total: dϕdv+dϕ=dϕ(dv+1)d_\phi d_v + d_\phi = d_\phi(d_v + 1).

With dϕ=dk=64d_\phi = d_k = 64 and dv=64d_v = 64: 64×65=4,16064 \times 65 = 4{,}160 elements per head. Across h=8h = 8 heads and L=12L = 12 layers, in fp16:

8×12×4,160×2=798,720 bytes0.8 MB8 \times 12 \times 4{,}160 \times 2 = 798{,}720 \text{ bytes} \approx 0.8 \text{ MB}

Compare to the KV cache for n=128,000n = 128{,}000 tokens: 4×512×12×128,0003.154 \times 512 \times 12 \times 128{,}000 \approx 3.15 GB. The recurrent state is 3,940×3{,}940\times smaller.

Compute per step: Computing ϕ(ki)vi\phi(k_i) v_i^\top is a dϕ×dvd_\phi \times d_v outer product: dϕdvd_\phi d_v multiplications. Computing ϕ(qi)Si\phi(q_i)^\top S_i is a 1×dϕ1 \times d_\phi times dϕ×dvd_\phi \times d_v product: dϕdvd_\phi d_v multiplications. Total per head: O(dϕdv)=O(dk2)O(d_\phi d_v) = O(d_k^2).

This is constant — it does not depend on ii or on how many tokens have been generated. Every token costs the same, regardless of position.

7.5 The complete cost comparison

PropertySoftmax attentionLinear attention (recurrent)
State size per head2×t×dk2 \times t \times d_k (KV cache, grows with tt)dk×dv+dkd_k \times d_v + d_k (constant)
Compute per token (generation)O(tdk)O(t \, d_k) (grows with tt)O(dk2)O(d_k^2) (constant)
Total cost for nn tokens (generation)O(n2dk)O(n^2 d_k)O(ndk2)O(n \, d_k^2)
Training (full sequence)O(n2dk)O(n^2 d_k)O(ndk2)O(n \, d_k^2) with scan; O(n2dk)O(n^2 d_k) naive

7.6 Numerical cost comparison

For generation of n=128,000n = 128{,}000 tokens, per head (dk=64d_k = 64):

Softmax attention: n2dk/2=128,0002×64/25.24×1011n^2 d_k / 2 = 128{,}000^2 \times 64 / 2 \approx 5.24 \times 10^{11} multiply-adds.

Linear attention: n×dk2=128,000×642=5.24×108n \times d_k^2 = 128{,}000 \times 64^2 = 5.24 \times 10^8 multiply-adds.

Ratio: 5.24×1011/5.24×108=1,000×5.24 \times 10^{11} / 5.24 \times 10^8 = 1{,}000\times. The linear version is three orders of magnitude cheaper.

This matches the claim of Katharopoulos et al. (2020): “The model has been shown to improve inference speeds up to three orders of magnitude without much loss in predictive performance.”

7.7 The training parallelism issue

This is the part that confuses almost everyone when they first encounter linear attention.

During inference (generation), the recurrent form is strictly better: constant memory, constant compute per token. But during training, there is a catch.

Training processes the full sequence at once (teacher forcing). The softmax attention computes all nn outputs in parallel using matrix multiplication — QKQK^\top and then AVAV. These are large, dense matrix multiplications that GPUs execute extremely efficiently.

The recurrent form computes outputs sequentially: S1o1S_1 \to o_1, then S2o2S_2 \to o_2, then S3o3S_3 \to o_3, and so on. Each step depends on the previous state Si1S_{i-1}. This is a sequential dependency — you cannot compute o3o_3 until S2S_2 is done. On a GPU with thousands of parallel cores, this sequential dependency means most cores sit idle.

The asymptotic complexity is better (O(ndk2)O(n d_k^2) vs O(n2dk)O(n^2 d_k)), but the wall-clock time can be worse because of low GPU utilization. This is a crucial practical distinction noted by the Efficient Transformers survey (Tay et al., 2022): “running unidirectional (causal) implementation of kernel-based attention on an autoregressive task can be several times slower than vanilla Transformer during parallelized training due to the need to do a left to right pass (i.e., scan operation) in similar spirit to Recurrent neural networks.”

The resolution is the parallel scan algorithm (Blelloch, 1990), which computes prefix sums in O(logn)O(\log n) parallel steps instead of nn sequential steps. Modern implementations (like those in Mamba and RWKV) use optimized scan operations to achieve competitive training throughput.


8. What We Gain and What We Lose

8.1 What we gain

Constant memory during generation. The recurrent state (S,z)(S, z) has size O(dk2)O(d_k^2) per head per layer, independent of sequence length. For our running model: 0.8 MB total vs 3.15 GB for the KV cache at 128K tokens. This eliminates the KV cache wall entirely.

Constant compute per token during generation. Each new token requires O(dk2)O(d_k^2) operations per head, regardless of how many tokens preceded it. The 128,000th token is no more expensive than the 1st.

Linear total cost. Generating nn tokens costs O(ndk2)O(n d_k^2), linear in nn. This enables generation at sequence lengths that are impractical with softmax attention.

8.2 What we lose

Exact softmax attention. The elu+1 kernel is not equivalent to the softmax kernel. The attention distributions are different (as we saw in Section 5.6), and this can affect model quality. Empirically, Katharopoulos et al. (2020) report competitive but not identical performance to softmax transformers on language modeling and speech recognition.

Information capacity of the state. The recurrent state SRdk×dvS \in \mathbb{R}^{d_k \times d_v} must compress all information from all past tokens into a fixed-size matrix. With dk=dv=64d_k = d_v = 64, this is 64×64=4,09664 \times 64 = 4{,}096 numbers. Compare to the KV cache, which stores 2×dk=1282 \times d_k = 128 numbers per past token — 128,000 tokens would require 128×128,000=16,384,000128 \times 128{,}000 = 16{,}384{,}000 numbers per head. The recurrent state compresses this by a factor of 4,000×4{,}000\times.

This compression is necessarily lossy. If token 50,000 contained critical information that token 128,000 needs, the softmax attention can retrieve it directly from the KV cache. The recurrent state can only access what survived 78,000 rank-1 updates to the state matrix SS.

Training parallelism (naive implementation). As discussed in Section 7.7, the sequential nature of the recurrence reduces GPU utilization during training. Modern implementations mitigate this with parallel scan, but the engineering complexity is higher.

8.3 The landscape after linear attention

The kernel-based linear attention of Katharopoulos et al. (2020) was one of the first rigorous demonstrations that the attention mechanism could be fundamentally replaced. It showed that the mathematical structure of attention — the softmax, the pairwise comparisons, the n×nn \times n matrix — is not sacred. What matters is the function computed: a weighted combination of values based on query-key relevance. The softmax is one way to define relevance. The kernel feature map is another. And once you have the feature map, the associativity of matrix multiplication gives you linear cost for free.

This insight opened the door to a family of architectures that blur the line between transformers and RNNs: RetNet (Sun et al., 2023), RWKV (Peng et al., 2023), Mamba (Gu and Dao, 2023), and others. Each proposes a different way to define the recurrent state and the update rule, with the shared goal of constant-time, constant-memory token generation without sacrificing the quality that made transformers dominant.

The Efficient Transformers survey (Tay et al., 2022) presciently noted: “It is then a question of whether that new xformer will still be a Transformer.” The kernel view of attention suggests the answer: the boundary between transformers and RNNs was always algebraic, not architectural.


Summary

After twelve blogs of modifying attention — reducing KV heads, compressing representations, sparsifying patterns, gating residuals and activations — the softmax normalization remained untouched, and with it the quadratic cost of computing pairwise query-key similarities. This blog derived the exact costs that persist (the O(n2dmodel)O(n^2 d_\text{model}) attention FLOPs that dominate beyond n=dffn = d_{ff} tokens, the linearly growing KV cache that reaches hundreds of gigabytes at long contexts, the per-token generation cost that makes the last token nn times more expensive than the first), identified the softmax as the root cause (its infinite-dimensional feature map prevents factoring the similarity into independent query and key functions), replaced it with a finite-dimensional kernel (ϕ(x)=elu(x)+1\phi(x) = \text{elu}(x) + 1), applied the associativity of matrix multiplication to change the computation order from (ϕ(Q)ϕ(K))V(\phi(Q)\phi(K)^\top)V to ϕ(Q)(ϕ(K)V)\phi(Q)(\phi(K)^\top V), and showed that the result is an RNN with state (Si,zi)(S_i, z_i) that achieves constant memory (dk×dvd_k \times d_v per head), constant compute per token (O(dk2)O(d_k^2)), and linear total generation cost (O(ndk2)O(n d_k^2)) — three orders of magnitude faster than softmax attention at 128K tokens, at the price of compressing all past information into a fixed-size state matrix.


Previous: Gated Attention: Replacing Residuals and ReLU with Learned Gates

Next: Hybrid Architectures: RetNet and the Three Computation Paradigms

Enjoyed this post?

Subscribe to get notified when I publish new posts. No spam, unsubscribe anytime.