揭示Transformer如何通过多模块协同实现上下文学习的理论机制。
Unveiling Induction Heads: Provable Training Dynamics and Feature Learning in Transformers

- 构建双注意力层模型,分析梯度流在n元马尔可夫数据上的收敛行为。
- 证明最终模型形成广义归纳头机制,各模块协同完成复制、选择与分类。
- 首次从理论上解释注意力、归一化等组件如何共同支持上下文学习。
上下文学习(ICL)是大语言模型的核心能力,但其理论基础仍不清晰,尤其缺乏对Transformer其他组件如何贡献于ICL的理解。本文研究在n元马尔可夫链数据上,双注意力层Transformer通过交叉熵ICL损失进行梯度流训练的过程。模型包含相对位置编码、多头Softmax注意力及带归一化的前馈网络。理论证明:梯度流收敛至一个极限模型,该模型实现一种带有可学习特征的广义归纳头机制,由所有组件协同作用形成。第一注意力层作为'复制器',将窗口内历史标记复制到每个位置;归一化前馈网络作为'选择器',仅基于信息相关父节点生成特征向量;第二注意力层作为'分类器',比较特征相似性以生成目标输出。实验验证了理论结果。
原文摘要 · Abstract (English)
In-context learning (ICL) is a cornerstone of large language model (LLM) functionality, yet its theoretical foundations remain elusive due to the complexity of transformer architectures. In particular, most existing work only theoretically explains how the attention mechanism facilitates ICL under certain data models. It remains unclear how the other building blocks of the transformer contribute to ICL. To address this question, we study how a two-attention-layer transformer is trained to perform ICL on $n$-gram Markov chain data, where each token in the Markov chain statistically depends on the previous $n$ tokens. We analyze a sophisticated transformer model featuring relative positional embedding, multi-head softmax attention, and a feed-forward layer with normalization. We prove that the gradient flow with respect to a cross-entropy ICL loss converges to a limiting model that performs a generalized version of the induction head mechanism with a learned feature, resulting from the congruous contribution of all the building blocks. In the limiting model, the first attention layer acts as a $\mathit{copier}$, copying past tokens within a given window to each position, and the feed-forward network with normalization acts as a $\mathit{selector}$ that generates a feature vector by only looking at informationally relevant parents from the window. Finally, the second attention layer is a $\mathit{classifier}$ that compares these features with the feature at the output position, and uses the resulting similarity scores to generate the desired output. Our theory is further validated by experiments.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。