# Linformer

**Linformer** 是 Facebook AI 的汪思农、Belinda Z. Li、Madian Khabsa、韩方和马浩于 2020 年 6 月提出的一种 [Transformer](https://www.edgepedia.org/zh/transformer) 变体，它用低秩近似替代标准自注意力，将注意力机制在序列长度 \( n \) 上的时间和空间复杂度从 \( O(n^2) \) 降至 \( O(n) \)。<sup>[1](https://arxiv.org/abs/2006.04768)</sup> 作者报告称，所得模型在语言建模和微调任务上与标准 Transformer 表现相当，同时在内存和时间效率上显著更优。<sup>[1](https://arxiv.org/abs/2006.04768)</sup>

| 关键事实 | 详情 |
|---|---|
| 来源 | 《Linformer：具有线性复杂度的自注意力》，arXiv:2006.04768，2020 年 6 月 8 日由 Facebook AI（西雅图）的汪思农、Belinda Z. Li、Madian Khabsa、韩方和马浩提交<sup>[1](https://arxiv.org/abs/2006.04768)</sup> |
| 核心思想 | 自注意力上下文映射矩阵可以用低秩矩阵近似；将键和值投影到固定维度 \( k \) 可使注意力在序列长度上呈线性<sup>[1](https://arxiv.org/abs/2006.04768)</sup> |
| 复杂度 | 每层 \( O(n) \) 时间和空间，\( O(1) \) 顺序操作，而标准 Transformer 为 \( O(n^2) \)<sup>[2](https://arxiv.org/pdf/2006.04768)</sup> |
| 理论基础 | 定理 1 表明秩为 \( \Theta(\log n) \) 的近似已足够，并用分布型 Johnson–Lindenstrauss 引理证明<sup>[2](https://arxiv.org/pdf/2006.04768)</sup> |
| 内存节省 | 对于 1,000 和 4,000 个 token 的输入序列，Linformer、Performer 和 Linear Transformer 比原始 Transformer 至少轻 56% 和 88%<sup>[3](https://doi.org/10.1145/3586074)</sup> |
| 长程竞技场得分 | 五项 LRA 任务平均 51.36（ListOps 35.7，Text 53.9，Retrieval 52.3，Image 38.6，Pathfinder 76.3）<sup>[4](https://lizeman.github.io/llm-arch-kb/attention/linformer/)</sup> |
| 主要局限 | 假设序列长度固定，无法高效应用于因果（自回归）注意力<sup>[5](https://github.com/lucidrains/linformer/blob/master/README.md)</sup><sup> • </sup><sup>[6](https://aclanthology.org/2022.acl-long.515.pdf)</sup> |

## 背景：自注意力瓶颈

在标准 Transformer 中，每个 token 的表示通过关注上一层所有其他 token 来更新。这种上下文映射操作正是 Transformer 在长序列上优于循环模型的原因，但计算 \( n \times n \) 注意力矩阵需要两个 \( n \times d \) 矩阵相乘，在时间和空间上均耗费 \( O(n^2) \)。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup> Linformer 论文将其问题表述为：Transformer 能否在不损失性能的情况下避免这一二次方操作。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup>

根据论文的叙述，此前的效率技术结果喜忧参半。稀疏注意力方法让每个 token 只关注一部分 token，但作者称其性能大幅下降而效率提升有限——仅 20% 的加速却带来 2% 的性能下降。Reformer 使用局部敏感哈希达到 \( O(n \log n) \) 复杂度，但其效率提升在实践中仅对长度超过 2,048 个 token 的序列显现，且多轮哈希增加了顺序操作的数量。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup>

## 低秩观察

论文的出发点是一个经验和理论主张：自注意力形成的随机矩阵是低秩的。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup> 作者对预训练的 RoBERTa-base（12 层）和 RoBERTa-large（24 层）模型在两个任务上的注意力矩阵应用奇异值分解——Wiki103 上的掩码语言建模和 IMDB 上的分类——对 10,000 个句子取平均。谱图在层、头和任务上均显示出明显的长尾分布，意味着注意力矩阵的大部分信息可以从其前几个最大奇异值中恢复。该分布在较高层更为偏斜，更多信息集中在最大奇异值中，有效秩更低。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup>

定理 1 将其形式化：对于任意 \( Q, K, V \in \mathbb{R}^{n \times d} \) 和值投影的任意列向量，存在一个低秩矩阵 \( \tilde{P} \)，其 \( \mathrm{rank}(\tilde{P}) = \Theta(\log n) \)，能以高概率近似注意力输出。证明使用一个随机投影矩阵 \( R \in \mathbb{R}^{k \times n} \) 构造近似，其元素独立同分布于 \( N(0, 1/k) \)，并应用分布型 Johnson–Lindenstrauss 引理，其中 \( k = 5 \log(n)/(\epsilon^2 - \epsilon^3) \)。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup>

## Linformer 机制

Linformer 不计算完整的 \( n \times n \) 注意力矩阵，而是通过线性投影将原始缩放点积注意力分解为多个较小的注意力，使这些操作的组合形成原始注意力的低秩分解。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup> 键和值被投影到固定的投影维度 \( k \)，该维度可以是常数且与序列长度无关，因此注意力计算不再随 \( n \) 呈二次方增长。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup><sup> • </sup><sup>[7](https://www.sciencestack.ai/paper/2006.04768v3)</sup>

论文还考察了跨头和跨层的参数共享。值得注意的是，最激进的逐层共享策略——在整个模型中使用单个投影矩阵——既最显著地减少了参数，又在测试的三种共享策略中取得了最佳准确率。<sup>[7](https://www.sciencestack.ai/paper/2006.04768v3)</sup>

在逐层复杂度方面，论文的对比表将 Linformer 列为 \( O(n) \) 时间、\( O(1) \) 顺序操作，与标准 Transformer 的 \( O(n^2) \)、稀疏 Transformer 的 \( O(n\sqrt{n}) \) 以及 Reformer 的 \( O(n \log n) \)（\( O(\log n) \) 顺序操作）并列。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup>

## 实证结果

作者按照 BERT 配方在 BookCorpus 加英文维基百科上以掩码语言建模目标预训练 Linformer，观察到预训练性能与标准 Transformer 相似。随后在三个 GLUE 任务和 IMDB 情感分析任务上微调，发现性能与标准预训练 Transformer 相当或略优，同时训练和推理速度显著提升。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup> 后来的一篇综述指出，下游评估是在开发集上各运行一次进行的，每个任务的差距仅零点几个百分点——例如 IMDB 94.0 对 94.1，QNLI 90.4 对 90.9——且缺乏置信区间；它还指出效率实验涉及在单块 GPU 上使用随机数据进行推理。<sup>[8](https://www.chriswirz.com/reviews/linformer-self-attention-linear-complexity/index.html)</sup>

独立基准测试给出了更为复杂的图景。在《ACM 计算综述》对更快更轻 Transformer 的调查中，Linformer、Performer 和 Linear Transformer 被描述为更适合解决内存瓶颈，对于 1,000 和 4,000 token 序列分别比原始 Transformer 至少轻 56% 和 88%，其中 Linformer 因实现简单而突出。<sup>[3](https://doi.org/10.1145/3586074)</sup> 在长程竞技场（LRA）基准——Tay 等人专门提出的用于评估长上下文场景下模型质量的套件<sup>[9](https://research.google/pubs/long-range-arena-a-benchmark-for-efficient-transformers/)</sup>——上，独立复现给 Linformer 的评分为五项任务平均 51.36，低于同一对比表中 [Longformer](https://www.edgepedia.org/zh/longformer) 的 53.46。<sup>[4](https://lizeman.github.io/llm-arch-kb/attention/linformer/)</sup><sup> • </sup><sup>[3](https://doi.org/10.1145/3586074)</sup> Nyströmformer 论文报告 LRA 平均准确率比 Linformer 高 +3.36%。<sup>[10](https://pmc.ncbi.nlm.nih.gov/articles/PMC8570649/)</sup> 一篇 2023 年 ICLR 论文比较峰值内存和运行时间时观察到，虽然 Nyströmformer、Linformer 和 Performer 更快，但它们牺牲了预测准确率。<sup>[11](https://openreview.net/pdf?id=E8mzu3JbdR)</sup>

Linformer 也被应用于语言之外的领域。在微软的视觉-longformer 对比中，Linformer 视觉模型在 224×224 分辨率的 ImageNet-1K 上达到 81.0% top-1 和 95.4% top-5 准确率，参数量 2630 万，5.62 GFLOPs；而 longformer 变体为 82.0% 和 95.8%，参数量 2460 万，5.12 GFLOPs；performer 变体 top-1 为 78.7%。<sup>[12](https://github.com/microsoft/vision-longformer)</sup>

## 局限

设计带来两个结构性约束。第一，Linformer 假设序列长度固定，因为投影矩阵与选定长度绑定；序列必须填充到数据集中的最大长度，这可能降低效率。<sup>[5](https://github.com/lucidrains/linformer/blob/master/README.md)</sup><sup> • </sup><sup>[3](https://doi.org/10.1145/3586074)</sup> 第二，后续工作指出 Linformer 无法高效应用于因果注意力——自回归语言生成所需的设置——并引用 Tay 等人将其归类为仅适用于编码器设置。<sup>[6](https://aclanthology.org/2022.acl-long.515.pdf)</sup><sup> • </sup><sup>[4](https://lizeman.github.io/llm-arch-kb/attention/linformer/)</sup> 原始论文本身没有包含 4,096 或更长序列长度下的稠密注意力比较，因为全注意力无法在作者使用的 V100 硬件上容纳。<sup>[4](https://lizeman.github.io/llm-arch-kb/attention/linformer/)</sup> 独立复现工作还发现，在长度 512 上训练的 Linformer 在迁移到 4,096 长度模型时权重适应不佳。<sup>[13](https://pmc.ncbi.nlm.nih.gov/articles/PMC10153756/)</sup>

## 在高效 Transformer 中的地位

Linformer 属于 2020 年高效注意力设计浪潮，同期的还有 Longformer、Reformer、Performer 和 [Big Bird](https://www.edgepedia.org/zh/big-bird)，各自用不同工具攻克二次方注意力成本：稀疏性、哈希、核随机特征，或就 Linformer 而言，低秩投影。<sup>[2](https://arxiv.org/pdf/2006.04768)</sup> 综述将其归类为适用于编码器设置的固定模式加低秩混合方法。<sup>[4](https://lizeman.github.io/llm-arch-kb/attention/linformer/)</sup> 其低秩前提至今仍有影响力：后续工作如 Primal-Attention（NeurIPS 2023）同样以注意力矩阵的低秩性质为设计动机，并报告该性质在更深层中更为显著。<sup>[14](https://papers.neurips.cc/paper_files/paper/2023/file/cd687a58a13b673eea3fc1b2e4944cf7-Paper-Conference.pdf)</sup> 该技术持续存在于长文档编码器应用中，并作为高效注意力研究的标准基线，而非应用于生产环境中的生成模型。<sup>[4](https://lizeman.github.io/llm-arch-kb/attention/linformer/)</sup>

## References

1. [Sinong Wang, Belinda Z. Li, Madian Khabsa, Han Fang, Hao Ma (2020). Linformer: Self-Attention with Linear Complexity. arXiv:2006.04768.](https://arxiv.org/abs/2006.04768)
2. [Linformer: Self-Attention with Linear Complexity (full PDF, arXiv:2006.04768v3)](https://arxiv.org/pdf/2006.04768)
3. [A Practical Survey on Faster and Lighter Transformers. ACM Computing Surveys.](https://doi.org/10.1145/3586074)
4. [Linformer — Low-Rank Attention Projection. LLM Architecture KB.](https://lizeman.github.io/llm-arch-kb/attention/linformer/)
5. [lucidrains/linformer README. GitHub.](https://github.com/lucidrains/linformer/blob/master/README.md)
6. [Efficient attention via clustering (ACL 2022), noting Linformer cannot be efficiently applied in causal attention.](https://aclanthology.org/2022.acl-long.515.pdf)
7. [Linformer: Self-Attention with Linear Complexity (v3 summary). ScienceStack.](https://www.sciencestack.ai/paper/2006.04768v3)
8. [A review of Linformer: Self-Attention with Linear Complexity. Chris Wirz.](https://www.chriswirz.com/reviews/linformer-self-attention-linear-complexity/index.html)
9. [Long Range Arena: A Benchmark for Efficient Transformers. Google Research.](https://research.google/pubs/long-range-arena-a-benchmark-for-efficient-transformers/)
10. [Nyströmformer: A Nystöm-based Algorithm for Approximating Self-Attention. PMC.](https://pmc.ncbi.nlm.nih.gov/articles/PMC8570649/)
11. [ICLR 2023 conference paper comparing peak memory and running time of efficient attention models.](https://openreview.net/pdf?id=E8mzu3JbdR)
12. [microsoft/vision-longformer. GitHub.](https://github.com/microsoft/vision-longformer)
13. [Multi Resolution Analysis (MRA) for Approximate Self-Attention. PMC.](https://pmc.ncbi.nlm.nih.gov/articles/PMC10153756/)
14. [Primal-Attention: Self-attention through Asymmetric Kernel SVD in Primal Representation. NeurIPS 2023.](https://papers.neurips.cc/paper_files/paper/2023/file/cd687a58a13b673eea3fc1b2e4944cf7-Paper-Conference.pdf)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Neural networks and deep learning › Neural network architectures › Attention and transformer architectures

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/linformer

English original: https://www.edgepedia.org/linformer
