# 门控线性注意力

**门控线性注意力**（Gated Linear Attention，GLA）是一种序列建模层，它通过数据相关的门控机制扩展了[线性注意力](https://www.edgepedia.org/zh/linear-attention)，使得由 GLA 层构建的 [Transformer](https://www.edgepedia.org/zh/transformer) 在训练时可以采用并行矩阵乘法，而在推理时则作为具有固定大小矩阵值状态和序列长度线性时间复杂度的循环网络运行。该模型由杨松林（Songlin Yang）、王柏林（Bailin Wang）、沈逸康（Yikang Shen）、拉梅斯瓦尔·潘达（Rameswar Panda）和尹金（Yoon Kim）在 2023 年 12 月的一篇 arXiv 论文中提出，并于 2024 年在第 41 届[国际机器学习大会](https://www.edgepedia.org/zh/international-conference-on-machine-learning)上发表。<sup>[1](https://proceedings.mlr.press/v235/yang24ab)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/2312.06635)</sup>

| 关键事实 | 详情 |
|---|---|
| 来源 | 《具有硬件高效训练的门控线性注意力 Transformer》，杨松林、王柏林、沈逸康、拉梅斯瓦尔·潘达和尹金；arXiv 2023 年 12 月 11 日；ICML 2024，PMLR 235:56501–56523<sup>[1](https://proceedings.mlr.press/v235/yang24ab)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/2312.06635)</sup> |
| 核心思想 | 将线性注意力表述为具有二维（矩阵值）隐藏状态的 RNN，并辅以数据相关的门控以增强表达能力<sup>[2](https://arxiv.org/abs/2312.06635)</sup> |
| 硬件算法 | FlashLinearAttention，一种 I/O 感知的实现，在内存移动与可并行性之间进行权衡；作者报告称，即使在 1K 序列长度下，作为独立层它也比 FlashAttention-2 更快<sup>[2](https://arxiv.org/abs/2312.06635)</sup> |
| 报告的性能 | 在 340M 参数/15B token 和 1.3B 参数/100B token 的规模下，GLA 的表现可与 LLaMA 架构 Transformer、RetNet 和 Mamba 相竞争<sup>[3](https://openreview.net/pdf?id=ia5XvxFUJT)</sup> |
| 长度泛化 | 在 2K token 上下文上训练的模型可以泛化到超过 20K 的序列，而困惑度没有显著上升<sup>[2](https://arxiv.org/abs/2312.06635)</sup> |
| 训练速度 | 训练吞吐量高于同等规模的 Mamba 模型<sup>[2](https://arxiv.org/abs/2312.06635)</sup> |
| 软件 | 开源的 `flash-linear-attention`（fla）库，提供 chunk、fused_recurrent 和 fused_chunk 三种 GLA 内核；MIT 许可的检查点如 gla-1.3B-100B<sup>[4](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/gla.py)</sup><sup> • </sup><sup>[5](https://huggingface.co/fla-hub/gla-1.3B-100B/blame/main/README.md)</sup> |

## 背景：线性注意力作为循环

线性注意力用核特征映射替代了标准注意力中的 softmax，因此注意力输出可以通过将键和值的外积累加到一个矩阵状态中来计算。这种累加正是一个线性循环：第 \( t \) 步的状态是前一步状态的衰减副本加上当前的键值外积，输出则是该状态作用于查询。由于循环是线性的，该层支持两种等价的计算形式：用于高效训练的整序列并行形式，以及用于推理的、具有固定大小二维隐藏状态的循环形式，其成本随序列长度线性增长而非二次增长。<sup>[2](https://arxiv.org/abs/2312.06635)</sup>

有两个问题促使了 GLA 的提出。首先，线性注意力在语言建模中通常不如普通的 softmax 注意力。其次，现有实现缺乏 I/O 感知，运行速度慢于高度优化的 softmax 注意力内核。RetNet 和 TransNormerLLM 等早期工作已经表明，在循环更新中加入全局（数据无关的）衰减项可以大幅提升性能；GLA 的贡献在于使衰减变为数据相关，从而让模型学习逐 token、逐通道的遗忘。<sup>[2](https://arxiv.org/abs/2312.06635)</sup><sup> • </sup><sup>[6](https://huggingface.co/papers/2312.06635)</sup>

## 方法

**数据相关的门控。** GLA 在加入新的键值外积之前，先用当前输入计算出的门乘以循环状态。该门是逐元素、逐通道的，在对数空间中应用，以使多步累积衰减在数值上保持稳定。该层还使用一个输出门（参考实现中是对层输出施加 swish 激活的门），并支持具有低秩门投影的多头操作。<sup>[2](https://arxiv.org/abs/2312.06635)</sup><sup> • </sup><sup>[4](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/gla.py)</sup>

**数值障碍。** 门控并行形式的直接且数值稳定的实现需要在对数空间中进行广义矩阵乘法，这无法利用现代 GPU 为标准矩阵乘法优化的张量核心。<sup>[6](https://huggingface.co/papers/2312.06635)</sup>

**FlashLinearAttention 与分块训练。** 论文的硬件高效算法将序列划分为块，并计算保持标准矩阵乘法形式的块并行更新，在内存移动与可并行性之间进行权衡。分块形式在完全并行和完全循环形式之间插值，是 FlashLinearAttention（FLA）实现的基础，作者报告称即使在 1K 序列长度下，作为独立层它也比 FlashAttention-2 更快。<sup>[2](https://arxiv.org/abs/2312.06635)</sup><sup> • </sup><sup>[7](https://proceedings.neurips.cc/paper_files/paper/2024/file/d3f39e51f5f634fb16cc3e658f8512b9-Paper-Conference.pdf)</sup> 后来的综述工作将 GLA 的分块公式描述为线性注意力的广义并行形式，能够实现细粒度的数据相关衰减。<sup>[8](https://www.asabook.cc/en/papers/gated-linear-attention/)</sup>

## 实证结果

论文从头训练了两个规模的 GLA Transformer，并与匹配的基线进行对比：Transformer++（采用旋转位置嵌入、SwiGLU 和 RMSNorm 的 LLaMA 架构）、RetNet 和 [Mamba](https://www.edgepedia.org/zh/mamba-deep-learning-architecture)，所有模型均在相同数据上训练相同数量的 token。<sup>[3](https://openreview.net/pdf?id=ia5XvxFUJT)</sup>

| 规模 | 模型 | Wiki. 困惑度 | LMB. 困惑度 | 下游平均准确率 |
|---|---|---|---|---|
| 340M / 15B tokens | Transformer++ | 28.39 | 42.69 | 41.2 |
| 340M / 15B tokens | Mamba | 28.39 | 39.66 | 41.8 |
| 340M / 15B tokens | GLA | 28.65 | 43.35 | 41.5 |
| 1.3B / 100B tokens | Transformer++ | 16.85 | 13.44 | 50.9 |
| 1.3B / 100B tokens | Mamba | 17.06 | 13.89 | 50.0 |
| 1.3B / 100B tokens | GLA | 17.22 | 14.47 | 51.0 |

GLA 在两个规模上的困惑度均略逊于 softmax 注意力 Transformer++（340M 规模下 28.65 对 28.39；1.3B 规模下 17.22 对 16.85），但在 PIQA、HellaSwag、Winogrande、ARC-e 和 ARC-c 等下游基准的平均值上持平或超越。<sup>[3](https://openreview.net/pdf?id=ia5XvxFUJT)</sup>

**长度泛化**是作者强调的结果：在 2K token 上下文上训练的 GLA 模型可以泛化到超过 20K 的序列，而困惑度没有显著上升。<sup>[2](https://arxiv.org/abs/2312.06635)</sup> 在训练吞吐量方面，GLA Transformer 超过了同等规模的 Mamba 模型。<sup>[2](https://arxiv.org/abs/2312.06635)</sup> 一篇对该论文的评述指出，所有报告的结果都处于中等规模范围，因此门控是否能在更大规模下完全弥合与 softmax 注意力的表达能力差距，在发表时仍未经过测试。<sup>[9](https://pith.science/paper/2312.06635)</sup>

## 软件与采用

作者发布了 `flash-linear-attention`（fla）库，提供基于 Triton 的三种模式的 GLA 内核（chunk、fused_recurrent 和 fused_chunk）、一个可配置头数、键/值扩展比、短卷积和可选 swish 输出门的 `GatedLinearAttention` 层，以及 MIT 许可的预训练检查点，如基于 100B token 的 [SlimPajama](https://www.edgepedia.org/zh/slimpajama) 训练的 gla-1.3B-100B。<sup>[4](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/gla.py)</sup><sup> • </sup><sup>[5](https://huggingface.co/fla-hub/gla-1.3B-100B/blame/main/README.md)</sup> 该库此后已扩展到涵盖许多线性注意力和状态空间架构，GLA 被列为其 2024 年模型之一。<sup>[10](https://github.com/fla-org/flash-linear-attention/blob/main/README.md)</sup>

GLA 已被后续工作用作组件或基线。门控槽注意力（Gated Slot Attention，GSA）是 NeurIPS 2024 的一个模型，它引入了受 GLA 启发的门控机制，并复用了其硬件高效训练算法；在将 Mistral-7B 微调为循环架构时，GSA 在长上下文任务上优于微调为 GLA。<sup>[11](https://proceedings.neurips.cc/paper_files/paper/2024/hash/d3f39e51f5f634fb16cc3e658f8512b9-Abstract-Conference.html)</sup> 用于图像生成的 DiG 扩散骨干网络采用了 GLA，报告称 DiG-XL/2 在 1024 分辨率下比基于 Mamba 的模型快 4.2 倍。<sup>[12](https://openaccess.thecvf.com/content/CVPR2025/papers/Zhu_DiG_Scalable_and_Efficient_Diffusion_Models_with_Gated_Linear_Attention_CVPR_2025_paper.pdf)</sup> 一项 2025 年关于混合线性-全注意力模型的系统性研究训练了 72 个开源模型，涵盖包括 GLA 在内的六种线性变体，并推荐 HGRN-2 或 GatedDeltaNet，采用 3:1 到 6:1 的线性-全注意力比例以达到 Transformer 级别的召回能力。<sup>[13](https://arxiv.org/abs/2507.06457)</sup>

## 在门控线性模型中的定位

GLA 属于 2023–2024 年间为线性循环添加数据相关衰减的一系列架构，包括 RetNet（头级数据无关衰减）、Mamba 和 GateLoop（数据相关的选择性状态转移）、HGRN2（具有外积状态扩展的门控线性 RNN）以及 Gated DeltaNet（带门控的 delta 规则更新）。<sup>[7](https://proceedings.neurips.cc/paper_files/paper/2024/file/d3f39e51f5f634fb16cc3e658f8512b9-Paper-Conference.pdf)</sup><sup> • </sup><sup>[14](https://arxiv.org/abs/2404.07904)</sup> 在训练吞吐量的基准测量中，Gated DeltaNet 在一个混合器基准中达到每秒 3.891M token，而密集多头注意力为 2.941M，此后的比较通常将 Gated DeltaNet 视为语言建模中更强的有界状态基线。<sup>[15](https://knlp.io/linear_attention.html)</sup> Google 的 Griffin 和 Hawk 模型使用了相关类型的门控线性循环，而 RecurrentGemma 则建立在 Griffin 的门控线性循环与局部滑动窗口注意力的混合架构之上。<sup>[16](https://arxiv.org/abs/2402.19427)</sup><sup> • </sup><sup>[17](https://developers.googleblog.com/gemma-explained-recurrentgemma-architecture/)</sup>

这些固定状态模型的共同局限是召回：线性注意力将历史压缩到有界状态中，因此召回密集型任务仍然比具有 token 可寻址键值缓存的 softmax 注意力更困难，而基本的召回-内存权衡适用于所有恒定内存推理模型。<sup>[7](https://proceedings.neurips.cc/paper_files/paper/2024/file/d3f39e51f5f634fb16cc3e658f8512b9-Paper-Conference.pdf)</sup>

## References

1. [Yang, S., Wang, B., Shen, Y., Panda, R., & Kim, Y. (2024). Gated Linear Attention Transformers with Hardware-Efficient Training. ICML 2024, PMLR 235:56501–56523.](https://proceedings.mlr.press/v235/yang24ab)
2. [Yang, S., et al. Gated Linear Attention Transformers with Hardware-Efficient Training. arXiv:2312.06635.](https://arxiv.org/abs/2312.06635)
3. [Yang, S., et al. Gated Linear Attention Transformers with Hardware-Efficient Training (OpenReview PDF, with main results table and training details).](https://openreview.net/pdf?id=ia5XvxFUJT)
4. [flash-linear-attention: fla/layers/gla.py (GLA layer implementation).](https://github.com/fla-org/flash-linear-attention/blob/main/fla/layers/gla.py)
5. [fla-hub/gla-1.3B-100B model card, Hugging Face.](https://huggingface.co/fla-hub/gla-1.3B-100B/blame/main/README.md)
6. [Gated Linear Attention Transformers with Hardware-Efficient Training, Hugging Face paper page (full abstract).](https://huggingface.co/papers/2312.06635)
7. [28], and there is a fundamental recall-memory trade-off [3, 91] where all inference-time-constant-memory models face inherent limitations. (ii) in terms of training efficiency, while linear attention supports hardware-efficient chunkwise training [96] as implemented in flashlinearattention (fla [95]), training from scratch on trillions of tokens remains prohibitively expensive. a paradigm, “finetuning pretrained transformers to rnns” (short for t2r [41]), has recently gained great attention [101, 10, 54, 13, 7, 90]. this approach circumvents the high cost of training from scratch by requiring only a few billion tokens for finetuning—about 1–3% of the total cost. however, linear attention uses a different kernel method from softmax, leading to performance discrepancies when finetuning pretrained softmax attention models to linear attention [101].](https://proceedings.neurips.cc/paper_files/paper/2024/file/d3f39e51f5f634fb16cc3e658f8512b9-Paper-Conference.pdf)
8. [Gated Linear Attention, ASa Book paper summary.](https://www.asabook.cc/en/papers/gated-linear-attention/)
9. [Pith Review: Gated Linear Attention Transformers with Hardware-Efficient Training.](https://pith.science/paper/2312.06635)
10. [flash-linear-attention README (model table and release history).](https://github.com/fla-org/flash-linear-attention/blob/main/README.md)
11. [Zhang, Y., Yang, S., et al. (2024). Gated Slot Attention for Efficient Linear-Time Sequence Modeling. NeurIPS 2024.](https://proceedings.neurips.cc/paper_files/paper/2024/hash/d3f39e51f5f634fb16cc3e658f8512b9-Abstract-Conference.html)
12. [Zhu, et al. (2025). DiG: Scalable and Efficient Diffusion Models with Gated Linear Attention. CVPR 2025.](https://openaccess.thecvf.com/content/CVPR2025/papers/Zhu_DiG_Scalable_and_Efficient_Diffusion_Models_with_Gated_Linear_Attention_CVPR_2025_paper.pdf)
13. [Wang, D., Zhu, R.-J., et al. (2025). A Systematic Analysis of Hybrid Linear Attention. arXiv:2507.06457.](https://arxiv.org/abs/2507.06457)
14. [Qin, Z., Yang, S., et al. (2024). HGRN2: Gated Linear RNNs with State Expansion. arXiv:2404.07904.](https://arxiv.org/abs/2404.07904)
15. [Linear Attention & Bounded Memory: DeltaNet, MoM, DeltaProduct, Trellis (knlp.io benchmark notes).](https://knlp.io/linear_attention.html)
16. [De, S., et al. (2024). Griffin: Mixing Gated Linear Recurrences with Local Attention for Efficient Language Models. arXiv:2402.19427.](https://arxiv.org/abs/2402.19427)
17. [Gemma explained: RecurrentGemma architecture, Google Developers Blog.](https://developers.googleblog.com/gemma-explained-recurrentgemma-architecture/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › 基础模型方法与训练 › 大语言模型架构与扩展

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/gated-linear-attention

English original: https://www.edgepedia.org/gated-linear-attention
