AI Systems · advanced
Transformer Block
Pre-LayerNorm, residuals, MLP, GELU.
Mental model
A transformer block is two sublayers — multi-head attention then a position-wise MLP — each wrapped in a residual connection and layer norm. Residuals keep gradients flowing; pre-LayerNorm (norm before the sublayer) makes deep stacks train stably.
How to study Transformer Block
Begin by restating the mental model in your own words, then connect it to a concrete system you have built or operated. Name the mechanism, the constraint it addresses, and the trade-off it introduces. Use Stanford CS336 — Language Modeling from Scratch (course), CS336 Assignment 1 — Build a transformer block from scratch, CS224n — Self-Attention & Transformers (Stanford) to check details, but close the source before writing your explanation. Retrieval is the learning step; rereading is only preparation.
Next, compare Transformer Block with Training & Debugging. Ask what changes in correctness, latency, resource use, operability, and failure recovery. Complete Pre-LayerNorm residual block and preserve the command, input, output, and one failed attempt as evidence. Finish by explaining the idea without jargon to someone who has not studied the track.
Proof of understanding
- Explain the mechanism from first principles and identify the state it reads or changes.
- Give one situation where the concept is the right choice and one where it is not.
- Predict a realistic failure mode before running the drill, then compare the prediction with evidence.
- Connect the result to a roadmap or build artifact instead of treating the concept as isolated trivia.
Common mistakes
- Dropping residual connections and watching deep models fail to train
- Post-LayerNorm placement causing instability in deep stacks
- Mismatched dimensions between the attention output and the MLP
Learn from primary sources
- Stanford CS336 — Language Modeling from Scratch (course) (course)
- CS336 Assignment 1 — Build a transformer block from scratch (doc)
- CS224n — Self-Attention & Transformers (Stanford) (paper)
- The Illustrated Transformer (article)
- The Illustrated GPT-2 — transformer block stack (article)
- The Annotated Transformer (article)
Practice and explain it back
Pre-LayerNorm residual block
x=[1,2], sublayer(x)=x+[0.1,−0.1]. Write pre-norm update: x + sublayer(LayerNorm(x)). If LayerNorm is identity here, new x?
Expected evidence: [1.1,1.9] — residual preserves a gradient highway.
Open the interactive drill →Review prompts
- Pre-LayerNorm versus post-LayerNorm — why did deep stacks move to pre-norm?
Build evidence
Synthesize: AI Models & Training
Move from transformer foundations through pre-training, fine-tuning, post-training, compression, and evaluation. Produce one working system, benchmark, or evidence-backed design that integrates the path.
- Implements or precisely models the core mechanisms from all three milestones
- Includes at least one injected failure or adversarial case and demonstrates recovery
- Reports quality, latency, resource, reliability, or usability measurements relevant to the domain
- Ships a concise architecture note explaining decisions, trade-offs, and remaining risks