论文

arXiv 论文揭示语言模型上下文学习的内部电路机制

A Circuit for In-Context Learning

精选理由

这篇论文把 ICL 的每一步都拆开了,用 patching 证明哪层头在存演示、哪层头在查标签,机制可解释性方向的可以细看。

一篇 arXiv 论文研究了 3 个 3-4B 参数 transformer 中 few-shot 句子分类的上下文学习(ICL)机制。模型在标签 token 位置实现键值记忆:中层注意力头将前面演示的类别写入残差流作为 key,上层头在查询时匹配并复制出对应标签。研究通过 activation patching、头消融和 steering 做因果验证,仅命名的那些注意力头就能复现该行为。该电路在 0.6B 到 8B 的同一模型家族中出现,经指令微调后依然保留。

原文 · arXiv cs.LG

A Circuit for In-Context Learning

In-context learning (ICL) lets a pretrained language model perform a task specified by demonstrations in its prompt, with no parameter updates. With arbitrary labels the model must acquire a mapping it cannot have memorized, so the learning happens within a single forward pass. Prior work found candidate parts but not how they compose. We give the composition for few-shot sentence classification with arbitrary labels in three 3-4B-parameter transformers. The model implements a key-value memory over label-token positions: each label token's residual stream encodes the label's identity, and middle-layer attention heads write into it the class of the preceding demonstration as its key. At the query, upper-layer heads match the query's class against the stored keys and copy out the matching labels. An early-layer signal encoding whether any label has repeated gates this readout; in its absence the readers attend to the start of the prompt and no label is favored. A separate set of heads suppresses the most recently seen label. Each step is established causally by activation patching, head ablations against random-head baselines, and steering with the model's own signals. A softmax model of the readout with two constants per head, fit on one prompt distribution, predicts another. The computation is conserved across the three models, the named heads alone reproduce the behaviour, and a transformer trained from scratch grows the same parts. The circuit transfers to semantic concepts and to labels with meaning, and the same parts, with a causal gate, appear from 0.6B to 8B in one family and survive instruction tuning. ICL in this setting is a gated associative lookup built from a few identifiable component types, traced causally from demonstrations to prediction.