Transformers Learn Two Competing Sorting Algorithms, and Only One Length-Generalizes
Computer Science > Machine Learning arXiv:2610.26974 (cs) [Submitted on 18 Oct 2026] Title: Transformers Learn Two Competing Sorting Algorithms, and Only One Length-Generalizes Authors: Khashayar Gatmiry, Nathan W. Henry, Chloe H. Su, Emmanuel Abbe, Samy Bengio, Melanie Weber, Jonathan A. Kelner, Peter L. Bartlett.
计算机科学 > 机器学习 arXiv:2610.26974 (cs) [提交于 2026 年 10 月 18 日] 标题:Transformer 学习两种竞争性的排序算法,且仅有一种能实现长度泛化。作者:Khashayar Gatmiry, Nathan W. Henry, Chloe H. Su, Emmanuel Abbe, Samy Bengio, Melanie Weber, Jonathan A. Kelner, Peter L. Bartlett。
Abstract: We show that two-layer transformers trained to near-zero loss on a sorting task converge to one of two mechanistically distinct solution classes depending on their initialization. One solution class consists of solutions that do not use the second attention layer to sort, but also fail to length-generalize. The other, which uses both attention layers, length-generalizes perfectly, despite only being trained to sort sequences of a fixed length.
摘要:我们展示了在排序任务上训练至接近零损失的双层 Transformer,根据其初始化的不同,会收敛到两类机制截然不同的解之一。其中一类解不使用第二个注意力层进行排序,但也无法实现长度泛化。另一类解则同时使用两个注意力层,尽管仅在固定长度的序列上进行训练,却能实现完美的长度泛化。
By carefully choosing the weight initialization, we demonstrate that we can steer the model toward learning one solution or the other. We identify input types that are rare at the training length but increasingly common at longer sequence lengths as a source of this disparity in length-generalization, and find that layer-wise specialization of the second class of solutions allows them to handle these rare sequences more accurately.
通过仔细选择权重初始化,我们证明了可以引导模型学习其中一种解。我们将那些在训练长度下罕见但在更长序列中变得普遍的输入类型,确定为导致这种长度泛化差异的根源,并发现第二类解的层级专业化使其能够更准确地处理这些罕见序列。
In the second class, we discover a mechanism by which the second layer attention can use the contextual information gathered by the first layer attention to reduce its search space. On the related task of finding the minimum of a sequence, we find a similar two solution-type phenomenon, but with the opposite generalization pattern, where a different form of layer-wise specialization causes the two-layer solution to length-generalize worse than the single-attention solution.
在第二类解中,我们发现了一种机制,即第二层注意力可以利用第一层注意力收集的上下文信息来缩小其搜索空间。在寻找序列最小值的相关任务中,我们发现了类似的两种解类型现象,但其泛化模式相反,即另一种形式的层级专业化导致双层解的长度泛化效果不如单注意力解。