Causal Mask & Multi-Head Attention Explained: No Looking Ahead | LLM Course #19
Why can't a GPT peek at the next word? In Lecture 19 of the LLM Masterclass we add the causal mask to self-attention (a triangle of allowed cells, minus infinity before softmax), see what it does to our sentence "After my morning chai, I feel ___", then run several attention heads in parallel, concatenate them and project with W_O. Finally we write the CausalSelfAttention class in NumPy. 💻 Code for this lecture (branch lecture-19): https://github.com/programmingline/chotugpt/tree/lecture-19 Lecture 19 of 32 — LLM Masterclass · Module 4: Inside the Transformer ⚠️ ChotuGPT is a simplified teaching model. The token vectors and weights in this lecture are illustrative numbers for teaching only; the heads' weights are random until we train them in Module 5. In this lecture: • Why the model must not look ahead: training predicts every next token in one pass, and the future doesn't exist when generating • The causal mask: each token sees only itself and earlier tokens (a lower triangle) • Future scores become minus infinity, exp(−∞) = 0, so their weights are exactly zero • Our sentence: After attends only to itself; chai → chai 44%, morning 29%; feel's row is unchanged • Multi-head attention: several heads in parallel, each with its own W_Q, W_K, W_V • Concatenate the heads, then the output projection W_O; shapes 7 × 4 in and out • Real sizes: original Transformer 8 heads × 64 = 512; GPT-2 small 12 heads × 64 = 768 • NumPy code: causal_mask(), attention(..., mask) and the CausalSelfAttention class Chapters: 00:00 Lecture 18 quiz answer 00:13 Self-attention, part 2 00:23 Why we must not peek 00:48 One small trick 00:55 The causal mask 01:22 Masked attention weights 01:41 Multi-head attention 02:16 Following the shapes 02:43 CausalSelfAttention in NumPy 03:19 Quiz 03:40 Half of a Transformer block 03:53 Recap & next lecture ✅ Lecture 18 quiz answer: A, to keep scores from growing too large, so softmax stays smooth and the model keeps learning. ❓ Quiz: What does the causal mask stop? A) Tokens looking at future tokens B) Tokens looking at themselves C) Training. Comment your answer. I reveal it in Lecture 20! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 18 — Self-attention, part 1: queries, keys and values ⏭ Next: Lecture 20 — Feed-forward, residuals and LayerNorm Sources: A. Vaswani et al., "Attention Is All You Need", NeurIPS 2017 (sections 3.2.2-3.2.3: multi-head attention, masking with −∞ in the decoder; h = 8, d_k = 64, d_model = 512). A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (GPT-2 small: 12 layers, d_model = 768). #LLM #MultiHeadAttention #Transformer…more
Why can't a GPT peek at the next word? In Lecture 19 of the LLM Masterclass we add the causal mask to self-attention (a triangle of allowed cells, minus infinity before softmax), see what it does to our sentence "After my morning chai, I feel ___", then run several attention heads in parallel, concatenate them and project with W_O. Finally we write the CausalSelfAttention class in NumPy. 💻 Code for this lecture (branch lecture-19): https://github.com/programmingline/chotugpt/tree/lecture-19 Lecture 19 of 32 — LLM Masterclass · Module 4: Inside the Transformer ⚠️ ChotuGPT is a simplified teaching model. The token vectors and weights in this lecture are illustrative numbers for teaching only; the heads' weights are random until we train them in Module 5. In this lecture: • Why the model must not look ahead: training predicts every next token in one pass, and the future doesn't exist when generating • The causal mask: each token sees only itself and earlier tokens (a lower triangle) • Future scores become minus infinity, exp(−∞) = 0, so their weights are exactly zero • Our sentence: After attends only to itself; chai → chai 44%, morning 29%; feel's row is unchanged • Multi-head attention: several heads in parallel, each with its own W_Q, W_K, W_V • Concatenate the heads, then the output projection W_O; shapes 7 × 4 in and out • Real sizes: original Transformer 8 heads × 64 = 512; GPT-2 small 12 heads × 64 = 768 • NumPy code: causal_mask(), attention(..., mask) and the CausalSelfAttention class Chapters: 00:00 Lecture 18 quiz answer 00:13 Self-attention, part 2 00:23 Why we must not peek 00:48 One small trick 00:55 The causal mask 01:22 Masked attention weights 01:41 Multi-head attention 02:16 Following the shapes 02:43 CausalSelfAttention in NumPy 03:19 Quiz 03:40 Half of a Transformer block 03:53 Recap & next lecture ✅ Lecture 18 quiz answer: A, to keep scores from growing too large, so softmax stays smooth and the model keeps learning. ❓ Quiz: What does the causal mask stop? A) Tokens looking at future tokens B) Tokens looking at themselves C) Training. Comment your answer. I reveal it in Lecture 20! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 18 — Self-attention, part 1: queries, keys and values ⏭ Next: Lecture 20 — Feed-forward, residuals and LayerNorm Sources: A. Vaswani et al., "Attention Is All You Need", NeurIPS 2017 (sections 3.2.2-3.2.3: multi-head attention, masking with −∞ in the decoder; h = 8, d_k = 64, d_model = 512). A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (GPT-2 small: 12 layers, d_model = 768). #LLM #MultiHeadAttention #Transformer