Encoder self-attention versus decoder self-attention, what is the single structural difference at the mask level?
Encoder self-attention is bidirectional (no mask); decoder self-attention is causal (lower-triangular mask). Everything else in the sub-layer is identical.
Picture a room of 10 people each writing a story together. In the encoder room, every person can read every other person's notes before writing their own line, full context. In the decoder room, each person can only read notes from people who finished earlier, no peeking at what comes next. The room layouts are identical, the writers use the same pens and the same paper, only one rule changes: who can read whom. That rule is the causal mask.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
State the one structural difference (the causal mask), describe its shape and placement (lower-triangular, applied before softmax on the score matrix), justify it from autoregressive generation, and close with how FlashAttention exploits it for kernel speedup.
| Component | Encoder self-attention | Decoder self-attention |
|---|---|---|
| W_Q, W_K, W_V projections | d_model x d_model | d_model x d_model |
| Multi-head reshape | Standard split into num_heads | Standard split into num_heads |
| Scale | 1 / sqrt(d_k) | 1 / sqrt(d_k) |
| Causal mask | None | Lower-triangular, scores above diagonal = -inf |
| Softmax + V matmul | Standard | Standard |
| Output projection W_O | d_model x d_model | d_model x d_model |
Real products, models, and research that use this idea.
- BERT (encoder-only): every encoder block uses no causal mask; the bidirectional context is the whole point of the architecture.
- Llama 4 Maverick (decoder-only): every block applies a causal mask to the score matrix; FlashAttention-3 skips the upper-triangular blocks for a 2x kernel speedup.
- T5 (encoder-decoder): the encoder stack uses no mask; the decoder stack applies a causal mask to its self-attention sub-layer.
- Mistral 7B: causal mask plus a sliding window, position i attends only to positions [i - 4096, i] within the sequence.
- PyTorch's `F.scaled_dot_product_attention(..., is_causal=True)`: the canonical API for declaring the causal mask at the kernel level.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would training break if you forgot to apply the causal mask on the decoder?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Listing many architectural differences between encoder and decoder self-attention. The Q, K, V projections, multi-head structure, and output projection are identical; only the mask differs.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.