Repeat After Me: Transformers are Better than State Space Models at Copying
- Publication
- ICML 2024Conference
- First submitted to arXiv
- arXiv:2402.01032
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{mambarepeatafterme2024,
title = {{Repeat After Me: Transformers are Better than State Space Models at Copying}},
author = {Samy Jelassi and David Brandfonbrener and Sham M. Kakade and Eran Malach},
year = {2024},
eprint = {2402.01032},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2402.01032}
}
Two months after Mamba came out, Samy Jelassi, David Brandfonbrener, Sham M. Kakade, and Eran Malach at Harvard uploaded this paper. The title states outright that Transformers beat state space models when it comes to copying.
Copying sounds like a dull skill. But when a reader hands a model a document and asks it to find a date, a name, or a line of code, what the model is doing is lifting a short stretch of the original text out of its context. If it can't do that well, a low perplexity won't help.
Defining the Problem
The authors discuss S4, Mamba, traditional RNNs, and linear attention and parallel RNNs (both trainable in parallel) together as one broad class of models. No matter how many tokens they have processed, all of them rely only on a state that is fixed in size and independent of sequence length. The authors call this class GSSMs (generalized state space models).
The selling point of GSSMs is that predicting each token takes only memory. A Transformer, by contrast, has to keep every token it has seen in the KV cache, which grows with length.
The authors want to know whether saving that memory comes at a price, and if it does, what that price is.
They chose the simplest task of all, copying, to test this.
Solving the Problem
The Copy Task
The task format is:
<BOS> x1 x2 ... xL <COPY>
After seeing <COPY>, the model must output through verbatim. Scoring uses string-level accuracy: a string counts as correct only if all of it is right.
Too Small a State Can't Copy It All
Start with the GSSM side. To reproduce a whole string unchanged, you first have to remember the whole string. Where? The only place available is that fixed-size state.
The authors write the number of distinct values the state can take as ; its memory capacity in bits is . For strings whose characters are drawn uniformly from options and whose length is , there are possible inputs, but the state can distinguish at most of them. The error rate here is the probability that the whole string is not copied exactly. The lower bound given by Theorem 2.7 is:
(The main text of the paper prints a strict inequality, while the counting proof in Appendix D yields a non-strict one (greater than or equal to); we follow the proof here.)
In a form that's easier to remember (Corollary 2.8), the error rate exceeds one half whenever the state has fewer than bits.
A few numbers give a feel for it. A string of length 300 over 26 letters carries about bits; with a 50,000-token vocabulary and length 2,000, about 31,219 bits. The authors give an example of their own: 1,000 32-bit floats hold enough information for at least 2,000 tokens.
So this theoretical lower bound is actually quite loose. A real Mamba state is far larger than 1,000 numbers, and the 1,410 bits needed to copy 300 English letters are no problem at all. The authors also concede that the theory only shows “a state that can't hold the string can't copy all of it.” Whether a model with enough room will learn to store the string in its state is a question for experiments.
Transformers Find Their Place with n-grams
On the other side, the authors construct a two-block Transformer and prove that the length of the strings it can copy can grow exponentially with the model dimension (Theorem 2.3; the introduction phrases it in terms of the number of attention heads). The error is governed by the probability that the string contains a repeated n-gram, which is small for uniformly random strings.
Its method is a lot like how a person copies a long passage:
- Look at the last few characters you just wrote, say the last 3.
- Go back to the original and find where those 3 characters appear.
- Write down the next character after that position.
This is an n-gram lookup. Attention is a natural fit for “comparing the current fragment against every earlier position.” To make an attention head look only at the most recent tokens and assemble an n-gram hash from them, the authors design a hard-cutoff version of the ALiBi positional bias, called Hard-ALiBi, which sets the bias to beyond positions and to 0 within them.
When the original contains two identical n-grams followed by different characters, this method fails, because the model can't tell which one to continue copying from. In uniformly random strings, the probability of such a repeat falls exponentially with (Lemma 2.4).
Discussion
Synthetic Data: Learning Fast, Generalizing Far

Start with the left panel. All models are trained from scratch: the Transformers and Mamba have about 160 million parameters, while the LSTM, which trained unstably at larger sizes, uses about 40 million. Strings are at most 300 characters long, drawn from the 26 letters of the English alphabet.
All four Transformers learn the task within 100,000 examples; Mamba needs around two million. The authors point out that the Transformers need about 100 times fewer examples. The LSTM doesn't learn it at all within this number of examples.
The middle panel tests length generalization: train only on strings up to length 50, then test on longer ones. As soon as they pass the training length, Mamba and LSTM quickly drop to 0. The Transformers vary with their positional encoding, with the RoPE version also falling off fast, NoPE and ALiBi holding out longer, and the Hard-ALiBi version still nearly perfect at 1,000.
The authors also confirm that the Transformers really are doing n-gram lookups. When two identical n-grams are deliberately planted in the string, repeats of length 4 or less have almost no effect, while from length 5 on the accuracy starts to fall, matching the behavior of a perfect 5-gram model.
Tell It What to Look For First, and It's Fine
There is also a controlled comparison. The task gives the model a string plus one of its n-grams as a key, and asks it to output the tokens that follow the key. The authors built two versions:
Both versions train on strings up to length 30 and are tested up to length 100:
- Key at the end (suffix key): the model only learns what to look for after reading the whole string. The Transformers still do reasonably well out to length 100, while GSSMs fail as soon as they leave the training length.
- Key at the start (prefix key): the model knows from the outset what to look for. While reading, it only has to watch for that key and, once it appears, note the next few characters. This time the GSSMs stay perfect all the way, even beating the NoPE and ALiBi Transformers.
The only difference is when the question shows up.
What a fixed-size state does well is “summarizing as it reads.” If it knows in advance what to keep, it can throw away whatever is irrelevant; if the question only appears after reading, it has to hold on to everything that might be needed. Once the information exceeds the state's capacity, there's no way to guarantee it can all be recovered.
Pretrained Models: The Phone Book
The experiments so far all used small models trained from scratch. Next, the authors compare off-the-shelf pretrained models: Pythia (Transformer) from 410M to 2.8B, against Mamba from 360M to 2.8B. Both were trained on the Pile with the same tokenizer, so the main difference is the architecture. At the same scale, Mamba even has slightly lower perplexity on the Pile.
The best-known result is the phone-book test in the right panel. The prompt contains a randomly generated phone book, with lines like this:
John Powell: 609-323-7777
Two example questions with answers follow, and then the model is asked for one person's number. The longer the phone book, the faster Mamba's accuracy falls; at around 70 entries, the smallest Pythia, 410M, already beats the largest Mamba, 2.8B.

Three other tests point the same way:
- Copying natural language: take a passage from C4, put it in the prompt twice, then add the first word of a third copy and ask the model to continue. Even the smallest Pythia beats the largest Mamba by a wide margin.
- Copying shuffled words: natural language can be compressed, so shuffling the word order makes copying harder. Both sides get worse, Mamba more so, and the largest Mamba is already at 0 by 300 tokens. This fits the inference that “a fixed state can only cope by compressing.”
- SQuAD question answering: 2.8B against 2.8B, the two are about even on short passages; the longer the passage, the faster Mamba's F1 falls.
What This Paper Leaves Open
In the related-work section, the authors also mention examples pointing the other way. Some tasks only require tracking a very small state. In flip-flop language modeling, for example, LSTMs generalize easily while Transformers struggle; for very long sequences like DNA, a fixed state is also more practical.
So this paper is about memory-intensive tasks. It doesn't claim GSSMs are necessarily worse at other tasks, nor does it test what happens when the two kinds of layers are mixed.
Conclusion
This paper turns “a fixed-size state has a cost” from an intuition into a measurable result:
- In theory, the length a two-block Transformer can copy can grow exponentially with model dimension; when a GSSM's state has fewer than bits, the probability of failing to copy a uniformly random string exactly exceeds one half.
- In practice, the limit often shows up earlier. Even when the state is big enough, Mamba has a hard time learning to store the whole string: it needs 100 times more training examples and fails once the length changes.
- Pretrained models behave the same way. Despite slightly lower perplexity, Mamba loses on phone-book lookup, copying, and long-passage question answering to Pythia models of the same size, and even to much smaller ones.
The “know what to look for first, and it's fine” comparison shows what a fixed state is good at and when it falls short, and it gives a very direct reason for “pairing state models with attention that can look back and find things.”