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.

✦ Interactive~20 min

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 x∈RT×dx \in \mathbb{R}^{T \times d} passes through LL identical layers. Each layer has two sub-blocks, and each sub-block reads the stream, computes something, and adds it back:

x←x+Attention⁡(RMSNorm⁡(x))x←x+MLP⁡(RMSNorm⁡(x))\begin{aligned} x &\leftarrow x + \operatorname{Attention}(\operatorname{RMSNorm}(x)) \\ x &\leftarrow x + \operatorname{MLP}(\operatorname{RMSNorm}(x)) \end{aligned}

That is the whole layer. Llama, Qwen, Mistral and most other open models use exactly this shape, with small variations.

The blocks

Model×32 layers · ≈8.03 B params
  1. attention sub-block

  2. 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

attention 19% · 41.9 MMLP 81% · 176.2 M

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.

fig. 1.2One layer of a Llama-style decoder, in data-flow order. Click a block to see its shape, parameters and FLOPs per token. Bar width shows each block's share of the layer's parameters.
  • 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: WQ,WK,WVW_Q, W_K, W_V in, WOW_O out.
  • The MLP works on each token independently. Modern models use SwiGLU: two parallel projections up to a wider dimension dffd_{ff}, a gated nonlinearity, and a projection back down.
MLP⁡(h)=(SiLU⁡(hWgate)⊙hWup) Wdown\operatorname{MLP}(h) = \big(\operatorname{SiLU}(h W_{\text{gate}}) \odot h W_{\text{up}}\big)\, W_{\text{down}}

Counting parameters

For a layer with width dd, MLP width dffd_{ff}, nhn_h query heads and nkvn_{kv} key/value heads of size dh=d/nhd_h = d / n_h:

d2⏟WQ+2 d nkvdh⏟WK, WV+d2⏟WO+3 d dff⏟MLP+2d⏟norms\underbrace{d^2}_{W_Q} + \underbrace{2\,d\,n_{kv} d_h}_{W_K,\,W_V} + \underbrace{d^2}_{W_O} + \underbrace{3\,d\,d_{ff}}_{\text{MLP}} + \underbrace{2d}_{\text{norms}}

Plug in Llama-3-8B (d=4096d = 4096, dff=14336d_{ff} = 14336, nh=32n_h = 32, nkv=8n_{kv} = 8):

PartParameters per layer
WQW_Q + WOW_O33.6 M
WKW_K + WVW_V (8 KV heads)8.4 M
MLP (gate, up, down)176.2 M
Layer total218.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

FLOPs per token≈2N\text{FLOPs per token} \approx 2N

where NN 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 answered
  1. 1.In a pre-norm decoder layer, what happens to the residual stream x?

  2. 2.Which block is the only one where tokens exchange information with each other?

  3. 3.Roughly how many FLOPs does a 70B-parameter model need per generated token, ignoring attention over the context?

  4. 4.In Llama-3-8B, where do most of a layer's parameters live?