mlsanityData to 26 Nov 2026

Transformers Learn Two Competing Sorting Algorithms, and Only One Length-Generalizes

arXiv 2610.26974 ↗Submitted 18 Oct 2026cs.LG cs.CLcited by 0

Not on the map. Why?

Abstract (arXiv)

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. 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.

Key facts. No citing papers in the map yet (data through 26 Nov 2026). Builds on 25 of its 27 references that are in the map. Submitted 18 Oct 2026.

Builds on

25 of 27 references.

References · 25

  1. 2023Progress measures for grokking via mechanistic interpretabilitycited by 919
  2. 2022What learning algorithm is in-context learning? Investigations with linear modelscited by 729
  3. 2020The Pitfalls of Simplicity Bias in Neural Networkscited by 362
  4. 2023The Impact of Positional Encoding on Length Generalization in Transformerscited by 348
  5. 2022Exploring Length Generalization in Large Language Modelscited by 219
  6. 2023What Algorithms can Transformers Learn? A Study in Length Generalizationcited by 219
  7. 2021Thinking Like Transformerscited by 201
  8. 2023The Clock and the Pizza: Two Stories in Mechanistic Explanation of Neural Networkscited by 172
  9. 2023Tracr: Compiled Transformers as a Laboratory for Interpretabilitycited by 125
  10. 2024Transformers Can Achieve Length Generalization But Not Robustlycited by 94
  11. 2024Softmax is not Enough (for Sharp Size Generalisation)cited by 60
  12. 2024Competition Dynamics Shape Algorithmic Phases of In-Context Learningcited by 59
13 more
  1. 2020Neural Execution Engines: Learning to Execute Subroutinescited by 40
  2. 2025In-Context Learning Strategies Emerge Rationallycited by 21
  3. 2022Systematic Generalization and Emergent Structures in Transformers Trained on Structured Taskscited by 20
  4. 2024Initialization is Critical to Whether Transformers Fit Composite Functions by Reasoning or Memorizingcited by 19
  5. 2023Improving Length-Generalization in Transformers via Task Hintingcited by 16
  6. 2024Simplicity Bias of Two-Layer Networks beyond Linearly Separable Datacited by 12
  7. 2025Random Scaling of Emergent Capabilitiescited by 9
  8. 2025Structure Development in List-Sorting Transformerscited by 7
  9. 2026Discovering Interpretable Algorithms by Decompiling Transformers to RASPcited by 7
  10. 2024Simplicity Bias via Global Convergence of Sharpness Minimizationcited by 6
  11. 2024Modular addition without black-boxes: Compressing explanations of MLPs that compute numerical integrationcited by 5
  12. 2025On the geometry and topology of representations: the manifolds of modular additioncited by 5
  13. 2023Hierarchical Simplicity Bias of Neural Networkscited by 1

Built on by

Not cited yet.