Transformers and LLMs

Take a transformer apart. Compute attention by hand, see how position is encoded, trace a token through every layer, run a real language model in your browser to inspect its predictions, clean a web crawl into training data, and see exactly what a chat assistant reads.

Level
Advanced
Length
13 lessons, 4 hr 2 min
Assumes
Neural networks and embeddings, or equivalent background.
Builds on
Neural networks and Language as vectors
  1. 1Predicting the next tokenAt every step a language model scores every token in its vocabulary. Softmax turns the scores into probabilities, and a sampling rule picks one. Temperature, top-k, and top-p shape that choice.16 min
  2. 2Before transformers: recurrent networksRecurrent networks read a sequence one step at a time and carry a hidden state forward. See why their gradients vanish or explode, how gates help, and why attention replaced the loop.20 min
  3. 3Attention, step by stepAttention lets every token gather information from the other tokens in its context. Work through it one query at a time, from scores to weights to a mixed output.20 min
  4. 4Many heads and the causal maskTransformers run several attention heads side by side, each free to learn its own pattern, and language models hide the future from every token with a causal mask.18 min
  5. 5Where words areAttention ignores word order unless the model is told where each token sits. Sinusoidal encodings add position as waves; rotary embeddings rotate queries and keys so attention sees relative distance.18 min
  6. 6The transformer blockA transformer is one block repeated many times. Each block reads from a shared residual stream, mixes information across tokens with attention, transforms each token with an MLP, and adds the results back.18 min
  7. 7Inside a real language modelRun SmolLM2-135M in your browser. Watch it split your text into tokens, score every token in its vocabulary, and write one token at a time.15 min
  8. 8How LLMs are trainedPretraining on trillions of tokens builds a base model; fine-tuning and preference tuning turn it into an assistant. Scaling laws tell you how to split a compute budget between model size and data.20 min
  9. 9Where training data comes fromWeb crawls, books, code, synthetic text, and human feedback, and the filtering and deduplication that turn a raw crawl into training tokens. Run a miniature pipeline yourself.25 min
  10. 10Fine-tuning and LoRAFine-tuning adapts a pretrained model to a new task. LoRA freezes the original weights and learns a small low-rank change instead, cutting the numbers you train by orders of magnitude.18 min
  11. 11Generating fast: the KV cacheEach new token needs the keys and values of every token before it. Storing them instead of recomputing them makes generation fast, and the store grows with every token.16 min
  12. 12Quantization: smaller numbersStoring weights in 8 or 4 bits instead of 16 cuts a model's memory by half or more. See how scales, groups, and outliers decide what that costs in accuracy.18 min
  13. 13How a chat assistant worksA chat assistant is a language model continuing one long text. See the chat template, the system prompt, a conversation sent again every turn, a context window filling up, and why the model needs today's date.20 min

Labs in this track

Try "embedding", "softmax", "overfitting", or "backpropagation".