Inside a GPT Block: Feed-Forward, Residuals & LayerNorm | LLM Course #20
What happens after attention? In Lecture 20 of the LLM Masterclass we build the other half of the Transformer block: the feed-forward network (MLP) that lets each token think on its own (widen 4×, GELU, shrink back), residual connections (a highway with side roads: out = x + f(x)), and LayerNorm (mean 0, spread 1 for every token). Then we join them into one pre-norm Block in NumPy, using our sentence "After my morning chai, I feel ___". This is our last NumPy lecture before ChotuGPT moves to PyTorch. 💻 Code for this lecture (branch lecture-20): https://github.com/programmingline/chotugpt/tree/lecture-20 Lecture 20 of 32 — LLM Masterclass · Module 4: Inside the Transformer ⚠️ ChotuGPT is a simplified teaching model. The weights in this lecture are random, so the numbers on screen are illustrative numbers for teaching only; we train real weights in Module 5. In this lecture: • Attention moves information between tokens; the feed-forward network lets each token work on what it gathered • The MLP: Linear 8 → 32 (4× wider), GELU, Linear 32 → 8, the same weights for every token (GPT-2 small: 768 → 3072 → 768) • GELU, the smooth cousin of ReLU: gelu(−0.5) = −0.15, gelu(2) = 1.95 • Residual connections: out = x + f(x), so the input is never lost and gradients flow straight back • LayerNorm: chai's vector goes from mean 0.22, std 0.86 to mean 0, std 1; then learned gamma and beta • Pre-norm block: x = x + attn(ln1(x)); x = x + mlp(ln2(x)); same shape (7, 8) in and out, so blocks stack • Counting weights: our toy block has 840 (attention 256, MLP 552, LayerNorms 32) • NumPy code: layer_norm(), MLP and the Block class in chotugpt/block.py Chapters: 00:00 Lecture 19 quiz answer 00:12 Feed-forward, residuals and LayerNorm 00:26 Talking, then thinking 00:46 The feed-forward network (MLP) 01:12 Halfway there 01:18 GELU 01:35 Residual connections 02:16 LayerNorm 02:54 One pre-norm Transformer block 03:25 The Block in NumPy 03:56 Quiz 04:18 Counting the weights 04:38 Recap & next lecture ✅ Lecture 19 quiz answer: A, tokens looking at future tokens. Each token may see only itself and the tokens before it. ❓ Quiz: What does a residual (skip) connection do? A) Adds the block's input back to its output B) Deletes old tokens C) Shuffles the words. Comment your answer. I reveal it in Lecture 21! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 19 — Self-attention, part 2: causal mask and multi-head ⏭ Next: Lecture 21 — The full GPT architecture Sources: A. Vaswani et al., "Attention Is All You Need", NeurIPS 2017 (position-wise feed-forward network, d_model = 512, d_ff = 2048; residual + LayerNorm after each sub-layer). K. He et al., "Deep Residual Learning for Image Recognition", 2015 (residual connections). J. L. Ba, J. R. Kiros, G. E. Hinton, "Layer Normalization", 2016. D. Hendrycks, K. Gimpel, "Gaussian Error Linear Units (GELUs)", 2016. A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (GPT-2: LayerNorm moved to the input of each sub-block; GPT-2 small d_model = 768). #LLM #Transformer #LayerNorm…more
What happens after attention? In Lecture 20 of the LLM Masterclass we build the other half of the Transformer block: the feed-forward network (MLP) that lets each token think on its own (widen 4×, GELU, shrink back), residual connections (a highway with side roads: out = x + f(x)), and LayerNorm (mean 0, spread 1 for every token). Then we join them into one pre-norm Block in NumPy, using our sentence "After my morning chai, I feel ___". This is our last NumPy lecture before ChotuGPT moves to PyTorch. 💻 Code for this lecture (branch lecture-20): https://github.com/programmingline/chotugpt/tree/lecture-20 Lecture 20 of 32 — LLM Masterclass · Module 4: Inside the Transformer ⚠️ ChotuGPT is a simplified teaching model. The weights in this lecture are random, so the numbers on screen are illustrative numbers for teaching only; we train real weights in Module 5. In this lecture: • Attention moves information between tokens; the feed-forward network lets each token work on what it gathered • The MLP: Linear 8 → 32 (4× wider), GELU, Linear 32 → 8, the same weights for every token (GPT-2 small: 768 → 3072 → 768) • GELU, the smooth cousin of ReLU: gelu(−0.5) = −0.15, gelu(2) = 1.95 • Residual connections: out = x + f(x), so the input is never lost and gradients flow straight back • LayerNorm: chai's vector goes from mean 0.22, std 0.86 to mean 0, std 1; then learned gamma and beta • Pre-norm block: x = x + attn(ln1(x)); x = x + mlp(ln2(x)); same shape (7, 8) in and out, so blocks stack • Counting weights: our toy block has 840 (attention 256, MLP 552, LayerNorms 32) • NumPy code: layer_norm(), MLP and the Block class in chotugpt/block.py Chapters: 00:00 Lecture 19 quiz answer 00:12 Feed-forward, residuals and LayerNorm 00:26 Talking, then thinking 00:46 The feed-forward network (MLP) 01:12 Halfway there 01:18 GELU 01:35 Residual connections 02:16 LayerNorm 02:54 One pre-norm Transformer block 03:25 The Block in NumPy 03:56 Quiz 04:18 Counting the weights 04:38 Recap & next lecture ✅ Lecture 19 quiz answer: A, tokens looking at future tokens. Each token may see only itself and the tokens before it. ❓ Quiz: What does a residual (skip) connection do? A) Adds the block's input back to its output B) Deletes old tokens C) Shuffles the words. Comment your answer. I reveal it in Lecture 21! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 19 — Self-attention, part 2: causal mask and multi-head ⏭ Next: Lecture 21 — The full GPT architecture Sources: A. Vaswani et al., "Attention Is All You Need", NeurIPS 2017 (position-wise feed-forward network, d_model = 512, d_ff = 2048; residual + LayerNorm after each sub-layer). K. He et al., "Deep Residual Learning for Image Recognition", 2015 (residual connections). J. L. Ba, J. R. Kiros, G. E. Hinton, "Layer Normalization", 2016. D. Hendrycks, K. Gimpel, "Gaussian Error Linear Units (GELUs)", 2016. A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (GPT-2: LayerNorm moved to the input of each sub-block; GPT-2 small d_model = 768). #LLM #Transformer #LayerNorm