Everwhy Open Everwhy
Computing

Could you build a tiny language model yourself, one tested piece at a time?

Build Your Own LLM 301 · about 14 minutes a level

Build a small language model in Python with numpy, one runnable piece at a time: counting models and a character bigram model, a byte-pair-encoding tokenizer, embeddings, self-attention with causal masking, a transformer block and a tiny decoder-only model, then gradients, an optimizer and a training loop, sampling with temperature, top-k and top-p, and evaluation with perplexity. The later units move to a deep-learning library and a GPU in outline, prepare clean training text you have the right to use, cover fine-tuning, instruction and preference tuning, safety testing, the cost of scaling, and running a small open model on your own computer.

Open in Everwhy Continue on the web

Go deeper, level by level

  1. Counting models and tokens: A counting language model in numpy, A character bigram model, A byte-pair-encoding tokenizer from scratch
  2. Embeddings and attention: Embedding tables: from token ids to vectors, Self-attention from scratch, Causal masking and attention heads
  3. A tiny transformer: Layer normalisation and residual connections, A complete transformer block, A forward pass through a tiny decoder-only model
  4. Training: Cross-entropy loss and its gradient, A small automatic-differentiation engine, An optimizer and a training loop
  5. Writing text and measuring it: Sampling: temperature, top-k and top-p, Evaluating with perplexity, Beyond one number: samples and small tests
  6. Real training: libraries, GPUs and data: Moving to a deep-learning library, Training on a GPU, in outline, Cleaning training text, Licences and permission for training text
  7. Adapting and testing your model: Fine-tuning on new text, Instruction tuning with a chat format, Preference tuning, in outline, Safety testing your model
  8. Scale and running it yourself: Counting compute and memory, The cost of scaling up, Running a small model on your own computer, From toy to real: where to go next

Every lesson shows its sources and how sure we are, and is checked by Everwhy.