How GPT Predicts the Next Token: One Forward Pass Explained | LLM Course #22
What actually happens inside a GPT when it reads a sentence? In Lecture 22 of the LLM Masterclass we push "After my morning chai, I feel" through ChotuGPT one step at a time and watch every tensor shape: token ids (1, 7), embeddings (1, 7, 128), four Transformer blocks, the final LayerNorm, logits (1, 7, 512) and the next-token probabilities. Then we let the untrained model write, and it writes gibberish. 💻 Code for this lecture (branch lecture-22): https://github.com/programmingline/chotugpt/tree/lecture-22 Lecture 22 of 32 — LLM Masterclass · Module 4: Inside the Transformer ⚠️ ChotuGPT is a simplified teaching model (891,648 parameters). Its weights are still random in this lecture, so its guesses are meaningless until we train it in Module 5. In this lecture: • Text to token ids with our 512-token byte-level BPE: [271, 289, 285, 277, 44, 256, 273], shape (1, 7) • Why ',' is id 44 (its byte value) and ' I' is id 256 (the first merge) • Token embedding (1, 7, 128) + position embedding (7, 128), broadcast over the batch • Four Transformer blocks: attention, feed-forward and residuals, same shape in and out • Final LayerNorm and the tied output layer: logits (1, 7, 512), a score for every token at every position • Softmax on the last position only: 512 probabilities that sum to 1 • The untrained model: top guesses ≈ 0.004 each, uniform would be 0.0020, ' fresh' gets 0.0022 • The generation loop (predict, pick, append, repeat): greedy gives '�ululul…', sampling gives other noise • PyTorch code: lectures/lecture22_forward_pass.py, plus tokenizer save/load as JSON Chapters: 00:00 Lecture 21 quiz answer 00:14 One forward pass, end to end 00:25 Text to token ids 00:51 Follow the numbers 01:00 Token + position embeddings 01:30 Four Transformer blocks 01:53 Logits and probabilities 02:23 What the untrained model guesses 02:49 The generation loop 03:16 lecture22_forward_pass.py 03:48 Quiz 04:08 Next: training data 04:19 Recap & next lecture ✅ Lecture 21 quiz answer: C, a score for every token in the vocabulary. Softmax turns those scores into next-token probabilities. ❓ Quiz: Before training, what does ChotuGPT write? A) Perfect English B) Random gibberish C) Always the word "fresh". Comment your answer. I reveal it in Lecture 23! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 21 — The full GPT architecture ⏭ Next: Lecture 23 — Pretraining data Sources: A. Radford et al., "Improving Language Understanding by Generative Pre-Training", OpenAI 2018 (GPT-1). A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (GPT-2: byte-level BPE, pre-norm blocks, final LayerNorm). O. Press and L. Wolf, "Using the Output Embedding to Improve Language Models", EACL 2017 (weight tying). PyTorch documentation: broadcasting semantics. #LLM #GPT #PyTorch…more
What actually happens inside a GPT when it reads a sentence? In Lecture 22 of the LLM Masterclass we push "After my morning chai, I feel" through ChotuGPT one step at a time and watch every tensor shape: token ids (1, 7), embeddings (1, 7, 128), four Transformer blocks, the final LayerNorm, logits (1, 7, 512) and the next-token probabilities. Then we let the untrained model write, and it writes gibberish. 💻 Code for this lecture (branch lecture-22): https://github.com/programmingline/chotugpt/tree/lecture-22 Lecture 22 of 32 — LLM Masterclass · Module 4: Inside the Transformer ⚠️ ChotuGPT is a simplified teaching model (891,648 parameters). Its weights are still random in this lecture, so its guesses are meaningless until we train it in Module 5. In this lecture: • Text to token ids with our 512-token byte-level BPE: [271, 289, 285, 277, 44, 256, 273], shape (1, 7) • Why ',' is id 44 (its byte value) and ' I' is id 256 (the first merge) • Token embedding (1, 7, 128) + position embedding (7, 128), broadcast over the batch • Four Transformer blocks: attention, feed-forward and residuals, same shape in and out • Final LayerNorm and the tied output layer: logits (1, 7, 512), a score for every token at every position • Softmax on the last position only: 512 probabilities that sum to 1 • The untrained model: top guesses ≈ 0.004 each, uniform would be 0.0020, ' fresh' gets 0.0022 • The generation loop (predict, pick, append, repeat): greedy gives '�ululul…', sampling gives other noise • PyTorch code: lectures/lecture22_forward_pass.py, plus tokenizer save/load as JSON Chapters: 00:00 Lecture 21 quiz answer 00:14 One forward pass, end to end 00:25 Text to token ids 00:51 Follow the numbers 01:00 Token + position embeddings 01:30 Four Transformer blocks 01:53 Logits and probabilities 02:23 What the untrained model guesses 02:49 The generation loop 03:16 lecture22_forward_pass.py 03:48 Quiz 04:08 Next: training data 04:19 Recap & next lecture ✅ Lecture 21 quiz answer: C, a score for every token in the vocabulary. Softmax turns those scores into next-token probabilities. ❓ Quiz: Before training, what does ChotuGPT write? A) Perfect English B) Random gibberish C) Always the word "fresh". Comment your answer. I reveal it in Lecture 23! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 21 — The full GPT architecture ⏭ Next: Lecture 23 — Pretraining data Sources: A. Radford et al., "Improving Language Understanding by Generative Pre-Training", OpenAI 2018 (GPT-1). A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (GPT-2: byte-level BPE, pre-norm blocks, final LayerNorm). O. Press and L. Wolf, "Using the Output Embedding to Improve Language Models", EACL 2017 (weight tying). PyTorch documentation: broadcasting semantics. #LLM #GPT #PyTorch