工作日早上 7 点读 AI,周日早上 8 点读周报订阅邮件

METAL LAB

Multi-Head Attention Residuals

arXiv:2607.272302026-07-21

让Transformer回看过去层信息时,不用一个共享问题,而是让每组特征各问各的,效果更好

标准Transformer只是把每一层的输出累加传给下一层,所以只能直接读到最近一层的状态;已有的“注意力残差”方法让每个子层用一个学习到的查询,回看之前所有层的输出。本文指出这个共享查询迫使所有特征子空间用同一种方式读取历史信息,于是提出把这个查询拆成H个独立的子空间“头”(MHAR),几乎不增加参数和计算量。在从头训练的100M、350M、1B规模模型上,MHAR全面优于标准Transformer,并在8B模型的继续训练中提升了GSM8K和GPQA得分。

METAL LAB 解读图

不同方法如何读取过去层信息

证据状态已报告实测结果

  1. 标准残差每一层只是把输出加到累加和上,只能直接读到最近一层的状态
  2. 注意力残差(H=1)用一个共享的学习查询扫描所有过去层的输出,强迫所有特征按相同比例读取历史
  3. MHAR(多头)将特征拆成H组,每组用独立的查询分别读取过去各层,几乎不增加参数成本
  4. 头数等于KV头数一种无需调参即可接近最优效果的默认设置
  5. 8B继续训练输出门初始为零,保持预训练模型不变,训练过程中逐渐打开路由通道
这是 METAL LAB 制作的解读图,并非论文作者提供的原图。

他们做了什么

  1. 现有的“注意力残差”方法让所有特征维度共享同一个学习查询来回看过去层的输出,本文将这个查询拆分成H个独立的头,每个头对应一部分特征子空间,称为多头注意力残差(MHAR)。
  2. 这一改动本质上是免费的重排:几乎不增加参数或计算量,当头数H=1时完全等价于原来的单头方法。
  3. 在FineWeb-Edu数据上从头训练100M、350M、1B规模的模型,将MHAR与标准Transformer、超连接(hyper-connections)、原始单头注意力残差进行了对比。
  4. 将路由头数设为与KV(键值)头数相同,是一种无需调参、接近最优的默认设置,直接分析训练好的查询也证实不同特征子空间确实偏好不同的层。
  5. 针对8B模型,作者设计了一种可无损嫁接到已训练模型上的转换方法,并实现了专用Triton内核以提升该机制的训练速度。
Figure 1: The depth read is an attention, so it should be multi-head. Left: attention residuals (19) feed every sublayer a learned mixture of the depth history (embedding and every earlier attention/MLP output). Zoom left: one routing site is exactly single-head attention over that history—one query, one depth map shared by all d channels (Eq. 2). Zoom right: MHAR gives each of H subspaces its own map (Eq. 3), parameter-free; H=1 recovers attention residuals exactly.
Figure 1: The depth read is an attention, so it should be multi-head. Left: attention residuals (19) feed every sublayer a learned mixture of the depth history (embedding and every earlier attention/MLP output). Zoom left: one routing site is exactly single-head attention over that history—one query, one depth map shared by all d channels (Eq. 2). Zoom right: MHAR gives each of H subspaces its own map (Eq. 3), parameter-free; H=1 recovers attention residuals exactly.
Table 1: From-scratch validation loss (↓, tail-mean of the last eleven evaluations, steps 15–20k; §3) at the tuned peak learning rate 1×10−3; Δ is relative to the same-rate baseline; CEGFLOPs is the compute-equivalent gain of MHAR over the baseline (see text). MHAR is best at every scale; single-head routing regresses below baseline at 350M and 1B.
Scaled/L/KVBaselineHyper-conn.Single-headMHARCEGFLOPs
100M512/12/43.3363.322 (−0.014)3.297 (−0.039)3.287 (−0.049)1.27×
350M1024/24/83.2903.260 (−0.030)3.345 (+0.055)3.210 (−0.080)1.49×
1B1280/36/83.1753.172 (−0.002)3.315 (+0.140)3.111 (−0.063)1.38×
Figure 2: What each method lets the current sublayer read across depth. Each column: forward stack (top) and the depth-read weight matrix (bottom; entry [i,c] is how strongly channel c reads depth row i; illustrative). Standard pre-norm: a one-hot read of the previous state. Attention residuals: one shared query reads all earlier sources—every column identical (rank-1). MHAR (ours): each subspace reads depth independently (block-wise columns).
Figure 2: What each method lets the current sublayer read across depth. Each column: forward stack (top) and the depth-read weight matrix (bottom; entry [i,c] is how strongly channel c reads depth row i; illustrative). Standard pre-norm: a one-hot read of the previous state. Attention residuals: one shared query reads all earlier sources—every column identical (rank-1). MHAR (ours): each subspace reads depth independently (block-wise columns).
Table 2: Zero-shot downstream evaluation at 100M, 350M, and 1B (single run). Each model is evaluated at its training context (100M: seq. 2048; 350M/1B: seq. 1024), so perplexity is comparable only within a scale. MHAR transfers its gain to held-out perplexity and LAMBADA at every scale, and HellaSwag at 350M (tied at 1B, marginally lower at 100M). 100M is the earlier 6×10−4 batch; 350M and 1B are the 5×10−4 batch.
ScaleMethodWikiText-2 PPL ↓LAMBADA ↑HellaSwag ↑
100MBaseline48.610.4%33.0%
100MMHAR43.813.4%32.5%
350MBaseline53.99.6%33.5%
350MMHAR46.013.4%35.5%
1BBaseline49.612.4%34.0%
1BMHAR40.115.0%34.0%
Figure 4: The H heads carry genuinely different depth-links. Each head’s token-averaged deviation w¯h−w¯ from the head-consensus routing at the first ten routing sites of the trained 1B MHAR model (dark cells: source not yet available). Deviations reach ±0.28 vs. 0.067 under a matched-norm random query, replicate on disjoint evaluation text (r=0.77), and are near-uncorrelated across heads.
Figure 4: The H heads carry genuinely different depth-links. Each head’s token-averaged deviation w¯h−w¯ from the head-consensus routing at the first ten routing sites of the trained 1B MHAR model (dark cells: source not yet available). Deviations reach ±0.28 vs. 0.067 under a matched-norm random query, replicate on disjoint evaluation text (r=0.77), and are near-uncorrelated across heads.
Table 3: Downstream accuracy (↑) after 8B mid-training on anneal_pt_v3 (final checkpoints, EMA weights). The control and MHAR columns are schedule-matched: identical LR schedule, batch, data order, and ≈10 B-token budget.
TaskBasePlain CPT+MHAR
GeneralMMLU0.5540.6430.645
GPQA0.2660.3150.346
Math & CodingGSM8K0.1900.4700.502
MATH0.0530.1910.191
HumanEval0.1220.4090.415
MBPP0.1480.3860.392
Figure 5: The conversion is exactly identity and introduces no optimization shock. Train loss of the schedule-matched 8B mid-training pair (identical seed and data order). Left: the two curves coincide. Right: the per-step |Δ​loss| starts at ∼10−5, rises only as the zero-initialized gate opens, and saturates 36× below step-to-step batch noise (dotted).
Figure 5: The conversion is exactly identity and introduces no optimization shock. Train loss of the schedule-matched 8B mid-training pair (identical seed and data order). Left: the two curves coincide. Right: the per-step |Δ​loss| starts at ∼10−5, rises only as the zero-initialized gate opens, and saturates 36× below step-to-step batch noise (dotted).
Table 4: Training speed and memory. Baseline Transformer vs. MHAR (torch.compiled reference kernels) vs. MHAR with our fused routing kernels, at the three model settings of Table 1. Throughput is relative to the same-scale baseline; MHAR is parameter-matched (+0.02%); single-head attention residuals are cost-identical to MHAR by construction. Protocol, absolute medians, and the full breakdown in Appendix F (Table 8).
Throughput (vs. base)Peak mem. (GB)
Method100M350M1B100M350M1B
Baseline Transformer1.00×1.00×1.00×41.519.419.0
MHAR0.54×0.32×0.23×52.440.147.5
MHAR + fused kernels (ours)0.88×0.71×0.55×42.020.020.1
Figure 6: The bypass wants more heads. Under-trained regime (100M, 1×10−3, 5K steps): routing-head (H) × KV grid of validation loss (lower/yellow better; gold stars: per-KV optimum; red dashed line: the H= KV diagonal). The boxed KV= 1 row is the one supra-noise signal (see text).
Figure 6: The bypass wants more heads. Under-trained regime (100M, 1×10−3, 5K steps): routing-head (H) × KV grid of validation loss (lower/yellow better; gold stars: per-KV optimum; red dashed line: the H= KV diagonal). The boxed KV= 1 row is the one supra-noise signal (see text).
Table 5: Routing-operation speedup, isolating the kernels from Table 4’s end-to-end numbers: wall-clock of all routing calls of one microbatch, forward+backward, bf16, single H100; the 8B column is the mid-training delta variant. End-to-end gains are smaller because routing is only part of a training step (batch settings and the eager-reference comparison in Appendix F).
Routing per microbatch (ms)100M350M1B8B (delta)
torch.compile30.253.1153.2651
Fused Triton (ours)5.711.442.4323
Speedup5.3×4.7×3.6×2.0×
Figure 8: Validation loss of MHAR at 100M as a function of routing heads H (x) and KV heads (y). Lower is better. Gold stars mark the best H per KV; the red dashed line is the head-matched diagonal H= KV. At 100M the KV= 4 and KV= 8 optima sit on the H= KV diagonal. The 100M architecture of Table 1, trained at 5×10−4.
Figure 8: Validation loss of MHAR at 100M as a function of routing heads H (x) and KV heads (y). Lower is better. Gold stars mark the best H per KV; the red dashed line is the head-matched diagonal H= KV. At 100M the KV= 4 and KV= 8 optima sit on the H= KV diagonal. The 100M architecture of Table 1, trained at 5×10−4.
Table 6: Direct probe of the trained single-head routing queries (no new training; S=4 slices). Width-disagreement is the mean KL(as∥α) between each query slice’s preferred depth distribution and the shared α; a matched-norm random query gives the null, and the learned excess (trained−null) is the genuine learned signal. The middle column is a width-isolating control (d​512→d​768 at fixed L​12/kv4/N): the learned disagreement rises while the null and collinearity stay flat, so width itself drives it. Across scale the learned excess grows monotonically 2.6× (→0.606) and is the primary, empirically-supported factor; source collinearity (mean pairwise cosine of the N=2​L+1 sources)—a secondary, hypothesized factor we do not cleanly isolate—is non-monotonic (1.9× from 100M to 350M, then lower at 1B, partly a dimension effect), so the disagreement factor is the clean cross-scale signal. Caveat: slices are sub-parts of one trained query (a proxy for independent heads) rather than independently trained routers.
100M124M (width ctrl)350M1B
Probe quantity (S=4)d512/L12/kv4d768/L12/kv4d1024/L24/kv8d1280/L36/kv8
Width-disagreement KL (trained)0.2730.3110.5700.701
random-query null0.0380.0300.0580.095
learned excess (trained−null)0.2350.2810.5120.606
Source collinearity (mean cos)0.0600.0630.1140.087
N sources (=2​L+1)25254973
Figure 11: Training loss at 100M (EMA-smoothed): MHAR stays below the standard baseline throughout training (inset: tail zoom, steps 12k–20k). The two runs are identical except for the routing mechanism (same node, software, data order, and global batch).
Figure 11: Training loss at 100M (EMA-smoothed): MHAR stays below the standard baseline throughout training (inset: tail zoom, steps 12k–20k). The two runs are identical except for the routing mechanism (same node, software, data order, and global batch).
Table 7: Head-alignment ablation (validation loss ↓, final-step eval): routing over arbitrary subspaces (MHAR) vs. aligned to KV head groups (MHAR-HW), with matched head count. Aligning to attention heads shows no measurable benefit over arbitrary subspace routing; all differences fall within the ±0.07 eval-noise floor (single-seed, final-step). Separate matched-run batch (§3); read the within-table Δ.
ScaleMHARMHAR-HWΔ(hw−mh)
100M3.3303.333+0.003
350M3.2253.223−0.002
1B3.2133.229+0.016
Table 8: Training compute and memory at the three model settings of Table 1 (single H100; per-GPU microbatch as in training: batch 8 / sequence 2048 at 100M, 4/1024 at 350M, 2/1024 at 1B; identical node and software for all cells; 120-step runs on a verified-healthy GPU). Each cell is the median of three steady-state windows (within ±1%); peak memory reproduces to the decimal across sessions, and the 350M compiled/fused cells reproduce in a second independent session. Throughput is relative to the same-scale baseline (absolute baseline medians: 113.9k / 45.0k / 19.7k tokens/s). Parameters are matched to the baseline up to the O⁡(d) routing queries (+0.02%). †Single-head attention residuals are cost-identical to MHAR by construction in every row—the head split is a parameter-, FLOP-, and memory-free reshape (§2)—so they are not listed separately.
100M350M1B
Method†thr.mem. (GB)thr.mem. (GB)thr.mem. (GB)
Baseline (additive residual)1.00×41.51.00×19.41.00×19.0
MHAR, reference kernels0.31×68.80.16×70.9OOM (>80 GB)
MHAR, torch.compile0.54×52.40.32×40.10.23×47.5
MHAR, fused Triton (ours)0.88×42.00.71×20.00.55×20.1
Table 9: Width-control (tail-mean validation loss, steps 15–20k). Holding depth (L=12) and KV (4) fixed and only widening d→d​768: single-head routing’s edge over the baseline erodes (−→−0.033) while MHAR holds (−→−0.053), so MHAR’s advantage over single-head more than doubles (−→−0.020). This is the loss-level counterpart of the width-isolated disagreement in Table 6 (which rises →0.281 over the same widening). Single-seed; the single-head deficit shift (0.008) is near the per-seed noise floor, so the robust signal is the widening MHAR−single gap.
WidthBaselineSingle-headMHARMHAR−single
100M (d​512)3.4623.421 (−0.041)3.412 (−0.050)−0.009
124M (d​768)3.2203.187 (−0.033)3.167 (−0.053)−0.020
Table 10: Best-vs-best learning rate: each method at its own optimum over peak LR ∈{1,5}×10−4, 10−3 (validation loss ↓, tail-mean of the last eleven evaluations; Δ vs. the same-scale best baseline; superscript is the selected LR). MHAR wins at every scale even when every method is given its own optimal learning rate; single-head, even at its kinder 5×10−4, still regresses at 350M and 1B.
ScaleBaselineHyper-conn.Single-headMHAR
100M3.336 10−33.322 10−3 (−0.014)3.297 10−3 (−0.039)3.287 10−3 (−0.049)
350M3.290 10−33.260 10−3 (−0.030)3.328 5​e–​4 (+0.038)3.210 10−3 (−0.080)
1B3.175 10−33.172 10−3 (−0.002)3.279 5​e–​4 (+0.105)3.111 10−3 (−0.063)
Table 11: Seed robustness: paired per-seed delta vs. baseline (validation loss ↓, tail-mean of the last eleven evaluations; three seeds per method per scale, shared 5×10−4 recipe; ± is the per-seed standard deviation). Seed s fixes the data order across methods, so the comparison is paired. MHAR improves at every scale with |Δ|/SE≥10; single-head crosses from help to harm and is the highest-variance method.
ScaleHyper-conn.Single-headMHAR
100M−0.031±0.003−0.043±0.003−0.045±0.007
350M−0.028±0.009−0.005±0.015−0.090±0.004
1B−0.016±0.015+0.093±0.049−0.071±0.002
Table 12: Composition of the anneal_pt_v3 mid-training corpus: share of text bytes per source, measured over a uniform random sample of 48 of the 2,048 shards (187 GB of text; group rows sum their constituents, so rounded columns may differ in the last digit). The corpus is English-only; FinePDFs is restricted to its top three of twenty quality bins (verified from the per-document final_bucket metadata).
Source (per-document source tag)% bytes
Synthetic web rephrasings (Nemotron-CC-v2/v2.1)24.3
nemotron-cc-v2-high-quality-synthetic14.9
nemotron-cc-v2.1-high-quality-synthetic6.0
nemotron-cc-v2.1-high-quality-translated-to-english-synthetic3.5
Raw high-quality web (Nemotron-CC-v2/v2.1)16.6
nemotron-cc-v2-high-quality12.5
nemotron-cc-v2.1-high-quality-translated-to-english2.4
nemotron-cc-v2.1-high-quality1.6
Web PDFs15.9
finepdfs (top 3/20 quality bins)15.9
Synthetic diverse QA (Nemotron-CC-v2/v2.1)14.1
nemotron-cc-v2-diverse-qa8.3
nemotron-cc-v2-translated-diverse-qa5.3
nemotron-cc-v2.1-high-quality-dqa0.4
Code11.9
dolma-stack-edu6.7
nemotron-pretraining-sft-code2.8
nemotron-cc-code-v12.0
nemotron-pretraining-v1.1-code-concepts0.4
nemotron-pretraining-scientific-coding0.1
SFT / STEM / reasoning9.7
nemotron-pretraining-sft-general4.6
nemotron-pretraining-stem-sft4.1
nemotron-pretraining-rqa0.7
nemotron-pretraining-infinibyte-reasoning0.2
nemotron-pretraining-v1.1-multiple-choice0.1
nemotron-pretraining-v1.1-formal-logic<0.1
nemotron-pretraining-v1.1-unconditional-algorithmic<0.1
Math / arXiv6.9
nemotron-math-32.5

研究结果

  • 在100M/350M/1B规模下,相同学习率条件下MHAR相对标准Transformer的验证损失分别降低了0.049、0.080、0.063。
  • 原始单头方法在100M时有帮助(-0.039),但在350M时几乎无效(+0.055),在1B时明显变差(+0.140);即便各方法都用自己最优的学习率,单头方法在350M和1B仍然不如基线。
  • MHAR在所有规模上都优于超连接方法,且差距随规模增大而扩大(分别为0.035、0.050、0.061)。
  • 在WikiText-2困惑度、LAMBADA、HellaSwag等零样本下游评测中,MHAR的优势依然保持。
  • 在8B模型的继续训练实验中,与相同训练计划的对照组相比,MHAR使GSM8K提升3.2分、GPQA提升3.1分(p=0.004和p=0.038),定制Triton内核将训练吞吐量提升到基线速度的0.55至0.88倍。

可应用场景

  • 可作为几乎零成本的架构改进,用于提升Transformer预训练中跨层信息复用的能力。
  • 可嫁接到已训练好的大模型上进行继续训练,以在不破坏原模型的前提下提升推理相关基准的表现。
  • 将路由头数设为与KV头数相同,为设计更宽模型时如何设置该超参数提供了一个简单的经验法则。

局限与待验证事项

  • 实验仅验证到从头训练1B参数规模和8B参数继续训练规模,更大规模模型上的效果尚未验证。
  • 下游评测仅覆盖WikiText-2、LAMBADA、HellaSwag、GSM8K、GPQA等有限基准,更广泛的基准测试留待未来工作。
  • 在8B实验中,MMLU、MATH及代码类基准未观察到统计显著的差异。
  • 尚未与标准基线进行严格的等计算量(iso-compute)直接对比,该研究留作未来工作。
  • 为何最优头数往往接近KV头数、以及每个头具体学到了关注什么,目前还没有完全解释清楚。

为什么重要

该研究表明,随着Transformer变宽,强迫所有特征用同一个共享查询读取历史信息的代价会越来越大,而本文提供了一种几乎零成本的架构改进方案,对设计和扩展大型Transformer模型有实际参考价值。

本文术语

  • 残差流(residual stream) · Transformer中通过逐层相加的方式,将信息从一层传递到下一层的基本通道
  • 注意力残差(attention residuals) · 一种已有技术,子层不是简单相加,而是用学习到的注意力权重读取之前所有层输出的加权组合
  • 头(head) · 注意力机制中,把特征维度分成若干组,每组独立进行计算的单元
  • KV头 · 分组查询注意力(GQA)中用于键和值计算的头数,通常少于查询头数以节省计算量
  • 验证损失(validation loss) · 衡量模型在未参与训练的数据上预测效果的指标,数值越低越好

论文原文摘要(英文)

Transformers propagate information across depth through a single additive residual stream: every sublayer reads only the most recent state. Attention residuals relax this by letting each sublayer attend, through a learned softmax. However, that read uses a single query shared across the entire width, so every feature subspace must read the depth history through one distribution. The cost of this forced compromise grows with how much the subspaces disagree about which layers to read, and disagree

作者 · Cheng Luo

在 arXiv 阅读

最新论文

全部论文 →

METAL LAB 最新报道

图片来源: Cheng Luo et al., arXiv:2607.27230, CC BY 4.0