Encoder-Decoder Models Separate Source and Target Streams

An encoder-decoder Transformer contextualizes a source and generates a separate causal target. Trace source encoding, shifted target inputs, cross-attention, and the three distinct attention routes.

An encoder-decoder Transformer maintains two sequences. The encoder turns a source sequence into contextual records. The decoder builds target records causally while consulting those encoder records through cross-attention.

For source length TsT_s and target-input length TtT_t:

Xs:(B,Ts,dmodel)Hs:(B,Ts,dmodel),X_s:(B,T_s,d_{model})\longrightarrow H_s:(B,T_s,d_{model}), Xt:(B,Tt,dmodel)Ht:(B,Tt,dmodel).X_t:(B,T_t,d_{model})\longrightarrow H_t:(B,T_t,d_{model}).

Each decoder layer ordinarily contains causal target self-attention, source-target cross-attention, and an MLP, with residual and normalization details stated separately.

Shift the Target Stream

Suppose source A B C should produce target X Y <eos>. A teacher-forced training example may use:

decoder inputprediction target
<bos>X
<bos> XY
<bos> X Y<eos>

The encoder sees A B C. The decoder never receives the token it is currently asked to predict at the same prediction step. A missing or inconsistent shift can leak the answer while leaving dimensions valid.

Three Routes Serve Different Purposes

  • Encoder self-attention exchanges information within the source.
  • Decoder self-attention exchanges information within the allowed target prefix.
  • Cross-attention lets a target query retrieve from encoded source records.

Removing any one route changes the function. Cross-attention does not replace causal target self-attention: generated target tokens also provide relevant context for later target predictions.

Conditional Generation Is Broader Than Translation

The original Transformer studied translation, and encoder-decoder models are natural when one sequence conditions another. The same information flow can support summarization and other structured source-to-target tasks. Performance still depends on objective, data, model size, decoding, and evaluation.

Q1. Find the decoder input

When the target is X Y <eos>, what target prefix should be available while the model predicts Y under the table's convention?

Answer it first, then check.

Hint
Do not include the token currently being predicted.
Solution
The available target prefix is <bos> X.
Not attempted
Review

Not marked done.

References

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.

LLM PrimerEncoder-Decoder Models Separate Source and Target Streamshttps://llmprimer.com/transformers/transformer-architecture-families/encoder-decoder-models-separate-source-and-target-streams© 2026 LLM Primer