ConceptsINTERNALS.md

Encoder, decoder, or both

The three shapes of transformer, and why the same attention can read, write, or translate.

  • All three are built from the same attention. They differ in which tokens each token may look at.
  • A bidirectional encoder lets every token look at every token, on both sides. It reads well but cannot write.
  • A decoder-only model lets a token look only at the tokens before it. That is what lets it write one token at a time.
  • An encoder-decoder reads the input both ways, then writes the output while looking back at that input.

Try this: switch between the three kinds and watch which cells in the grid are open for token 3.

Try it

This page needs JavaScript.

What just happened

  1. Each row of the grid is a token that is looking. Each column is a token it might look at. An orange cell means the pair is allowed. A blocked cell gets a score of minus infinity before the softmax, so its attention weight is exactly 0.
  2. In an encoder-only model nothing is blocked, so all pairs are open. The word “” is read with the words after it as well as before it. That is why encoders are good at understanding a text.
  3. In a decoder-only model only the lower triangle is open: of pairs. Token sees tokens, itself and the ones before it. A token never sees the future, so the model can be asked to predict the next token at every position at once, and when it writes, it can do it one token at a time.
  4. An encoder-decoder uses both. The first tokens are the input and read each other freely. Each output token sees the whole input and the outputs before it, of pairs. The training task follows from the mask: guess hidden words, predict the next token, or turn an input into an output.
What is simplified here

In a real encoder-decoder the input goes through its own stack of layers, and each decoder layer has a second attention step, called cross-attention, that looks at the encoder’s final output. The single grid here shows the same permissions in one picture. The page uses six words so the grid fits, where a real model sees thousands of tokens.

Where you meet it

Where it breaks

Why not use the bidirectional one for everything, since it sees more? Try making it write. Every new token changes what the earlier tokens mean, so their vectors cannot be saved, and each step reads the whole text again.

Counted in attention scores for one head in one layer, writing tokens costs scores for the rereading model and for a decoder that keeps a KV cache, about as much. An encoder-decoder with a -token input needs . The decoder has its own cost: each token is read without knowing the words that come after it, so a word at the start of a prompt never benefits from the end of it.

Check yourself

In a decoder-only model, can the vector for the first token change when you add more words after it?

No. The first token sees only itself, so nothing after it can reach it. The same holds for every earlier token when a new one arrives. That is why their keys and values can be cached, and why writing stays cheap.

Why does BERT not write text?

It is trained to fill in hidden words using both sides, with no left-to-right order. Nothing in that training teaches it to continue a text, and every added word would change what the earlier words mean. It can be coaxed to produce text, but it is not shaped for it.

A decoder-only model can translate too, by reading the source as part of its prompt. What does an encoder-decoder add?

A two-way reading of the source: every source word is read with the words on both sides, before any output is written. A decoder-only model reads the source left to right, so early source words are not informed by later ones. In practice large decoder-only models make up for that with size and data.

Used in

Go deeper