Exercises — Encoder vs decoder vs encoder-decoder
This page is a graded ladder. Each rung is harder than the last, and every problem carries a full worked solution hidden inside a collapsible callout so you can quiz yourself. Try each problem cold, then open the solution.
Before we start, one promise: every symbol used below is defined the moment it appears. If a term feels new, it was built for you in the parent note — this page assumes only that you have read it.

Above: the mask grid. A row is the token doing the looking (the query). A column is the token being looked at (the key). A filled cell means "token is allowed to attend to token ". Keep this picture beside you for the whole page.
Level 1 — Recognition
Exercise 1.1
For a 4-token sequence, an encoder uses a bidirectional mask (all cells filled) and a decoder uses a causal mask (a cell is filled only when ). How many filled cells does each mask have? What fraction of the encoder's cells does the decoder keep?
Recall Solution 1.1
The grid is with , so cells total.
- Encoder (bidirectional): every cell filled cells.
- Decoder (causal): cell filled when . Row has exactly filled cells (it can see itself and everything before). So the count is That is the triangular number .
- Fraction kept: .
Look at the right panel of the figure: the decoder mask is exactly the lower triangle (including the diagonal) of the full square.
Exercise 1.2
Match each model to its architecture: BERT, GPT, T5.
Recall Solution 1.2
- BERT → encoder-only (bidirectional; reads and understands).
- GPT → decoder-only (causal; generates left-to-right).
- T5 → encoder-decoder (reads a source two-way, writes a target one-way).
Level 2 — Application
Exercise 2.1
A decoder computes raw attention scores for query token (the third token, 0-indexed... here we use 1-indexed, so is the second token) over a 3-token sequence. The scaled scores are Apply the causal mask by adding (where if , else ), then compute the attention weights via softmax. Give the numbers.
Recall Solution 2.1
Step 1 — what the mask does. For query , cell is a future token (), so . Cells are allowed, .
Masked scores: .
Step 2 — why . Softmax exponentiates, and . So the future token gets weight exactly zero and drops out — the query never leaks information from the future.
Step 3 — softmax over the survivors. The weights over the visible tokens sum to , and the future token is silenced.
Exercise 2.2
An encoder (bidirectional) sees the same three scaled scores for query : , no mask. Compute . Compare to the decoder's answer above and explain the difference in one sentence.
Recall Solution 2.2
No mask, so all three compete: Comparison: the encoder gives token 1 only , while the decoder gave it . Adding the strong future token 3 into the denominator steals probability mass from token 1 — the decoder, blind to the future, hands that mass back to the past.
Level 3 — Analysis
Exercise 3.1
In an encoder-decoder translating a source of length into a target of length , count the attention connections of each type when computing the whole forward pass once (ignore the constant, count query–key pairs):
- Encoder self-attention pairs.
- Decoder causal self-attention pairs.
- Cross-attention pairs (decoder queries over encoder keys).
Recall Solution 3.1
1. Encoder self-attention (Self-Attention Mechanism, bidirectional): every source token attends to every source token pairs.
2. Decoder causal self-attention: target token sees only , so it's the triangular count over : pairs.
3. Cross-attention (Cross-Attention): each of the decoder queries attends to all encoder keys — no mask here, because the entire source is known before generation begins. So pairs.
Total: attention pairs.
Key insight: cross-attention is a full rectangle (), never triangular — the source has no "future" to hide from the target.
Exercise 3.2
Explain, using the mask picture, why a decoder-only model can be trained on a whole sentence in one forward pass (all positions at once) even though at inference it generates one token at a time.
Recall Solution 3.2
Training: you already have the full target sentence. Feed it in. The causal mask guarantees that the prediction at position is computed using only positions — the mask physically blocks positions . So all next-token predictions are computed simultaneously and independently, yet each obeys the "no peeking ahead" rule. This is Causal Language Modeling: one pass, supervised predictions.
Inference: you do not have the future tokens — they are what you are trying to produce. So you must loop: predict , append it, predict , and so on. This is Autoregressive Generation.
The mask is what makes these two regimes consistent: training with the mask simulates the information-starvation of inference, so the model never learns to depend on tokens it won't have.
Level 4 — Synthesis
Exercise 4.1
You must build a system that reads a legal contract and answers "Is clause 7 enforceable? (yes/no)". The answer is always contained in the input. Which of the three architectures is most efficient, and why is a decoder-only model wasteful here?
Recall Solution 4.1
Choice: encoder-only (BERT-style).
Why. The task is understanding, not generating — the output is a single label, and the evidence lives entirely inside the input. Bidirectional attention lets "clause 7" attend simultaneously to text before and after it, which matters because enforceability may hinge on a definition that appears later in the contract. A Masked Language Modeling-pretrained encoder is designed exactly for this two-way evidence gathering.
Why a decoder is wasteful. A decoder-only model would read the contract left-to-right under a causal mask, so early tokens could never see later clauses — it would have to re-encode the relevant context by generating it, spending compute on autoregressive generation you don't need. You'd pay for a generation machine to do a classification job. Even if it worked, throwing away the future context is a strict information handicap.
Exercise 4.2
Design a training recipe (in words) for an English→French translator using an encoder-decoder, and state exactly which mask each of the three attention blocks uses.
Recall Solution 4.2
Data: aligned pairs (English source , French target ).
Forward pass, per block:
- Encoder self-attention — mask = all 1s (bidirectional). The full English sentence is known, so every source word gets full context. Uses Positional Encoding so word order survives.
- Decoder self-attention — mask = lower-triangular causal. The French target is fed in shifted-right (teacher forcing), and position may see only , matching inference.
- Cross-attention — mask = all 1s ( rectangle). Each French query reads the entire encoded English.
Loss: cross-entropy of summed over all target positions, computed in a single pass thanks to the causal mask. This is the seq2seq objective. Multi-Head Attention runs all of this in parallel heads so different heads can specialise (e.g. one head aligns articles, another aligns verbs).
Level 5 — Mastery
Exercise 5.1
A decoder generates over a vocabulary and outputs, for a 3-token sequence, the conditional probabilities Compute the full joint probability and its per-token perplexity. (Perplexity ; it measures "on average, how many equally-likely choices the model felt it faced" — lower is more confident.)
Recall Solution 5.1
Step 1 — joint via chain rule. A decoder factorises the sequence probability as a product (Causal Language Modeling): Why a product? The causal mask means each factor conditions only on the past, and the chain rule of probability turns the joint into exactly this ordered product.
Step 2 — average log-likelihood. Step 3 — perplexity. So on average the model behaved as if choosing among about equally likely tokens per step.
Exercise 5.2
Two heads in a Multi-Head Attention cross-attention layer produce, for one French query token, these attention weight vectors over 4 English source tokens:
- Head A:
- Head B:
Compute the Shannon entropy (bits) of each head's distribution and interpret: which head is "sharply aligned" to one source word and which is "spread out"?
Recall Solution 5.2
Entropy measures how concentrated a distribution is: bits = all mass on one option, bits = perfectly uniform over 4.
Head A: , so . , so , times 3 .
Head B (uniform):
Interpretation: Head B is at the maximum ( bits) — perfectly spread, no alignment preference. Head A ( bits) is much lower — it has locked onto the first English word (weight ), a sharp alignment typical of a head that has learned word-to-word correspondence. Different heads specialising this way is precisely why multi-head attention beats a single averaged head.
Recall Quick self-test (reveal answers)
Encoder mask on 4 tokens has how many filled cells? ::: 16 (all of ). Decoder mask on 4 tokens has how many filled cells? ::: 10 (the triangular number ). Cross-attention on source , target has how many pairs? ::: 20 (a full rectangle). Which mask does cross-attention use? ::: All 1s — the whole source is known, nothing to hide. Joint prob of conditionals? ::: (chain-rule product).