Skip to main content
Back

Ruhr University Catches Mamba Silently Failing on Long Sequences

Researchers show Mamba silently tunes itself to training sequence lengths, and a single prepended token can restore length-adaptive behavior.

Pro

  • Mamba silently learns different algorithms for different training sequence lengths rather than one length-invariant strategy.
  • Reconstruction quality collapses when inference length exceeds roughly 4x the training image-token length T_I.
  • Short-context models update state fast but forget; long-context models retain but adapt slowly.
  • Prepending a single token encoding sequence length gives Mamba length-adaptive dynamics and improves in-range performance.
  • State dimensionality had no measurable effect on length generalization, contradicting the Stuffed Mamba hypothesis.
  • Code and Omniglot probing setup available at wiskott-lab/image-reconstruction-mamba.

State space models like Mamba were supposed to fix one of the transformer's most annoying limitations: quadratic scaling with sequence length. But the promise came with an asterisk. When you feed a trained Mamba model sequences longer than what it saw in training, quality collapses. A new paper from researchers at Ruhr University Bochum, Bielefeld, and Royal Holloway pins down why, using a vision task designed specifically to make Mamba's internal behavior visible.

The team, led by Jan Rathjens, trains Mamba to reconstruct handwritten characters from randomly sampled image patches, then peeks at what the model has stored in its hidden state at every point in the sequence. The result is a rare interpretable window into how Mamba actually processes long inputs, and it suggests the community has been thinking about the length generalization problem in the wrong frame.

The mystery Mamba never really solved

Mamba is a general-purpose sequence model that offers competitive performance relative to transformers with linear rather than quadratic cost. That efficiency has made it a favorite target for long-context experiments. But there's a catch: while the original paper reported strong length generalization, subsequent studies have shown that Mamba often fails to generalize to longer sequences once inference sequences exceed training lengths.

Multiple explanations exist in the literature, and they contradict each other. Some blame Mamba's implicit receptive field (DeciMamba, LongMamba). Others blame out-of-distribution drift in the discretization parameter delta_t (MambaExtend, MambaModulation). Others still argue the hidden state is overparameterized and cannot learn to forget (Stuffed Mamba). The diversity of theories is itself a signal that no one really knows what is going on inside.

Turning Mamba into a visual thinker

The clever move here is the task design. Instead of NLP benchmarks where failure modes are hard to see, the authors train Mamba on 128x128 Omniglot character images. They sample overlapping 4x4 patches at random spatial locations, project each into a 14-dimensional embedding, and concatenate normalized (x,y) coordinates to encode position. A separator token marks the end of the image context, followed by query tokens that ask the model to predict image content at arbitrary locations.

Mamba image reconstruction pipeline with image tokens, separator, and query tokens

Because 1024 query tokens are enough to densely cover a 128x128 image, you can literally render what the model thinks the picture looks like at any point mid-sequence. The default configuration uses eight Mamba blocks with model dimension 16 and state size 8. The team also removed the depthwise convolution at the start of each block, since random patch order makes local ordering meaningless.

What the reconstructions reveal

The core finding is uncomfortable: Mamba does not learn a length-agnostic algorithm. It learns different algorithms for different training lengths, and those strategies do not transfer.

Concretely, when the training image-token length is T_I, models perform best around V_I approximately 2 x T_I at inference. Push to V_I approximately 4 x T_I and reconstruction quality collapses below the mean-image baseline. Deeper models improve quality but do not extend the generalization threshold. State size, which prior work blamed as the culprit, had essentially no effect on length generalization in these experiments.

The probing experiments are the most striking part. In one, the authors present a character only in one image quadrant and vary the scan order:

  • A model trained with T_I = 1024 lets the character fade over time as more tokens arrive, and produces blurrier reconstructions when the informative quadrant comes second.
  • A model trained with T_I = 4096 preserves the character longer but produces weaker contrast.
Reconstructions of quadrants under different scanning orders for models trained with different sequence lengths

In a second experiment, the model processes 16,384 tokens of one image then 16,384 of another. The short-context model adapts almost immediately. The long-context model still shows artifacts from the first image thousands of tokens later. In other words, models trained on shorter sequences update their internal state faster; models trained on longer sequences update slower. Mamba tunes its processing speed to the length distribution it saw.

The fix: tell Mamba how long the sequence is

If Mamba is silently optimizing an average over training lengths, the natural fix is to let it condition on the actual length. The authors introduce prepended Mamba, which puts a single token encoding the sequence length at the front of the input, letting the model stash that value in its hidden state and adjust dynamics accordingly.

The results support the hypothesis:

  • Prepended Mamba outperforms the default model across sequence lengths within the training range.
  • The gap is largest when querying already-observed locations, meaning the model uses stored information more effectively.
  • Analysis of ||delta_t||_2 across blocks shows that prepended Mamba's dynamics actually vary with total sequence length, while the default model's stay nearly invariant.
  • A truncated variant trained only on 512 to 1024 tokens improves within that band but degrades outside it faster.
  • An expanded variant trained with T_I = 65536 improves on long sequences at the cost of short ones, exactly as the trade-off theory predicts.

What it means for anyone using Mamba

The paper reframes length generalization as a trade-off problem, not a scaling problem. If you train a Mamba on 4K sequences hoping it will gracefully handle 16K at inference, this work suggests you will get worse quality on both regimes than a model trained with the right length signal.

Practical implications:

  1. If your deployment length is roughly known, prepend a length token or use scaling approaches like MambaExtend or MambaModulation. Even a rough estimate helps.
  2. Training on a wider length range does not come for free. Expect worse performance on the short end.
  3. Do not assume state size increases will fix generalization. In this study it did not.
  4. Techniques that drop tokens for longer context, such as DeciMamba and LongMamba, likely trade short-sequence performance for long-sequence performance rather than eliminate the trade-off.

The limitation is honest and important: prepended Mamba needs the sequence length in advance. For streaming applications where you do not know how long the input will be, that assumption breaks. Transformers, by comparison, generalize far better in this setup, largely because the authors ensured all positional encodings were seen during training, isolating the length generalization variable from the positional encoding one.

Code, training scripts, and hyperparameters are available in the wiskott-lab/image-reconstruction-mamba repository, built on top of Hugging Face Transformers. Models were trained on A100 and H100 GPUs for roughly 16,000 epochs, so reproducing the full study is nontrivial but the setup and probes are straightforward to adapt to other state space architectures.