Skip to content
mlmentorship

Encoder-decoder architectures

An encoder summarizes the input into a representation; a decoder generates the output conditioned on it. The structure behind translation, T5, summarization, and many multimodal models.

Published · 5 min read ·Core ·Intermediate
Reading depth

Summary

An attention-based autoregressive encoder-decoder routes source representations and a growing target prefix through different attention paths. Its source memory can remain fixed during generation while target state grows; this requires fixed source input and parameters. Other encoder-decoder systems, including nonautoregressive decoders, need not share this factorization.

The structure

For one decoder layer, let and use:

Source K/V projections belong to that layer; different layers need not share them. Target self-attention reads the causal target prefix, whereas cross-attention reads the permitted source memory. Padding and source-availability masks remain separate from the target causal mask.

Consider three source positions producing , then output token y1 followed by EOS. One encoder pass produces H and the layer’s source K/V. BOS supplies the first incremental target query: three source score pairs precede y1. The next query, after appending y1, contributes three more pairs before EOS. Total: one encoder pass, two incremental decoder steps, and six cross-score pairs per head and layer.

Source and target state

The source memory stays fixed while the generated prefix grows

Reuse source memory while the generated prefix growsThe encoder source key/value memory stays fixed. Decoder prefixes and their own caches grow across generation steps.Keep the source memory; grow thedecoder prefixKVBOSKVBOSAKVBOSABSame source identity; prefixes BOS,BOS A, BOS A B.
Derivation
Three growing prefixes share one fixed encoder memoryOne encoder-memory block feeds three successive decoder iterations. Prefixes grow from start to start-A to start-A-B. Cross-attention source K/V remain fixed while the causal decoder prefix and its cache grow. The shared input lines distinguish reuse from re-encoding.Encode the complete source ONCEcross-attention K/V stay fixeddecode 1: <start>decode 2: <start> Adecode 3: <start> A BThe output prefix and decoder cache grow;source K/V do not.
Each decode step reads the same encoded source. Only the output prefix and decoder-side cache grow. Fixed source K/V do not mean fixed decoder state.
Check: Which representation remains fixed while the generated prefix grows?
Read it this way: The encoder source key/value memory stays fixed. Decoder prefixes and their own caches grow across generation steps.
Which inventory grows between step one and step three?

Switch to Teacher-Forced Scoring

New task: Score observed labels y1, EOS rather than sampling them.

Picture and change: Decoder inputs are BOS, y1; labels are y1, EOS. Suitable causal-transformer implementations evaluate both target positions in parallel, with source access retained except padding.

Check: The first prediction cannot read the second input position containing y1. Two target positions still make six source score pairs. Unshifted inputs y1, EOS would leak each answer through its own position despite a causal mask.

Use when: Computing training loss or conditional sequence likelihood.

Change the Source

New task: A fourth token arrives for a bidirectional encoder.

Picture and change: Recompute or explicitly invalidate affected source encodings and derived caches; older source states can change too.

Check: New H has shape and four cross keys, not three old keys plus an independently encoded token. Previously decoded target states may also be incompatible with the changed source.

Use when: The input changes; streaming needs a separately specified information-flow and cache policy.

Cross-attention complexity

For source length and evaluated target length , dense cross-attention makes score pairs per head and layer. Score and value arithmetic also depend on their dimensions; projection, encoder, and target-self-attention costs are additional. The incremental trace evaluates only new target queries; repeatedly recomputing the full prefix performs different work.

Encoder-only vs. decoder-only vs. encoder-decoder

Model classUse casesExamples
Encoder-onlyEmbeddings, classification, retrievalBERT, RoBERTa, sentence-T5
Decoder-onlyGeneration, chat, codeGPT-2/3/4, Llama, Mistral, Claude
Encoder-decoderTranslation, summarization, structured outputT5, BART, mT5, FLAN-T5

Why decoder-only models took over chat

The retained historical heading is not an architectural superiority theorem. Compare training recipe, source/target lengths, reuse, quality, and deployment costs rather than asserting that a clear split guarantees victory.

Variants and their distinctions

  • Original transformer (Vaswani 2017): encoder-decoder for NMT.
  • BERT: encoder-only, masked-language-model objective.
  • GPT: decoder-only, autoregressive next-token.
  • T5 (Raffel 2019): encoder-decoder, span-corruption objective; everything is text-to-text.
  • BART: encoder-decoder, denoising autoencoder.
  • FLAN-T5: T5 + instruction fine-tuning.

Common pitfalls

  • Unshifted targets: causality alone does not hide the current input token.
  • Wrong cache lifetime: source memory is reusable only under its fixed-input/parameter contract.
  • Universal dominance: neither a separate encoder nor a single stack guarantees better cost or quality.