Unit 6 of 7 · about 6 hours · explain, implement
The modern decoder block: residuals, RMSNorm and SwiGLU
Attention mixes positions; the feed-forward network transforms each position, and residual additions preserve the model width. Normalization placement and initialization change how a deep stack carries signals and gradients.
You'll be able to
- Explain the pre-norm residual stream and contrast its gradient path with post-norm.
- Implement per-token RMSNorm, a bias-free SwiGLU feed-forward network and both residual sublayers.
- Contrast LayerNorm with RMSNorm and a two-matrix GELU FFN with three-matrix SwiGLU.
- Distinguish component initialization from depth-dependent residual-projection scaling and state the chosen recipe.
Covers
Read
In this order, only the parts listed
0 of 4 done
- Normalization, gating and norm placementModern LLM Notebook - decoder construction path
Component Upgrades: LayerNorm and RMSNorm hand calculations, RMSNorm full implementation, The SwiGLU gating mechanism, Full SwiGLU FFN implementation, and Post-Norm versus Pre-Norm comparison
Read the formulas and code. Skip the eight-layer gradient demonstration; a single random example is not a general stability proof.
- GELU FFN baselineLLMs from Scratch companion - attention and GPT construction
Section 4.3, Implementing a feed forward network with GELU activations
- Initialization and modern block specificationsStanford CS336 Assignment 1 - model contract
Section 3.3.1, Parameter Initialization, printed p. 17; section 3.4 through 3.4.2, printed pp. 19 to 22; section 3.5 through problem (
transformer_block), printed p. 26 - Depth-scaled residual initializationnanoGPT - residual projection initialization
MLPandBlockclasses;GPT.__init__fromself.apply(self._init_weights)through thec_proj.weightinitialization loop;GPT._init_weightsUse this as a contrasting GPT-2 recipe, not a replacement for the CS336 test architecture. Skip pretrained loading, optimizer setup and generation.
Focus on
- Write , then . Normalize the branch input, not the residual sum; final stack normalization comes later.
- LayerNorm subtracts the feature mean and divides by feature standard deviation. RMSNorm divides by , without centering, and uses one learned gain per feature.
- SwiGLU is . Its gate is not bounded to . Three bias-free matrices cost parameters; a two-matrix GELU FFN costs at the same hidden width.
- CS336 specifies truncated-normal component weights and unit RMSNorm gains. nanoGPT instead uses base and residual-projection ; that scales initial branch weights, not the residual identity path.
Common mistakes · 3
- Normalizing across tokens instead of the feature axis. That couples positions and can leak future information even when attention is causal.
- Squaring low-precision activations before upcasting. Overflow has already happened; accumulate RMS statistics in
float32first. - Copying the notebook's explanation that a SwiGLU gate is a probability. SiLU can be negative or exceed one; the sigmoid factor alone is bounded.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.For , unit gains and negligible , how do LayerNorm and RMSNorm differ?Answer
LayerNorm gives , before any affine shift. RMSNorm gives , whose mean is not zero.
2.Why does SwiGLU often use near instead of ?Answer
Three matrices give weights. Setting matches the weights of a two-matrix FFN with hidden width , before rounding or biases.
3.What is the clean gradient path supplied by ?Answer
Its Jacobian includes an identity term plus the branch Jacobian. Unlike post-norm, the residual identity contribution does not pass through a normalization Jacobian; this is not a guarantee against every training instability.
4.In nanoGPT, why are both attention and
MLPc_projweights scaled with layer count?AnswerBoth project branch outputs back into the residual stream. With two residual branches per layer, the recipe reduces their initial standard deviation by to limit accumulation across depth.
Practice
- CS336 Assignment 1:
rmsnormandpositionwise_feedforwardProblems (
rmsnorm) and (positionwise_feedforward), sections 3.4.1 and 3.4.2; implementrun_rmsnorm,run_siluandrun_swigluand passtest_rmsnorm,test_silu_matches_pytorchandtest_swiglu - CS336 Assignment 1:
transformer_blockProblem (
transformer_block), section 3.5; implementrun_transformer_blockand passtest_transformer_blockAdd your own zero-branch identity and gradient test, zero-input RMSNorm test and a test showing that changing another token cannot change this token's norm or FFN output.
You're done when
All four named public tests pass, the zero-branch block passes output and input-gradient identity checks, and you explain norm placement, gating and your initialization recipe without notes.
Only when you can do this without notes.
Leads to
Notes
Resources · 4 for this unit, 2 more in the section
- Modern LLM Notebook - decoder construction pathWalking Labs · primary
Work through English notebooks 03-embedding, 04-position-encoding, 05-transformer-block, 06-mini-gpt and the selected normalization, RoPE, GQA and SwiGLU comparisons in 08-gpt2-to-modern-models. Re-derive shapes and rerun with changed dimensions.
- LLMs from Scratch companion - attention and GPT constructionSebastian Raschka · required
Use the ch03 attention notebooks and ch04 model code. Rebuild the model without a high-level Transformer module and compare one forward and backward path with an independent small reference.
- Stanford CS336 Assignment 1 - model contractStanford University · required
Use the named linear, embedding, rmsnorm, positionwise_feedforward, rope, softmax, scaled_dot_product_attention, multihead_self_attention, transformer_block, transformer_lm and transformer_accounting specifications and public tests. Keep implementation independent and respect the course AI and honor-code boundary.
- nanoGPT - residual projection initializationAndrej Karpathy · support
Read
model.py:MLP,Block,GPT.__init__'s special scaled initialization ofc_proj.weight, andGPT._init_weights. Contrast the depth-dependent residual initialization with CS336's component initialization; do not adopt the training or generation code.
Also in this section
- PyTorch scaled dot product attention referencePyTorch · support
Use the functional contract only after the explicit attention oracle passes. Transfer identical weights and masks, compare outputs and gradients, and explain dtype, causal and dropout behavior from the documented semantics.
- Deep-ML candidate - three transformer closing drillsDeep-ML · optional
After the platform quality sample passes, attempt at most problems 490, 53 and 904 as changed-prompt drills for attention shapes and multi-head composition. Preserve independent tests; a judge result does not replace the section checkpoint.