PreprintCited by 53Topic ModelingNatural Language Processing TechniquesDomain Adaptation and Few-Shot Learning

Exploring Length Generalization in Large Language Models

Open access
Anil, Cem, Wu, Yuhuai, Andreassen, Anders, Lewkowycz, Aitor, Misra, Vedant, Ramasesh, Vinay, Slone, Ambrose, Gur-Ari, Guy +2 more·2022·arXiv

Abstract

The ability to extrapolate from short problem instances to longer ones is an important form of out-of-distribution generalization in reasoning tasks, and is crucial when learning from datasets where longer problem instances are rare. These include theorem proving, solving quantitative mathematics problems, and reading/summarizing novels. In this paper, we run careful empirical studies exploring the length generalization capabilities of transformer-based language models. We first establish that naively finetuning transformers on length generalization tasks shows significant generalization deficiencies independent of model scale. We then show that combining pretrained large language models' in-context learning abilities with scratchpad prompting (asking the model to output solution steps before producing an answer) results in a dramatic improvement in length generalization. We run careful failure analyses on each of the learning modalities and identify common sources of mistakes that highlight opportunities in equipping language models with the ability to generalize to longer problems.

AI snapshot

Written from the paper’s full text.

TL;DR

The paper empirically studies length generalization in transformer LLMs on parity and variable assignment, finding that finetuning—with or without scratchpad—fails to extrapolate to longer instances regardless of model scale. Few-shot scratchpad prompting can dramatically improve length generalization, especially when the base model already has nontrivial task performance.

Key findings

  • Finetuned LaMDA models of 244m, 422m, 1b, and 64b parameters showed near-perfect in-distribution performance but rapid out-of-distribution degradation on parity (train lengths 10–21, eval 3–40) and variable assignment (train lengths 3–8, eval 3–19); scale had little effect on length generalization.
  • On a parity variant with fixed 30 bits and varied number of ones (train 10–20 ones, test 1–30), in-distribution accuracy was 100% while OOD performance was roughly random, suggesting transformers learn non-sequential counting/thresholding shortcuts rather than sequential algorithms.
  • Scratchpad finetuning still failed at length generalization; error analysis identified distractor tokens as the main culprit, with distracting input tokens hurting more than distracting scratchpad tokens, and removing all distractor tokens led to perfect length generalization.
  • Few-shot scratchpad prompting with pretrained LaMDA 128b on a coin-flip parity task improved length generalization up to 20 flips; few-shot finetuning helped parity but not variable assignment, where the base model lacked strong pretrained performance.

Methods

  • Empirical study using LaMDA decoder-only models on two synthetic tasks: parity (bitstrings, plus a natural-language coin-flip framing) and Boolean variable assignment (Python-like programs, with diverse and chain-like splits). Parity datasets contained 1,000,000 samples; variable assignment splits contained 1,500,000 samples each.
  • Compared finetuning, few-shot prompting/in-context learning, scratchpad/chain-of-thought, and their combinations; used AdaFactor, greedy decoding, learning-rate and batch-size sweeps, and trained until in-distribution validation accuracy settled.
  • Ran targeted analyses of per-step scratchpad error, positional encoding and EOS effects, computational graph depth, and distractor masking.

Limitations

  • Stated: the hypothesis that few-shot finetuning helps only when the base model already has nontrivial task performance is left for future rigorous evaluation; a systematic study of how prompt style affects length generalization is also left as future work.
  • Evident from the text: the experiments focus on two synthetic algorithmic tasks and LaMDA decoder-only models, so the findings may not directly transfer to broader natural reasoning tasks or other architectures.

Model-written summary. Check it against the paper before relying on it.

Integrity signals

Registry

DataCite · 10.48550/arxiv.2207.04901

Cites

43 of 43 references verified0 citation errors · 0 not checkable

Statistics

Nothing to recompute.

Claims the paper makes

11 of 12 cited sources checked · 7 supported · 0 flagged · accuracy not yet measured

  • Findingcitation
    § 2 Length Generalization · p. 3

    Real-world datasets often contain few long problem instances.

    “real-world datasets often contain few long instances (see Figure 2).”
    • Also stated in: § Contributions Our main contributions are as follows: · p. 3 · plain
      “Real world datasets have heavy tails in length: (left) Histogram of lengths for proofs presented in the Archive of Formal Proofs (right) Histogram of the number of tokens for solutions in the MATH dataset. [6]”
    Quote in the paperNot a substantial claim1 of 1 sources checked
    • cites [6] [6] Dan Hendrycks, Collin Burns, Saurav Kadavath, Akul Arora, Steven Basart, Eric Tang, Dawn Song, and Jacob Steinhardt. Measuring mathematical problem solving with the math dataset. arXiv preprint arXiv:2103.03874 , 2021. - source

      The Introduction (p. 1) describes the MATH dataset, and A.4 Difficulty Analysis (p. 14) compares the difficulty of long and short problems. Neither reports how frequently long instances occur in real-world datasets. Searches for “few long instances” and problem-length distributions found no passage addressing that claim. (LLM, conf=0.74)

      Experimental — accuracy not yet measured
  • Findingown data
    § Contributions Our main contributions are as follows: · p. 2

    Finetuning with scratchpad also fails to generalize to longer problems.

    “We establish finetuning with scratchpad also fails to generalize to longer problems, in contrast to what is suggested by previous works [3].”
    • Also stated in: § 4 Scratchpad Finetuning Still Fails at Length Generalization · p. 6 · plain
      “we find that scratchpad finetuning suffers from similar length generalization pathologies than vanilla finetuning does.”
    Quote in the paperSubstantial claim1 of 1 sources checked
    • cites [3] [3] Maxwell Nye, Anders Johan Andreassen, Guy Gur-Ari, Henryk Michalewski, Jacob Austin, David Bieber, David Dohan, Aitor Lewkowycz, Maarten Bosma, David Luan, et al. Show your work: Scratchpads for intermediate computation with language models. arXiv preprint arXiv:2112.00114 , 2021. - source
  • Premisecitation
    § 4 Scratchpad Finetuning Still Fails at Length Generalization · p. 6

    LaMDA models use T5 position biases to handle position information.

    “LaMDA models use T5 position biases [16] to handle position information.”
    Quote in the paperSubstantial claim1 of 1 sources checked
    • cites [16] [16] Colin Raffel, Noam Shazeer, Adam Roberts, Katherine Lee, Sharan Narang, Michael Matena, Yanqi Zhou, Wei Li, and Peter J Liu. Exploring the limits of transfer learning with a unified text-to-text transformer. arXiv preprint arXiv:1910.10683 , 2019. - source
  • Premisecitation
    § 4 Scratchpad Finetuning Still Fails at Length Generalization · p. 6

    Nye et al. [15] use scratchpad finetuning to achieve strong in-distribution performance on execution-based tasks such as code execution and computing polynomials.

    “Nye et al. [15] use scratchpad finetuning to achieve strong in-distribution performance on execution based tasks such as code execution and computing polynomials.”
    Quote in the paperSubstantial claim1 of 1 sources checked
    • cites [15] [15] Maxwell Nye, Anders Johan Andreassen, Guy Gur-Ari, Henryk Michalewski, Jacob Austin, David Bieber, David Dohan, Aitor Lewkowycz, Maarten Bosma, David Luan, et al. Show your work: Scratchpads for intermediate computation with language models. arXiv preprint arXiv:2112.00114 , 2021. - source
  • Findingcitation
    § 7 Conclusion · p. 9

    Unlike raw finetuning, few-shot scratchpad prompting scales with model size.

    “Unlike raw finetuning, this approach does scale with model size [4].”
    Quote in the paperSubstantial claim1 of 1 sources checked
    • cites [4] [4] Jason Wei, Xuezhi Wang, Dale Schuurmans, Maarten Bosma, Ed Chi, Quoc Le, and Denny Zhou. Chain of thought prompting elicits reasoning in large language models, 2022. URL https://arxiv.org/abs/2201.11903 . - source

      The Results (p. 4) report that few-shot chain-of-thought prompting gains performance as model size increases, with gains emerging around 100B parameters. The Discussion (p. 8) contrasts its scaling with standard prompting, not raw finetuning; our copy does not establish that raw finetuning fails to scale. (LLM, conf=0.86)

      Experimental — accuracy not yet measured
  • Classified as probably not a substantial claim. 2
  • May combine two claims. 12
  • Classified as probably not a substantial claim.

    Premisecitation
    § 3.3 In-Distribution Generalization Doesn't Predict OOD Generalization on Length Generalization Tasks · p. 6

    Prior work establishes that in many tasks, in-distribution loss is a strong predictor of out-of-distribution generalization.

    “Prior work on out-of-distribution generalization establishes that in many tasks, in-distribution loss is a strong predictor of out-of-distribution generalization [14].”
    Quote in the paperSubstantial claim1 of 1 sources checked
    • cites [14] [14] Vaishnavh Nagarajan, Anders Andreassen, and Behnam Neyshabur. Understanding the failure modes of out-of-distribution generalization. arXiv preprint arXiv:2010.15775 , 2020. - source

      The Introduction (p. 1) describes minimizing in-distribution training loss, while Section 3 (p. 4) shows an example in which an ERM classifier’s out-of-distribution accuracy deteriorates. Neither passage assesses whether in-distribution loss is a strong predictor of out-of-distribution generalization across many tasks. Searches for that relationship and for loss–accuracy correlations found no passage establishing the attributed claim. (LLM, conf=0.72)

      Experimental — accuracy not yet measured
  • Classified as probably not a substantial claim. May combine two claims.

    Premisecitation
    § 4 Scratchpad Finetuning Still Fails at Length Generalization · p. 6

    Newman et al. [17] report that networks trained with EOS token prediction often suffer from generalizing to longer problem instances, because of the models' tendency to emit EOS tokens prematurely, as well as the EOS tokens' effect on the representations that get learned.

    “Similarly, Newman et al. [17] report that networks trained with EOS token prediction often suffer from generalizing to longer problem instances, because of the models' tendency to emit EOS tokens prematurely, as well as the EOS tokens' effect on the representations that get learned.”
    Quote in the paperSubstantial claim1 of 1 sources checked
    • cites [17] [17] Benjamin Newman, John Hewitt, Percy Liang, and Christopher D Manning. The eos decision and length extrapolation. arXiv preprint arXiv:2010.07174 , 2020. - source
  • May combine two claims.

    Premisecitation
    § 5 Scratchpad Prompting Significantly Improves Length Generalization · p. 7

    Pretrained LLMs, without finetuning, can solve grade school math word problems and execute pieces of code with nontrivial correctness when prompted with the right scratchpad strategy.

    “They demonstrate that pretrained LLMs, without the help of any finetuning, can solve grade school math word problems and execute pieces of code with nontrivial correctness [15], when prompted with the right scratchpad strategy.”
    Quote in the paperSubstantial claim1 of 1 sources checked
    • cites [15] [15] Maxwell Nye, Anders Johan Andreassen, Guy Gur-Ari, Henryk Michalewski, Jacob Austin, David Bieber, David Dohan, Aitor Lewkowycz, Maarten Bosma, David Luan, et al. Show your work: Scratchpads for intermediate computation with language models. arXiv preprint arXiv:2112.00114 , 2021. - source

      Section 5.1 reports that few-shot scratchpad prompting improved execution accuracy on synthetic Python programs from 11% to 26.5%, supporting the code-execution part without fine-tuning. The paper does not report solving grade-school math word problems: its math experiments concern integer addition (§3) and polynomial evaluation (§4). Its results therefore do not support that part of the claim. (LLM, conf=0.89)

      Experimental — accuracy not yet measured
  • May combine two claims.

    Findingown data
    § Contributions Our main contributions are as follows: · p. 2

    Different hyperparameter choices for finetuning have a large effect on length generalization performance, while having minimal effect on final in-distribution performance.

    “Surprisingly, different hyperparameter choices for finetuning have a large effect on length generalization performance, while having minimal effect on the final in-distribution performance (Section 3.3).”
    • Also stated in: § 3.3 In-Distribution Generalization Doesn't Predict OOD Generalization on Length Generalization Tasks · p. 6 · plain
      “Figure 5 shows how the same model trained on the same data achieving roughly the same in-distribution cross entropy loss behaves on OOD data, where the difference is solely induced by the choice of different hyperparameters.”
    Quote in the paperSubstantial claim
  • May combine two claims.

    Findingown data
    § Contributions Our main contributions are as follows: · p. 2

    In the in-context learning regime, use of a scratchpad shows qualitatively different behavior and significantly alleviates the decay of performance on longer problems.

    “We show that in the in-context learning regime, use of a scratchpad shows a qualitatively different behavior and significantly alleviates the decay of performance on longer problems.”
    • Also stated in: § 5 Scratchpad Prompting Significantly Improves Length Generalization · p. 7 · boosted
      “Scratchpad Prompting Significantly Improves Length Generalization”
    Quote in the paperSubstantial claim
  • May combine two claims.

    Findingown data
    § Exemplars (x3) · p. 8

    The vanilla (no-shot) finetuning baseline outperforms the no-finetuning baseline but does worse on larger lengths, a pathology that doesn't appear with few-shot finetuning.

    “Note that the vanilla (i.e. no shot) finetuning baseline also outperforms the no-finetuning baseline, it actually does worse on the larger lengths - a pathology that doesn't appear with few-shot finetuning.”
    Quote in the paperSubstantial claim
  • May combine two claims.

    Findingown data
    § 1 Introduction · p. 2

    Each technique (finetuning, prompting, scratchpad) and combinations thereof have different modes of failure and present different trade-offs regarding in- and out-of-distribution coverage.

    “We find that each technique (and the combinations thereof) have different modes of failure and present different trade-offs regarding in and out-of-distribution coverage.”
    Quote in the paperSubstantial claim
  • May combine two claims.

    Findingown data
    § 4 Scratchpad Finetuning Still Fails at Length Generalization · p. 7

    The per-step error rates of the LaMDA 128b model, few-shot finetuned on the coin-flip version of the parity task, remain roughly constant across scratchpad steps, in contrast to zero-shot scratchpad finetuned models where per-step error rates increase abruptly on OOD lengths.

    “The per-step error rates of the LaMDA 128b model, few-shot finetuned on the coin-flip version of the parity task remain roughly constant across the scratchpad steps. This is in stark contrast with zero-shot scratchpad finetuned models, where the per-step error rates increase abruptly when the model is evaluated on OOD lengths.”
    Quote in the paperSubstantial claim
  • May combine two claims.

    Findingown data
    § 4 Scratchpad Finetuning Still Fails at Length Generalization · p. 6

    Padding input bit-strings and scratchpad content with dummy padding tokens to equalize token count and aligning T5 position bias bins helps but does not eliminate significant length generalization issues in scratchpad finetuning.

    “While this intervention helps, the trained models still display significant length generalization issues.”
    Quote in the paperSubstantial claim
  • May combine two claims.

    Findingreasoning
    § 7 Conclusion · p. 9

    Both vanilla finetuning and finetuning with scratchpads suffer from a lack of length generalization.

    “Both vanilla finetuning and finetuning with scratchpads suffer from a lack of length generalization”
    Quote in the paperSubstantial claim
  • May combine two claims.

    Premisecitation
    § 3.2 Transformers Prefer Parallel Strategies over Sequential Ones · p. 5

    Self-attention is an equivariant transformation capable of performing pooling operations like max-pooling.

    “This is not surprising, given that self-attention is an equivariant transformation capable of performing pooling operations like max-pooling [12].”
    Quote in the paperSubstantial claim0 of 1 sources checked
    • cites [12] [12] Juho Lee, Yoonho Lee, Jungtaek Kim, Adam Kosiorek, Seungjin Choi, and Yee Whye Teh. Set transformer: A framework for attention-based permutation-invariant neural networks. In International Conference on Machine Learning , pages 3744-3753. PMLR, 2019. - source

      Checked before — its earlier verdict is held back until a new check runs.

  • May combine two claims.

    Interpretationown data
    § 4 Scratchpad Finetuning Still Fails at Length Generalization · p. 6

    If the input length is out-of-distribution, even in-distribution scratchpad steps are inaccurate, implying the model hasn't learned an attention pattern that generalizes to longer bit-strings.

    “The fact that the model makes mistakes in in-distribution scratchpad steps when the input has an OOD length implies that the attention mechanism isn't capturing the relevant part of the input to form the scratchpad output.”
    • Also stated in: § 4 Scratchpad Finetuning Still Fails at Length Generalization · p. 7 · plain
      “If the input length is out-of-distribution, even in-distribution scratchpad steps are inaccurate, implying the model hasn't learned an attention pattern that generalizes to longer bit-strings.”
    Quote in the paperSubstantial claim
  • May combine two claims.

    Interpretationown data
    § F Distractor Analysis for Scratchpad Strategies · p. 16

    Untrained position encodings and/or EOS token prediction are not the cause of the length generalization pathologies.

    “it's not untrained position encodings and/or the EOS token prediction that causes the aforementioned pathologies.”
    Quote in the paperSubstantial claim
13 of 13
  • The paper relies on this without support.

    Learning an algorithm can in principle enable a model to extrapolate from short examples to problems of arbitrary length.

    “We consider problems in which learning an algorithm can in principle enable a model to extrapolate from short examples to problems of arbitrary length.”
  • The paper relies on this without support.

    The parity and variable assignment tasks capture the relevant structure of length generalization in real-world tasks.

    “These problems are illuminating because their simplicity allows us to probe the failure modes as well as contrast the learned solutions with the ground truth algorithm.”
    “These tasks avoid problem-specific subtleties that could mislead our analyses, while strongly capturing the deterministic Markov process structure.”
  • The paper relies on this without support.

    Length can be characterized as the number of state transitions in a deterministic Markov process across a wide range of sequence problems.

    “we take the approach of characterizing length in the context of a deterministic Markov process . From this perspective, length is simply the number of state transitions experienced by an initial world state.”
  • The paper relies on this without support.

    The parity task admits a sequential solution that enables length generalization in a straightforward way.

    “The parity task admits a sequential solution that enables length generalization in a straightforward way: simply process the bits left-to-right and record the parity of the bits processed so far as the state.”
  • The paper relies on this without support.

    The Boolean Variable Assignment task captures arbitrarily long, potentially branching unidirectional execution flows.

    “The Boolean Variable Assignment task is designed to capture arbitrarily long, potentially branching unidirectional execution flows.”
5 of 5

Claims other papers make about this one

Each is checked against this paper’s own text: does it say what the citing paper says it does?

  • Even models finetuned from pretrained LLMs struggle on simple algorithmic tasks.

    “even models finetuned from pretrained LLMs struggle on simple algorithmic tasks (Anil et al., 2022).”

    From What Algorithms can Transformers Learn? A Study in Length Generalization (2023) · cites this paper as Anil et al., 2022

    Section 3 (p. 4) reports that models finetuned from pretrained language-model checkpoints perform poorly on out-of-distribution lengths in simple algorithmic tasks. The Contributions section (p. 2) also says they can attain perfect in-distribution accuracy, so the unqualified claim that they struggle on the tasks omits the crucial length-generalization condition. (LLM, conf=0.88)

    Experimental — accuracy not yet measured
  • Pretrained LLMs cannot solve addition and parity robustly without careful prompting.

    “even pretrained LLMs cannot solve these tasks robustly (Brown et al., 2020;Chowdhery et al., 2022;Anil et al., 2022) without careful prompting (Zhou et al., 2022b).”

    From What Algorithms can Transformers Learn? A Study in Length Generalization (2023) · cites this paper as Anil et al., 2022

    The Introduction (p. 1) says LLMs struggle to generalize to problems of arbitrary length, and Section 5.1 (p. 7) reports that the right scratchpad prompt improves pretrained models’ performance on a parity-based coin-flipping task. But the paper’s own tasks are parity and variable assignment, not addition; its evidence therefore does not establish the claim for both named tasks. (LLM, conf=0.84)

    Experimental — accuracy not yet measured
  • To learn a model that can effectively generalize to longer problems, it is important to consider architectures that can adaptively adjust the computational budget to the difficulty of the tasks.

    “To learn a model that can effectively generalize to longer problems, it is important to consider architectures that can adaptively adjust the computational budget to the difficulty of the tasks (Anil et al., 2022;Du et al., 2022;2024).”

    From Looped Transformers for Length Generalization (2024) · cites this paper as Anil et al., 2022

    Section 2 (p. 3) says longer problem instances require more compute, and Section 5.1 (p. 7) describes scratchpad solutions with a step for each operation. But the paper does not recommend architectures that adaptively adjust computational budgets; its Conclusion (p. 9) emphasizes a length-generalization approach that does not require changing model architecture. (LLM, conf=0.82)

    Experimental — accuracy not yet measured
  • Even the largest and latest LLMs trained on much of the existing text on the Internet still struggle with length generalization.

    “However, even the largest and latest LLMs like Achiam et al. (2023) trained on much of the existing text on the Internet, still struggle with length generalization (Wu et al., 2023;Anil et al., 2022;Lee et al., 2024).”

    From Looped Transformers for Length Generalization (2024) · cites this paper as Anil et al., 2022

    The Conclusion (p. 9) says length generalization remains challenging for language models up to the 100B-parameter scale, and Section 3.1 (p. 4) reports weak extrapolation in the models tested. The paper does not establish the claim for the latest models cited in the sentence or for models trained on much of the Internet’s text; it also reports substantial improvements with few-shot scratchpad prompting. (LLM, conf=0.86)

    Experimental — accuracy not yet measured

Self-check

Percentages, sums and reported differences re-computed from the paper’s own numbers.

Each contradiction between two passages listed here was confirmed by an independent check.

  • Conflicting parity training-length ranges

    Major

    The main text says the parity models were trained on bitstrings of length 10 to 21, while the dataset details give a training range of 3 to 20.

  • Conflicting descriptions of baseline variable-assignment performance

    Minor

    The Figure 7 caption says the unfinetuned base model does not perform poorly on variable assignment. The accompanying discussion says its length generalization is not strong and that it must acquire a new skill through finetuning.

2 of 2

Cites

Flagged first, then by citations

Research works printed but not linked: 1

  1. [15][15] Maxwell Nye, Anders Johan Andreassen, Guy Gur-Ari, Henryk Michalewski, Jacob Austin, David Bieber, David Dohan, Aitor Lewkowycz, Maarten Bosma, David Luan, et al. Show your work: Scratchpads for intermediate computation with language models. arXiv preprint arXiv:2112.00114 , 2021. - · arXiv

Cited by

Sorted by citations

Authors
Anil, C., Wu, Y., Andreassen, A., Lewkowycz, A., Misra, V., Ramasesh, V., Slone, A., Gur-Ari, G., Dyer, E., & Neyshabur, B.
Affiliations
Google (United States) (US)
Venue
arXiv
Publisher
arXiv
Published
2022
ISSN
2331-8422 (ISSN-L)
OpenAlex
W4320087317
Cited by
44
References
33
Citations by year
2025: 11 · 2024: 14 · 2023: 19
Type
posted content
Language
en
Open access
Locations
  • arXiv (Cornell University) (submittedVersion)
  • arXiv (Cornell University) (cc-by)
Topics
  • Physical Sciences › Computer Science › Artificial Intelligence › Topic Modeling
  • Physical Sciences › Computer Science › Artificial Intelligence › Natural Language Processing Techniques
  • Physical Sciences › Computer Science › Artificial Intelligence › Domain Adaptation and Few-Shot Learning
Keywords
length generalization, out-of-distribution generalization, large language models, transformer models, in-context learning, fine-tuning, failure analysis, mathematical reasoning, theorem proving

About these checks

Registry, retraction, Cites and statistics results are computed, not judged, with one exception: a printed reference that no index lists is read once by a model before it counts as a citation error. Claims are extracted by a model and kept only when their quote appears verbatim in the text; each cited source is then checked, with a model’s help, for whether it says what the claim relies on. Those verdicts can be wrong — read the claim’s quote and the cited source before relying on one.

Claims read by deepseek-v4.1-flash under definition version 3 (extract-v3) on 2026-10-23.

Classified by jev-1.13-20260917 (gold-guideline-v0.2) on 2026-10-23.

Cited sources are checked by gpt-6-sol.

Statistics and arithmetic are recomputed, not judged.

Confidence bands are shown only for fields calibrated on our annotated test set. None is calibrated yet, so no band is shown.

What counts as a claim ›How we measure confidence ›How we check this ›

Are you an author, or do you see a problem with this page? Request a review