How Large Language Models Learn, Think and Generate
Introduction: The Black Box on Your Screen
- The Question Everyone Is Asking
- What This Book Will Explain
- A First-Pass Tour: From Prompt to Response
Chapter 1: The Fundamental Idea - Predicting the Next Token
- Autocomplete with Ambition
- Why Prediction Requires “Understanding”
- The Training Objective in Plain Language
- From One Word at a Time to Coherent Thought
- More Challenging Prediction Tasks
Chapter 2: From Text to Numbers - Tokenization and Preprocessing
- Why Models Cannot Read Words
- Building a Vocabulary: Tokens vs. Words
- Byte-Pair Encoding and Subword Strategies
- Data Collection: Where the Training Corpus Comes From
- Tokenization Edge Cases and Their Consequences
- The Impact of Sequence Length on Training
Chapter 3: Embeddings - Meaning as Vectors
- The Embedding Layer: A Lookup Table with Power
- Semantic Geometry: Why Similar Words Are Nearby
- Positional Encodings: Giving the Model a Sense of Order
- What an Embedding Vector Actually Contains
- Embeddings in Context: From Static to Dynamic Representations
- The Mathematics of Cosine Similarity
- Embedding Dimensionality and Capacity
Chapter 4: The Transformer Architecture - An Overview
- Before Transformers: Why RNNs Could Not Scale
- The Big Picture: Layers, Blocks and Data Flow
- Feed-Forward Networks: The Hidden Computation
- Residual Connections and Layer Normalization
- Encoder, Decoder and Decoder-Only Architectures
- Parameter Counts and Model Sizing
- The Complete Forward Pass: A Numerical Example
Chapter 5: Self-Attention - The Heart of the Transformer
- What Does It Mean for One Token to Attend to Another?
- Queries, Keys and Values: The Attention Mechanism
- Softmax: Turning Scores into Weights
- Multi-Head Attention: Different Perspectives on the Same Text
- Tensor Shapes and Dimensionality Tracking
- Computational Complexity: The Quadratic Bottleneck
- Attention Patterns: What the Weights Reveal
Chapter 6: Training Mechanics - Loss, Gradients and Descent
- Measuring Failure: The Cross-Entropy Loss Function
- Backpropagation: How Errors Flow Backward
- Gradient Descent: Nudging Parameters in the Right Direction
- Adam and the Art of Optimization
- Tracing Gradients Through a Transformer Block
- Learning Rate Schedules: Warmup and Cosine Decay
- Gradient Clipping and Training Stability
- The Loss Landscape and Optimization Dynamics
Chapter 7: Pretraining - Learning from the Entire Internet
- The Pretraining Loop: One Step at a Time
- What the Model Learns: Patterns, Facts and Structure
- Scaling Laws: Why Bigger Is Better (and Why)
- Emergent Abilities: When More Parameters Change Everything
- Distributed Training: Parallelism at Scale
- Phases of Learning During Pretraining
- Data Quality Versus Quantity
Chapter 8: Knowledge Representation - What Lives in the Weights?
- Parameters as Compressed Knowledge
- Finding Facts Inside Neural Networks
- Knowledge Neurons and Factual Localization
- The Superposition Hypothesis: More Features Than Dimensions
- Sparse Autoencoders: Disentangling Features
- Feed-Forward Networks as Key-Value Memories
- Induction Heads: The Circuit Behind In-Context Learning
- The Limits of Current Understanding
- The Geometry of Reasoning: How Chains of Thought Emerge
- What Models Do Not Know (and Why They Hallucinate)
Chapter 9: Instruction Tuning - Teaching the Model to Obey
- From Completion Engine to Assistant
- Supervised Fine-Tuning: Learning from Examples
- Reinforcement Learning from Human Feedback
- Reward Model Training: From Preferences to Scores
- PPO Optimization: Reinforcement Learning in Practice
- Direct Preference Optimization: A Simpler Alternative
- The Trade-Off Between Capability and Alignment
Chapter 10: Inference - How a Response Is Generated, Token by Token
- The Inference Loop: One Token at a Time
- KV Caching: Avoiding Redundant Computation
- Temperature and Sampling: Controlling Creativity
- Greedy Decoding, Beam Search and Their Trade-offs
- Context Windows and Computational Cost
- Prefill Versus Decode: Two Different Computational Regimes
- KV Cache Memory: The Hidden Cost
- Speculative Decoding: Generating Multiple Tokens Per Step
- Repetition Penalties and Generation Controls
Chapter 11: Reasoning and Emergent Capabilities
- Did We Train Models to Reason? (No. So How?)
- In-Context Learning: Prompting as Programming
- Chain-of-Thought: Making Reasoning Visible
- The Boundary Between Pattern Matching and Reasoning
- The Emergence Debate: Real Phenomenon or Measurement Artifact?
- Systematic Evaluation of Reasoning Capabilities
- What In-Context Learning Reveals About Model Capabilities
Conclusion: What We Know, What We Do Not
- The Complete Picture: From Tokens to Intelligence-Like Behavior
- What LLMs Achieved (and How)
- Open Questions and Honest Uncertainties
