Full GPT Architecture Explained: Build the Complete Model | LLM Course #21
How does a whole GPT fit together? In Lecture 21 of the LLM Masterclass we stack Transformer blocks on top of token and position embeddings, add the final LayerNorm and the LM head that gives a score (logit) for every token in the vocabulary, tie the LM head to the embedding table, and count every weight in ChotuGPT: 891,648, compared with 117 million in GPT-1 and 175 billion in GPT-3. Then we move ChotuGPT to PyTorch, so loss.backward() does backprop for us. 💻 Code for this lecture (branch lecture-21): https://github.com/programmingline/chotugpt/tree/lecture-21 Lecture 21 of 32 — LLM Masterclass · Module 4: Inside the Transformer ⚠️ ChotuGPT is a simplified teaching model: 4 blocks, 4 heads, 128 numbers per token and a 512-token vocabulary, small enough to train on an ordinary computer. The weights are untrained in this lecture. In this lecture: • Stacking blocks: same shape in and out, so blocks stack (ChotuGPT 4, GPT-2 small 12, GPT-3 96) • Token embedding + position embedding at the bottom • Final LayerNorm and the LM head: 128 numbers → 512 scores (logits) at every position; softmax gives probabilities • Weight tying: the LM head reuses the token embedding table (as in the original Transformer) • GPTConfig: vocab_size 512, block_size 256, n_layer 4, n_head 4, n_embd 128 • Counting weights: token embedding 65,536 · position embedding 32,768 · one block 198,272 · total 891,648 • ChotuGPT vs GPT-1 (117M), GPT-2 (1.5B) and GPT-3 (175B) • Why PyTorch now: autograd does lecture 13's backprop with loss.backward() • Untrained loss 6.319, close to ln 512 = 6.238 (guessing evenly) Chapters: 00:00 Lecture 20 quiz answer 00:14 The full GPT architecture 00:24 Stacking blocks 00:52 Almost complete 00:59 Final LayerNorm and the LM head 01:38 GPTConfig 02:00 Counting the weights 02:38 ChotuGPT vs GPT-1 and GPT-3 02:58 Why PyTorch now 03:20 ChotuGPT in PyTorch 03:58 Quiz 04:20 A quick check 04:38 Recap & next lecture ✅ Lecture 20 quiz answer: A, it adds the block's input back to its output, so the input is never lost and gradients flow straight back. ❓ Quiz: For each position, the final layer of GPT outputs… A) One finished sentence B) An image C) A score for every token in the vocabulary. Comment your answer. I reveal it in Lecture 22! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 20 — Feed-forward, residuals and LayerNorm ⏭ Next: Lecture 22 — One forward pass, end to end Sources: A. Radford et al., "Improving Language Understanding by Generative Pre-Training", OpenAI 2018 (GPT-1: 12 layers, 117M parameters). A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (GPT-2: 12 layers in the smallest model, 1.5B in the largest). T. Brown et al., "Language Models are Few-Shot Learners", 2020 (GPT-3: 175B parameters, 96 layers). A. Vaswani et al., "Attention Is All You Need", 2017 (section 3.4: shared weights between the embedding and the pre-softmax linear layer). PyTorch documentation, autograd. #LLM #GPT #PyTorch…more
How does a whole GPT fit together? In Lecture 21 of the LLM Masterclass we stack Transformer blocks on top of token and position embeddings, add the final LayerNorm and the LM head that gives a score (logit) for every token in the vocabulary, tie the LM head to the embedding table, and count every weight in ChotuGPT: 891,648, compared with 117 million in GPT-1 and 175 billion in GPT-3. Then we move ChotuGPT to PyTorch, so loss.backward() does backprop for us. 💻 Code for this lecture (branch lecture-21): https://github.com/programmingline/chotugpt/tree/lecture-21 Lecture 21 of 32 — LLM Masterclass · Module 4: Inside the Transformer ⚠️ ChotuGPT is a simplified teaching model: 4 blocks, 4 heads, 128 numbers per token and a 512-token vocabulary, small enough to train on an ordinary computer. The weights are untrained in this lecture. In this lecture: • Stacking blocks: same shape in and out, so blocks stack (ChotuGPT 4, GPT-2 small 12, GPT-3 96) • Token embedding + position embedding at the bottom • Final LayerNorm and the LM head: 128 numbers → 512 scores (logits) at every position; softmax gives probabilities • Weight tying: the LM head reuses the token embedding table (as in the original Transformer) • GPTConfig: vocab_size 512, block_size 256, n_layer 4, n_head 4, n_embd 128 • Counting weights: token embedding 65,536 · position embedding 32,768 · one block 198,272 · total 891,648 • ChotuGPT vs GPT-1 (117M), GPT-2 (1.5B) and GPT-3 (175B) • Why PyTorch now: autograd does lecture 13's backprop with loss.backward() • Untrained loss 6.319, close to ln 512 = 6.238 (guessing evenly) Chapters: 00:00 Lecture 20 quiz answer 00:14 The full GPT architecture 00:24 Stacking blocks 00:52 Almost complete 00:59 Final LayerNorm and the LM head 01:38 GPTConfig 02:00 Counting the weights 02:38 ChotuGPT vs GPT-1 and GPT-3 02:58 Why PyTorch now 03:20 ChotuGPT in PyTorch 03:58 Quiz 04:20 A quick check 04:38 Recap & next lecture ✅ Lecture 20 quiz answer: A, it adds the block's input back to its output, so the input is never lost and gradients flow straight back. ❓ Quiz: For each position, the final layer of GPT outputs… A) One finished sentence B) An image C) A score for every token in the vocabulary. Comment your answer. I reveal it in Lecture 22! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 20 — Feed-forward, residuals and LayerNorm ⏭ Next: Lecture 22 — One forward pass, end to end Sources: A. Radford et al., "Improving Language Understanding by Generative Pre-Training", OpenAI 2018 (GPT-1: 12 layers, 117M parameters). A. Radford et al., "Language Models are Unsupervised Multitask Learners", OpenAI 2019 (GPT-2: 12 layers in the smallest model, 1.5B in the largest). T. Brown et al., "Language Models are Few-Shot Learners", 2020 (GPT-3: 175B parameters, 96 layers). A. Vaswani et al., "Attention Is All You Need", 2017 (section 3.4: shared weights between the embedding and the pre-softmax linear layer). PyTorch documentation, autograd. #LLM #GPT #PyTorch