arXiv:2610.26974cs.LGOct 2026

Manuscript Offprint

arXiv:2610.26974

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

Khashayar Gatmiry, Nathan W. Henry, Chloe H. Su, Emmanuel Abbe, Samy Bengio, Melanie Weber, Jonathan A. Kelner, Peter L. Bartlett

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Khashayar Gatmiry ∗^{*}

UC Berkeley & Apple

Nathan W. Henry ∗^{*}

UC Berkeley

Chloe H. Su

Kempner Institute,

Harvard University

Emmanuel Abbe

EPFL, Apple

Samy Bengio

Apple

Melanie Weber

Harvard University

Jonathan A. Kelner

MIT

Peter L. Bartlett

UC Berkeley &

Google DeepMind

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

1 Introduction

A recurring hope in mechanistic interpretability is that neural networks trained on well-understood tasks will rediscover familiar algorithms, and that similar mechanisms will recur across independently trained models (Olah et al., 2020; Akyürek et al., 2023). Recent work has complicated this picture by demonstrating settings in which neural networks can implement one of multiple solutions to the same task. For modular addition, Zhong et al.

∗^{*}Equal contribution. Author ordering determined by a coin flip.

(a) Sorting vocabulary size 256, σ=0.01\sigma = 0.01, train length 16

Figure

(b) Minimum-finding vocabulary size 4096, σ=0.006\sigma = 0.006, train length 16

Figure

(c) Coin denomination vocabulary size 64, σ=0.1\sigma = 0.1, train length 8

Figure 1: Two-layer transformers trained from different initializations learn distinct solution types on three tasks that one layer could solve, and which type length-generalizes depends on the task. Each curve is one seed's token accuracy, evaluated beyond its training length. Curves are colored by removability, defined as the model's sequence accuracy when the second attention layer is ablated. The side histograms show how removability is distributed across seeds. Single-stage models are unaffected by the ablation, while two-stage models have poor accuracy without the second attention layer. (a) Sorting: removability is either 0 or 1, and two-stage solutions length-generalize almost perfectly while single-stage solutions do not. (b) Minimum-finding: The pattern reverses where single-stage solutions generalize better than two-stage solutions. (c) Greedy coin denomination: the same pattern as sorting occurs but with a broader spread across seeds and a weaker correlation between solution type and generalization.

(2023) identified two mechanistically different solutions, termed the “Clock” algorithm and the “Pizza” algorithm (see also Nanda et al., 2023; Yip et al., 2024), and showed that changes in architectural hyperparameters can determine which solution emerges. Multiple solutions with different generalization behavior out of distribution have since been found in other settings, including in-context learning (Park et al., 2024; Wurgaft et al., 2025), state tracking (Li et al., 2025), and algorithmic tasks such as counting and addition (Zhao et al., 2025). Our paper introduces a new general setting for studying when neural networks converge to one of two qualitatively different solutions. In particular, we study tasks that are possible for a one-layer transformer to implement, and train them with a two-layer transformer.

While common intuition about training dynamics suggests that neural networks have a "simplicity bias" where they tend to converge to simpler solutions, and that simpler solutions generalize better than complex ones (Gatmiry et al., 2024; Du, 2024; Shah et al., 2020), recent work refutes this conventional wisdom (Tsoy and Konstantinov, 2024). We find that under some parameter initializations, these two layer transformers converge to single-stage solutions, where the second attention can be ablated without impacting accuracy, while for other initializations, the transformers converge to a solution that meaningfully uses both attention layers. We find that for the task of minimum-finding, the simpler single-layer solutions generalize better. However, for the task of sorting numbers, we find that the more complex two-layer solutions generalize better, counter to common intuition about simplicity bias. In a third, greedy coin denomination task, we find a larger variation in types of solutions learned, and more mixed results about generalization, but with a correlation between reliance on the 2nd attention layer and length-generalization abilities.

This paper focuses on the task of sorting numbers, where we find the cleanest split in solution types across seeds. We study this task in more detail with mechanistic interpretability, aiming to explain the unintuitive phenomenon of the more complex 2-layer solution length-generalizing better. Sorting a sequence of numbers is a task in FTC0FTC^{0} which can theoretically be performed with just a single layer transformer given a large enough embedding size. In fact, Weiss et al. (2021) show that training a single layer transformer for sorting reaches accuracy close to 1 on the lengths trained on.

We argue for two mechanisms of why length-generalization occurs in the more complex solution. First, the more complex solution learns a form of cross-layer specialization to more accurately sort sequences with numbers close to each other. This is a property of sequences that is rare at the length trained at, but increasingly common at longer evaluation lengths. Second, we show evidence of a hinting mechanism, where the first layer can provide a range of possible next tokens to the second, which may make it easier to select the correct next token out of a candidate set of potential next tokens that grows with larger input lengths. We additionally show how to steer the model toward each solution type by varying the initialization scale across layers.

Organization. Section 2 introduces the task and experimental setup. Section 3 presents the two solution types and describes each at a high level. Section 4 gives a mechanistic analysis of the two-stage solution, identifying two properties that account for its length-generalization advantage. Section 5 investigates what determines which algorithm emerges from training. Section 6 discusses results for algorithms other than sorting, and Section 7 discusses related work.

References (56)