Files
myWiki/concepts/linear-attention.md

48 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "线性注意力 (Linear Attention)"
created: 2026-06-18
updated: 2026-06-18
type: concept
tags: [attention, efficiency, linear-complexity]
sources:
- dao-transformers-are-ssms-2024
---
# 线性注意力 (Linear Attention)
线性注意力是 Katharopoulos et al. (2020) 提出的注意力变体——将 Softmax 注意力转化为**线性复杂度**的核化形式,揭示了 "Transformers are RNNs" 的对偶关系。
## 核心技巧
利用矩阵乘法的结合律:
```
Y = softmax(QK^T) · V [O(T²) — 标准 Attention]
↓ 去掉 softmax引入核特征映射 φ
Y = (φ(Q) φ(K)^T) · V [核化 Attention]
Y = φ(Q) · (φ(K)^T · V) [结合律重排 → O(T)]
```
因果版本在右侧引入因果掩码 L下三角 1 矩阵)后,可展开为**循环形式**。
## 在 SSD 框架中的扩展
Dao & Gu (2024) 将线性注意力推广为 [[structured-masked-attention|结构化掩码注意力SMA]]
- 因果掩码 L 从**全 1** 推广为**数据依赖的衰减掩码** (a_t ∈ [0,1])
- SMA ⇔ SSM 的对偶关系:任何有快速循环形式的核注意力必然是 SSM
## 变体与进展
| 变体 | 关键创新 |
|------|---------|
| 原始 Linear Attention | φ = elu(x) + 1 |
| RetNet (Sun et al., 2023) | 更一般的 L 结构 |
| GateLoop (Katsch, 2023) | 门控线性注意力 |
| SMA (Dao & Gu, 2024) | 数据依赖的 L + 半可分矩阵连接 |
## 参考
- [[structured-masked-attention|SMA]]
- [[structured-state-space-duality|SSD]]
- [[state-space-models|状态空间模型]]
- [[dao-transformers-are-ssms-2024|论文]]