Build a modern LLM from scratch. Every line commented. Explained like we are five.
Python attention-mechanismdeep-learningeducationalfrom-scratchgptlanguage-modelllamallmmachine-learningnatural-language-processingpythonpytorchtokenisationtransformerstutorial
fix rotate_half to match emb, update dataset namespace, add perplexity topic
raiyanyahyae179d035df71
diff --git a/README.md b/README.mdindex 0750cfc..14a375c 100644--- a/README.md+++ b/README.md@@ -7,7 +7,7 @@<p align="center"><img src="https://img.shields.io/badge/chapters-12-blue" alt="12 chapters"><img src="https://img.shields.io/badge/lines-7%2C500%2B-green" alt="7,500+ lines">- <img src="https://img.shields.io/badge/topics_explained-26-teal" alt="26 topic explainers">+ <img src="https://img.shields.io/badge/topics_explained-27-teal" alt="27 topic explainers"><img src="https://img.shields.io/badge/code%20commented-100%25-brightgreen" alt="100% commented"><img src="https://img.shields.io/badge/prerequisite-python%20basics-orange" alt="Python basics only"><img src="https://img.shields.io/badge/architecture-LLaMA%203%20style-purple" alt="LLaMA 3 style">@@ -23,7 +23,7 @@This is a **12-chapter, 7,500+ line interactive textbook** that teaches you how to build, train and run a modern language model from absolute scratch. The same family of architecture behind ChatGPT, Claude, LLaMA and Mistral.-Alongside the chapters there are **26 standalone topic explainers** covering every technique in depth. RoPE, attention, RMSNorm, SwiGLU, KV cache, AdamW, mixed precision and more. Plus two narrative walkthroughs that trace a single sentence through the entire model step by step. Each file follows the same style: child language, no jargon, a code example you can run.+Alongside the chapters there are **27 standalone topic explainers** covering every technique in depth. RoPE, attention, RMSNorm, SwiGLU, KV cache, AdamW, mixed precision and more. Plus two narrative walkthroughs that trace a single sentence through the entire model step by step. Each file follows the same style: child language, no jargon, a code example you can run.You won't just read about Transformers. You'll **write every line yourself**: tokenizer, embeddings, attention, training loop, inference engine. Every single line annotated to explain **what** it does and **why** it's there.@@ -197,6 +197,7 @@ The last two files are narrative walkthroughs. A Token's Journey follows one sen| Loss Curves | [how_to_read_loss.md](explanations%20and%20examples%20WIP/how_to_read_loss.md) | Diagnose training problems from the loss curve || Mixture of Experts | [mixture_of_experts.md](explanations%20and%20examples%20WIP/mixture_of_experts.md) | How MoE scales models with sparse routing || Speculative Decoding | [speculative_decoding.md](explanations%20and%20examples%20WIP/speculative_decoding.md) | 2-3× faster generation with a draft model |+| Perplexity | [perplexity.md](explanations%20and%20examples%20WIP/perplexity.md) | The one number that measures your model || Cheatsheet | [cheatsheet.md](explanations%20and%20examples%20WIP/cheatsheet.md) | Every formula and hyperparameter in one place || FAQ | [faq.md](explanations%20and%20examples%20WIP/faq.md) | Troubleshooting common problems || Encoder vs Decoder | [encoder_decoder_architectures.md](explanations%20and%20examples%20WIP/encoder_decoder_architectures.md) | GPT vs BERT vs T5 explained |@@ -292,7 +293,7 @@ Each chapter follows the same **4-step structure**:│ ├── 04_data_preparation.md│ ├── 05_full_finetune.md│ └── 📓 notebooks/lora_finetune.ipynb-├── 📚 explanations and examples WIP/ ← Standalone explainers (26 topics)+├── 📚 explanations and examples WIP/ ← Standalone explainers (27 topics)└── 📄 CONTRIBUTING.md```diff --git a/chapters/04_positional_encoding.md b/chapters/04_positional_encoding.mdindex 1e30334..9415d89 100644--- a/chapters/04_positional_encoding.md+++ b/chapters/04_positional_encoding.md@@ -207,24 +207,11 @@ class RotaryPositionalEmbedding(nn.Module):self.register_buffer("sin_cached", emb.sin()) # sin of each angle@staticmethod- def rotate_half(x: torch.Tensor) -> torch.Tensor:- """- WHAT: Prepare a vector for the rotation formula.- WHY: The rotation formula is: x' = x*cos + rotate_half(x)*sin-- For vector [x0, x1, x2, x3, x4, x5]:- rotate_half returns [-x1, x0, -x3, x2, -x5, x4]-- Why this works: For pair (x0, x1) rotated by angle θ:- x0' = x0*cos(θ) - x1*sin(θ) ← matches: x0*cos + (-x1)*sin- x1' = x0*sin(θ) + x1*cos(θ) ← matches: x1*cos + (x0)*sin-- So executing (x*cos + rotate_half(x)*sin) performs rotation- on every dimension pair simultaneously — no loop needed!- """- x1 = x[..., : x.shape[-1] // 2] # First half: [x0, x2, x4, ...]- x2 = x[..., x.shape[-1] // 2 :] # Second half: [x1, x3, x5, ...]- return torch.cat([-x2, x1], dim=-1) # [-x1, x0, -x3, x2, -x5, x4, ...]+ @staticmethod+ def rotate_half(x):+ x_even = x[..., 0::2]+ x_odd = x[..., 1::2]+ return torch.stack([-x_odd, x_even], dim=-1).flatten(-2)def forward(self, x: torch.Tensor, seq_len: int) -> torch.Tensor:"""diff --git a/chapters/08_training.md b/chapters/08_training.mdindex 7a6cf5f..cef81d3 100644--- a/chapters/08_training.md+++ b/chapters/08_training.md@@ -274,7 +274,7 @@ from datasets import load_datasetdef load_training_data(max_samples: int = None):"""Download WikiText-103 — clean Wikipedia text."""print("Loading dataset: wikitext-103-raw-v1...")- dataset = load_dataset("wikitext", "wikitext-103-raw-v1", split="train")+ dataset = load_dataset("Salesforce/wikitext", "wikitext-103-raw-v1", split="train")texts = [item["text"] for item in dataset if item["text"].strip()]if max_samples:texts = texts[:max_samples]diff --git a/explanations and examples WIP/perplexity.md b/explanations and examples WIP/perplexity.mdnew file mode 100644index 0000000..786437e--- /dev/null+++ b/explanations and examples WIP/perplexity.md@@ -0,0 +1,277 @@+# Perplexity: The One Number That Measures Your Model++## The short answer++Perplexity is a single number that tells you how good a language model+is. Lower is better. A perplexity of 10 means the model is as confused+as if it had to choose between 10 equally likely words at every step.+A perplexity of 1 means the model knows exactly what word comes next+every time. Real language models on real text usually have perplexity+between 10 and 100.++Perplexity is not abstract. It is the exponentiated cross entropy loss.+If your training loss is 3.0 your perplexity is e to the power of 3+which is about 20. The model is as confused as if picking randomly+among 20 options.++## Where the number comes from++Every time the model predicts the next word it assigns a probability to+every token in the vocabulary. The correct token gets some probability+P. The model is uncertain about that prediction. Cross entropy loss+measures that uncertainty as negative log of P. Perplexity is e to the+power of that loss.++```+For a single prediction:+ Model says P("mat") = 0.25+ Loss = -ln(0.25) = 1.386+ Perplexity = e^1.386 = 4.0++Interpretation: The model was as uncertain as if it had to pick+randomly among 4 equally likely options.+```++The magic of perplexity is that it translates an abstract loss number+into something you can visualize. A loss of 1.386 means nothing to most+people. A perplexity of 4 means the model is choosing among 4 options.+That is tangible. You can imagine picking randomly from 4 words.++## Perplexity versus loss++During training you watch the loss go down. The loss starts around 10.8+for a model with 50257 vocabulary tokens. That is uninterpretable. But+you can convert it.++```+loss = 10.82: perplexity = e^10.82 ≈ 50,000 (random. Model knows nothing)+loss = 7.0: perplexity = e^7.0 ≈ 1,100 (learning word frequencies)+loss = 5.0: perplexity = e^5.0 ≈ 150 (learning basic grammar)+loss = 3.0: perplexity = e^3.0 ≈ 20 (decent language model)+loss = 2.0: perplexity = e^2.0 ≈ 7.4 (good language model)+loss = 1.5: perplexity = e^1.5 ≈ 4.5 (very good)+loss = 1.0: perplexity = e^1.0 ≈ 2.7 (excellent)+```++Perplexity gives you a mental model for what the loss actually means.+When your loss goes from 10.8 to 7.0 you have not just improved by+3.8 units. You have gone from being as confused as 50000 options to+being as confused as 1100 options. That is dramatic improvement.++## How to compute it++In code perplexity is one line.++```python+import math++loss = 3.0 # Your model's cross entropy loss+perplexity = math.exp(loss)++print(f"Loss: {loss:.4f}")+print(f"Perplexity: {perplexity:.2f}")+print(f"The model is as uncertain as picking among {perplexity:.0f} options.")+```++For a batch of predictions compute the average loss first then exponentiate.++```python+total_loss = 0+total_tokens = 0+for input_ids, target_ids in dataloader:+ with torch.no_grad():+ logits = model(input_ids)+ loss = F.cross_entropy(+ logits.view(-1, vocab_size),+ target_ids.view(-1),+ reduction='sum'+ )+ total_loss += loss.item()+ total_tokens += target_ids.numel()++average_loss = total_loss / total_tokens+perplexity = math.exp(average_loss)+print(f"Validation perplexity: {perplexity:.2f}")+```++Always compute perplexity on data the model has not seen during+training. Training perplexity can be misleadingly low because the model+has memorized parts of the training data. Validation perplexity+measures how well the model generalizes.++## What different perplexity values mean++### Perplexity around 50000++Your model is random. It assigns equal probability to every token in+the vocabulary. It has learned nothing. This is normal at step zero+of training. If it stays here after thousands of steps something is+broken. Check your loss function and optimizer.++### Perplexity around 1000++The model has learned that some words are more common than others.+It knows that *the* appears often and *xylophone* appears rarely. It+uses these frequencies in its predictions. But it does not yet+understand word order or grammar or meaning. The output is gibberish+but the gibberish contains common words in roughly the right+proportions.++### Perplexity around 100++The model has learned basic grammar. It knows that articles precede+nouns. It knows that verbs agree with subjects in number. It knows+that periods end sentences. The output has recognizable sentence+structure even if the content is nonsensical. This is where most small+models plateau after limited training.++### Perplexity around 20++The model writes coherent text. Sentences have subjects and verbs and+objects in the right order. The content is sometimes factual and+sometimes invented. This is the level of GPT-1 from 2018. A model+with 17 million parameters trained on 100 million tokens might reach+this level.++### Perplexity around 10++The model writes good text. The content is mostly factual. Few obvious+errors. This is the level of GPT-2 from 2019. A model with 150 million+parameters trained on billions of tokens can reach this.++### Perplexity around 5++The model writes excellent text. Rarely makes factual errors. Handles+complex reasoning. This is the level of GPT-3 from 2020 and modern+small models like LLaMA 7B. Training these models costs millions of+dollars.++### Perplexity below 3++The model approaches human performance on language modeling. It+predicts what a human would write with high accuracy. Models at this+level are measured on harder tasks like question answering and code+generation because perplexity stops being a useful metric. The+difference between perplexity 2.5 and 2.3 is hard to feel but+expensive to achieve.++## Why perplexity is not everything++Perplexity measures how well the model predicts the next word. It does+not measure whether the model is helpful or truthful or safe or+creative. A model can have great perplexity and still generate+harmful content. It can have great perplexity and still hallucinate+facts. It can have great perplexity and still be boring.++Perplexity also depends on the dataset. A model trained on children's+books will have low perplexity on children's books and high perplexity+on legal documents. Perplexity is always relative to the test data.+Comparing perplexity between models is only meaningful when the models+are evaluated on the same dataset with the same tokenizer.++Different tokenizers produce different perplexity values for the same+model on the same data. A tokenizer with a larger vocabulary usually+gives lower perplexity because each token encodes more information and+there are fewer predictions to make per sentence. This is why you+cannot compare perplexity between models that use different tokenizers.++## The relationship to bits per character++Perplexity can be converted to bits per character. Bits per character+measures how many bits of information the model needs on average to+encode each character of text. Lower is better. The relationship is:++```+bits_per_character = ln(perplexity) / (ln(2) × characters_per_token)+```++For GPT-2 each token covers about 4 characters on average.++```+perplexity = 20:+ bits_per_char = ln(20) / (0.693 × 4) ≈ 1.08 bits per character++perplexity = 10:+ bits_per_char = ln(10) / (0.693 × 4) ≈ 0.83 bits per character+```++This tells you that the model needs about one bit of information per+character to encode English text. The theoretical minimum entropy of+English is about 0.6 to 1.0 bits per character. Models are approaching+that limit. Further improvements in perplexity will require better+understanding of meaning not just better statistics.++## Perplexity during training++A good training run should show perplexity decreasing smoothly. The+starting perplexity should be close to the vocabulary size. The final+perplexity depends on model size and data quality and training+duration.++```+Step Loss Perplexity+0 10.82 50,257 Model knows nothing+100 9.23 10,240 Learning frequencies+500 7.45 1,720 Learning word patterns+1,000 6.12 455 Emerging grammar+5,000 4.23 69 Coherent phrases+10,000 3.45 31 Decent sentences+50,000 2.89 18 Good model+```++If perplexity stops decreasing before step 10000 something is limiting+the model. The learning rate might be too low. The model capacity+might be exhausted. The data might not contain enough patterns to learn+from. Try increasing the learning rate or the model size or the dataset+size.++If perplexity decreases on training data but increases on validation+data the model is overfitting. It is memorizing the training set+instead of learning general patterns. The training and validation+curves diverge. Add more dropout or weight decay or use early stopping.++## Perplexity of famous models++All numbers are approximate and depend on the evaluation dataset.++```+Model Params Perplexity (WikiText-103)+Random baseline : 50,257+GPT-1 (2018) 117M ~35+GPT-2 Small (2019) 124M ~19+GPT-2 Medium 350M ~15+GPT-2 Large 774M ~12+GPT-3 Small (2020) 125M ~18+GPT-3 XL 1.3B ~10+GPT-3 6.7B ~8+GPT-3 175B ~5+LLaMA 7B (2023) 7B ~7+LLaMA 13B ~6+LLaMA 70B ~4+```++Notice something. GPT-2 Small at 124 million parameters achieves+perplexity 19. GPT-3 at 125 million parameters achieves perplexity+18. Same architecture. Same size. Different training. GPT-3 was+trained on more data for longer. The extra compute improved perplexity+even without increasing model size. This is why data quality and+training duration matter as much as model architecture.++## What you need to remember++Perplexity is the exponentiated cross entropy loss. A perplexity of N+means the model is as uncertain as if it had to pick randomly among N+options. Lower is better. Random models start at around the vocabulary+size. Good models reach single digits.++Perplexity translates the abstract loss number into something visual.+When your loss drops from 10 to 5 your model has gone from being+confused among 22000 options to being confused among 150 options.+That is the difference between a model that knows nothing and a model+that knows something.++Perplexity does not measure helpfulness or truthfulness or safety.+It only measures how well the model predicts the next word. For+evaluating whether your model is useful you need other metrics. But+for tracking whether your model is learning perplexity is the single+most important number.diff --git a/fine-tuning/notebooks/lora_finetune.ipynb b/fine-tuning/notebooks/lora_finetune.ipynbindex 191f45f..5aaecbc 100644--- a/fine-tuning/notebooks/lora_finetune.ipynb+++ b/fine-tuning/notebooks/lora_finetune.ipynb@@ -69,11 +69,11 @@" self.register_buffer(\"sin_cached\", emb.sin())\n","\n"," @staticmethod\n",+ " @staticmethod\n"," def rotate_half(x):\n",- " x1 = x[..., : x.shape[-1] // 2]\n",- " x2 = x[..., x.shape[-1] // 2 :]\n",- " return torch.cat([-x2, x1], dim=-1)\n",- "\n",+ " x_even = x[..., 0::2]\n",+ " x_odd = x[..., 1::2]\n",+ " return torch.stack([-x_odd, x_even], dim=-1).flatten(-2)\n"," def forward(self, x, seq_len):\n"," cos = self.cos_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n"," sin = self.sin_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n",diff --git a/main.py b/main.pyindex 65f3bdf..b8d0feb 100644--- a/main.py+++ b/main.py@@ -36,11 +36,12 @@ class RotaryPositionalEmbedding(nn.Module):self.register_buffer("cos_cached", emb.cos())self.register_buffer("sin_cached", emb.sin())+ @staticmethod@staticmethoddef rotate_half(x):- x1 = x[..., : x.shape[-1] // 2]- x2 = x[..., x.shape[-1] // 2 :]- return torch.cat([-x2, x1], dim=-1)+ x_even = x[..., 0::2]+ x_odd = x[..., 1::2]+ return torch.stack([-x_odd, x_even], dim=-1).flatten(-2)def forward(self, x, seq_len):cos = self.cos_cached[:seq_len].unsqueeze(0).unsqueeze(0)@@ -265,7 +266,7 @@ class TextDataset(Dataset):def load_training_data(max_samples=None):print("Loading dataset: wikitext-103-raw-v1...")- dataset = load_dataset("wikitext", "wikitext-103-raw-v1", split="train")+ dataset = load_dataset("Salesforce/wikitext", "wikitext-103-raw-v1", split="train")texts = [item["text"] for item in dataset if item["text"].strip()]if max_samples:texts = texts[:max_samples]diff --git a/notebooks/04_positional_encoding.ipynb b/notebooks/04_positional_encoding.ipynbindex ae7c3fc..c941da7 100644--- a/notebooks/04_positional_encoding.ipynb+++ b/notebooks/04_positional_encoding.ipynb@@ -76,11 +76,11 @@" self.register_buffer(\"sin_cached\", emb.sin())\n","\n"," @staticmethod\n",+ " @staticmethod\n"," def rotate_half(x):\n",- " x1 = x[..., : x.shape[-1] // 2]\n",- " x2 = x[..., x.shape[-1] // 2 :]\n",- " return torch.cat([-x2, x1], dim=-1)\n",- "\n",+ " x_even = x[..., 0::2]\n",+ " x_odd = x[..., 1::2]\n",+ " return torch.stack([-x_odd, x_even], dim=-1).flatten(-2)\n"," def forward(self, x, seq_len):\n"," cos = self.cos_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n"," sin = self.sin_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n",diff --git a/notebooks/05_attention.ipynb b/notebooks/05_attention.ipynbindex c8b5fd4..5727029 100644--- a/notebooks/05_attention.ipynb+++ b/notebooks/05_attention.ipynb@@ -125,11 +125,11 @@" self.register_buffer(\"sin_cached\", emb.sin())\n","\n"," @staticmethod\n",+ " @staticmethod\n"," def rotate_half(x):\n",- " x1 = x[..., : x.shape[-1] // 2]\n",- " x2 = x[..., x.shape[-1] // 2 :]\n",- " return torch.cat([-x2, x1], dim=-1)\n",- "\n",+ " x_even = x[..., 0::2]\n",+ " x_odd = x[..., 1::2]\n",+ " return torch.stack([-x_odd, x_even], dim=-1).flatten(-2)\n"," def forward(self, x, seq_len):\n"," cos = self.cos_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n"," sin = self.sin_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n",diff --git a/notebooks/06_transformer_block.ipynb b/notebooks/06_transformer_block.ipynbindex 761f626..28a32ac 100644--- a/notebooks/06_transformer_block.ipynb+++ b/notebooks/06_transformer_block.ipynb@@ -81,11 +81,11 @@" self.register_buffer(\"sin_cached\", emb.sin())\n","\n"," @staticmethod\n",+ " @staticmethod\n"," def rotate_half(x):\n",- " x1 = x[..., : x.shape[-1] // 2]\n",- " x2 = x[..., x.shape[-1] // 2 :]\n",- " return torch.cat([-x2, x1], dim=-1)\n",- "\n",+ " x_even = x[..., 0::2]\n",+ " x_odd = x[..., 1::2]\n",+ " return torch.stack([-x_odd, x_even], dim=-1).flatten(-2)\n"," def forward(self, x, seq_len):\n"," cos = self.cos_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n"," sin = self.sin_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n",diff --git a/notebooks/07_gpt_model.ipynb b/notebooks/07_gpt_model.ipynbindex 0cf4845..1554b49 100644--- a/notebooks/07_gpt_model.ipynb+++ b/notebooks/07_gpt_model.ipynb@@ -75,11 +75,11 @@" self.register_buffer(\"sin_cached\", emb.sin())\n","\n"," @staticmethod\n",+ " @staticmethod\n"," def rotate_half(x):\n",- " x1 = x[..., : x.shape[-1] // 2]\n",- " x2 = x[..., x.shape[-1] // 2 :]\n",- " return torch.cat([-x2, x1], dim=-1)\n",- "\n",+ " x_even = x[..., 0::2]\n",+ " x_odd = x[..., 1::2]\n",+ " return torch.stack([-x_odd, x_even], dim=-1).flatten(-2)\n"," def forward(self, x, seq_len):\n"," cos = self.cos_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n"," sin = self.sin_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n",diff --git a/notebooks/08_training.ipynb b/notebooks/08_training.ipynbindex 8affaff..97e142f 100644--- a/notebooks/08_training.ipynb+++ b/notebooks/08_training.ipynb@@ -88,11 +88,11 @@" self.register_buffer(\"sin_cached\", emb.sin())\n","\n"," @staticmethod\n",+ " @staticmethod\n"," def rotate_half(x):\n",- " x1 = x[..., : x.shape[-1] // 2]\n",- " x2 = x[..., x.shape[-1] // 2 :]\n",- " return torch.cat([-x2, x1], dim=-1)\n",- "\n",+ " x_even = x[..., 0::2]\n",+ " x_odd = x[..., 1::2]\n",+ " return torch.stack([-x_odd, x_even], dim=-1).flatten(-2)\n"," def forward(self, x, seq_len):\n"," cos = self.cos_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n"," sin = self.sin_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n",diff --git a/notebooks/09_inference.ipynb b/notebooks/09_inference.ipynbindex d20e09f..1554c0a 100644--- a/notebooks/09_inference.ipynb+++ b/notebooks/09_inference.ipynb@@ -59,11 +59,11 @@" self.register_buffer(\"sin_cached\", emb.sin())\n","\n"," @staticmethod\n",+ " @staticmethod\n"," def rotate_half(x):\n",- " x1 = x[..., : x.shape[-1] // 2]\n",- " x2 = x[..., x.shape[-1] // 2 :]\n",- " return torch.cat([-x2, x1], dim=-1)\n",- "\n",+ " x_even = x[..., 0::2]\n",+ " x_odd = x[..., 1::2]\n",+ " return torch.stack([-x_odd, x_even], dim=-1).flatten(-2)\n"," def forward(self, x, seq_len):\n"," cos = self.cos_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n"," sin = self.sin_cached[:seq_len].unsqueeze(0).unsqueeze(0)\n",