# Linformer: Self-Attention with Linear Complexity

Sinong Wang, Belinda Z. Li, Madian Khabsa, Han Fang, Hao Ma · arXiv · 2020-6-8

- **Problem** Self-attention costs \\(O(n^2)\\) in time and space with sequence length, which makes Transformers slow to train and serve on long inputs.
- **Method** Projects keys and values from length n to a smaller length k with learned n-by-k matrices, so the context mapping costs \\(O(nk)\\) instead of \\(O(n^2)\\).
- **Results** The n = 512, k = 256 model with shared key-value and layerwise sharing averages 92.30 on four dev tasks, against 92.25 for RoBERTa-base; at n = 512, k = 128, inference is 1.5x faster than the Transformer.

## Why self-attention is the bottleneck

Self-attention lets every token draw on every other token in the sequence, which is how long-range information stays available to a Transformer. The context mapping matrix is n by n, so computing it takes \\(O(n^2)\\) time and space as the sequence grows. The open question is whether attention can avoid this quadratic step without losing accuracy.

Sparse attention lowers the cost to about \\(O(n\\sqrt{n})\\) by letting each token attend to only part of the sequence. The authors report that such methods lose accuracy quickly for little speed: a 2% drop for a 20% speed-up. The Reformer's LSH attention has a theoretical cost of \\(O(n\\log(n))\\), but the efficiency gains it reports only appear for sequences longer than 2048, and its multi-round hashing adds sequential steps.

Table 1 places these architectures side by side on per-layer time complexity and on the minimum number of sequential operations. Linformer is listed there with \\(O(n)\\) complexity per layer and \\(O(1)\\) sequential operations.

## Evidence that attention is low rank

The spectrum check uses pretrained RoBERTa-base and RoBERTa-large models, with masked language modeling on Wiki103 and classification on IMDB. For each layer and head, the singular values of the context mapping matrix are computed and the normalized cumulative sum is averaged over 10k sentences. The curves have a long tail, so a few leading singular values carry most of the information, and higher layers are more skewed than lower ones.

At the 128th of 512 singular values, higher layers have concentrated more of the mass, which means their rank is lower.

Theorem 1 says that for a column vector of the value projection, a matrix \\(\\tilde P\\) of rank \\(\\Theta(\\log(n))\\) exists whose product with that vector is within relative error \\(\\epsilon\\) of \\(P w^T\\), with probability greater than \\(1-o(1)\\). The proof draws R as a k-by-n matrix with i.i.d. N(0, 1/k) entries and applies the distributional Johnson–Lindenstrauss lemma, which gives \\(k = 5\\log(n)/(\\epsilon^2-\\epsilon^3)\\).

A truncated singular value decomposition of P gives a low-rank approximation, and the paper ties its accuracy to the Eckart–Young–Mirsky Theorem. That route needs an SVD for every self-attention matrix, which adds cost of its own, so the authors look for a construction that avoids it.

## Projecting keys and values to length k

Each head keeps the usual query, key and value projections and adds two learned matrices, \\(E\_i\\) and \\(F\_i\\), each of size n by k. Keys and values are multiplied by these matrices before attention, so each head's projected key and value are k by d rather than n by d.

Each head is computed as a softmax over \\(QW^Q\_i(E\_iKW^K\_i)^T/\\sqrt{d\_k}\\), which gives an n-by-k matrix \\(\\bar P\\), and this matrix multiplies \\(F\_iVW^V\_i\\), a k-by-d matrix. Because \\(\\bar P\\) has only k columns, this step costs \\(O(nk)\\) in time and space, so a small projected dimension k is what keeps the cost low.

Theorem 2 sets the required k as \\(k = \\min\\{\\Theta(9d\\log(d)/\\epsilon^2), 5\\Theta(\\log(n)/\\epsilon^2)\\}\\). Under that choice, there exist \\(E\_i\\) and \\(F\_i\\) such that for a row vector w of \\(QW^Q\_i(KW^K\_i)^T/\\sqrt{d}\\), the projected softmax output is within relative error \\(\\epsilon\\) of the exact one, with probability greater than \\(1-o(1)\\).

The first term does not depend on n, which is why the authors describe the choice of k as independent of sequence length, while the second term still depends on log(n).

The proof sets \\(E\_i = \\delta R\\) and \\(F\_i = e^{-\\delta R}\\), where R is a k-by-n Gaussian matrix and \\(\\delta\\) is a small constant. Applying the single-row result to every row of A gives a bound with \\(k = 5\\log(nd)/(\\epsilon^2-\\epsilon^3)\\), and the authors then use the rank of A being d, on a submatrix of 2d rows, to replace the dependence on n with one on log(d).

## Sharing and sizing the projections

The projections can be shared in three ways: headwise sharing uses one E and one F per layer, key-value sharing uses one E per layer for keys and values, and layerwise sharing uses one E across all layers. In a 12-layer, 12-head model these choices leave 24, 12 and 1 distinct projection matrices.

The projected dimension can differ across heads and layers. Because higher layers show more skewed spectra, the authors suggest a smaller k for them.

The projection need not be linear: mean or max pooling, or a convolution with kernel and stride set to n/k, are alternatives, and a convolution has trainable parameters.

## Pretraining perplexity across k, sharing and n

Pretraining uses masked language modeling on BookCorpus and English Wikipedia, about 3300M words in total, and Linformer is compared with a RoBERTa baseline that is based on the Transformer. The runs were parallelized across 64 Tesla V100 GPUs for 250k updates.

The authors vary the projected dimension k, using the same k in every layer and head, and plot validation perplexity against updates for maximum sequence lengths of 512 and 1024. Linformer improves as k grows, and at k = 128 with n = 512 or k = 256 with n = 1024 its perplexity is already nearly on par with the Transformer.

With n = 512, using a single projection matrix for all layers gives validation perplexity almost matching the non-shared model. The authors read this as a way to cut extra parameters, and with them memory use, with little cost in quality.

With k fixed at 256, the authors pretrain for n in 512, 1024, 2048 and 4096. After convergence the final perplexities stay about the same as n grows, which the authors take as support for linear-time behavior.

## Fine-tuning accuracy on four tasks

The pretrained models are fine-tuned on SST-2, IMDB, QNLI and QQP and compared with RoBERTa, 12-layer BERT-base and 6-layer Distilled BERT. Accuracy is the dev-set score, averaged over the four tasks.

Linformer with n = 512 and k = 128 averages 91.75, against 92.25 for RoBERTa-base. At k = 256, the Linformer with shared key-value and layerwise sharing averages 92.30. BERT-base averages 91.90 and Distilled BERT averages 90.45.

The n = 1024, k = 256 layerwise model averages 92.18, close to the 92.30 of the n = 512, k = 256 counterpart with the same sharing. The authors take this to mean that accuracy depends mainly on k rather than on the ratio of n to k.

All models, including the Transformer baselines, shared the objective, the corpus and up to 250k updates, but Linformer reached those updates in much less wall-clock time and so trained for less time.

## Inference speed and memory against the Transformer

Inference speed and memory were measured for Linformer with layerwise sharing against a standard Transformer. Random data up to a chosen sequence length went through full forward passes on a 16GB Tesla V100 GPU, with batch size set to the largest batch that fits in memory.

Holding the total number of tokens fixed, the Transformer slows at longer sequence lengths while the Linformer time stays comparatively flat.

At n = 512 and k = 128, Linformer has 1.5x faster inference and allows a 1.7x larger maximum batch size than the Transformer. At k = 128 the time saved rises to 20x at n = 65536, and the memory saved reaches 60x at the same length and projected dimension. The savings grow with sequence length, so the largest gains appear at the longest sequences tested.

## Limits of the measurements and tests

The speed and memory numbers come from random inputs on one 16GB Tesla V100, with batch size set by the largest batch that fits, so they describe this benchmark setting rather than any downstream task. The downstream Linformer models reached 250k updates in much less wall-clock time, so they trained for less time than the baselines.

Pretraining is compared only under masked language modeling on BookCorpus plus English Wikipedia, and downstream accuracy is reported on four tasks. The Reformer comparison rests on efficiency gains reported in prior work, which appear only for sequences longer than 2048.

Theorem 1 is stated for a column vector of the value projection, and Theorem 2 for a row vector of the scaled query-key product, each with probability greater than \\(1-o(1)\\). The experiments measure perplexity, accuracy and speed or memory, and they do not measure the approximation error of P directly.

## Paper info

- **Authors**: Sinong Wang, Belinda Z. Li, Madian Khabsa, Han Fang, Hao Ma
- **Venue**: arXiv · Preprint
- **First published**: 2020-6-8
- **Added**: 2026-10-20
- **Updated**: 2020-6-14
- **Version**: v3
- **arXiv**: 2006.04768
- **Category**: cs.LG · stat.ML
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/2006.04768v3) · [PDF ↗](https://arxiv.org/pdf/2006.04768v3)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Sinong Wang, Belinda Z. Li, Madian Khabsa, Han Fang, Hao Ma (2020). Linformer: Self-Attention with Linear Complexity. arXiv:2006.04768v3. https://arxiv.org/abs/2006.04768v3

**BibTeX**

```
@misc{wang2020linformer,
  title = {{Linformer: Self-Attention with Linear Complexity}},
  author = {Sinong Wang and Belinda Z. Li and Madian Khabsa and Han Fang and Hao Ma},
  year = {2020},
  eprint = {2006.04768},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2006.04768v3}
}
```

## Other paper commentary

- [Reformer: The Efficient Transformer](https://refivu.com/en/papers/0dd490a3-709f-4bac-ae3f-de0bc9e197db-reformer-the-efficient-transformer.md)
- [Generating Long Sequences with Sparse Transformers](https://refivu.com/en/papers/25192f20-3966-40d3-94ed-65ebcaba60f7-generating-long-sequences-with-sparse-transformers.md)
- [Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity](https://refivu.com/en/papers/2712d011-b4e2-4181-89ce-e47fc4a5117c-switch-transformers-scaling-to-trillion-parameter-models.md)
- [Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer](https://refivu.com/en/papers/01d9fd25-ae98-40fb-a44a-08c4b727555b-outrageously-large-neural-networks-the-sparsely-gated.md)
- [FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness](https://refivu.com/en/papers/cc000b48-2d42-4287-aae7-f3583a852720-flashattention-fast-and-memory-efficient-exact-attention.md)
- [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter](https://refivu.com/en/papers/70da27bf-5c2f-4e05-bfe2-7c145011b0d9-distilbert-a-distilled-version-of-bert-smaller-faster.md)
- [DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning](https://refivu.com/en/papers/2227da56-269c-4116-84dc-2bc76bf3e6c7-deepseek-r1-incentivizing-reasoning-capability-in-llms-via.md)
- [RT-2: Vision-Language-Action Models Transfer Web Knowledge to Robotic Control](https://refivu.com/en/papers/01f24caf-5337-46f9-abb9-a961b79018a1-rt-2-vision-language-action-models-transfer-web.md)
- [Gemma: Open Models Based on Gemini Research and Technology](https://refivu.com/en/papers/e2b5810b-65da-4a36-9bd2-a17a9a6f8512-gemma-open-models-based-on-gemini-research-and.md)
- [Decision Transformer: Reinforcement Learning via Sequence Modeling](https://refivu.com/en/papers/386e76e8-fee6-4cba-84e8-0ef90a33b3b6-decision-transformer-reinforcement-learning-via-sequence.md)
- [A Generalist Agent](https://refivu.com/en/papers/890475cb-e797-43df-8de3-0d0eb9ed882e-a-generalist-agent.md)
- [Denoising Diffusion Implicit Models](https://refivu.com/en/papers/211aa1a1-08e2-40a7-b187-72384021f47a-denoising-diffusion-implicit-models.md)

## Terms in this paper

- [Accuracy](https://refivu.com/en/terms/1995c0cd6282-accuracy)
- [Transformer](https://refivu.com/en/terms/48e8be2570bc-transformer)
- [perplexity](https://refivu.com/en/terms/2b204e513801-perplexity)
- [self-attention](https://refivu.com/en/terms/426589b81077-self-attention)
- [ROBERTA](https://refivu.com/en/terms/f42b278690e5-roberta)
- [SST-2](https://refivu.com/en/terms/8904146eae2e-sst-2)
- [IMDb](https://refivu.com/en/terms/72b8a8b81376-imdb)
- [English Wikipedia](https://refivu.com/en/terms/34ae8aa404c0-english-wikipedia)
- [Reformer](https://refivu.com/en/terms/09e7750949bd-reformer)
- [QQP](https://refivu.com/en/terms/629a79a11072-qqp)
- [Inference Speed](https://refivu.com/en/terms/782b4bb7700b-inference-speed)
- [Linformer](https://refivu.com/en/terms/760bfa259c19-linformer)
- [singular value decomposition](https://refivu.com/en/terms/b3f15c76a7e2-singular-value-decomposition)
- [Sparse Transformer](https://refivu.com/en/terms/f87fc7b2ecf4-sparse-transformer)
- [BookCorpus](https://refivu.com/en/terms/45a77eab4fca-bookcorpus)
- [QNLI](https://refivu.com/en/terms/a4b2d0e8a1a9-qnli)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/470ce67a-0f79-4a06-ae89-f26384095187-linformer-self-attention-with-linear-complexity)
