LESSON 3.5 · Code · 140 min

Implement a Transformer block line by line

A common pre-norm GPT block applies x = x + Attention(LayerNorm(x)), followed by x = x + MLP(LayerNorm(x)). Causal self-attention lets each position read only itself and earlier to

DIRECT ANSWER · VERIFIED SOURCES ·

How does a modern GPT Transformer block update the residual stream?

A common pre-norm GPT block applies x = x + Attention(LayerNorm(x)), followed by x = x + MLP(LayerNorm(x)). Causal self-attention lets each position read only itself and earlier tokens, while the MLP transforms the channels at each position independently. Both branches add their updates back to the residual stream, helping information and gradients travel through many layers.

Lecture alignment

Lecture alignment: trace LayerNorm → causal multi-head attention → residual add → LayerNorm → GELU/MLP → residual add, checking the code and tensor shapes at every step.

Key takeaways

  • Attention mixes information across positions; the MLP mixes feature channels within each position.
  • Pre-norm places LayerNorm before each sublayer; the original Transformer paper used a different post-norm order.
  • A residual add requires matching shapes, so concatenated heads must be projected back to the model dimension.

Boundary and caveat

A “Transformer block” is not one immutable recipe. Models vary normalization order, activation, positional encoding, attention type, and biases; follow the target architecture’s configuration. Dropout is normally active only during training.

Primary sources

Learning objectives

  1. Explain what problem “Implement a Transformer block line by line” solves without hiding behind terminology.
  2. Trace the variables and causal links across attention, MLP, dropout.
  3. Complete “Write a fully testable Transformer block” and judge the result with evidence rather than intuition.

Core concepts

attention

attention is part of the lesson’s causal model. State its inputs, outputs, invariants, and failure mode; then verify it with a hand-check or a minimal experiment before moving to an optimized implementation.

MLP

MLP is part of the lesson’s causal model. State its inputs, outputs, invariants, and failure mode; then verify it with a hand-check or a minimal experiment.

dropout

dropout is part of the lesson’s causal model. State its inputs, outputs, invariants, and failure mode; then verify it with a hand-check or a minimal experiment.

and LayerNorm composition

and LayerNorm composition is part of the lesson’s causal model. State its inputs, outputs, invariants, and failure mode; then verify it with a hand-check or a minimal experiment.

Build and verify

Write a fully testable Transformer block

  • Predict: write the expected output, trend, or failure before running code.
  • Build: implement only the minimum components needed to answer the question.
  • Verify: compare with a baseline or trusted implementation; save seeds, parameters, and raw outputs.
  • Transfer: change one shape, dataset, scale, or workload condition and explain whether the conclusion still holds.

Open the complete interactive lesson