nanoGPT — The Full Pipeline, Text to Probability

One continuous walkthrough, split into 20 focused pages. Each page pairs the English explanation with the live Jupyter source and the Chinese-language deep-dive notes behind it.

  1. 1Raw text → Tokenizer → Token IDs
  2. 2Batch Assembly — get_batch()
  3. 3Token Embedding Lookup
  4. 4Position Embedding Lookup — a SECOND, separate table
  5. 5Combine: x = tok_emb + pos_emb
  6. 6Single-Head Attention
  7. 7Multi-Head: Concat + Project
  8. 8Transformer Block
  9. 9Stack ×n_layer
  10. 10Final LayerNorm + LM Head → Logits
  11. 11Loss — Cross-Entropy
  12. 12Backward Pass + Optimizer Step
  13. 13Prefill — Process the Whole Prompt in Parallel
  14. 14Incremental Decode — One New Token at a Time
  15. 15Softmax → Probability Distribution
  16. 16Sample Next Token
  17. 17Append & Loop
  18. 18KV Cache — Reuse Instead of Recompute
  19. 19Sliding Window — Cap at block_size
  20. 20Loop Back to Decode