L1 Beginner · Module 1: The LLM forward pass, for inference engineers
1.2Inside a transformer layer
A decoder-only LLM is one layer design repeated 24 to 80 times. Learn its blocks once, and you can work out a model's parameter count, its FLOPs per token, and which parts of it are expensive to run.
By the end you will be able to
- ✓Name the blocks of a pre-norm decoder layer and the order data flows through them
- ✓Explain the residual stream and why layers add rather than overwrite
- ✓Count a layer's parameters from its config and split them between attention and MLP
- ✓Use the 2N rule to estimate FLOPs per token
After the embedding lookup (1.1), the residual stream passes through identical layers. Each layer has two sub-blocks, and each sub-block reads the stream, computes something, and adds it back:
That is the whole layer. Llama, Qwen, Mistral and most other open models use exactly this shape, with small variations.
The blocks
attention sub-block
MLP sub-block
matmul: reads weights
Q, K, V projections
Three matmuls (often fused into one). K and V are what the KV cache stores. With GQA there are fewer K/V heads than Q heads, so K and V are narrower.
- shape
- 4096 → 4096 + 2×1024
- params
- 25.2 M
- FLOPs/token
- 50.3 M
Where one layer's weights live
Matmuls are 99.97% of the per-token FLOPs outside attention. Every one of them reads its whole weight matrix from memory, once per step.
Bar width = share of the layer's parameters. Embeddings (1.05 B, untied) sit outside the layers.
- RMSNorm rescales each token’s vector to unit root-mean-square, then multiplies by a learned gain. It keeps activations in a numerically safe range. It is called pre-norm because it is applied before each sub-block, not after.
- Attention is the only place where tokens exchange information. It gets its own chapter (1.3). Around it sit four weight matrices: in, out.
- The MLP works on each token independently. Modern models use SwiGLU: two parallel projections up to a wider dimension , a gated nonlinearity, and a projection back down.
Counting parameters
For a layer with width , MLP width , query heads and key/value heads of size :
Plug in Llama-3-8B (, , , ):
| Part | Parameters per layer |
|---|---|
| + | 33.6 M |
| + (8 KV heads) | 8.4 M |
| MLP (gate, up, down) | 176.2 M |
| Layer total | 218.1 M |
Multiply by 32 layers to get 6.98 B, then add the untied embedding table and LM head (2 × 525 M) for 8.03 B in total. The MLP holds about 80% of each layer’s weights.
FLOPs per token: the 2N rule
Every weight in a matmul takes part in one multiply and one add for each token that passes through. So a forward pass costs about
where is the number of parameters used in matmuls (the embedding lookup is free). For Llama-3-8B that is about 15 GFLOP per token, plus the attention-over-context term, which grows with sequence length and is small at short contexts.
Sources
Check your understanding
0 / 4 answered1.In a pre-norm decoder layer, what happens to the residual stream x?
2.Which block is the only one where tokens exchange information with each other?
3.Roughly how many FLOPs does a 70B-parameter model need per generated token, ignoring attention over the context?
4.In Llama-3-8B, where do most of a layer's parameters live?