How a Transformer Works: Step a Sequence Through the Stack
Walk a sequence through all seventeen stages of a transformer block, with the exact parameter count, tensor shape and FLOP cost of each computed from a configuration you set.
A row lookup in the embedding table. No arithmetic happens here at all — this is the largest single weight matrix in most models and it costs nothing to use.
Forward pass over 2,048 tokens: 31.83 TFLOP, of which 3.5% is the quadratic attention term. Raise the sequence length and watch that share move — it is the entire argument for every long-context trick there is.
- Embedding table (vocab × d)
- 524.3 M
- One block
- 218.1 M
- All 32 blocks
- 6.98 B
- Output head
- 524.3 M
- Total
- 8.03 B
- Forward FLOPs — weight matmuls, linear in T
- 30.73 TFLOP
- Forward FLOPs — attention scores and values, T²
- 1.10 TFLOP
- Forward FLOPs — total
- 31.83 TFLOP
- Rough training cost per token (6 × non-embedding params)
- 41.88 GFLOP
What the number is telling you
Two things dominate a transformer and they are not the ones the diagrams emphasise. The first is that the feed-forward block, not attention, holds most of the weights: at a typical 3.5× expansion with SwiGLU it is three matrices of d × ff against attention's four of roughly d × d, which is about two-thirds of every block. When someone says a model is mostly a big lookup table, this is the arithmetic they mean.
The second is that the parameter count and the compute cost do not move together. Step to the score matrix and watch the FLOPs line: it is the only stage with no weights at all, and it is the only one that grows with the square of the sequence. At two thousand tokens it is a rounding error. At a hundred thousand it is most of the bill. Every technique with a name — sliding windows, grouped-query attention, sparse patterns, state-space hybrids — is an attack on that one line, and you can watch each of them work by changing a field above.
What this leaves out is everything that is not a matmul. Memory is the obvious omission: the score matrix is heads × T × T numbers, and on a long sequence that tensor, not the weights, is what exhausts a GPU — which is why FlashAttention, which changes no arithmetic at all, was such a large practical win. Bandwidth is the other. During single-token generation almost nothing here is compute-bound; the GPU spends its time reading weights and the KV cache out of memory, and a FLOP count predicts throughput badly. Treat this page as an answer to "where does the model put its parameters and its arithmetic", not as a latency model.