# Understanding and Improving Length Generalization in Recurrent Models

Initial state tweaks let recurrent models generalize to longer sequences

Ruiz, Ricardo Buitrago, Gu, Albert · arXiv · 2025-7-3

- **Problem** Recurrent models often fail on sequences longer than their training context. Training covers only some of the states the recurrence can reach, so the model is unfamiliar with the rest.
- **Method** State Passing post-trains the model with each sequence starting from the final state of a different input, instead of from zeros.
- **Results** The interventions enable length generalization from 2k to 128k and improve long-context tasks.

## Why recurrent models break past their training context

Recurrent models such as state space models and linear attention squeeze the earlier part of a sequence into one state. Each output reads the past only through that state, so the cost grows linearly with sequence length. In principle the recurrence can be rolled forward past the training context, since the model simply keeps updating its state.

In practice this benefit is often lost, because performance can drop considerably once the sequence exceeds the training context. The authors frame this as two questions: why these models fail to length generalize, and how to enable it efficiently across several recurrent models.

Earlier fixes for Mamba either force the model to forget previous context or skip tokens in the state update. Both approaches reduce the effective context of the processed sequence, but they change Mamba's internal mechanism. Because of that change, these methods might not transfer easily to other architectures. Other works link length generalization to state capacity and overfitting, and propose training on longer sequences.

They also propose Truncated Backpropagation Through Time (TBTT) as a way to enable length generalization.

The authors instead reason about the distribution of states the model is trained on. Their aim is a precise hypothesis for why recurrent models fail to length generalize. Figure 1 shows perplexity by token position for Mamba-1, Mamba-2 and GLA, with and without State Passing. Mamba-2 and GLA are post-trained for 500 steps, while Mamba-1 is post-trained for 1000 steps.

The Pile validation dataset is the evaluation data for these curves.

The study asks why the failure happens and how to fix it across several recurrent models at once. Its answer centers on the states the model sees during training, rather than on changing each architecture.

## The unexplored states hypothesis

The core idea is that a recurrent model fails on long inputs when its training covered only some of the states it can reach. The states meant here are attainable states, the ones the recurrence would produce if it kept running over arbitrarily long sequences. The authors write the distribution of the state at time t as D\_t := h\_t(X\_{0:t}), where h\_t rolls out the state recurrence on a sequence.

A model trained with context T sees distributions D\_t only for t below T, and it never sees D\_t' for t' at or above T. So a state distribution beyond the training context is unfamiliar to the model, and the model isn't guaranteed to perform well on it.

The argument rests on an assumption about inputs: for many tasks, the input distribution at time t barely changes after the initial tokens. In text, for example, the marginal distribution of tokens at two positions t and t' shouldn't differ much. That assumption lets the authors focus on the distribution of the state rather than on the inputs.

The function f that maps the input x\_t and the state h\_t to the output y\_t must then perform well on the states it actually meets. The authors see the short-context overfitting to early-position states as the source of the failure. If that holds, changing the initial state during training should widen state coverage without touching the architecture.

The next step, the SSM equations for a non-zero initial state, shows how a changed initial state propagates to the output.

## Training context and training length

Position-wise perplexity averages the perplexity of the next token at each position over a dataset. Length generalization means that, beyond the training context, these position-wise perplexities stay at or below the lowest value reached inside the training context. The authors define the minimum inside the training context as the value p★, reached at position t★.

Across Mamba-2 and GLA models trained from scratch on the Pile, a longer training context gave better length generalization. The Mamba-2 45m model was trained for 22.5B tokens, which is 25x Chinchilla laws. The 70m and 85m models were trained for 34B tokens, which is 20x Chinchilla laws. The 125m model was trained for 25B tokens, which is 10x Chinchilla laws.

The models that were trained with short contexts show perplexities that diverge after the context length. Larger models need a longer context to length generalize, so the context length required grows with model size.

Training longer also hurts length generalization, which means the failure depends on the training budget as well as the context. A Mamba-2 85m model with context 1024 failed to length generalize only after training for more than 7.5x Chinchilla scaling laws. Its checkpoints span 4.25B to 34B tokens, so the failure appears only in the later checkpoints.

The authors therefore conclude that extended training hinders length generalization.

![Figure 3: Position-wise perplexities of a Mamba-2 85m model trained for different number of tokens with a context of 1024 on the Pile (Gao et al. 2020). 2.5x m…](https://refivu.com/papers/f9cd6a9a-de1d-4e94-a91d-e03ad7e1036c/figures/figure-4ec3dcc534995ff928ceaa1e9344669f.png?thumb=512)

**Figure 3** : Position-wise perplexities of a Mamba-2 85m model trained for different number of tokens with a context of 1024 on the Pile (Gao et al. 2020). 2.5x means that the model is trained for 2.5 times what Chinchilla scaling laws dictate for that model size. Thus, the checkpoints correspond to a range between 4.25B and 34B tokens. In this case, the failure to length general- ize occurs after training for more than 7.5x Chinchilla laws.

[Original paper](https://arxiv.org/abs/2507.02782v2) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

The warm-up period also changes the outcome, so the training recipe matters beyond context and budget. In a Mamba-2 45m run with context 1024, a warm-up lasting 25% of training hurt performance beyond the training context, compared to 10%. The 45m run was trained for 9B tokens, which is 10x Chinchilla scaling laws. The authors don't treat this recipe sensitivity as a fixed cost, since their interventions remove the need to tune it.

![Figure 11: Mamba-2 45m trained on a context of𝑇=1024with two different warm-up periods of the learning rate scheduler (10% and 25% refer to having a warm-up pe…](https://refivu.com/papers/f9cd6a9a-de1d-4e94-a91d-e03ad7e1036c/figures/figure-fa3703f9b5ba5d66eb3b3582dd9dc68d.png?thumb=512)

**Figure 11** : Mamba-2 45m trained on a context of𝑇=1024with two different warm-up periods of the learning rate scheduler (10% and 25% refer to having a warm-up period that lasts 10% and 25% of the full training, respectively). An increased warm-up period hinders performance beyond the training context, demonstrating the impact of the training recipe for length generalization. The models are trained for 9B tokens (10x Chinchilla scaling laws) with the recipe given in Section B.1.

[Original paper](https://arxiv.org/abs/2507.02782v2) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

## Effective Remembrance, a metric for past influence

Effective Remembrance compares the next-token distribution from the full context with the one from only a recent suffix of the sequence. Given a context x\_{0:T}, the model outputs a vector of probabilities over the vocabulary for the next token position. The metric is defined as EffRem\_T(t) = d(q(·\|x\_{0:T}), q(·\|x\_{t:T})), where d is a distance between probability distributions.

Because total variation is bounded between 0 and 1, the Effective Remembrance value stays between 0 and 1.

A value of 0 means the predictions from x\_{t:T} and x\_{0:T} are the same, so the model doesn't effectively remember the tokens before t. A high value means that removing the earlier tokens from the context changes the prediction significantly. For text, the authors expect Effective Remembrance near 0 for t far below T, and approaching 1 as t approaches T.

The 1.3b Mamba-2 model has high Effective Remembrance values for small t, so its outputs depend strongly on the initial tokens. The authors read this as overfitting to the states produced early in the sequence, when there is no previous context and the state is zero-initialized. The same figure shows State Passing post-trained models with smaller values for small t, so they are less affected by the first tokens.

The authors tested total variation, Jensen-Shannon distance, and one minus cosine similarity as distances. The shape of the Effective Remembrance curve doesn't change much with the choice of distance, although the absolute values vary.

## How the state distribution drifts

The norm of the Mamba-2 130m official checkpoint grows with position, so the states drift after the training context. In the official checkpoint, the standard deviation at position 8192 is almost twice the one at position 2048. The Mamba-2 130m model post-trained with State Passing keeps its standard deviation largely steady after the training context of 2048.

The drift is tied to the state distribution rather than to the state's size alone.

![Figure 4: Norm of the full state of the Mamba-2 130m of- ficial checkpoints versus the sequence position𝑡(ℎ 𝑡 in the notation of Section 2.2). The norm is take…](https://refivu.com/papers/f9cd6a9a-de1d-4e94-a91d-e03ad7e1036c/figures/figure-edf8c1f5733092e2101da7c98e7c85e0.png?thumb=512)

**Figure 4** : Norm of the full state of the Mamba-2 130m of- ficial checkpoints versus the sequence position𝑡(ℎ 𝑡 in the notation of Section 2.2). The norm is taken across all ele- ments of the state in all layers. The Mamba-2 130m post- trained with State Passing (Section 4.1) produces states whose standard deviation do not significantly change af- ter the training context𝑇=2048. In contrast, the official Mamba-2 checkpoint reaches a standard deviation almost twice as large at position𝑡=8192than the one at position 𝑡=2048.

[Original paper](https://arxiv.org/abs/2507.02782v2) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) · Ruiz, Ricardo Buitrago, Gu, Albert · Understanding and Improving Length Generalization in Recurrent Models · Original paper figure · Extracted the figure area from page 6 of the original PDF.

The authors argue that large norms are not the cause, because the State Passing model has a higher state norm at almost all positions. What matters is whether the distribution keeps changing after the training context. The authors trace this change to specific layers and heads, not the whole state. Figure 9 tracks the standard deviation of those selected heads over time for the Mamba-2 130m official checkpoint.

The authors conclude that the solution is having states whose distribution doesn't change much after the training context, not avoiding large standard deviations.

## Changing the initial state during post-training

Each one draws the starting state from a distribution closer to the states the recurrence actually reaches.

With a nonzero starting state the recurrence unrolls to \\\[h\_t=\\sum\_{s=0}^{t}A\_{\\times s+1:t}\\,B\_s x\_s+A\_{\\times 0:t}\\,h\_{-1}\\\] The output picks up a matching extra term \\(C\_t^{\\top}A\_{\\times 0:t}h\_{-1}\\), so the starting state only adds a correction.

The authors believe Gaussian samples are far from the real state distribution of large Mamba-2 models. Too high a standard deviation hurts performance, and too small a standard deviation does not enable length generalization.

![Figure 12: Position-wise perplexity of Mamba-2 125m post-trained for 100 steps with random initial state at different standard deviations𝜎. This post-training…](https://refivu.com/papers/f9cd6a9a-de1d-4e94-a91d-e03ad7e1036c/figures/figure-35c8c5d8f29645b8c168c9a7b56609af.png?thumb=512)

**Figure 12** : Position-wise perplexity of Mamba-2 125m post-trained for 100 steps with random initial state at different standard deviations𝜎. This post-training technique enables length generalization, but if the noise level is too high it hurts performance.

[Original paper](https://arxiv.org/abs/2507.02782v2) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

Fitted Noise keeps a per-layer, per-head mean updated as \\\[\\mu\_{(li)}\\leftarrow(1-\\beta)\\,\\mathrm{Mean}(h^{(li)}\_T)+\\beta\\,\\mu\_{(li)}\\\] with \\(\\beta=0.1\\). Each training step samples the starting state from a Gaussian with those fitted parameters, where the mean and variance are taken over the head dimension and the state expansion dimension N.

After 100 steps this improves the 370m and 780m models but still fails on the 1.3b model. The authors suspect that independent sampling can't capture the stronger dependencies between state values in the 1.3b model.

State Passing starts each sequence from the final state of a different sequence, shuffling the batch and reusing the final states of the previous batch. A dropout with probability \\(p=0.1\\) zero-initializes the state, so the model still handles inputs with no prior context. With 100 steps this enables length generalization, which the authors take as support for the hypothesis.

TBTT splits a long sequence into chunks and starts each chunk from the final state of the previous one, with gradients stopped between chunks. Compared with State Passing, TBTT doesn't shuffle the sequences and needs a sampler that keeps chunks in order.

## Pile perplexity after post-training

On the Pile, the four interventions were compared on the 370m, 780m and 1.3b official Mamba-2 checkpoints after 100 post-training steps. State Passing directly samples from the distribution of attainable states, which the authors say is why it performs best. The authors judge State Passing and TBTT the strongest of the four interventions, and together they have the best performance.

Random Noise slightly improves the 370m model but doesn't work for the 780m and 1.3b models.

TBTT achieves very similar performance to State Passing on the Pile perplexity evaluation. TBTT splits a long sequence into chunks and passes the final state of each chunk as the next initial state. The authors take this to mean that exploring attainable state distributions matters more than processing long chunked sequences. Because the gradient stops between chunks, the model still learns from the previous context carried in the initial state.

Figure 1 post-trains Mamba-1 for 1000 steps, while Mamba-2 and GLA are post-trained for 500 steps. The Mamba-2 ablation in Figure 5 uses 100 post-training steps, a much smaller budget. The GLA curve shows the model is extremely affected by tokens just 512 positions apart, the training context of those models.

The same fix worked for Mamba-1, Mamba-2 and GLA, which suggests that the states matter more than any single architecture's internals. State Passing also changes how the state update behaves, so its distribution stays steady after the training context.

## Long-context tasks: BABILong, passkey retrieval and synthetic copying

BABILong tests both common sense understanding and the capture of long-range dependencies in text. The authors evaluated a Mamba-2 780m model under three settings: the official checkpoint, a State Passing post-trained checkpoint, and a TBTT finetuned checkpoint. Both interventions improve the baseline in few-shot and finetuned settings. The models were trained and finetuned on sequences of length 2048, so the evaluation extrapolates far beyond the training context.

Because BABILong needs reasoning over distant tokens, this shows that the gains are not limited to perplexity.

The authors compared zero-shot evaluation, standard finetuning and finetuning with fitted noise, all at training context 2048. Models finetuned with fitted noise handle token relationships much more than 2k positions apart. Finetuning with fitted noise solves passkey sequences at least twice as long as standard finetuning does. The 780m model reaches perfect accuracy on sequences of length 256k.

The authors chose fitted noise because the passkey token distribution is not stationary, so a final state might be a poor initial state. They also add a period after the passkey, since models sometimes repeated the passkey several times in a row.

The synthetic copying task asks a model to copy an arbitrary sequence of tokens. The Mamba-2 model in this test has 45 million parameters, is initialized randomly, and is trained from scratch. Training uses one million sequences of length between 50 and 100, and evaluation uses sequences of length 300.

![Table 1: Length generalization results for the synthetic copying task (Jelassi et al. 2024), which consists of copying an arbitrary sequence of tokens. The mod…](https://refivu.com/papers/f9cd6a9a-de1d-4e94-a91d-e03ad7e1036c/figures/figure-8521277d38cb9b3185094bb959d7ae4c.png?thumb=512)

**Table 1** : Length generalization results for the synthetic copying task (Jelassi et al. 2024), which consists of copying an arbitrary sequence of tokens. The models have 45 million parameters, are initialized randomly and trained on 1 million sequences of length between 50 and 100. They are evaluated on sequences of length 300. The State Passing intervention greatly improves length generalization.

[Original paper](https://arxiv.org/abs/2507.02782v2) · [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)

## How the interventions differ from earlier length generalization fixes

Several earlier Mamba-specific fixes change how the state updates, which means they touch the model's internal computation. Some of these methods update the state only on selected tokens, which shortens the effective context. Others force the model to forget earlier context, or recalibrate the discretization modules of Mamba. Those methods show gains on passkey retrieval and document retrieval, but they change the state update and may be hard to transfer to other architectures.

The authors' interventions don't change the implementation of the architecture, so they apply to several recurrent models.

S. Wang (2024) ties the failure of zero-initialized recurrent models to polynomial extrapolation. That work proposes pre-training with State Passing and TBTT to enable length generalization. Yingfa Chen et al. (2024) show that training on longer sequences enables length generalization. They argue the failure comes from overparametrization for the training context and from the model not learning to forget past tokens.

The authors build on these insights by specifying which state distributions the model must be trained on.

In algorithmic extrapolation, Bansal et al. (2022) proposes Incremental Progress Training, which reuses states from earlier iterations. The algorithmic tasks differ, since there the state should converge to a fixed point. Some of the techniques are still similar, and the authors' unexplored states hypothesis offers a framework that explains why these interventions work.

## Where the results stop

The perplexity evidence comes from text on the Pile, so the authors left other long-range distributions for future work. Random Noise helps the 370m model but doesn't work for the 780m and 1.3b models. Both noise methods depend on the standard deviation, which needs tuning for each model. An excessively high noise level degrades performance, while a standard deviation that is too small doesn't enable length generalization.

The Figure 1 runs post-train Mamba-2 and GLA for 500 steps and Mamba-1 for 1000 steps. The 100-step ablations use a smaller post-training budget, so they aren't directly comparable to the Figure 1 runs. xLSTM doesn't fit the formulation in Equation 1, so the authors leave it outside the scope of this work. The authors hypothesize that xLSTM would show a similar behavior, but they didn't test it.

The copying result comes from one Mamba-2 model with 45 million parameters, trained from scratch. The warm-up comparison is a single Mamba-2 45m run with a context length of 1024. The passkey finetuning used fitted noise, a different intervention from the State Passing used for perplexity. The authors chose fitted noise for passkey because the passkey token distribution isn't stationary, so the final state may be a poor initial state.

## Paper info

- **Authors**: Ruiz, Ricardo Buitrago, Gu, Albert
- **Venue**: arXiv · Preprint
- **First published**: 2025-7-3
- **Added**: 2026-10-30
- **Updated**: 2025-10-12
- **Version**: v2
- **arXiv**: 2507.02782
- **Original paper terms**: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
- **What we show**: Paper info, commentary and permitted figures from the original paper

[Original paper ↗](https://arxiv.org/abs/2507.02782v2) · [PDF ↗](https://arxiv.org/pdf/2507.02782v2)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Abstract

Recently, recurrent models such as state space models and linear attention have become popular due to their linear complexity in the sequence length. Thanks to their recurrent nature, in principle they can process arbitrarily long sequences, but their performance sometimes drops considerably beyond their training context lengths-i.e. they fail to length generalize. In this work, we provide comprehensive empirical and theoretical analysis to support the unexplored states hypothesis, which posits that models fail to length generalize when during training they are only exposed to a limited subset of the distribution of all attainable states (i.e. states that would be attained if the recurrence was applied to long sequences). Furthermore, we investigate simple training interventions that aim to increase the coverage of the states that the model is trained on, e.g. by initializing the state with Gaussian noise or with the final state of a different input sequence. With only 500 post-training steps ($\\sim 0.1\\%$ of the pre-training budget), these interventions enable length generalization for sequences that are orders of magnitude longer than the training context (e.g. $2k\\longrightarrow 128k$) and show improved performance in long context tasks, thus presenting a simple and efficient way to enable robust length generalization in general recurrent models.

## Quote from the paper

Ruiz, Ricardo Buitrago, Gu, Albert (2025). Understanding and Improving Length Generalization in Recurrent Models. arXiv:2507.02782v2. https://arxiv.org/abs/2507.02782v2

**BibTeX**

```
@misc{buitrago2025understanding,
  title = {{Understanding and Improving Length Generalization in Recurrent Models}},
  author = {Ruiz, Ricardo Buitrago and Gu, Albert},
  year = {2025},
  eprint = {2507.02782},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2507.02782v2}
}
```

## Other paper commentary

- [Deep Double Descent: Where Bigger Models and More Data Hurt](https://refivu.com/en/papers/b2896f17-e97a-4348-9222-94e1bc37bc56-deep-double-descent-where-bigger-models-and-more.md)
- [mixup: Beyond Empirical Risk Minimization](https://refivu.com/en/papers/cc1e3261-af7e-47fa-99d8-0806fbe6afcf-mixup-beyond-empirical-risk-minimization.md)
- [Weight Normalization: A Simple Reparameterization to Accelerate Training of Deep Neural Networks](https://refivu.com/en/papers/c1050ccf-ffa6-4158-8fff-24b3a8223df6-weight-normalization-a-simple-reparameterization-to.md)
- [Neural Tangent Kernel: Convergence and Generalization in Neural Networks](https://refivu.com/en/papers/00c81960-6246-4381-80a5-696fbeb26240-neural-tangent-kernel-convergence-and-generalization-in.md)
- [The Lottery Ticket Hypothesis: Finding Sparse, Trainable Neural Networks](https://refivu.com/en/papers/9c072b5c-bd96-4202-8960-653ce6a7f2fb-the-lottery-ticket-hypothesis-finding-sparse-trainable.md)
- [Adam: A Method for Stochastic Optimization](https://refivu.com/en/papers/1e4ce3cf-c297-4440-9078-a86a09a9caca-adam-a-method-for-stochastic-optimization.md)
- [Diffusion Posterior Sampling is Computationally Intractable](https://refivu.com/en/papers/ac02f2c2-04dc-4800-8421-4bd7e87ecef5-diffusion-posterior-sampling-is-computationally-intractable.md)
- [Weight-sparse transformers have interpretable circuits](https://refivu.com/en/papers/57b058ed-824e-41f1-9bf7-37d4673924a5-weight-sparse-transformers-have-interpretable-circuits.md)
- [The Diffusion Duality](https://refivu.com/en/papers/f26ce130-61df-42ce-8b6b-010dee449d4d-the-diffusion-duality.md)
- [What Has a Foundation Model Found? Using Inductive Bias to Probe for World Models](https://refivu.com/en/papers/054ba948-d190-41ad-a02e-59a8b451566a-what-has-a-foundation-model-found-using-inductive.md)
- [MixMin: Finding Data Mixtures via Convex Minimization](https://refivu.com/en/papers/1812a7ac-7070-4e11-93d3-514e69d7cf83-mixmin-finding-data-mixtures-via-convex-minimization.md)
- [On the Computational Complexity of Performative Prediction](https://refivu.com/en/papers/7a4b721a-31e6-4690-bc6d-b8f8286675f0-on-the-computational-complexity-of-performative-prediction.md)

## Terms in this paper

- [Mamba-2](https://refivu.com/en/terms/caf9f0e79adb-mamba-2)
- [total variation](https://refivu.com/en/terms/1be23df5f7af-total-variation)
- [Pile](https://refivu.com/en/terms/52bbdb3a0c68-pile)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/f9cd6a9a-de1d-4e94-a91d-e03ad7e1036c-understanding-and-improving-length-generalization-in)
