from_scratch.llm

Neural network → LLM · in 17 steps

BUILD AN LLMFROM SCRATCH

Seventeen small, runnable Python scripts that climb from a 60-line neural network to a 2026-architecture, instruction- and preference-tuned GPT — down to a quantized KV cache. No black boxes — every idea inside a frontier model, built and run by hand.

17
runnable scripts
0
magic / black boxes
465K
params in the GPT
2026
architecture & alignment

The arc

Three acts, one machine.

Each script teaches exactly one new idea and builds directly on the last. Nothing is imported that you haven't already built yourself.

Act I · Language

A neural net learns "what comes next." Counting, then gradient descent, then turning text into tokens.

Act II · Toolkit

Embeddings and context windows, how autograd works, and the switch to PyTorch so gradients compute themselves.

Act III · Transformer

Attention, the transformer block, a real GPT on Shakespeare — then sampling, training, and alignment up to 2026 spec.

The climb

The ladder.

Seventeen rungs, each its own page — the idea, the key formula, the theory, the real terminal output, and the full source. Every page also links to an executed Jupyter notebook you can run yourself.

Foundation
From neural net to language
The deep-learning toolkit
The Transformer
Make it usable
State of the art · 2026
Inference efficiency · 2026

Checked May 2026

What's latest — and what we use.

The transformer hasn't been replaced; it's been refined. Two fronts have moved since 2017 — the architecture, and how models are aligned after pre-training. Steps #13 and #14 bring this ladder up to current spec.

Architecture · #09 (2017) → #14 (2026)
ComponentClassicModern
Positionlearned absoluteRoPE
NormLayerNormRMSNorm
Feed-forwardReLU MLPSwiGLU
Attentionmulti-headGQA
Resultval 1.674 / 465Kval 1.634 / 418K
Post-training stack · 2026

Every major model this past year — DeepSeek-R1, Nemotron, GPT-5-class — uses a modular stack rather than plain RLHF:

SFT→ DPO→ GRPO / RLVR

SFT · #12 instruction following
DPO · #13 preference alignment, no reward model
GRPO RL with verifiable rewards, for reasoning

The other front is inference efficiency (steps #15–#16): the KV cache, then quantizing it to ~3 bits with Google's TurboQuant. Beyond that, all variations on what you've built — Multi-Latent Attention (DeepSeek) compresses the cache further; Mixture-of-Experts routes each token to a few specialist MLPs; QK-Norm adds stability. The atom is unchanged — it's attention, all the way down.

Get going

Run it yourself.

Pure Python. One-time setup, then run any script. The GPT trains in ~2 minutes on a laptop CPU and caches its weights so every later step loads instantly.

# one-time setup
python3 -m venv .venv
.venv/bin/pip install numpy torch

# climb the ladder (run from the repo root)
.venv/bin/python steps/01_foundation/00_neural_network.py   # the atom
.venv/bin/python steps/04_transformer/09_tiny_gpt.py       # a real GPT on Shakespeare
.venv/bin/python steps/06_sota_2026/14_modern_gpt.py       # the 2026 architecture