Transformer parallelism
A Socratic walk-through of transformer parallelism — reasoned out one step at a time, not lectured.
The question we started with
THE QUESTION #Why did reading every word of a sentence at once beat reading them in order?
Language arrives in order. We speak in order, we read in order, and for most of the 2010s the models that handled language processed it in order too — recurrent networks, taking one word, updating a hidden state, taking the next. It matched the phenomenon so naturally that the design felt almost forced.
Then in 2017 a paper called "Attention Is All You Need" removed the recurrence entirely, processed the whole sentence at once, and won. The strange part is that the new design is, per sentence, more arithmetic, not less. So the interesting question is not "why is it faster?" but something sharper: how can doing more total work be the thing that made larger models possible?
Reasoning it through
REASONING #Separate two quantities that our intuition keeps welding together. One is total work: how many multiplications must happen. The other is depth: how many of those must happen strictly one after another, because each needs the previous one's result.
For a recurrent network reading a sentence of length n, total work is proportional to n, which sounds excellent. But the depth is also n — state 40 cannot be computed until state 39 exists. Now ask the question that decides everything: what does a machine with thousands of parallel arithmetic units do with a chain of 40 dependent steps? Nothing useful. It performs one small step, waits, performs the next. The hardware idles.
Attention makes the opposite trade. Every position attends to every other, so the work grows with n squared — for a sentence of 64 words, roughly four thousand pairwise scores instead of 64 updates. But the sequential depth is one. All those scores depend only on inputs that already exist, so they can be computed simultaneously. And they are not merely parallel in principle: the whole operation is a matrix multiplication, which is precisely the shape modern accelerators are built to execute at their peak rate.
So the trade is: multiply the work by n, divide the depth by n. On hardware whose parallel capacity vastly exceeds what a serial chain can use, that is a superb bargain — and it is a bargain that gets better as chips get wider, which is the direction chips have been getting.
Push on a second consequence, which mattered as much. In a recurrent network, information from word 3 reaches word 60 by passing through 57 successive state updates, each one a lossy squeeze through the same fixed-size vector. Gradients flowing back along that chain shrink or explode; LSTMs and GRUs were built to mitigate this and only partly succeeded. In attention, word 60 reads word 3 directly. The path length between any two positions is constant. Both the forward signal and the training gradient travel one hop instead of fifty-seven — which is why long-range dependencies became learnable rather than merely representable.
Now the objection you should raise: if all positions are processed simultaneously, how does the model know the order of the words? It does not, and that is a real hole in the design. Attention is permutation-equivariant — shuffle the inputs and you shuffle the outputs identically. Order has to be injected separately, as positional encoding: a signal added to or folded into each token's representation that says where it sits. The original paper used fixed sinusoids; today rotary embeddings are common. Order became a piece of content rather than a property of the process, and that substitution is the deepest change the architecture made.
Is the quadratic cost a problem? Yes, eventually. At a few thousand tokens it is comfortably affordable; at a hundred thousand it dominates, and a large research literature — sparse attention, linear approximations, state-space models such as Mamba — exists to attack precisely that term. It is worth being honest that the quadratic cost was never a virtue. It was an acceptable price at the sequence lengths of 2017, and the architecture won because that price bought parallelism at exactly the moment when hardware had parallelism to sell.
The analogy
THE ANALOGY #Think of a relay race against a search party. The relay is fast per runner and unimprovable by adding people: the fourth runner cannot start until the third arrives, so a hundred volunteers change nothing. The search party covering a field can absorb every volunteer you have — the total ground walked is far greater, but the time taken collapses because nobody waits for anybody.
A search party's members work independently and never need each other's findings, whereas attention layers are stacked one on another — the parallelism is within a layer, and a deep model still executes its layers strictly in sequence, so the design removed one serial chain and left another firmly in place.
Clarifying the model
THE MODEL #Three refinements.
First, the parallelism belongs to training and prompt processing, not to generation. When a transformer writes, it produces one token at a time and each depends on the last, so the serial chain that recurrence imposed on reading returns in full for writing. The architecture parallelised the half of the problem where the answer was already known.
Second, "n squared" is worth keeping in perspective. Attention is quadratic in sequence length, but the feed-forward layers — which hold most of a model's parameters — are linear in it and have a large constant. For typical sequence lengths, the feed-forward layers, not attention, consume most of the compute. Attention dominates only when contexts grow long.
Third, correct a tempting inference. Transformers did not beat recurrent networks because attention is a better model of language. Both are universal enough in practice. Transformers won because their computation graph is wide and shallow rather than narrow and deep, and that shape is what turns more GPUs into a better model. The architecture was, in effect, chosen by the hardware — and much of what followed, including the scaling laws that made large models a deliberate engineering programme, rests on that fit.
A picture of it
THE PICTURE #How to readThe horizontal axis is sentence length; the vertical axis counts only the steps that cannot be done at the same time as any other. The rising diagonal is the recurrent design: a longer sentence means a longer chain of waiting, and no amount of hardware shortens it. The flat line along the bottom is attention, whose sequential depth per layer stays at one however long the sequence grows. The picture deliberately does not plot total arithmetic — on that measure the flat line would be the steeply rising one, and the whole point is that the two measures point in opposite directions.
What became clearer
WHAT CLEARED #The transformer traded total arithmetic for sequential depth, and that was the right trade because the hardware had far more parallel capacity than a serial chain could ever use. Doing n-squared work in one step beats doing n work in n steps whenever the machine can absorb the width. The same change shortened every path between two words to a single hop, which is what made long-range dependencies trainable rather than merely possible — and it cost the architecture its native sense of order, which had to be handed back as positional encoding, a piece of content rather than a property of the process.
Where to go next
ONWARD #- Why generation loses this parallelism entirely, and what speculative decoding does about it.
- How rotary positional embeddings encode relative distance rather than absolute position, and why that extrapolates better.
Key terms
TERMS #| Term | What it means |
|---|---|
| Recurrent network | a model that reads a sequence one element at a time, carrying a hidden state forward. |
| Sequential depth | the number of operations that must happen strictly one after another, as distinct from total work. |
| Self-attention | each position computing a weighted combination of all positions, in a single parallel step. |
| Positional encoding | an added signal telling the model where each token sits, since parallel processing discards order. |
| Permutation equivariance | the property that reordering the inputs reorders the outputs identically, which attention has and language does not. |
Every term the collection defines is gathered in the glossary.