Encoder self-attention versus decoder self-attention, what is the single structural difference at the mask level?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
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.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
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.