In-context Learning and Induction Heads
Transformer Circuits 原文(HTML) · arXiv abs · arXiv PDF
① Paper Information
- Title:In-context Learning and Induction Heads
- Authors:Catherine Olsson, Nelson Elhage, Neel Nanda, Nicholas Joseph, Nova DasSarma, Tom Henighan, Ben Mann, Amanda Askell, Yuntao Bai, Anna Chen, Tom Conerly, Dawn Drain, Deep Ganguli, Zac Hatfield-Dodds, Danny Hernandez, Scott Johnston, Andy Jones, Jackson Kernion, Liane Lovitt, Kamal Ndousse, Dario Amodei, Tom Brown, Jack Clark, Jared Kaplan, Sam McCandlish, Chris Olah(共 26 人,Anthropic;页面把前三位标为 core research contributors)
- Year / Venue:2022。发表于 Transformer Circuits Thread(页面标注 “Published Mar 8, 2022”),同文以 arXiv:2209.11895 预印本发布(v1 提交于 2022-09-24,DOI 10.48550/arXiv.2209.11895)。我没有找到它的会议或期刊版本。
- Version used:transformer-circuits.pub 上的 HTML 版本。页面 change log 有两条修订:2022-09-20 修正了 prefix matching 定义中的措辞,并补充了 copying 评估器使用 ReLU 的细节;2023-03-06 为错字修正。arXiv 只有 v1。原文各节没有编号,下文用节标题定位,例如 “Argument 3”、“Model Analysis Table”、附录 “Data Collection”。
- Keywords:Induction Heads, In-Context Learning, Phase Change, K-Composition, Ablation, Training Dynamics
推荐理由:这篇论文试图把一个宏观能力、一个微观电路和一段训练动力学连成一条因果链。宏观能力是 in-context learning(ICL),被操作化为”第 500 个 token 的 loss 比第 50 个低多少”;微观电路是 induction head,即 式的”找到上一次出现的位置,复制它后面的 token”;训练动力学是一次发生在训练早期、窄窗口内的 phase change,ICL score、induction head 的形成和 loss 曲线上的一个”bump”在这个窗口里同时出现。作者在 34 个模型上做了超过 50,000 次 attention head ablation,并把证据拆成六条论证,逐条标注它在每类模型上的强度与类型(causal、interventional、correlational、mechanistic、analogy)。这种显式的证据分级本身就值得学习。它也是一个检验”机制解释能推广多远”的好案例:后续工作(Yin & Steinhardt,ICML 2025)在更大的模型和不同的 ICL 度量下,对它的核心结论作出了重要修正。
核心科学问题:
语言模型”越往后读、预测越准”的能力在训练中几乎是一次性出现的。这种宏观能力能否追溯到一个可以精确描述的微观电路?如果能,我们凭什么证据(相关、干预,还是机制)说”这个电路解释了这个能力”?这种说法又能推广到多大的模型、多宽的 ICL 定义?
② Background & Motivation
1 两种意义上的 in-context learning
GPT-3 让 few-shot prompting 广为人知:在 prompt 中放入若干示例,模型在某个任务上的准确率随示例增多而提高。还有一个更基础、更宽泛的现象:每个 token 的 loss 随它在上下文中的位置下降,即模型在利用前文。原文 Related Work 在讨论 ICL 时引用了 Brown et al.(GPT-3)、Kaplan et al. 等工作。
本文采用后一种定义(Key Concepts > In-context Learning),并把它压缩成一个数(③-5):第 500 个 token 的 loss 与第 50 个 token 的 loss 之差。选择它的理由很实际:它与任务无关,可以在任何语言模型、任何训练 checkpoint 上计算。代价是它不区分”从示例中学会一个新任务”与”重复前文出现过的一个名字”。这一张力会在⑤中回来。
2 前驱:A Mathematical Framework for Transformer Circuits
Elhage et al.(Transformer Circuits Thread,2021-12-22) 研究了只含 attention 的小 Transformer(无 MLP,忽略 bias,把 LayerNorm 并入相邻权重),并把 residual stream 看作一个线性的”通信通道”。他们的主要发现:
- 0 层模型只能学 bigram 统计;
- 1 层模型等于 bigram 加若干 “skip-trigram”,形如 。一个 1 层的 copying head 能实现 :前文出现过 ,就提高 的概率;
- 2 层模型中出现了 induction head:layer 0 的 previous-token head 与 layer 1 的 head 通过 K-composition 组合,实现 。他们写道,这一两层算法”知道这个 token 上次是如何被使用的”,而不是泛泛地寻找可以重复的 token。
这篇前驱留下三个问题:(i) induction head 是否存在于带 MLP 的大模型中?(ii) 它与 ICL 在数量上有什么关系?(iii) 它在训练中何时、如何形成?本文正面回答这三个问题。
3 一个代表性的困难
设模型读到一段完全随机的 token:
第一遍中任何位置的下一个 token 都无法预测(训练数据中没有相关统计)。第二遍中,在读到 时,正确答案 只能从前文中**按”它前面是谁”**取回。1 层 copying head 只能提高”前文出现过的所有 token”的概率,第二遍中这样的 token 有 25 个,它们都在前文出现过,因此它无法挑出正确的那一个。这个例子也正是本文测量 induction head 的方法(③-5)。
4 本文的新视角
本文把三个层次接在一起:
难点在于:完整的因果分析(ablation、逆向工程)只在小模型中可行;作者没有对全尺寸模型做 ablation(Argument 3)。他们的做法是在三类模型上收集六条性质不同的证据(小 attention-only 模型、带 MLP 的小模型、最大 13B 参数的全尺寸模型),合起来构成原文所说的 “circumstantial case”(这有点像流行病学中综合多条间接证据的做法,这个类比是我的说法)。作者自己的总体评价是:“a relatively strong, though not conclusive, case”(Arguments Summary)。
③ Core Ideas & Technical Details
本节围绕四个技术核心组织:(1) circuit 记号,以及”1 层为什么做不到 induction”;(2) 两层 induction circuit 的数学结构;(3) softmax 对 induction head 的定量要求;(4) 度量与干预的设计。
1 记号:residual stream、QK circuit 与 OV circuit
以下记号来自 Elhage et al.(2021),本文直接沿用。设 为 residual stream( 个位置,每个位置 维)。一个 attention head 的输出写成张量积形式(Framework,“Attention Heads as Information Movement”):
在位置之间混合信息, 独立作用于每个位置的向量。Framework 在这一节写的是普通的 softmax;(带自回归 mask 的 softmax)是它在 Notation 附录中定义、在后文使用的记号。两个低秩矩阵决定了 head 的全部行为:
- QK circuit :第 个元素是 query token 对 key token 的 attention 分数,决定”往哪里看”。
- OV circuit :第 个元素表示”如果关注到 token ,会把 token 的 logit 提高多少”,决定”看到之后写什么”。
两者都是 vocab × vocab 的矩阵(这里暂时忽略 positional embedding),因此可以直接按 token 解读。1 层 attention-only 模型的 logits 可以展开为
第一项是 direct path,只能表示 bigram;每个 head 贡献一项 skip-trigram。Framework 正文逐项给出了这两部分,求和式是我按其文字写出的。
2 为什么 1 层 attention-only 模型做不到 induction
本小节是 Explanatory Derivation(原文与 Framework 用文字陈述了结论,下面的形式化是我补充的)。
在 1 层模型中,位置 对位置 的分数 只依赖于 ,其中 是 token, 是位置。关键在于 不依赖于 。于是对任意两个 key 位置 (二者都不等于 或 ),
与 、 无关。Induction 需要的恰恰是”当且仅当 时偏向关注 “,1 层 head 做不到这种选择。
细节:比值论证覆盖了什么,没有覆盖什么
-
比值不变不等于每个 不变:改变 会改变 ,从而改变 softmax 的分母。但分母对所有 key 是同一个数,所以它不能有选择地放大 而不放大 。
-
例外是位置信息。若重复周期 是固定且已知的,1 层 head 可以按相对位置 去关注,并复制该位置的 token。所以准确的说法是:对任意(未知)重复周期,1 层 attention-only 模型不能实现 induction。自然文本中的重复没有固定周期,这与本文观察到 1 层模型不形成 ICL 相变是一致的。
-
1 层模型可以关注前文中与当前 token 相同的位置 ( 是 能看到的),但那里存放的是 的身份,不是 的身份。若要输出 ,信息必须先从位置 移动到位置 ,或者从 移动到 ;这需要另一层 attention。
-
上面没有处理 OV 输出中来自位置 、 的直接贡献。在随机 token 上,训练统计中不存在” 之后是 “的关联,这些贡献不能提供正确答案。完整的不可能性证明需要更仔细地处理这一项,这里只给出机制层面的论证。
这个论证也直接预言了 ③-6 中 smeared key 修改为什么有效:只要让 key 本身含有 的信息,1 层就够了。
3 两层 induction circuit
3.1 结构:previous-token head + induction head
flowchart LR
P["位置 j−1:token A"] -->|"layer 1 的 previous-token head:OV 把 A 的信息写入位置 j"| K["位置 j:token B,残差中含有『我前面是 A』"]
Q["位置 i:token A(query)"] -->|"layer 2 的 induction head:QK 让 query 中的 A 匹配 key 中的『前面是 A』"| K
K -->|"induction head 的 OV:复制 B"| O["位置 i 的 logits:B 被提高"]
3.2 K-composition 的代数
设 layer 1 有一个 previous-token head ,在位置 几乎只关注 :。经过 layer 1 后,位置 的残差为
Layer 2 的 head 的分数 展开后含有一项
它通过 key 一侧读入了上一层 head 写入的信息,所以叫 K-composition。若 (一个”token 相等检测器”),则这一项约为 :query 是 时,恰好在 之后的位置 得到高分。这就是 Framework 所说的”key is computed from tokens shifted one token back”。
同理,若上一层 head 的输出被 query 一侧读入,称为 Q-composition;被 value 一侧读入,称为 V-composition。Framework(“Analyzing a Two-Layer Model”)用 Frobenius 范数定义三种 composition score:
Framework 说明他们默认减去同形状随机矩阵的经验期望值。对他们分析的 2 层模型,Framework 写道:“only significant K-composition, and only with one layer 0 head”。
3.3 Copying:OV circuit 的正特征值
找到 之后,还要把它写成”提高 的 logit”。若 OV circuit 近似一个正定的”恒等映射”,关注到 就会提高 自己的 logit。Framework(“Detecting Copying Behavior”)的判据是:copying 要求 OV circuit 的特征值为正,并用一个基于特征值的汇总统计量来检测。作者明确说正特征值是很强的 copying 证据,但不是 “dispositive proof”。Framework 在这一节报告:“It appears that 10 out of 12 heads are significantly copying!”
3.4 一个手工构造的 toy circuit(Explanatory)
为了让上面的代数可以执行,我构造了一个 2 层、每层 1 个 head 的 attention-only 模型,并用 numpy 验证。它不是原论文的模型。
第 1–2 步:previous-token head 与 QK 匹配
取 residual stream 为三个分块:token 块 、“前一个 token”块 、位置块 (one-hot)。
第 1 步,previous-token head :QK 只读位置块,令 ;OV 把 token 块复制到”前一个 token”块,即 (只写出 token、prev 两块)。 较大时,位置 的 prev 块约为 。
第 2 步,induction head :,即 query 读 token 块、key 读 prev 块,所以 。
代入 ③-3.2 的公式:,而 。这个电路完全落在 K-composition 上。未减随机基线的原始比值为:Q-composition 恰好为 0,K-composition 为 (我用代码核对过)。Framework 默认还要减去同形状随机矩阵的期望值;在这个高维、满秩的 toy 中,减去基线后 K-composition 的数值会很小,所以原始比值的绝对大小没有意义,有意义的是 K 与 Q 的对比。
第 3 步:复制,以及数值结果
第 3 步,OV: 把被关注位置的 token 块复制到输出,unembedding 读出后乘以 。所以位置 的 logits 为 :若 曾在 处出现,输出约为 。
数值设置:;25 个 token 从词表中独立、有放回地均匀抽取,重复两遍;300 条序列;,,;第一个位置的 previous-token head 只能关注自己。loss 只在第二遍内部的 24 次预测上平均(不含”从第一遍末尾预测第二遍第一个 token”的那一次)。
结果:第二遍的平均 loss 为 0.170 nats(换随机种子在 0.155–0.170 之间);作为对照,均匀猜测为 。这 0.17 nats 主要来自有放回抽样造成的重复 token(同一个 后面跟着不同的 ):若 25 个 token 互不相同,loss 降到约 0.034。若完全去掉 previous-token head(让 的 key 看不到前一个 token),第二遍的平均 loss 升到 5.813 nats(互不相同时约 5.842)。
5.813 仍低于 :去掉 previous-token head 后, 均匀关注前文,等于把”前文出现过的 token”都提高一点,第二遍的 25 个 token 恰好都出现过。这是一个 unigram cache 效应,它也会让后面位置的 loss 下降。这一点在⑤-2 中很重要。
4 Softmax 对 induction head 的要求:logit gap 必须随 增长
本小节是 Explanatory Derivation,原文没有讨论。Induction head 的任务是在 个 key 中只选中一个位置()。设匹配位置的分数为 ,其余 个 key 的分数为 ,定义 logit gap
则匹配位置得到的 attention 精确地等于
也就是说,要在长度 的上下文中保持 90% 的注意力在 上, 至少要达到 。 时约为 8.43,(本文模型的训练上下文长度)时约为 11.21。若 固定为 8: 时 , 时 。
先预测:若所有 attention logit 都被限制在 内, 最大能有多大?这与哪篇旧笔记的结论相同?
此时 ,所以 。这正是 Vol. 001 · Theoretical Limitations of Self-Attention in Neural Sequence Models 中 Hahn 的 soft-attention 论证的第一步:有界 logits 下单个位置的权重为 。Induction head 是一个”必须把注意力集中在单个位置”的 head,所以它正好处在 Hahn 限制的打击范围内;它能工作,是因为 足够大,而不是因为限制不成立。
下面的演示把这个要求与最终的预测 loss 联系起来。设定(Explanatory,不是原论文实验):词表 (与本文模型的 词表相同);前文 个 key 中恰好一个是匹配位置 ,其余 个 key 分数相同,且各是互不相同、也不等于 的 token;induction head 的 OV 以强度 复制被关注的 token;没有其他贡献。于是 的 logit 为 ,其余 个上下文 token 各为 ,其余 个 token 为 0。
拖动 、 与 ,观察:(a) 固定 时, 每翻一倍, 就向 0 退化,loss 从接近 0 迅速升到接近 ;(b) 维持 所需的 随 线性增长,每翻一倍约增加 。
交互演示的代码
// 交互演示:induction head 的 logit gap Δ、上下文长度 n、复制强度 β 如何决定 B 的注意力与预测 loss
const box = dv.container.createEl("div");
box.style.cssText = "border: 1px solid rgba(128,128,128,0.35); border-radius: 10px; padding: 12px 16px; margin: 16px 0;";
const render = (el, md) => el.replaceChildren(dv.el("span", md));
const V = 65536;
const slider = (name, min, max, step, val) => {
const row = box.createEl("div");
render(row.createEl("span"), `$${name} =$ `);
const s = row.createEl("input", { type: "range" });
Object.assign(s, { min, max, step, value: val });
const shown = row.createEl("span", { text: " " + val });
s.addEventListener("input", () => { shown.textContent = " " + s.value; update(); });
return s;
};
const D = slider(String.raw`\Delta`, 0, 20, 0.1, 8);
const L = slider(String.raw`\log_2 n`, 1, 16, 1, 9);
const B = slider(String.raw`\beta`, 0, 30, 0.5, 15);
const out = box.createEl("div");
function calc(d, n, beta) {
const alpha = 1 / (1 + (n - 1) * Math.exp(-d));
const c = beta * (1 - alpha) / (n - 1);
const terms = [beta * alpha, Math.log(n - 1) + c];
if (V - n > 0) terms.push(Math.log(V - n));
const m = Math.max(...terms);
const lse = m + Math.log(terms.reduce((s, t) => s + Math.exp(t - m), 0));
const loss = lse - beta * alpha;
return { alpha, loss, p: Math.exp(-loss), dstar: Math.log(9 * (n - 1)) };
}
function update() {
const d = Number(D.value), n = 2 ** Number(L.value), beta = Number(B.value);
const r = calc(d, n, beta);
render(out, String.raw`$n=${n}$:$\alpha=1/(1+(n-1)e^{-\Delta})=${r.alpha.toFixed(4)}$;$p(B)=${r.p.toFixed(4)}$,loss $=${r.loss.toFixed(3)}$ nats($\ln V\approx 11.090$);维持 $\alpha\ge 0.9$ 需要 $\Delta^*=\ln(9(n-1))=${r.dstar.toFixed(3)}$`);
}
update();结论:在这个理想化设定中,、 时, 的 loss 约为 0.166 nats, 时升到约 7.09 nats;把 提高到 11.2 后, 的 loss 回到约 0.087 nats(以上数值我用 node 计算过)。由此得到一个可检验的推论:一个在训练中只见过短上下文的 induction head,如果其 不随 调整,在更长的上下文中会因稀释而失效。⑥-5 的开放问题会把这一点精确化。还要注意,本文的 ICL score 只在第 500 个 token 处测量,它本身看不到长上下文中的这种稀释。
5 度量:把”ICL”与”induction head”变成数字
5.1 ICL score
原文定义(Key Concepts > In-context Learning):“the loss of the 500th token in the context minus the average loss of the 50th token in the context, averaged over dataset examples”。记 ,则
作者说 500 与 50 “somewhat arbitrarily” 选定:500 接近 512 长度评估上下文的末尾;到第 50 个 token 时,文本的一些基本属性(如语言与文档类型)已经确立,而它仍然接近开头。
一个符号上的细节:按 Key Concepts 的字面定义,模型利用上下文时该量为负;而 Argument 1 复述定义时顺序相反,写作 “the 50th token loss minus the 500th token loss”。原文报告的数值(“less than 0.15 nats”、“roughly 0.4 nats”)都是正数,符合后一种写法。下文沿用正数的说法,“ICL 约 0.4 nats”表示 。
5.2 Induction head 的两个性质及其评估器
原文的形式化定义(Key Concepts > Induction Heads):induction head 是在重复的随机 token 序列上同时表现出以下两个性质的 head。
- Prefix matching:在 的第二个 处,head 关注前一次 之后的那个 token 。这一目标在原文中有明确的表述:Key Concepts 说第二个 head 找到前文中 出现的位置后 “attends to the next token (call it B)“,并说它 “attends to the token which induction would suggest comes next”。原文还指出,常见的 induction head 会对前面若干个 token 做 fuzzy match,而不只是匹配一个 token。(注意:原文几处文字定义,包括 Key Concepts 中的 “previous tokens that were followed by the current and/or recent tokens”,以及下文评估器的那句话,字面上读起来指向的是更早一个位置;2022-09-20 的 change log 也修改过这里的措辞。本笔记以上面两处明确的表述为准。)
- Copying:head 的输出提高被关注 token 的 logit。
原文强调这个定义是纯行为的:“any heads that empirically increase the likelihood of [B] given [A][B]…[A] when shown a repeated sequence of completely random tokens”。在实际模型中这两个性质都只是近似满足,测量结果是一个连续谱。
附录 Data Collection(“Head activation evaluators”)给出评估器,每个分数都在 10 个样本上平均:
- Prefix matching score:生成 25 个随机 token(排除最常见与最不常见的 token),重复 4 遍,在开头加一个 start-of-sequence token;计算 attention pattern。原文的原句是:“the average of all attention pattern entries attending from a given token back to the tokens that preceded the same token in earlier repeats”。这句话字面上指向前几遍中同一 token 之前的位置,与 Key Concepts 中”关注 induction 预测的下一个 token”的表述不一致。我按 Key Concepts 的含义把它形式化为:位置 ()的目标集合为 ,即前几遍中同一 token 之后的位置,分数为 这些条目的平均。这是我的形式化,不是原文的公式。
- Copying score:同样生成 25 个随机 token(原文承认这里漏加了 start-of-sequence token);取该 head 输出经 unembedding 后的 logit 效应,减去均值后过 ReLU;分数为”被关注 token 的 logit 提升量”占”所有 token 的提升量”的比例。原文说这个比例的范围是 0(只提高其他 token)到 0.5(只提高当前 token),再线性缩放到 。
5.3 Per-token loss 的 PCA
为了比较不同模型、不同 checkpoint 的”行为”,作者对每个 snapshot 在 10,000 个随机抽取的 512-token 样本上记录逐 token 的 loss,然后从每个样本中随机取一个 token,把 10,000 个 loss 拼成一个向量;同一类模型(小模型、全尺寸模型各为一类)的所有 snapshot 的向量拼接在一起做 PCA,重点看前两个主成分(Key Concepts > Per-Token Loss Analysis;附录 Data Collection)。训练轨迹在 PCA 空间中的”拐弯”,是 phase change 的第四个宏观信号(④-2)。Argument 1 还考察了 loss 对 的导数在训练中的变化。
6 干预:smeared keys 与 ablation
6.1 Smeared keys:只为 induction head 修路
如果 ③-2 的论证正确,那么 1 层模型的瓶颈是”key 看不到前一个 token”。作者因此对每个 head 引入一个可训练的实数 ,把 key 换成当前与前一个 token 的 key 的插值(Argument 2;Model Details):
上下文中的第一个 token 不做插值。原文页面上左侧的记号在我读到的文本提取中不清晰,我用 区分插值前后的 key。于是
第二项依赖 ,所以 1 层模型就能实现 prefix matching。原文的说法是:标准 Transformer 需要两层 attention,“only because the key vectors need to be a function of the attended token and the token before it”。
这个修改的预测很清楚:1 层模型将出现 ICL,多层模型的 phase change 可能提前。
6.2 Ablation:两种”去掉一个 head”
附录 Data Collection 的 “Per-token losses (attention head ablations)” 一节区分两种 ablation:
- Pattern-preserving ablation(Model Analysis Table 中展示的就是这一种):先正常运行一次,记录所有 attention pattern;第二次运行时把被 ablate 的 head 的输出向量置零,并强制所有 attention pattern 等于第一次记录的版本。
- Full ablation(附录展示):只把该 head 的输出向量置零,其余照常计算。
Argument 3 说明,ablation 在测试时进行,每个 checkpoint 上每次 ablation 用 10,000 个样本评估。对 attention-only 模型,作者给出的理由是:所有 ICL 最终都来自 attention head,而冻结 attention pattern 后各 head 的贡献”almost linear”,所以这种 ablation 是衡量贡献的原则性方法。
为什么冻结 pattern 后”几乎线性”?这是我的解释(Explanatory):attention pattern 固定后,attention-only 网络对 token embedding 是一个线性映射,logits 是所有路径贡献之和,只有最后的 LayerNorm 与 softmax 引入非线性。把 head 的输出置零,恰好删除所有经过 的输出的路径: 的 direct path,以及后续 head 通过 value 读入 输出的 V-composition 路径。但 通过 Q/K-composition 对后续 head attention pattern 的影响不会被删除,因为 pattern 被冻结了。这个不对称性是⑤-3 与⑦思考题的出发点。
对 phase change 的归因,作者计算每个 head 的”ablation 改变向量”(ablate 前后 per-token loss 的变化)与模型在 phase change 前后的”before-and-after 向量”之间的点积。对 induction heads,这个点积为负,即 ablate 它们会把模型往”相变之前”推回去。
④ Main Results
以下均为经验结论,没有定理。
1 模型与训练
- 小模型(Model Details):1–6 层的 attention-only 与带 MLP 两组;context window 8192,词表 ,,每层 12 个 head;训练 10,000 步(约 10B tokens),每 50 步存一个 snapshot,共 200 个。
- Smeared-key 模型:原文只展示了 1 层与 2 层规模的结果(“We present these only at one-layer and two-layer sizes”)。
- 全尺寸模型:context window 与词表同小模型;snapshot 间隔按 2 倍指数增长,每个模型约 14–15 个时间点。规模如下(Model Details 中的表格):
| 层数 | non-embedding 参数 | dmodel | 每层 head 数 | dhead |
|---|---|---|---|---|
| 4 | 13M | 512 | 8 | 64 |
| 6 | 42M | 768 | 12 | 64 |
| 10 | 200M | 1280 | 20 | 64 |
| 16 | 810M | 2048 | 32 | 64 |
| 24 | 2.7B | 3072 | 48 | 64 |
| 40 | 13B | 5120 | 40 | 128 |
表中 。原文的概述说,全部证据来自对 34 个 decoder-only Transformer 在训练过程中的分析,以及超过 50,000 次 attention head ablation。
2 Phase change(Argument 1)
先预测:1 层 attention-only 模型训练同样久,它的 ICL score 曲线会是什么样?
原文 Argument 1:1 层模型中 induction head 从不形成,ICL 也从未实质性发展;它没有 phase change。这与 ③-2 的论证一致。Argument 2 中的 smeared-key 1 层模型则出现了 ICL。
原文报告:
- 多于一层的模型在训练早期的一个窄窗口内同时发生四件事:ICL score 急剧提升;induction head 形成;loss 曲线出现一个小 “bump”,作者说这是整个训练中 loss 唯一不凸的地方;per-token loss 的 PCA 轨迹突然转向。
- 窗口:Argument 1 写作 “roughly 2.5 to 5 billion tokens”,并说对大模型这大约是训练进程的 1–2%。但 Model Details 对小模型写的是 “approximately 1 – 3 billion tokens”。两处数字不一致,原文没有说明原因。
- 量级:窗口之前 ICL “less than 0.15 nats”,之后 “roughly 0.4 nats”,并且此后在训练中保持不变,在不同规模的模型之间也大致相同(1 层模型除外)。
- “over 75% of final in-context learning forms in this window”。作者紧接着提醒,不能据此天真地认为 induction head 负责 75% 的 ICL。
- 原文指出,这个窗口看起来与学习率、warmup 或 weight decay 的预设日程变化都不对应。
3 六条论证的结果
| Argument | 做了什么、得到什么 | 原文的证据类型标签 |
|---|---|---|
| 1 Macroscopic co-occurrence | ICL score、induction head、loss bump、PCA 转向在同一窗口出现;1 层模型四者都没有 | Correlational(三类模型均为 Medium) |
| 2 Macroscopic co-perturbation | smeared keys:1 层模型出现 ICL,2 层模型的 ICL 形成得更早(原文写作 "two-layer and larger models",但只展示了 1、2 层规模的结果) | Interventional(小模型 Medium,大模型 Weak) |
| 3 Direct ablation | 小 attention-only 模型中"almost all"的 ICL 来自 induction heads,从 phase change 开始一直到训练结束都如此;ablate 其他多数 head 反而增加 ICL;带 MLP 的模型只是 suggestive;全尺寸模型没有 ablation | Causal |
| 4 Examples of generality | 13B 模型中,同一个 head 既满足 induction head 的定义,又做更抽象的事(见下表) | Plausibility |
| 5 Mechanistic plausibility | 小模型中机制清楚,可以理解为"把前文当作最近邻算法的数据点";带 MLP 的模型目前无法逆向工程 | Mechanistic |
| 6 Continuity | 多于一层的模型都经历 phase change,前后的 ICL 量相近,PCA 轨迹相似,都形成 induction head | Analogy |
原文 “Arguments Summary” 给出的综合评级(两行分别是”induction heads 贡献了一部分 ICL”与”贡献了大部分 ICL”):
| 主张 | Small Attention-Only | Small with MLPs | Large Models |
|---|---|---|---|
| Contributes Some | Strong, Causal | Strong, Causal | Medium, Correlational & Mechanistic |
| Contributes Majority | Strong, Causal | Medium, Causal | Medium, Correlational |
4 Argument 4:13B 模型中”更抽象的” induction heads
三个 head 都在 40 层、13B 参数的模型中(Argument 4 中的表格):
| Head | 层 | Copying score | Prefix matching score | 展示的行为 |
|---|---|---|---|---|
| Literal copying | 21 / 40 | 0.89 | 0.75 | 在《哈利·波特》第一段上预测重复出现的 "Dursley"、"Potters" |
| Translation | 7 / 40 | 0.20 | 0.85 | 英、法、德语之间的翻译;attention 大致呈 "off-diagonal",logit attribution 不完全尖锐 |
| Pattern matching | 8 / 40(正文写作 layer 26) | 0.69 | 0.94 | 合成任务(月份 / 颜色 × 动物 / 水果,四类标签 0–3):从冒号处约 65% 的 attention 落在正确位置 |
这些例子没有报告翻译准确率或 loss 改善量;作者把它们定位为表明普遍性的例证,并说这部分证据对一般 ICL “only suggestive”。
5 这些结果严格支持什么、不支持什么
支持:
- 在小 attention-only 模型中,按 ICL score 度量、并在 pattern-preserving ablation 的归因约定下,induction heads 是 ICL 的主要来源(Argument 3,因果证据)。
- 对这些模型族,“key 能否依赖前一个 token”是 ICL 出现时机的一个可操控因素(Argument 2,干预证据)。
- 在所研究的 34 个模型中,多于一层的模型都在训练早期经历一个四种信号同时出现的转变(Argument 1,相关证据)。
- 大模型中存在同时满足 induction head 定义、又参与翻译和模式匹配的 head(Argument 4,存在性例证)。
不支持:
- 不支持”大模型中大部分 ICL 来自 induction heads”的因果结论:全尺寸模型没有 ablation。原文对大模型的最强评级是 Medium, Correlational,其余为 Weak, Interventional(Argument 2)、Plausibility(Argument 4)、Mechanistic(Argument 5,只针对 “Contributes Some”)与 Analogy(Argument 6)。
- 不支持把结论推广到 few-shot 任务准确率意义上的 ICL:全文只用 token-loss 意义上的 ICL score 作度量。
- 不支持”phase change 由 induction head 的形成导致”的强因果解读:作者自己提出了共同潜变量的可能性(⑤-4)。
⑤ Critical Analysis
1 真正的原创性
与 Elhage et al.(2021)相比,本文新增的不是 induction head 本身,而是三件事:(i) 把 induction head 从 2 层 toy 模型推进到 13B 模型,并给出一个可以批量运行的行为定义与评估器;(ii) 把它与训练动力学联系起来:原文 Related Work 说,“as far as we know”,这是第一个对机器学习中的 phase change 给出机制性解释的案例;(iii) 方法论上,它把”一个电路解释了一个能力”拆成可以分别评级的证据,并公开每一条在每类模型上有多强。后一点经常被忽略,但它让读者能精确地知道哪些结论站得住。
2 ICL score 的构念效度
ICL score 把”任何让后面 token 更好预测的机制”都算作 ICL。至少有三类机制会让它变大:
- 按前一个 token 取回并复制(induction);
- 不看前一个 token、只提高前文出现过的 token(unigram cache 或 topic 效应)。③-3.4 的 toy 中,去掉 previous-token head 后第二遍的 loss 仍比均匀猜测低约 0.4 nats(5.813 对 6.215);
- 从示例中识别任务并执行(few-shot 意义上的 ICL)。
本文的 Argument 3 证明的是:在小 attention-only 模型中,induction heads 承担了这个宽泛度量的大部分。至于第 3 类、通常被称为”in-context learning”的能力,本文的证据只有 Argument 4 中的几个例证。
后续工作直接检验了这一点。Yin & Steinhardt(ICML 2025) 在 12 个 70M–7B 参数的模型中比较 induction heads 与 function vector(FV)heads。他们的摘要说 few-shot ICL 表现主要依赖 FV heads,“especially in larger models”;在 Pythia 6.9B 上(Figure 1a),ablate FV heads 显著降低 few-shot ICL 准确率,ablate induction heads 的影响与 ablate 随机 head 相差无几。在 token-loss difference 上,两类 head 的作用正好相反(Section 2.3);但 Section 4.2 补充了规模条件:低于 160M 参数时,两类 ablation 对 token-loss difference 都没有显著影响;超过 345M 时,ablate induction heads 的影响大于 ablate FV heads,而这一差距随规模增大而缩小。他们建议把 “ICL score” 改称 “token-loss difference”,并把与先前结论的差异归于三点:度量不同、两类 head 相互关联而此前的 ablation 没有控制这一点、此前的结论主要来自小模型。在他们最小的 70M 模型中,两类 head 对 few-shot ICL 的因果作用相近。
反方视角:Yin & Steinhardt 是否”推翻”了本文?
不完全是。按本文自己的度量(token-loss difference),他们在 345M 以上的模型中得到与本文一致的结果:induction heads 对它的影响更大(虽然差距随规模缩小)。真正被修正的是一个外推:从”induction heads 解释了 token-loss 意义上的 ICL”到”induction heads 是 few-shot ICL 的主要机制”。本文 Argument 4 与 Discussion 中的若干表述鼓励了这一外推,但它在本文中只有 plausibility 级别的证据。Yin & Steinhardt 还发现许多 FV heads 在训练早期先表现为 induction heads,之后才转为 FV 机制(反方向从未出现),并据此推测 induction 可能促进了更复杂的 FV 机制的学习。如果这一点成立,本文”induction heads 是 ICL 的起点”的图景在发展意义上反而得到了支持。
3 Pattern-preserving ablation 的归因约定
按 ③-6.2 的分析,冻结 attention pattern 后,previous-token head 通过 K-composition 对 induction head 的贡献不会因 ablate previous-token head 而消失,因为 induction head 的 pattern 被冻结了。所以在这种 ablation 下,整个”previous-token head → induction head”电路的效应被记在下游的 induction head 名下。
这不是错误,而是一种约定:它回答的是”ICL 的效应通过哪些 head 的输出进入 logits”,而不是”哪些 head 是这个电路的必要组成部分”。“almost all the in-context learning … appears to come from these induction heads” 应当在这个约定下理解。原文附录展示了 full ablation 作为另一种方法;⑦ 的思考题用 toy 模型把两种约定的差别算出来。
4 共同潜变量与 smeared key 的选择性
作者在 Argument 1 的 “Assessing the Evidence” 中自己列出了主要的混杂:大模型只有约 15 个时间点;同一时刻可能形成了其他贡献 ICL 的机制;phase change 也可能是”模型学会通过 residual stream 组合层”的时刻;共现可能主要来自一个共同的潜变量,而不是 induction head 直接导致整个 ICL 变化。
Argument 2 被用来回应这一点,但我认为它的选择性不够强(Independent Analysis)。Smeared key 让每一个 head 的 key 都能看到前一个 token,这不仅为 induction head 修了路,也为任何”按 bigram 上下文检索”的机制修了路,例如按前一个 token 匹配、但不做复制的 head。所以 Argument 2 严格支持的是:“key 能否依赖前一个 token”是 ICL 出现时机的瓶颈。若要把效应归于 induction head 本身,需要一个只启用 induction 而不启用其他前一个 token 依赖机制的干预,例如只对一个 head 开放 smeared key,并同时检查它是否满足 prefix matching 与 copying。
5 Phase change 的机制解释:后续工作补上了什么
本文给出的是”phase change 期间形成了 induction head”,没有解释为什么形成过程是突然的。两项后续工作在 toy 设置中给出了机制性的回答:
- Singh et al.(ICML 2024) 提出 “clamping”:在训练过程中把某些激活固定住,从而在训练中做因果操作,而不只是在训练后。在 2 层 attention-only 模型上(Omniglot 的少样本任务),他们找到三个相互作用的 subcircuit(previous-token 复制、induction head 的 QK 匹配、label 复制),phase change 来自它们之间的相互作用;多个 induction head 的效应是可加的、冗余的,ablate 任意单个 head 几乎不降低性能(Figure 4a:只有一个 head 带来约 1% 的下降)。这直接印证了本文自己的提醒:冗余会掩盖单个 head 的重要性。他们还在脚注中指出,与本文不同,他们的网络中有不可忽略的 V-composition。
- Reddy(ICLR 2024) 在一个 in-context 分类任务中先构造了一个三参数的 2 层 induction head 最小模型,再令其中一个参数为 0,得到只含”刻画 induction head 的两个参数”的模型,并写道这一约化 “strongly suggests that induction head formation drives the abrupt transition”。另一个 phenomenological 模型则把突变归因于 “the sequential learning of three nested logits enabled by an intrinsic curriculum”。
两者都支持”induction head 的形成本身可以产生突变”,但都在合成任务上,不能直接说明自然语言预训练中 phase change 的全部来源。
6 报告中的不一致
- Phase change 窗口:Argument 1 为 2.5–5B tokens,Model Details 对小模型为 1–3B tokens。
- Pattern-matching head 的层:表格为 8/40,正文为 layer 26。
- Argument 4 正文说 “we study up to 12B parameter models”,而其他地方的最大模型为 13B。
它们不影响主要结论,但说明具体数字需要回到对应位置核对。
7 研究评价
| Originality | 高。第一次把一个可精确描述的电路同时联系到宏观能力与训练动力学。 |
| Technical Soundness | 小模型部分扎实,ablation 规模大且方法说明清楚;归因约定(冻结 pattern)需要读者自己理解其含义。 |
| Evidence Strength | 分层明确:小 attention-only 模型为因果证据;大模型没有 ablation,以 Medium 级别的相关证据为主,辅以弱干预、机制合理性与类比;大模型上的结论已被 few-shot 度量下的结果部分修正。 |
| Scope / Generality | 中等:单一的 ICL 度量(token-loss difference),单一机构的一族模型。 |
| Long-term Research Value | 很高。induction head 成为机制可解释性的标准对象,也是研究 phase change 与 ICL 来源的共同起点。 |
⑥ Connections & Research Insights
1 与 Vol. 001、Vol. 010 的联系:单点选择需要随 增长的 logit
③-4 的恒等式 把三篇笔记连在一起:
- Vol. 001 · Theoretical Limitations of Self-Attention in Neural Sequence Models:Hahn 的 soft-attention 论证依赖有界 logits,其第一步正是 时的特例,单个位置的权重为 。
- Vol. 010 · Thinking Like Transformers:那篇笔记 ③-2.3 用同样的思路量化了 softmax 与 average-hard attention 的距离( 个被选中位置、 个未选中位置、分数差 ),并在 Takeaways 中指出 selector_width 需要随 增长的 logit 尺度。
Induction head 是一个” 的 selector”:它要求在 个位置中精确选中一个。所以 RASP 中”选择一个位置”的原语,在 softmax Transformer 中的代价是 。它与 Hahn 的限制并不矛盾:Hahn 讨论的是 logit 有界、 的渐近情形,而训练出的 induction head 只需要在训练长度内维持足够大的 。
2 与 Vol. 002 的联系:phase change 与 progress measure
Vol. 002 · Progress measures for grokking via mechanistic interpretability 也研究”内部机制的形成与宏观指标的突变同时出现”。两者有一个方法上的对照:002 的 restricted / excluded loss 以最终模型的 key frequencies 为参数,那篇笔记 ⑤-2 指出这使度量本质上是回溯性的;本文的 prefix matching 与 copying score 则由假设的机制在合成输入上定义,不依赖最终模型的参数,可以在任何 checkpoint、任何模型上直接计算。这是本文能在 34 个模型上做比较的原因。
但不能把两种现象等同:002 的 grokking 依赖 weight decay,发生在训练集已被拟合之后很久;本文的 phase change 发生在训练早期(对大模型约为训练进程的 1–2%),并伴随 loss 曲线上一个短暂的不凸段。二者都是”突变”,机制和时间尺度都不同。
3 与 Vol. 003 的联系:同名不同物的 “ICL”
Vol. 003 · What learning algorithm is in-context learning Investigations with linear models 中的 ICL 是在高斯线性回归上从示例推断函数,那里的行为匹配实验发现 kNN 类预测器与 ICL 的一致性远差于 OLS。本文 Argument 5 则把 induction head 理解为”把前文当作最近邻算法的数据点”。放在一起看,被称为 ICL 的至少有三种不同的计算:最近邻式的取回与复制(induction heads)、在函数类上的隐式估计(003),以及 few-shot 任务中由 FV heads 承担的任务识别与执行(Yin & Steinhardt)。“ICL 的机制是什么”这个问题,必须先固定是哪一种 ICL、用哪个度量。
4 与 Vol. 006、Vol. 008 的联系:证据层级
本文最强的因果证据是 head 粒度的 ablation 与架构干预。与 Vol. 006 · Finding Alignments Between Interpretable Causal Variables and Distributed Neural Representations 中的 interchange intervention,以及 Vol. 008 · Locating and Editing Factual Associations in GPT 中的 causal tracing 相比,它缺少一种变量级的干预。一个直接的实验是:取两条重复随机序列 、,把 中 previous-token head 在位置 写入的”前一个 token”子空间分量替换进 。如果高层解释正确,induction head 的注意力目标与输出应当随之移动到 所指示的位置。这会把”induction head 依赖前一个 token 的信息”从必要性证据升级为对一个具体变量的因果检验。
5 一个具体的开放研究问题(独立研究设想,非原论文结果)
问题:induction head 在超出训练长度的上下文中失效,是否可以由”logit gap 被校准到训练长度”定量预测?
精确陈述。对一个 induction head、一个只有唯一匹配位置 的 query,按 ③-4 定义 。恒等式 是精确的。假说 H:
- 训练后 induction head 的 分布由训练上下文长度 决定,大致满足 ;
- 推理时 不随实际长度 调整,因此 时 与重复随机 token 上的 induction loss 按 ③-4 的公式退化;
- 加入随长度缩放的 logit(例如把 attention logits 乘以 )可以消除这种退化。
为什么现有工作没有解决:本文只在 512-token 的评估上下文内测量 ICL score(第 500 个 token),训练上下文为 8192,没有研究长度外推;001、010 的分析给出的是表达能力层面的界,不涉及训练出的 head 实际取到的 。我没有对这一问题做系统的文献检索,可能已有相关工作,这一判断本身需要核实。
实验设置:训练 2 层 attention-only 模型(与本文小模型同构),;为避免位置外推本身成为混杂,比较 NoPE 与 RoPE 两种位置编码;评估序列为长度 (最多 )的随机 token,在其中插入一次 ,结尾为 ,并控制 与结尾的距离。测量每个 induction head 的 、 与 的 loss。
变量与资源:、、匹配距离、位置编码类型、induction head 数量(冗余)、是否有 BOS(attention sink)。
支持 / 反驳标准:
- 支持 H(1): 随 近似线性增长。
- 支持 H(2):用 处估计的 代入恒等式,可以定量预测 时的 。
- 反驳 H(2): 随 增长(例如非匹配位置的分数在长上下文中系统性下降),或者 的下降主要由匹配距离而非 决定;后者说明失效来自位置外推,而不是稀释。
主要技术困难:(a) 多个冗余的 induction head 可以相互补偿(Singh et al.),需要在 head 集合层面而不是单个 head 层面检验;(b) BOS 等 attention sink 会吸收注意力,使”其余 个 key 同分”的近似失效,但恒等式本身仍然成立,只需在 的定义中把 sink 计入非匹配位置;(c) fuzzy matching 的 head 在多个部分匹配的位置之间分配注意力,此时需要把”唯一匹配”推广为匹配集合上的总质量。
⑦ Takeaways & Further Reading
Three Takeaways
- Induction 需要两步信息移动,而 1 层 attention 只能做一步。 关键是 key 必须同时依赖”被关注的 token”和”它前面的 token”。标准 Transformer 用 previous-token head 加 K-composition 实现这一点,smeared key 则直接把前一个 token 的 key 混进来。同一个结构性事实解释了 1 层模型没有 ICL 相变,以及 smeared key 让它出现。
- “电路解释能力”的证据必须分层报告。 本文在小 attention-only 模型上有因果证据(ablation);在大模型上没有 ablation,最强的是 Medium 级别的相关证据,其余是弱干预、机制合理性与类比。“induction heads 是 ICL 的主要机制”这一结论对前者成立,对后者是推测,并且在 few-shot 准确率意义上已被 FV heads 的结果部分修正。
- 度量决定了你发现的机制。 ICL score(token-loss difference)与 few-shot 准确率挑出的是不同的 head;pattern-preserving 与 full ablation 把同一电路的效应记在不同 head 名下。在问”哪个机制负责 X”之前,先固定 X 的度量和归因约定。
One Thought Question
在一个 2 层 attention-only 模型中,induction circuit 由 layer 1 的 previous-token head 和 layer 2 的 induction head 组成,且 的 value 只读取 token 身份(没有 V-composition)。在重复随机序列上分别做以下 ablation,第二遍的 loss 各会怎样?
(a) 对 做 pattern-preserving ablation;(b) 对 做 full ablation;(c) 对 做 pattern-preserving ablation。
据此,“小 attention-only 模型中几乎所有 ICL 都来自 induction heads”这句话在什么意义上成立?如果有人据此主张”previous-token head 对 ICL 不重要”,错在哪里?
提示 1
冻结 attention pattern 时, 对 的影响走的是哪条路径?是 的 query/key,还是 value?
提示 2
的输出只通过 K-composition 进入 的 key,从而影响 的 attention pattern。Pattern 被冻结后,这条路径被保留。 对 logits 的 direct path 在随机序列上几乎不提供信息。
参考解答
以下是我的分析与 toy 计算(Explanatory),不是原论文的结果。用 ③-3.4 的 toy 模型(,25 个 token 重复两遍,300 条序列)计算第二遍的平均 loss:clean 为 0.170 nats;(a) 的 pattern-preserving ablation 仍为 0.170,完全不变;(b) 的 full ablation 为 5.813;(c) 的 pattern-preserving ablation 为 6.215,即 ,因为没有其他 head 向 logits 写入信息。
所以在 pattern-preserving 约定下,整个电路的效应都被记在 名下。“几乎所有 ICL 来自 induction heads”的准确含义是:ICL 的效应通过 induction heads 的输出进入 logits。它不意味着上游的 previous-token head 不重要:full ablation 下,去掉 几乎摧毁了全部 induction 效应,剩下的约 0.4 nats 来自 unigram cache(⑤-2)。主张”previous-token head 不重要”,是把一种归因约定误读成了必要性结论。在真实模型中,previous-token head 的输出还可能通过 V-composition 或 direct path 产生少量效应;Singh et al. 也报告了不可忽略的 V-composition,所以 (a) 的结果不一定严格为零。
Further Reading
- Which Attention Heads Matter for In-Context Learning? — Kayo Yin, Jacob Steinhardt,ICML 2025(PMLR 267;arXiv:2502.14010)。
- 联系:对本文核心结论最直接的修正。在 12 个模型上表明,few-shot ICL 准确率主要依赖 FV heads,而本文的 token-loss difference(在 345M 以上的模型中)主要依赖 induction heads;还发现许多 FV heads 由 induction heads 演化而来。它说明”ICL 的机制”随度量而变。
- What needs to go right for an induction head? A mechanistic study of in-context learning circuits and their formation — Aaditya K. Singh, Ted Moskovitz, Felix Hill, Stephanie C. Y. Chan, Andrew M. Saxe,ICML 2024(PMLR 235;arXiv:2404.07129)。
- 联系:在方法上直接扩展本文的 ablation:用训练中的 clamping 做因果操作,把 phase change 分解为三个相互作用的 subcircuit,并揭示多个 induction head 之间的冗余。
- The mechanistic basis of data dependence and abrupt learning in an in-context classification task — Gautam Reddy,ICLR 2024(oral;arXiv:2312.03002)。
- 联系:理论上的互补。用最小的 induction head 模型表明突变由 induction head 的形成驱动,再用一个 phenomenological 模型把突变归因于”嵌套 logits 的顺序学习”,回答了本文没有回答的”为什么形成过程是突然的”,并讨论了数据分布性质的作用。
Final Takeaway
这篇论文最值得长期保存的洞察是:一个宏观能力的出现,可以被追溯到一个具体电路的形成,但这条追溯链的每一环都需要单独的证据。 在小 attention-only 模型中,“key 需要看到前一个 token”这一结构性事实、ICL 的相变和 induction head 的 ablation 效应严丝合缝地对上了。到了大模型,没有 ablation,链条主要靠相关证据与类比;后续工作表明,换成 few-shot 准确率这个度量,主要机制就换成了另一类 head。Induction head 是 ICL 的一个起点,而不是它的全部。