§11.2Efficient Self-Attention: Sparse & Linearized

Part III NLPT pp. 81–84 · ~3 min read

  • efficient attention
  • sparse attention
  • linearized attention
  • self-attention

Self-attention is the heart of the transformer — but it has a scaling problem. Because the weights come from comparing every token with every other token, the layer’s time and memory grow like O(n2)O(n^2) in the sequence length nn. That quadratic cost is the bottleneck for long documents, speech, and vision. Two families of efficient attention attack it: introducing sparsity, or replacing the attention operation with a kernel.

Sparse attention

Sparse attention simply computes fewer query-key pairs, chosen by a fixed pattern. Most schemes are built from a handful of atomic patterns — switch between them below and watch the sparsity (the fraction of skipped pairs) change:

queries ↓
keys (attended to) →

fullStandard (dense) attention — every query attends to every key. O(n²).

196/196 query-key pairs computed — 0% sparse. Blank squares are pairs we skip entirely.

Figure 11-5 (recreated). Atomic sparse-attention patterns — switch between them. A filled square is a computed query-key pair; blank is discarded.
  • Global — a few special tokens attend to everything (and everything to them).
  • Band — attend to a diagonal band of nearby tokens (a local window).
  • Dilated — a band with gaps, widening reach without extra cost.
  • Random — sample a few random keys per query.
  • Block-local — attend only within fixed blocks.

Real models mix these: Longformer combines global + band, and BigBird adds random on top. The payoff is huge — both push the maximum sequence length to 4,096 tokens, 8× BERT’s 512. The pattern can even be learned rather than fixed (Reformer clusters similar tokens with a hash function).

Linearized attention

The other approach changes the order of operations. For a general similarity function, an attention output is:

yi=jsim(Qi,Kj)ksim(Qi,Kk)Vjy_i = \sum_j \frac{\operatorname{sim}(Q_i, K_j)}{\sum_k \operatorname{sim}(Q_i, K_k)}\, V_j

Standard attention uses sim(Qi,Kj)=exp(QiKj)\operatorname{sim}(Q_i, K_j) = \exp(Q_i^\top K_j), which couples every ii with every jj — the O(n2)O(n^2) cost. Linearized attention writes the similarity as a kernel that factorizes:

sim(Qi,Kj)=φ(Qi)φ(Kj)\operatorname{sim}(Q_i, K_j) = \varphi(Q_i)^\top \varphi(K_j)
  • φ\varphi — a feature map applied to each query and key separately.
  • Because φ(Qi)\varphi(Q_i) doesn’t depend on jj or kk, we can pull it out of the sums:
yi=φ(Qi)jφ(Kj)Vjφ(Qi)kφ(Kk)y_i = \frac{\varphi(Q_i)^\top \sum_j \varphi(K_j) V_j^\top}{\varphi(Q_i)^\top \sum_k \varphi(K_k)}

Intuition — compute the shared part once

The terms jφ(Kj)Vj\sum_j \varphi(K_j) V_j^\top and kφ(Kk)\sum_k \varphi(K_k) don’t depend on the query ii at all — so we compute them once and reuse them for every query. Standard attention rebuilds the full n×n score matrix; this factorization turns the cost from quadratic into linear in the sequence length.

Here’s the difference in cost as sequences grow — the whole reason these methods exist:

standard O(n²)linearized O(n)sequence length n →time / memory →
Standard attention scales quadratically; linearized attention scales linearly. Popular implementations include Linear Transformer and Performer.

Both routes — sparse and linearized — let transformers handle far longer sequences. Next: transformers leaving text behind entirely, for vision and tables.

Check yourself: efficient attention

1.Why is standard self-attention a bottleneck for long sequences?

2.How does sparse attention reduce cost?

3.What do Longformer and BigBird achieve by mixing sparse patterns?

4.What is the key trick behind linearized attention?

5.In the reordered form, why does computing Σⱼ φ(Kⱼ)Vⱼᵀ once give a speedup?

5 questions