2405.17381-various-lengths-constant-speed-lightning-attention
Various Lengths, Constant Speed: Efficient Language Modeling with Lightning Attention
这篇论文把 linear attention 在 causal LM 中“理论复杂度低、实际 GPU 训练慢”的核心原因定位到 prefix cumsum / scan 路径,并用 Lightning Attention 把注意力拆成块内 left product 与块间 right product:块内保留并行矩阵乘,块间维护键值累计状态,再用 tiling / IO-aware kernel 提升硬件效率;随后作者为该算子配套设计 TransNormerLLM,在 44M 到 15B 规模上展示了更稳定的长序列训练速度和接近主流 Transformer LLM 的任务表现。
Source
- Title: Various Lengths, Constant Speed: Efficient Language Modeling with Lightning Attention
- arXiv: https://arxiv.org/abs/2405.17381
- HTML v2: https://arxiv.org/html/2405.17381v2
- PDF v2: https://arxiv.org/pdf/2405.17381
- TeX Source v2: https://arxiv.org/e-print/2405.17381
- Code/Project: https://github.com/OpenNLPLab/TransnormerLLM
- Authors: Zhen Qin, Weigao Sun, Dong Li, Xuyang Shen, Weixuan Sun, Yiran Zhong
- Submitted: 2024-05-27
- Current version read: v2, revised 2024-06-20
- Subjects: Computation and Language (
cs.CL) - Comments: Accepted by ICML 2024. Yiran Zhong is the corresponding author.
作者与关系
- Zhen Qin: TapTap。
- Weigao Sun: OpenNLPLab, Shanghai AI Lab。
- Dong Li: OpenNLPLab, Shanghai AI Lab。
- Xuyang Shen: OpenNLPLab, Shanghai AI Lab。
- Weixuan Sun: OpenNLPLab, Shanghai AI Lab。
- Yiran Zhong: OpenNLPLab, Shanghai AI Lab.
阅读目标与判断边界
本笔记关注:
- Lightning Attention 如何把 causal linear attention 的
cumsum瓶颈转成 block-wise 计算。 - TransNormerLLM 为了补齐 linear attention 的精度和稳定性做了哪些结构改动。
- 实验中哪些证据支持“长序列效率”,哪些证据支持“能力接近 Transformer”。
- 本文和 MiniMax-M1、long-output RL、long-context serving 论文之间的关系。
判断边界:
- Lightning Attention 是 norm linear attention 的高效实现,精确对象是该类 linear attention;softmax attention 的精确等价替代需要另行证明。
- “constant speed” 应按论文实验条件理解:固定硬件、batch / model 配置和内存约束下,随着 sequence length 增长,TNL 的 tokens per GPU per second 下降显著弱于 softmax Transformer / FlashAttention-2 等对照。
- TNL benchmark 使用自收集数据和不同训练 token 数,和开源 Transformer 模型的横向比较需要谨慎;它更适合证明“linear attention 架构可以进入可用 LLM 区间”,单独归因到某个算子会过强。
- 论文主要验证 pretraining / inference efficiency,没有处理 RL rollout、policy update、reward model 或 tool-use harness。
论文脉络
1. 问题背景
Linear attention 的理论吸引力很直接:如果 attention 可以写成
论文将 causal linear attention 的两种直接实现拆开比较:
左乘路径先算完整块的
其中
右乘路径维护 prefix 状态:
这一路径复杂度是
2. 核心假设或切入点
作者的切入点是:causal linear attention 不必在“完整 left product”和“逐 token right product”之间二选一。序列可以按 block 切开,block 内 token 数较小,使用 left product 可以保留并行矩阵乘;block 间只需要把之前 block 的历史信息压缩进一个
给定 block size
直觉上,块内处理“近邻因果关系”,块间处理“过去所有 block 的压缩历史”。这样既保留了 GPU 友好的块状矩阵乘,也保留了 linear attention 对长历史的低复杂度优势。
3. 方法 / 系统 / 理论框架
Lightning Attention 的 forward pass 将
论文给出的复杂度定理是:
其中
Lightning Attention 还支持带 decay 的形式。TNL 使用的 LRPE-d 位置编码可写成:
省略复数旋转部分后,它对应如下 recurrence:
论文把这个 decay recurrence 也写成 block-wise Lightning Attention with decay。对第
下层有更小的理论感受野,上层有更长的历史覆盖,这与 TransNormer 的层级感受野动机一致。
4. TransNormerLLM 结构
作者认为只有高效 attention kernel 不够,还需要一个适配 linear attention 的 LLM 架构。TransNormerLLM 的 block 由两部分组成:
- Gated Linear Attention, GLA,用于 token mixing。
- Simple Gated Linear Unit, SGLU,用于 channel mixing。
GLA 的形式是:
作者选择 Swish 作为 1+elu 会出现 NaN 风险。
SGLU 去掉了普通 GLU 中的 activation:
归一化使用 SimpleRMSNorm:
实验显示 SRMSNorm、RMSNorm、LayerNorm 的 loss / PPL 差异很小,SRMSNorm 的主要价值在于配合 Triton 优化大维度速度。
5. 结论链条
论文的因果链条是:
- Linear attention 的理论瓶颈已经较低,但 causal LM 中的 prefix scan 降低 GPU 训练效率。
- Lightning Attention 用 block-wise divide-and-conquer 消除长序列 scan 路径,把块内二次计算限制在
,块间用 state 累计历史。 - IO-aware tiling 让该算法在 HBM/SRAM 数据搬运上更接近 FlashAttention 风格的硬件友好实现。
- TNL 用 LRPE-d、GLA、SGLU、SRMSNorm 等结构改动缓解 linear attention 的表达能力和训练稳定性问题。
- 因此,linear attention 架构可以在长序列训练速度上明显优于 softmax Transformer,同时在常规 LLM benchmark 上达到可竞争水平。
关键实验/定理
结果 1:Lightning Attention 模块速度和显存
- 设置:A100 80G,PyTorch / Metaseq,Lightning Attention 用 Triton 实现;对比 vanilla norm linear attention PyTorch 实现和 FlashAttention-2。
- 指标:forward/backward runtime、memory footprint,sequence length 变化。
- 结果:Lightning Attention 的 forward/backward runtime 随 sequence length 呈明显线性增长趋势;vanilla 和 FlashAttention-2 曲线更接近二次增长。显存上,vanilla 很快耗尽,Lightning Attention 的趋势接近 FlashAttention-2 且更低。
- 解读:这直接支持论文最核心的系统主张:通过 block-wise + IO-aware 实现,linear attention 的理论复杂度可以转化为真实 GPU 路径收益。
结果 2:复杂度定理
- 设置:block size 为
,序列长度为 ,hidden dimension 为 。 - 结论:
- 解读:当实践中
时,复杂度接近 。相比 softmax attention 的 ,长序列增长时优势会随 放大。
结果 3:Wikitext-103 小模型语言建模
- 设置:约 44M 参数模型,对比 Transformer、FLASH、Performer、cosFormer、RWKV、HGRN、TNN 等结构。
- 指标:validation / test perplexity。
- 结果:TNL 达到 val PPL
23.46、test PPL24.03,在表中优于 TNN test PPL24.67、HGRN test PPL24.82和 attention Transformer test PPL24.78。 - 解读:小规模 controlled setting 支持 TNL 在 perplexity 上没有明显牺牲,说明效率提升没有以大幅语言建模退化为代价。
结果 4:1B / 3B 训练速度与 loss 曲线
- 设置:TNL、LLaMA-FA2、HGRN、TNN 在相同 30B corpus 和相同硬件下重训,序列长度从 1K 扩展到 128K。
- 指标:training loss、tokens per GPU per second。
- 结果:TNL 在 1B 和 3B 都取得最低训练 loss;当 sequence length 增长时,TNL 的 TGS 保持较高,其他结构下降更明显。
- 解读:这是本文对“长序列训练速度 + 训练质量”的核心证据。它比单独 kernel benchmark 更有价值,因为它检验了完整模型结构。
结果 5:7B inference throughput
- 设置:A100 80G,7B 模型,512-token prompt,最多生成 1024 new tokens,使用 HuggingFace 标准代码路径,batch size 按显存约束选择。
- 指标:inference throughput。
- 结果:TNL with Lightning Attention 相比 Transformer 结构模型最高达到约
11xthroughput。 - 解读:这支持 recurrent / linear attention 在 decode 阶段的优势。实际部署收益仍取决于服务框架、batching、KV cache、kernel 和模型并行开销。
结果 6:通用 benchmark 与长文档 benchmark
- 设置:385M、1B、7B、15B TNL 在自收集数据上训练;评测 BoolQ、PIQA、HellaSwag、WinoGrande、ARC、OpenBookQA、MMLU、C-Eval,以及 SCROLLS。
- 指标:0-shot commonsense、5-shot MMLU/C-Eval、SCROLLS ROUGE/F1/EM。
- 结果:15B TNL 在表中达到 HellaSwag
82.18、WinoGrande75.61、ARC-c50.51、MMLU60.06、C-Eval53.01;1B TNL 在 SCROLLS Avg12.51,高于列出的 1B 级 baseline。 - 解读:TNL 能进入可用 LLM 性能区间,但横向模型训练 token、数据配方和评测设置存在差异,不能把全部收益归因给 Lightning Attention。
结果 7:结构消融
- Positional encoding:LRPE-d 的 PPL
4.728最好;作者最终选择 mix 策略,因为可提升约15%到20%训练速度且性能影响较小。 - Decay temperature:加入层/头相关 decay temperature 后 PPL
4.770,去掉后 PPL4.804。 - Gate:有 gate 时 loss
2.248、PPL4.770;无 gate 时 loss2.263、PPL4.820。 - Normalization:SRMSNorm、RMSNorm、LayerNorm 表现接近,速度工程价值大于精度差异。
- GLA activation:Swish 和
1+elu表现接近,但1+elu在 7B 上出现 NaN,最终选择 Swish。 - GLU activation:去掉 activation 的 SGLU PPL
4.770,Swish GLU PPL4.788,说明简化通道混合没有明显损失。
证据链强度评估
强证据
- 算法部分清楚给出 left product、right product、block-wise forward/backward 和复杂度定理,机制可复查。
- 模块级 speed/memory benchmark 直接比较 vanilla linear attention 和 FlashAttention-2,支撑 Lightning Attention 的系统收益。
- 1B / 3B 同语料重训对比把“kernel 快”推进到“完整模型训练 loss 和 TGS 仍占优”。
中等强度证据
- 44M 到 15B benchmark 覆盖广,说明 TNL 已扩展到小模型之外的规模;但训练数据和 token 数不完全对齐,横向比较只能作为可竞争性证据。
- 7B inference throughput 最高
11x的结果很有吸引力,但它和 HuggingFace 标准实现、batch size 选择、服务框架优化程度密切相关。 - 消融实验支持 LRPE-d、gate、decay temperature 等设计,但大多在 385M / 100K updates setting 下完成,扩展到 15B 后仍需更多大规模 ablation。
需要谨慎的推论
- “unlimited sequence length” 更适合作为算法方向表述;真实模型仍受数值稳定性、训练数据长度、position encoding、显存、吞吐和评测覆盖限制。
- Lightning Attention 的精确性对应 norm linear attention,不等价于 softmax attention 的精确替代。下游任务中何时需要 softmax 的精确 pairwise interaction,需要单独实验。
- TNL 能力来自 Lightning Attention、架构改动、数据、训练配方和工程实现的组合,单独归因到 Lightning Attention 会过强。
- 本文没有讨论 RL 训练中的 rollout/trainer mismatch。MiniMax-M1 后续报告表明,长上下文架构进入 RL 后,数值一致性和 precision setting 会变成关键工程变量。
OpenReview / 审稿意见吸收
- Venue status: 当前档案未记录公开 peer-review 状态。
- Public reviews: 当前档案未记录可可靠匹配的 OpenReview / ARR / 会议 reviewer comments。
- Ratings / confidence: 无公开评分可用于校准。
- Reviewer consensus: 暂无。
- Main criticisms: 暂无公开 reviewer 质疑可引用;可信度主要由论文、技术报告、项目证据和本地一致性检查决定。
- Author response: 暂无公开 rebuttal 记录。
- 对本文可信度的影响: 按未完成公开审稿吸收处理,结论需要依赖实验设置、baseline 强度、复现证据和跨论文一致性校准。
本地讨论补充
1. 讨论收敛点
- 如果只想理解 MiniMax-M1 中 Lightning Attention 的来源,优先读本文,比
2401.04658更完整。2401.04658更偏 technical report,本文是 ICML 2024 版本,包含 TNL 架构和更完整实验。 - 两篇 Lightning Attention 原始论文采用个人作者署名,作者均为 Zhen Qin、Weigao Sun、Dong Li、Xuyang Shen、Weixuan Sun、Yiran Zhong。MiniMax-M1 是后续系统报告,使用了 Lightning Attention / TransNormer 系统线;作者关系应记录为同名线索和技术采用关系。
2. 修正后的理解
- Lightning Attention 的核心贡献是把 causal linear attention 的低复杂度路径改造成 GPU 友好的块化计算路径;它没有把自身定位成新的 softmax 近似。
- TNL 的作用是补齐 linear attention 的模型架构侧问题。只看 Lightning Attention 算子会低估本文贡献;只看 benchmark 又会掩盖作者真正解决的 scan / IO / block computation 问题。
- MiniMax-M1 采用 hybrid Lightning Attention + softmax attention,说明大规模 long-output reasoning model 很可能会在效率和精确全局交互之间做混合设计;直接全量替换为 pure linear attention 仍需要更多证据。
3. 单 token 推理与历史 KV state
在 decode 阶段,新进来一个 token
如果使用 Gated Linear Attention,还会计算 gate:
历史状态是一个固定大小矩阵:
带 decay 时写成:
递推形式为:
或带 decay:
当前 token 读取历史的方式是:
展开后得到:
因此,历史 KV 可以理解为所有历史 token 的 key-value 外积累加。每个
4. 分块的含义
论文中的分块主要对应训练 / prefill 阶段的序列维度切分。给定长度
[1..B] [B+1..2B] [2B+1..3B] ...
对于当前 block
块内因果交互:
当前 block 读取过去 blocks:
处理完当前 block 后更新历史状态:
最终输出是:
这个设计把逐 token scan 改成逐 block scan:block 内部用小规模 left product,保留矩阵乘并行性;block 之间用
5. 无 softmax 的合理性与边界
Lightning Attention 应按 NormAttention / linear attention 家族理解,softmax attention 的精确等价需要另行证明。标准 softmax attention 先计算:
再对
或右乘形式:
它去掉 softmax 后,主要依赖四类机制维持可训练性和表达能力:
- feature map / activation:TNL 中
、 ,作者选择 Swish。 - output normalization:NormAttention / SRMSNorm 控制
的尺度。 - gate:GLA 用
调制输出,增强通道选择能力。 - decay / relative position:LRPE-d 用
和相对位置项提供距离相关的归纳偏置。
这种设计的代价是:模型没有显式归一化的 token-to-token 概率分布,历史信息被压进固定大小的
6. 与普通 decoder-only Transformer 的结构对比
普通 decoder-only Transformer block 可以抽象成:
X
-> Norm
-> softmax multi-head self-attention
-> residual
-> Norm
-> FFN / SwiGLU
-> residual
其 token mixing 路径通常是:
其中
TNL / Lightning Attention block 可以抽象成:
X
-> SRMSNorm / Norm
-> Gated Linear Attention with Lightning Attention
-> residual
-> SRMSNorm / Norm
-> SGLU
-> residual
其 token mixing 路径是:
训练 / prefill 阶段通过 block-wise 形式计算:
decode 时每层每个 head 保留 recurrent state:
或带 decay:
结构差异可以整理为:
| 维度 | 普通 Transformer | TNL / Lightning Attention |
|---|---|---|
| Token mixing | 显式构造 |
用 |
| 历史表示 | 保存每个历史 token 的 |
保存压缩后的 |
| Decode 成本 | 新 token 需要读取历史 KV,attention 读写随历史长度增长 | 新 token 更新 |
| Training / prefill | FlashAttention 优化 softmax attention 的 IO 路径,计算结构仍是 length-quadratic | Lightning Attention 改计算结构,复杂度为 |
| 位置机制 | RoPE / ALiBi / relative bias 常和 softmax attention 绑定 | LRPE-d / decay 可分解进 recurrence 和 block-wise 计算 |
| 归一化 | attention 内部由 softmax 归一化,block 外常用 RMSNorm / LayerNorm | NormAttention / SRMSNorm 控制 |
| Channel mixing | FFN / SwiGLU 等 MLP 子层 | SGLU,去掉 GLU activation 以降低开销 |
| 能力倾向 | 精确 pairwise interaction 和 retrieval 更直接 | 长序列效率更强,精确 token 选择依赖 feature map、norm、gate 和 decay |
因此,普通 Transformer 的关键资产是显式 pairwise attention distribution;TNL 的关键资产是固定大小 recurrent state 和 block-wise GPU 友好计算。前者更适合需要精确 token 选择的场景,后者更适合长上下文、长输出和高 rollout 成本场景。MiniMax-M1 的 hybrid 结构可以理解为把这两类资产组合起来:多数层用 Lightning Attention 降低长序列成本,周期性 softmax attention 层保留精确全局交互能力。
7. 后续复验指标
- 在现代 serving stack 中复测 Lightning Attention decode throughput,区分模型计算、attention kernel、KV cache 和通信开销。
- 在相同数据、相同 token budget、相同 optimizer 下,对比 softmax Transformer、pure Lightning Attention、hybrid Lightning Attention。
- 对长上下文任务补充 retrieval、multi-hop、needle-in-haystack、long-output reasoning 和 RL rollout 场景,确认“长序列速度”是否稳定转化为“长上下文能力”。
- 检查 train/inference probability correlation,尤其是 mixed precision、LM head、normalization 和 recurrent state 对 logprob 的影响。
主要启发
- Linear attention 的真正工程难点在于“低复杂度公式如何映射到 GPU 执行路径”。渐进复杂度只是第一层,scan、HBM/SRAM 搬运、tiling 和 backward pass 同样决定训练成本。
- 长上下文模型需要 attention 算子、位置编码、归一化、gate 和 FFN 结构共同设计。单个 kernel 很难独立承担全部能力和稳定性。
- Lightning Attention 为 long-output reasoning RL 提供了架构效率基础,但 RL 场景还需要处理 rollout 分布、logprob 一致性、reward 信号和 policy update 成本。
- MiniMax-M1 的 hybrid design 可以视为本文的后续系统化应用:大部分层追求长序列效率,周期性 softmax 层保留更精确的全局 pairwise interaction。
局限
- 作者没有给出 pure softmax Transformer、pure Lightning Attention、hybrid Lightning Attention 在同一大规模训练 recipe 下的完整因果拆解。
- 自收集数据细节有限,benchmark 对比受到数据、token 数和训练配方影响。
- 长序列速度实验很强,但长上下文能力实验覆盖相对有限,尤其缺少 128K 级别真实下游能力评测。
- 论文没有系统分析 precision、batching、parallelism、serving backend 对 Lightning Attention 数值一致性和吞吐的影响。
- 2024 年后的 Mamba、Hyena、RetNet、RWKV 变体和现代 FlashAttention / paged attention serving stack 发展较快,本文结论需要放进更新的 baseline 中复测。
跨论文关系
- 与 2506.13585 的关系:本文是 MiniMax-M1 采用 Lightning Attention / TransNormer 系统线的上游来源。MiniMax-M1 把它放进 hybrid MoE long-output reasoning model,并在 RL 中进一步暴露 train/inference probability mismatch 问题。
- 与 2605.14220 的关系:本文关注长序列 attention 计算效率;TIM 论文提醒,当这种架构进入 RL,rollout engine 和 trainer engine 的数值一致性会影响 policy update。
- 与 2511.02749 和 2405.19888 的关系:本文从模型架构和 kernel 层降低长序列成本;这些 serving 论文从 KV locality 与 application DAG 角度降低真实 workload 成本。
- 与 2409.19256 和 2606.00135 的关系:本文降低长上下文/长输出模型计算成本;RLHF/RLVR/tool-use 系统还要计入 rollout、reward/verifier/reference model、工具返回和 policy update。
- 跨论文关系定位:记录 Lightning Attention 与 TransNormerLLM,并连接到 MiniMax-M1、Long-context Serving、Training-Inference Mismatch 和 RL systems compute accounting。
Reference Intake Brief
Target
- Intended target system: 新增论文笔记 / Lightning Attention 与 long-context architecture 文档。
- Existing related assets:
content/utility/papers-index.md;已存档 MiniMax-M1 笔记 2506.13585。 - Proposed form: 新建独立 Markdown 文档,并更新
content/utility/papers-index.md和 MiniMax-M1 相关来源说明。
Reusable Elements
- Block-wise causal linear attention 公式:
、 、 更新。 - 复杂度结论:
,实践中 。 - LRPE-d / decay recurrence 与分层感受野设置。
- TNL 架构组合:GLA、SGLU、SRMSNorm、Swish、pre-norm。
- 长序列训练速度和 long-output RL 架构效率之间的连接语言。
Risks
- Copyright/over-copying: 本笔记采用转述、必要公式和少量结果摘录,未复制长段原文。
- Unsourced or unverifiable claims: 作者机构、版本、代码和实验事实来自 arXiv HTML / TeX source;MiniMax 作者关系只记录同名线索,不做身份合并。
- Tone/brand mismatch: 按本目录论文笔记风格,强调机制、证据强度和边界条件。
- Safety/compliance issues: 本文为模型架构与系统效率论文,无直接安全滥用流程。
- Overlap with existing assets: 与 MiniMax-M1 笔记的 Lightning Attention 小节有重叠,本文件作为上游原始论文笔记,MiniMax-M1 文件保留系统应用视角。
Skipped
| Material | Reason |
|---|---|
| 所有 benchmark 表格逐项复刻 | 数量较多,本笔记保留代表性数值和结论 |
| Appendix 中完整 backward proof | 公式较长,本笔记保留 forward / complexity / decay recurrence 的核心 |
2401.04658 完整分析 |
它是同作者 technical report,可后续独立沉淀 |
Recommendation
Decision: merge as a new paper note.
Why: 该论文是 MiniMax-M1 Lightning Attention 系统线的关键上游论文,也为本地 long-context architecture、long-output RL cost 和 serving efficiency 图谱补齐模型架构侧节点。