Imports
run firstThe lab file's imports. Run this cell first (or press Run all).
Hands-on lab 02 of 10 · Transformer block
Assemble one decoder block the way GPT-2 (LayerNorm + GELU MLP) and Llama (RMSNorm + SwiGLU) build it, then write a closed-form parameter count and check it against PyTorch's own count. Lab 01 built attention; here it becomes one sub-layer of a block.
[B, N, d])LayerNorm, per token, over the last dim (d values)
mu = mean(x) [B, N, 1] var = mean((x - mu)^2) (divide by d, NOT d - 1) LN(x) = gamma * (x - mu) / sqrt(var + eps) + beta gamma, beta: [d]
RMSNorm (Llama, Mistral, Qwen...): no mean subtraction, no beta
RMS(x) = gamma * x / sqrt(mean(x^2) + eps) gamma: [d]
Feed-forward network (FFN), applied to every token independently
GELU MLP: W2 GELU(W1 x + b1) + b2 d → d_ff → d (d_ff = 4d in GPT-2)
SwiGLU: W_down( SiLU(W_gate x) * (W_up x) ) d → d_ff (twice) → d
SiLU(z) = z * sigmoid(z); the "gate" decides, per hidden unit,
how much of W_up x passes through.
Self-attention: lab 01's multi-head attention (q/k/v/o projections).
Pre-norm block (every modern LLM and ViT)
h = x + Attn(Norm1(x)) the residual stream x is never normalised
y = h + FFN(Norm2(h)) itself; each sub-layer reads a normalised
copy and ADDS its result back.
(The 2017 paper used post-norm, x = Norm(x + Attn(x)), which trains worse when deep.)
Parameter count (d model width, f = d_ff)
attention 4 d^2 + 4 d biases (q, k, v, o), if bias GELU MLP 2 d f + f + d biases, if bias SwiGLU 3 d f (no biases in Llama-style models) LayerNorm 2 d (gamma, beta) RMSNorm d (gamma)
The number of heads does not change the count: heads only split d.
Preview (covered in the next tutorial, Build an LLM)
Editor keys: Tab / Shift+Tab indent and dedent by 4 spaces, Enter keeps the indentation, Shift+Enter runs the cell and moves to the next one.
Cells on this page:
The lab file's imports. Run this cell first (or press Run all).
class LayerNorm(nn.Module)LayerNorm over the last dimension, written out (do not call F.layer_norm).
forward(x)x: [..., d] → [..., d]
class RMSNorm(nn.Module)RMSNorm over the last dimension: gamma * x / sqrt(mean(x^2) + eps).
forward(x)x: [..., d] → [..., d]
class GeluMLP(nn.Module)GPT-2 / ViT FFN: fc2(GELU(fc1(x))), d → d_ff → d.
forward(x)class SwiGLU(nn.Module)Llama FFN: w_down( SiLU(w_gate(x)) * w_up(x) ), d → d_ff → d.
forward(x)attend(q, k, v, causal)Given: lab 01's scaled_dot_product_attention, as torch's fused kernel. q, k, v [B, h, N, d_k] → [B, h, N, d_k].
class SelfAttention(nn.Module)Multi-head self-attention. Same maths as lab 01's MultiHeadAttention.
forward(x, causal=True)x: [B, N, d] → [B, N, d] (returns only the output, no weights)
class Block(nn.Module)Pre-norm transformer block.
norm: "layernorm" (GPT-2, ViT) or "rmsnorm" (Llama) ffn: "gelu" (GPT-2, ViT) or "swiglu" (Llama) bias: whether the Linear layers have biases (GPT-2: yes, Llama: no)
forward(x, causal=True)x: [B, N, d] → [B, N, d]
count_block_params(d, n_heads, d_ff, norm='layernorm', ffn='gelu', bias=True)Number of parameters of Block(d, n_heads, d_ff, norm, ffn, bias), from a formula.
Do not build the module and call .numel(); work it out from the shapes. Sanity anchors:
GPT-2 small block: d=768, d_ff=3072, layernorm, gelu, bias → 7,087,872 Llama-2-7B block: d=4096, d_ff=11008, rmsnorm, swiglu, no bias → 202,383,360
Experiment here: print shapes, call your functions on small inputs. This cell runs in the same Python process as the cells above, is not part of Run all and is not saved to the lab file (your browser keeps it).