Notes

Decoder-Only Transformer

The original transformer came in two halves: an encoder that read the input and a decoder that wrote the output. The decoder-only transformer keeps the second half and throws the first away — a stack of identical blocks, each one masked self-attention plus a feed-forward layer, and nothing else.

What decoder-only actually changes

The defining mechanism is the causal mask: a position may attend to itself and everything before it, and to nothing after. There is no cross-attention block, because there is no encoder to attend to — the prompt and the generated continuation are one sequence. That has a concrete consequence for serving. Since earlier positions never see later ones, their keys and values never change, so they are computed once and reused: this is the KV cache, and it is why a decoder-only model generates at a usable speed.

Set against the alternatives
  • Encoder-only stacks, BERT-style, attend in both directions and produce representations — excellent for classification and embeddings, unable to generate.
  • Encoder-decoder stacks such as T5 suit a fixed input-to-output mapping like translation, at the cost of two parameter sets and a stricter task format.
  • Decoder-only gives up a bidirectional view of the input and gets back one stack, one objective and cheap incremental decoding.

At scale that trade settled decisively. Every widely used LLM is a decoder-only stack, and the refinements that followed — grouped-query attention, sparse attention, mixture-of-experts layers — are all modifications inside it rather than departures from it.

A decoder-only transformer is a stack of transformer blocks using masked, causal self-attention, where each position attends only to earlier ones, with no encoder and no cross-attention — prompt and continuation share one sequence. That causality is what makes the KV cache valid, giving cheap incremental generation. It is the architecture behind essentially every modern LLM, trading a bidirectional view of the input for a single stack and one training objective.

The first version of this design had two departments: one to read the incoming sentence, one to write the reply. The decoder-only transformer is what you get when you keep only the writing department and let it read as it goes. Every word it considers may look back at everything written before it, but never ahead — rather like writing a letter without being allowed to peek at your own later paragraphs. That single restriction turns out to be useful. Because the earlier part can never change, the system keeps its working notes instead of re-reading from the top, which is what makes replies arrive quickly.