# Why Did Transformers Replace RNNs and LSTMs?

> Why transformers took over from RNNs and LSTMs for language work, what they cost, and how to explain the switch for your AI interview.

**In short:** Transformers replaced RNNs and LSTMs because they handle all the words in a passage at the same time during training, instead of one after another. That makes training faster on GPUs, and attention links two distant words in one step.

AI and LLM interview question Q042, Transformer architecture. Intermediate, very common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/why-transformers-replaced-rnns-and-lstms

## Say it in 30 seconds

RNNs and LSTMs read text one word at a time, so each step waits for the one before. That is slow on GPUs, and in a long document the early details fade. Transformers use attention, so every word can look at every other word in one step, and training runs in parallel. The price is that attention gets costlier as the text grows, so I would name that trade-off too.

## Say it in 2 minutes

Before 2017, most translation systems read text the way you read aloud: one word at a time, from left to right. An RNN, which stands for recurrent neural network, keeps a running memory and updates it at every word. An LSTM is a kind of RNN with gates that decide what to keep and what to drop, so it holds on to more. Both are slow to train, because each step has to wait for the one before it.

There are two problems. First, a GPU is built to do many things at once, and a chain of steps that must run in order cannot use that power well. Second, learning across a long text is hard. The learning signal has to travel back through many steps, and it gets weaker and can vanish. So a clause near the start of a long contract can be lost by the time the model reaches the end.

A transformer uses attention instead. Each word looks at every other word in the passage in one step, and the model learns which ones matter. All the words are handled together, so training runs in parallel. Google reported in 2017 that the Transformer could speed up training by up to an order of magnitude, and it beat the recurrent models on translation benchmarks.

The trade-off is cost. Attention compares every word with every other word, so the work grows fast as the text gets longer. A model can only look back over a limited amount of text, its context window. Generating an answer still produces one token at a time, so the parallel gain mostly helps training. So in an interview I would say transformers won on parallel training and direct links, and that they pay for it on long inputs.

## What are RNNs, LSTMs and transformers, in plain words?

Picture a claims adjuster at an insurer reading a 15-page policy. One way is to read page by page, keeping a sticky note of what matters so far. The note gets crowded, and details from page 1 are smudged by page 15. The other way is to spread the whole policy on a desk and put any two clauses side by side. That is the difference between the two kinds of model.

An RNN (recurrent neural network) reads one word at a time and carries a running memory forward. An LSTM (long short-term memory) is an RNN with gates that control what it keeps. A transformer reads the whole passage and uses attention, so each word can look straight at the others.

Take a real case. An insurer's assistant has to answer a customer's question about a fee. The fee rule is in clause 1, and the answer depends on it in clause 40. The model has to connect two places that sit far apart in the text.

- **RNN:** A network that reads a sequence one step at a time and carries a running memory forward.
- **LSTM:** An RNN with gates that control what it keeps and drops, so it holds on to more.
- **Transformer:** A network built on attention, with no step-by-step loop over the words.
- **Attention:** A way for each word to weigh how much every other word matters to it.
- **Parallel:** Doing many things at the same time, which GPUs are built for.

## Why was reading one word at a time the problem?

Before 2017, RNNs were the usual choice for translation and other language tasks. The Google team that introduced the Transformer explained the catch, and the paper's authors gave the same reason.

> "The sequential nature of RNNs also makes it more difficult to fully take advantage of modern fast computing devices such as TPUs and GPUs, which excel at parallel and not sequential processing." (Google Research, 2017)
>
> "This inherently sequential nature precludes parallelization within training examples" (Vaswani and colleagues, 2017)

The first problem is distance. Reading one word at a time forces many steps to link words far apart, and each step can lose a little of the earlier detail. The second is hardware. A GPU (the graphics chip that does many sums at once) is built for work that runs side by side, and a chain of steps has to run in order.

*Diagram, Linking clause 1 to clause 40: Clause 1 sets the fee and clause 40 uses it. An RNN passes the fee along token by token, so the link is long and weak. An attention layer lets clause 40 look straight at the fee in one step.*
1. The fee rule goes into the RNN at token 12.
2. It has to travel 3,976 steps, and the detail can fade on the way.
3. The same rule goes into one attention layer.
4. Clause 40 looks straight at the fee rule and answers.

Training has a third snag. The signal that says the model was wrong has to travel back through every step, and over long stretches it fades. In 2012, Pascanu, Mikolov and Bengio analysed the vanishing and exploding gradient problems (the learning signal either fades to almost nothing or blows up), which Bengio and colleagues had detailed in 1994.

Put the policy in numbers. Say it is about 4,000 tokens long (a token is a short piece of text, often part of a word). The fee rule sits at token 12 and clause 40 sits at token 3,988. An RNN must pass the fee along 3,976 steps, while an attention layer links the two in one step. Here it is in plain Python. The numbers are fixed, so it shows the arithmetic, not a real model:

```python
# Imagine a 15-page policy of about 4,000 tokens (a made-up size). Clause 1 sets the fee, clause 40 uses it.
# How many steps must the fee rule travel to reach clause 40?
FEE_AT = 12
CLAUSE_40_AT = 3988

rnn_steps = CLAUSE_40_AT - FEE_AT  # in the simplest RNN, one token at a time
attention_steps = 1                # one attention layer links any two positions

print(f"Tokens between the two clauses: {CLAUSE_40_AT - FEE_AT}")
print(f"RNN steps to carry the fee rule: {rnn_steps}")
print(f"Attention steps to link them: {attention_steps}")
for gap in (10, 500, 3990):
    print(f"Gap {gap:>5}: RNN {gap:>5} steps, attention {attention_steps} step")
```

Output:

```text
Tokens between the two clauses: 3976
RNN steps to carry the fee rule: 3976
Attention steps to link them: 1
Gap    10: RNN    10 steps, attention 1 step
Gap   500: RNN   500 steps, attention 1 step
Gap  3990: RNN  3990 steps, attention 1 step
```

The numbers are made up, but the shape is real: the gap in steps is the problem attention removes. A production model has many layers and far more going on, so keep the example to the idea, not the exact count.

## How do transformers fix this, and what do they cost?

Transformers drop the loop. Each layer computes attention for all positions at once, so the words of a passage are handled together and any two words are linked in one step. The 2017 paper's abstract says the model is based solely on attention and drops recurrence and convolutions entirely.

- **RNN: One word after another.** Each step waits for the last, and long links need many steps.
- **Transformer: All words together.** Attention links any two words in one step, and training runs in parallel.
- **The cost: Attention grows fast.** Every word is compared with every other, so long texts need much more work.

*Diagram, Training in order or all at once: The same passage can be trained two ways. The RNN takes its steps in order, while the transformer hands all the words to the GPU together. Google reported in 2017 that this could speed training up by up to an order of magnitude (about ten times).*
1. The RNN reads each passage one word at a time.
2. Each step has to wait for the one before, so training is slow.
3. The transformer hands all the words of a passage to the GPU together.
4. The GPU works on them in parallel, so training finishes sooner.

The idea of attention came first. In 2014, Bahdanau and colleagues suspected that squeezing a whole sentence into one fixed-size list of numbers was a bottleneck, so they let the model search the source sentence for the parts it needed. Later models such as BERT, a well-known language model, are built on the transformer design.

They replaced RNNs for two reasons: they train faster on modern chips, and they link distant words more directly. The price is a cost that grows with the length of the text. Generating an answer still produces one token at a time, so the parallel gain mostly helps training.

> **Tip:** Say the trade-off before the interviewer asks. Transformers win on parallel training and direct links, and they pay for it on long inputs.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] RNNs and LSTMs read one step at a time, so training cannot run in parallel.
- [ ] Long-distance links need many steps, and the learning signal can vanish.
- [ ] Attention lets each word look at every other word in one step.
- [ ] Transformers train in parallel on GPUs, so training finishes sooner.
- [ ] The trade-off: attention cost grows fast with length, and the look-back is limited.
- [ ] An honest framing: a design change with costs and limits.

**Common traps**

- Trap: "Transformers are just a bigger LSTM." Better: They are a different design. No recurrence, and attention does the linking.
- Trap: "Transformers have no memory limit, so they never forget." Better: They can only look back over a limited amount of text, its context window, and attention cost grows with length.
- Trap: "LSTMs are useless on long text." Better: LSTMs hold on to more than plain RNNs, but they still pass through every step in order.

## Can you spot the wrong line?

**Spot the wrong line:** A teammate's slide for the interview says this. Which line is wrong?

A. Attention lets any word look at any other word in one step.
B. A recurrent model reads one word at a time, so its steps wait for each other.
C. Transformers have no limit on how much text they can look back on, so attention costs nothing extra.
D. Transformers train in parallel, which suits GPUs.

**Answer:** C. Attention is not free. Its work grows fast as the text gets longer, and the model can only look back over a limited amount of text. The other three lines are right.

**Quick check:** A fraud team scores an endless stream of card swipes on a small device, one reading at a time, with a few milliseconds per reading. Which is the strongest reason to consider a recurrent model?

A. Recurrent models are always more accurate than transformers.
B. A recurrent model keeps a fixed-size state and updates it with each reading, so each step costs about the same.
C. Recurrent models never need training data.
D. Transformers need the whole stream in memory before they can start.

**Answer:** B. The state stays one size, so each new reading costs about the same. A transformer only sees a limited window of recent text, so its design does not suit a stream that never ends.

## Can you answer these from memory?

**Flash cards**

- **Why are RNNs slow to train?** They read one word at a time, so each step waits for the one before.
- **What is an LSTM?** An RNN with gates that decide what to keep and what to drop.
- **What does attention do?** It lets each word look at every other word in one step.
- **Why does a fading signal hurt RNNs?** The learning signal fades over many steps, so distant clauses get lost.
- **What did the Transformer change?** It dropped recurrence and uses attention only, so the words train in parallel.
- **What is the trade-off?** Attention cost grows fast with text length, and the look-back is limited.

## Likely follow-up questions

- Q043 What is self-attention? https://designervenkat.online/interview/ai/what-is-self-attention
- Q044 What are queries, keys and values? https://designervenkat.online/interview/ai/what-are-queries-keys-and-values
- Q046 What is multi-head attention? https://designervenkat.online/interview/ai/what-is-multi-head-attention
- Q047 What is positional encoding, and why is it needed? https://designervenkat.online/interview/ai/what-is-positional-encoding
- Q049 Encoder-only vs decoder-only vs encoder-decoder? https://designervenkat.online/interview/ai/encoder-only-vs-decoder-only-vs-encoder-decoder

## Learn it in depth

- [Transformers](https://designervenkat.online/learn/machine-learning/transformers)
- [Recurrent Neural Networks](https://designervenkat.online/learn/machine-learning/recurrent-neural-networks)
- [LSTM](https://designervenkat.online/learn/machine-learning/lstm)
