Why the loop cannot be parallelized
Generation is a strict dependency chain. To sample token n+1 you need the logits at position n, which need the hidden state at position n, which needs token n to have already been sampled. There is no reordering that breaks this.
The practical consequence is that generation latency has a floor set by the number of sequential model invocations, not by total arithmetic. If one forward pass takes 5 ms, then 500 tokens take at least 2.5 seconds. Buying a GPU with twice the FLOPs does not help if each pass is waiting on memory. Buying two GPUs does not help either, unless you split each pass across them (Module 9) — you cannot run pass n and pass n+1 concurrently.
This is what makes LLM serving strange compared to most inference workloads. An image classifier is one forward pass; you scale it by adding replicas. A language model is N forward passes with a serial dependency, and the only ways out are:
- Make each pass cheaper — quantization, smaller models, better kernels (Modules 6, 7).
- Do more per pass — batching, so one weight load serves many sequences (Module 5).
- Take more than one token per pass — speculative decoding, the only technique that attacks the chain length itself (Module 8).
Category 3 is worth flagging now because it is the only one that shortens the dependency chain rather than making each link cheaper. That is why it gets a whole module and why people find it so satisfying.
One caveat on "inherently sequential": it is true for a fixed model and a fixed output. It is not a law of nature. Diffusion language models and other non-autoregressive approaches sidestep it entirely, at a quality cost that has so far kept them out of production for general text.
prompt "the cat sat"
|
v
[ PREFILL ] 3 positions in one pass --> " on"
|
v
[ DECODE ] 1 position --> " the" pass 2
[ DECODE ] 1 position --> " mat" pass 3
[ DECODE ] 1 position --> "." pass 4
[ DECODE ] 1 position --> <eos> pass 5
4 sequential passes for 4 tokens. Unavoidable.
REMEMBERToken n+1 is a function of token n, so the dependency chain has length N and no amount of hardware shortens it.