Same block, two regimes: training parallelism versus inference autoregression
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Walk through how the same transformer block behaves differently during training and during autoregressive inference, without changing any weights.
Training pushes the entire sequence through every block in one forward pass, with a causal mask preventing each position from peeking at future tokens.
Imagine the model is a teacher grading a 1000-word essay. In TRAINING mode, the teacher sees the whole essay at once and grades every word in parallel. They use a piece of cardboard (the causal mask) to cover the next word as they evaluate each one, so the grade for word 50 is fair, but the work happens all together. In INFERENCE mode, the student is WRITING the essay one word at a time. The teacher has to predict each next word as it comes. They keep a notebook of every previous word's worth of context (the KV cache) so they don't have to re-read the whole essay every time. Same teacher, same brain, very different workload. Writing one word at a time is way slower per unit work than grading the whole essay at once.
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.
5 min: training input shape `(B, T, D)` + causal mask + teacher forcing + parallel matmuls + inference input shape `(B, 1, D)` + KV cache + tall-skinny matmuls + prefill versus decode + same weights, different shapes.
| Aspect | Training | Inference (decode step) |
|---|---|---|
| Input shape to block | `(B, T, D)` | `(B, 1, D)` |
| K and V source | Computed fresh from full sequence | Cache + fresh K,V for current token |
| Causal mask | Required (upper-triangular) | Not needed; future tokens absent from cache |
| Attention score shape | `(B, H, T, T)` | `(B, H, 1, t)` tall-skinny |
| FFN matmul shape | `(B, T, D) × (D, 4D)` | `(B, 1, D) × (D, 4D)` |
| GPU bottleneck | Compute (tensor cores) | Memory bandwidth (HBM) |
| Token supervision | Teacher forcing on all positions | Sampled from model's own previous output |
| Backward pass | Yes | No |
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.
Saying the block 'does different things' in training versus inference. The block's weights and computation graph are identical; only the input shapes change.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.