LLM Kitchen
Explanations

Part X · Other kinds of models

Ch 38. Remembering without attention

Reading with a notepad

Attention lets every token look back at every earlier token (Ch 9). A reads one token at a time and keeps a small memory that it updates as it goes. To predict the next token it has only that memory. It’s like reading a book while keeping notes on a single index card.

Attention lets every token look back at every earlier token (Ch 9). A works differently: it reads one token at a time and keeps a small, fixed-size memory, a vector it updates after every token. To predict the next token it has only that memory, never the earlier tokens themselves. It’s like reading a book while keeping notes on a single index card.

Networks like this are much older than transformers.[1][1][1] Below is a tiny one, trained in your browser on the rhymes from Ch 1, like the model in Ch 7. Train it, then slide through the phrase and watch its 32-number memory change with each character:

Try it

A model with a memory

Computed in your browser

Its memory after “humpty d”

What it expects next

The model's top guesses for the next character
Next tokenProbability
c 4.8%
t 4.6%
p 4.3%
o 4.0%
03.4training steps →random guessing

step 0

2,941 weights. However much it has read, its memory is the same 32 numbers.

Before training, the memory changes but means nothing, and the guesses are random. After training, it has learned to store what matters for the next character. After “humpty d” it expects a u: all it has is its memory, and the memory still holds enough of “humpty” to know “dumpty” comes next.

The update, written down

The tiny model’s whole memory update is one line. With ht−1 the 32-number memory, xt the character just read, and learned weights Wh, Wx, Wy and biases b, c:

ht=tanh⁡(Whht−1+Wxxt+b),p(next)=softmax⁡(Wyht+c)

This is the design of Elman’s 1990 networks.[1] The tanh keeps every memory number between −1 and 1, so the memory can’t grow without limit however long the text. Training is the backpropagation from Ch 7, run backwards through the characters one step at a time.

Fixed memory, growing text

The appeal is cost. A transformer keeps something for every token it has read (Ch 26), so its memory grows with the text. A recurrent model’s memory stays the same size:

The appeal is cost. A transformer’s memory grows with the text, because it keeps something for every token so later tokens can look back: the KV cache from Ch 26. A recurrent model’s memory stays the same size however long the text gets:

Try it

Memory as the text grows

Computed in your browser
GPT-2 small, with attention
184,320 numbers
The tiny recurrent model above
32 numbers

So that later tokens can look back without redoing the work, GPT-2 small keeps a key and a value (Ch 9) for every token it has read, in each of its 12 blocks: 2 × 12 × 768 = 18,432 numbers per token, up to its limit of 1,024 tokens. This store is the KV cache (Ch 26). The recurrent model keeps only its memory.

The catch is the index card: whatever the model doesn’t note down is gone for good. Early recurrent networks forgot quickly; the LSTM added gates that learn what to keep.[2] LSTMs were the standard for language until the transformer replaced them.[3] It trained much faster: a recurrent model reads even its training text one token at a time, while a transformer takes in every position at once.[3]

The catch is the index card. Everything the model will ever need must be squeezed into those few numbers as it reads, and whatever it doesn’t note down is gone for good. Early recurrent networks were bad at keeping information for long; the LSTM added gates that learn what to keep and what to forget.[2][2] For years, LSTMs were the standard way to model language, until the transformer, which needs no recurrence at all, replaced them.[3][3]

The main reason was training speed. A recurrent model can’t work out its memory at step 10 before step 9, so even in training, where the whole text is known in advance, it reads one token at a time. A transformer handles every position of a training text at once, which suits GPUs. (Writing a reply still goes one token at a time; it’s training that runs in parallel.) The first transformer set a new record for a single model on English-to-French translation after 3.5 days on eight GPUs, at a small fraction of the training cost of the best earlier models.[3][3]

Why plain recurrent networks forget

To learn from a mistake 20 characters later, the error has to travel back through 20 copies of that update, multiplied each time by Wh and by the slope of tanh, which is never more than 1. Over many steps it usually shrinks to almost nothing, or now and then blows up. The LSTM’s fix was a separate cell carried forward by adding to it rather than squashing it, which its authors called a constant error carousel, with gates that learn when to write to it and when to read from it.[2]

Attention avoids the long chain altogether. For a text of n tokens, a recurrent layer needs n steps one after another, and information from a token n places back passes through n updates to arrive; a self-attention layer needs 1 step, and any earlier token is 1 step away. The price is comparing every pair of tokens, n2 comparisons instead of n updates.[3]

The size difference is large. At its full 1,024 tokens, GPT-2 small keeps 18,432 numbers per token, 18,874,368 in all; the tiny model here keeps 32.

The idea comes back

Recurrent ideas are back in newer designs like Mamba, which learns what to keep as it reads, and whose cost grows only in step with the length of the text.[4] But a fixed memory still struggles to copy long passages word for word, which a transformer does easily.[5]

Recurrent ideas are back in newer designs called state-space models. Mamba, from 2023, updates its fixed-size state in a way that depends on the token it is reading, so it can choose what to keep and what to forget, and its cost grows only in step with the length of the text.[4][4] The fixed memory still has a price: a transformer can copy a long passage it was shown word for word, while a model with a fixed-size state struggles once the passage outgrows its memory.[5][5]

Recipe card

Recap

  • A recurrent model reads one token at a time and carries a fixed-size memory forward.
  • Its memory doesn’t grow with the text, so long texts are cheap, but it has to decide what to forget.
  • Attention keeps every earlier token around, which costs more but lets the model look anything up again.
  • Transformers replaced recurrent models mainly because they train on every position of a text at once.
  • ht=tanh⁡(Whht−1+Wxxt+b); the error fades over many steps, which the LSTM’s added cell avoids.

Next question: everything so far has read text. How can a model that reads tokens look at a picture?

Sources

  1. Jeffrey L. Elman (1990). Finding Structure in Time. Cognitive Science 14(2).
  2. Sepp Hochreiter and Jürgen Schmidhuber (1997). Long Short-Term Memory. Neural Computation 9(8).
  3. Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
  4. Albert Gu and Tri Dao (2023). Mamba: Linear-Time Sequence Modeling with Selective State Spaces. arXiv.
  5. Samy Jelassi et al. (2024). Repeat After Me: Transformers are Better than State Space Models at Copying. ICML 2024.

Sources

  1. Jeffrey L. Elman (1990). Finding Structure in Time. Cognitive Science 14(2).
  2. Sepp Hochreiter and Jürgen Schmidhuber (1997). Long Short-Term Memory. Neural Computation 9(8).
  3. Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
  4. Albert Gu and Tri Dao (2023). Mamba: Linear-Time Sequence Modeling with Selective State Spaces. arXiv.
  5. Samy Jelassi et al. (2024). Repeat After Me: Transformers are Better than State Space Models at Copying. ICML 2024.

Sources

  1. Jeffrey L. Elman (1990). Finding Structure in Time. Cognitive Science 14(2).
  2. Sepp Hochreiter and Jürgen Schmidhuber (1997). Long Short-Term Memory. Neural Computation 9(8).
  3. Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
  4. Albert Gu and Tri Dao (2023). Mamba: Linear-Time Sequence Modeling with Selective State Spaces. arXiv.
  5. Samy Jelassi et al. (2024). Repeat After Me: Transformers are Better than State Space Models at Copying. ICML 2024.