Worked examples — Encoder vs decoder vs encoder-decoder
The parent note told you what the three architectures are. This page makes you compute them. We will hand-crank real numbers through attention masks, softmax, and cross-attention — one scenario at a time — until no case can surprise you.
Everything here rests on one machine you must already picture: the attention score. Before we open the matrix, let's re-earn every symbol.
The scenario matrix
Every situation this topic can hand you is one row below. The examples that follow are labelled with the cell they cover, so by the end no blank cell remains.
| Cell | Scenario class | Architecture | What makes it tricky |
|---|---|---|---|
| A | Bidirectional attention, all-1s mask | Encoder (BERT) | every token sees every token |
| B | Causal attention, lower-triangular mask | Decoder (GPT) | future tokens blocked with |
| C | Zero / degenerate input: sequence length 1 | Decoder | softmax over a single element |
| D | Limiting case: two equal scores | Encoder | ties → uniform split |
| E | Sign / negative scores | Any | negative dot products still valid |
| F | Cross-attention: decoder queries encoder | Encoder-Decoder (T5) | different masks stacked |
| G | Real-world word problem | Encoder-Decoder | translation alignment |
| H | Exam twist: scaling by actually changes the answer | Any | why the is not decoration |
Prerequisites you can revisit: Self-Attention Mechanism, Attention Masking, Cross-Attention, Causal Language Modeling, Masked Language Modeling, Autoregressive Generation.
Example 1 — Cell A: full bidirectional softmax (encoder)
Forecast: which token should token listen to most? Guess before computing.
-
Scale each score by . Why this step? Long vectors make raw dot products large; dividing by keeps softmax out of its flat, gradient-killing zone.
-
No masking added — encoder sees everything, so everywhere. Scores unchanged. Why this step? Cell A is defined by the all-1s mask: nothing is blocked, both past and future keys stay.
-
Exponentiate: , , . Sum . Why this step? Softmax turns any real numbers into positive weights that sum to .
-
Normalise:
Verify: ✓. Highest score () got the biggest weight, and the two equal scores got equal weights — matches the forecast.
Example 2 — Cell B & E: causal mask with a negative score (decoder)
Forecast: token has the biggest raw score — will it dominate?
-
Scale by : before masking. Why this step? Same health reason as before; note negative scores are perfectly legal — a negative dot product just means the query and key arrows point apart.
-
Apply the causal mask. Token may only attend to . So : Why this step? During Autoregressive Generation position does not exist yet when we compute position ; letting it leak would be cheating. This is Attention Masking in action.
-
Exponentiate (and ): , , , . Sum . Why this step? The is the switch: after exponentiation the blocked token contributes exactly .
-
Normalise over the three allowed tokens:
Verify: ✓. Despite token having the largest raw score, it gets weight — the mask overruled it. This is exactly Cell B + Cell E (negative scores handled correctly).
Example 3 — Cell C: degenerate input, sequence length 1
Forecast: with only one thing to look at, what must the weight be?
-
Scale: . Why this step? Consistency — even a lone token goes through the same pipeline.
-
Causal mask: , allowed. . Why this step? The token attends to itself; nothing to block.
-
Softmax over one element: Why this step? A pie with one slice is the whole pie. The value of the score () is irrelevant — this is the degenerate case.
-
Mix: .
Verify: exactly, independent of the score ✓. Sanity: a one-token sequence just passes its own value through, which is why the very first generated token depends only on the prompt embedding.
Example 4 — Cell D: limiting case of two exactly equal scores
Forecast: ties should split how?
-
Equal case, exponentiate: , sum . Why this step? Identical inputs to softmax must give identical outputs — softmax is symmetric.
-
Normalise: Why this step? A perfect tie → an even split. This is the uniform limit.
-
Nudge the second score up by : Why this step? We want the behaviour near the tie, not just at it — softmax is smooth, no jump.
-
Take : . Why this step? Confirms the split is continuous: infinitesimally breaking a tie moves the weight infinitesimally, never discontinuously.
Verify: and the limit equals ✓. No sharp jump — softmax never teleports probability.
Example 5 — Cell H: the exam twist, does change the answer?
Forecast: does dividing both scores by the same leave the weights unchanged? (Trap!)
-
Unscaled softmax: exponentiate , , sum . Why this step? This is what happens if you forget the scale — the gap of becomes enormous after exponentiation.
-
Scaled scores: , . Exponentiate , , sum . Why this step? Dividing by shrinks the gap, and softmax is nonlinear — so the weights genuinely change.
-
Compare: unscaled says "listen 98% to key 1"; scaled says "73%". Very different. Why this step? The exam trap is believing softmax is scale-invariant. It is not: for .
Verify: ✓ and ✓, and the two values differ ✓. Scaling is not decoration — it controls how sharp the attention is.
Example 6 — Cell F: cross-attention, decoder queries the encoder
Forecast: which encoder value dominates the output?
-
Scale by : . Why this step? Cross-attention uses the same score→normalise→mix pipeline; only the source of changes (encoder, not decoder).
-
No causal mask here. The decoder may look at all encoder positions (they are the given source, not future output). across the three. Why this step? This is the crucial difference from Cell B: cross-attention over encoder outputs is bidirectional over the source, even inside a causal decoder.
-
Softmax: , , , sum .
-
Mix the values: Why this step? The output is a weighted blend of encoder information; here and (opposite arrows) cancel in the second coordinate.
Verify: weights sum to : ✓. Output second coordinate ✓. Encoder token dominates as forecast.
Example 7 — Cell G: real-world word problem (translation alignment)
Forecast: a French noun should align to the English noun — verify the numbers agree.
-
Softmax the two scores : , , sum . Why this step? This turns the alignment scores into a soft "who does 'chat' translate?" distribution.
-
Interpret: of attention lands on "cat". Why this step? Machine-translation cross-attention is soft word alignment — the peak reveals the source word being translated.
-
Mix: . Why this step? The output vector is almost purely "cat"'s information, feeding the head that emits "chat".
Verify: ✓; the noun aligns to the noun ✓. Matches human intuition and the parent note's translation example.
Example 8 — Cell B (full picture): computing an autoregressive product probability
Forecast: joint probability of three independent-looking factors — bigger or smaller than any single one?
-
Write the chain rule. Why this step? Causal Language Modeling decomposes a joint distribution into a product of conditionals — that is precisely why the mask must block the future.
-
Multiply: . Why this step? Probabilities in multiplied together shrink — this is why long generations use log-probabilities to avoid underflow.
Verify: ✓, and every factor, as expected for a product of probabilities.
Recall Quick self-test
Why does a blocked (future) token get weight exactly 0? ::: We add to its score; , so its softmax weight is . In a length-1 sequence, what is the self-attention weight? ::: Exactly , regardless of the score — softmax over one element. Does dividing all scores by leave softmax weights unchanged? ::: No — softmax is nonlinear; scaling changes how sharp the distribution is. In cross-attention inside a causal decoder, can the decoder see all encoder positions? ::: Yes — the causal mask only restricts decoder self-attention, not cross-attention over the source. How do two exactly-equal scores split their weight? ::: Evenly (uniform), e.g. for a tie of two.