Same block, two regimes: training parallelism versus inference autoregression
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.
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.
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.
- vLLM (Kwon et al., 2023) is built specifically around the prefill versus decode shape difference; it uses PagedAttention to manage KV cache memory efficiently during decode.
- FlashAttention (Dao et al., 2022) optimizes the training-style attention matmul; FlashDecoding (2023) and FlashAttention v3 (2024) extend the optimization to decode-step shapes.
- Llama 3.1 70B has 80 blocks; during training each forward pass processes a 8192-token sequence in parallel; during decode each step processes a single token with the KV cache holding the prior context.
- Speculative decoding (e.g., Leviathan et al., 2023) exploits the decode-step bandwidth bottleneck: a small draft model proposes multiple tokens, the big model verifies them in one batched forward pass that better amortizes HBM reads.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is the FFN sublayer the dominant cost during prefill but the attention sublayer the dominant cost during long-context decode?
QWhat changes if you train with the same shape pattern as inference (one token at a time)?
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.
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.
Same topic, related formats. Practice these next.