Encoder-Decoder Architecture › Information Flow in Encoder-Decoder Models

Michael BrenndoerferUpdated June 20, 20258 min read
Language AI HandbookAll chapters
Part 16 of 67Transformer Architectures
Chapter 113Encoder-Decoder Architecture › Information Flow in Encoder-Decoder Models
Reading section 6 of 13 · Information Flow in Encoder-Decoder Models · 8 minSection 6 of 13 · 8 min

Book contents

Section 6 of 13 · 8 min read

Encoder-Decoder Architecture › Information Flow in Encoder-Decoder ModelsOne section · Sections in this chapter6 / 13 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The encoder-decoder architecture's power comes from a carefully designed asymmetry in how information flows. The encoder and decoder process their sequences differently, and cross-attention creates a one-way bridge between them.

Encoder: Bidirectional Processing

The encoder enjoys complete freedom: every source position can attend to every other source position through bidirectional self-attention. When encoding "The cat sat on the mat," the representation for "cat" incorporates context from both directions: "The" before it and "sat on the mat" after it. This bidirectional view is essential for understanding meaning, since words often depend on both their left and right context for disambiguation.

The encoder runs in one pass, processing all source positions simultaneously (in parallel). By the time the decoder begins, the encoder has built rich, contextualized representations that capture the full meaning of each source word.

Decoder: Causal Self-Attention + Full Cross-Attention

The decoder operates under two different attention regimes simultaneously, so the distinction matters:

Self-attention is causal (masked). When the decoder generates the third target word, it can only attend to the first and second target words, never to future tokens that haven't been generated yet. This respects the autoregressive nature of generation: we can't condition on outputs we haven't produced.

Cross-attention is unrestricted. When generating that same third target word, the decoder can attend to any source position, including positions near the end of the source sequence. There's no masking here because the source is fully available; it was processed by the encoder before generation began.

This asymmetry enables a powerful capability: the decoder can "look ahead" in the source sequence while generating sequentially in the target. When translating "The cat sat" to "Le chat était assis," the decoder can attend to "sat" when generating "Le", which is useful for determining that the sentence structure will require masculine agreement.

Out[15]:
Visualization
Lower triangular matrix showing causal self-attention pattern in decoder.
Decoder self-attention is causal. Each target position can only attend to previous target positions and itself. This prevents information leakage during autoregressive generation.
Full rectangular matrix showing cross-attention from all target to all source positions.
Cross-attention is unrestricted. Each target position can attend to all source positions, enabling the decoder to access relevant source information at any generation step.

Layer-by-Layer Refinement

Both encoder and decoder stack multiple layers, and depth enables increasingly abstract representations. But the pattern of refinement differs between them.

Each encoder layer refines source representations by allowing every position to gather information from all other positions. Early layers might resolve local ambiguities; later layers build higher-level semantic representations. A 6-layer encoder gives each word 6 opportunities to absorb context from the entire source.

Each decoder layer follows the three-step sequence we saw earlier:

  1. Refine target representations based on target context so far (masked self-attention)
  2. Enrich target representations with relevant source information (cross-attention)
  3. Apply nonlinear transformation (FFN)

The critical design choice is that cross-attention happens at every decoder layer, not just once. This means source information can influence target representations repeatedly, at multiple levels of abstraction. Early decoder layers might capture lexical correspondences, such as "chat" attending to "cat." Later decoder layers might capture more abstract semantic relationships, like attending to source context that helps determine verb tense or article agreement.

Out[16]:
Visualization
Heatmap showing cross-attention weights for layer 1.
Cross-attention in Layer 1 (earliest). Shows more diffuse attention as the model gathers broad context.
Heatmap showing cross-attention weights for layer 4.
Cross-attention in Layer 4 (deepest). Shows more focused attention to specific source positions.

To quantify how attention patterns change across layers, we can measure the entropy of each target position's attention distribution. Higher entropy indicates diffuse attention (looking at many source positions), while lower entropy indicates focused attention (concentrating on a few positions).

Out[17]:
Visualization
Line plot showing attention entropy decreasing across layers for different target positions.
Attention entropy across decoder layers for each target position. Early layers tend to have higher entropy (more diffuse attention), while later layers often show lower entropy (more focused on specific source positions). This progression reflects the model building from broad context to precise alignment.