Sequence Models & Transformers

encoder and decoder stacks

/ en-KOH-der and dee-KOH-der staks /

A Transformer is built from two kinds of layer stacks that do complementary jobs. An encoder stack reads an entire input at once and builds a rich, context-aware understanding of it — every token can see every other token, in both directions. A decoder stack generates an output one token at a time, and crucially each token it produces can only see the tokens that came before it, never the future, so generation stays honest about what is known so far.

The original 2017 design used both together for translation: the encoder digested the source sentence into a set of context-laden vectors, and the decoder, while writing the translation word by word, reached back into those encoder vectors through cross-attention to stay faithful to the source. But the two halves can also live apart. Encoder-only models (the BERT family) are great at understanding tasks — classification, search, answering questions about a given text — because they read everything bidirectionally. Decoder-only models (the GPT family, today's chatbots) are built to generate, so they dominate open-ended writing.

Knowing which stack a model uses tells you a lot about what it is for. An encoder-only model can read and judge but cannot freely generate long text; a decoder-only model can generate fluently but processes its input through the same one-directional, generation-oriented lens. Modern large language models are overwhelmingly decoder-only — a notable shift from the original both-halves design — because that single stack turned out to scale beautifully for the predict-the-next-token objective that powers chat assistants.

A translation model: the encoder reads "I am hungry" all at once and builds context vectors; the decoder writes "J'ai faim" one word at a time, each new word attending both to what it has already written and, via cross-attention, to the encoder's view of the English.

Encoder understands the whole input; decoder writes the output left to right.

"Transformer" is often used loosely to mean "decoder-only chatbot," but the original design had both halves and many useful models are encoder-only. The right stack depends on the job: reading and judging favors encoders; open-ended generating favors decoders.

Also called
encoderdecoderencoder-decoder编码器解码器編碼器解碼器