Source: Mayank A.'s LinkedIn post (2026-10-27) on FareedKhan-dev/train-llm-from-scratch by Fareed Khan. MIT license, 13.3K stars and 1.8K forks on 2026-11-21 (the post said 12.5K), created January 2025, last push 2026-11-16.
The claim
The post calls the repo an entire LLM engineering course in one codebase: a Transformer written in plain PyTorch, then pretraining, SFT, a reward model, DPO/ORPO/KTO, PPO and GRPO on the same backbone, plus a modern variant (RoPE, RMSNorm, SwiGLU, GQA, MLA, MoE, KV cache) and a 369K-parameter model that trains on a laptop CPU in about five minutes. The README supports all of that. It also covers more than the post lists: Muon, LoRA, IPO, SimPO, Dr. GRPO, DAPO, GSPO, speculative decoding and int8 inference, all hand-written with no trl, peft or transformers.
How it is built
One backbone, wrapped rather than rewritten. The teaching
Transformergains a singleforward_hiddenmethod that returns the hidden states beforelm_head. The reward head, the PPO value head and all log-probability math compose around that one method, so every stage changes only the data and the loss. This is the design idea worth stealing for any teaching codebase.Two architectures behind one flag.
--arch modernswaps the 2017/GPT-2 block for the current open-model block. On the laptop preset the modern model reaches dev loss 2.78 against 3.30 for the classic one, with 42% fewer parameters (author's numbers).Data in four streams. Pretraining text from The Pile (uncopyrighted subset) in HDF5; SFT from Alpaca, Dolly and GSM8K, packed; preferences from HH-RLHF and UltraFeedback; RL prompts from GSM8K plus an arithmetic curriculum. Chat roles are plain-text markers on the
r50k_basetokenizer, and reasoning answers use<think>…</think><answer>…</answer>so a verifier can parse the number.Stage objectives, each a few lines. SFT is cross-entropy masked to assistant tokens plus the closing end-of-turn token (so the model learns to stop). The reward model is a linear head trained with Bradley-Terry loss. DPO compares policy and frozen-reference log-ratios, with flags for ORPO, KTO, IPO, SimPO and label smoothing. PPO uses a shared-backbone value head, GAE, and a per-token KL penalty, with the reward from either the reward model or a GSM8K checker. GRPO drops the value network and normalizes rewards within a group of samples, with flags for the 2025 fixes (Dr. GRPO, DAPO, GSPO).
Engineering around it. Typed dataclass configs that reject unknown keys with a suggestion, jaxtyping shape annotations checked at runtime in tests, about 220 pytest tests including an end-to-end run of every stage on generated data, mypy and ruff in CI on Linux, Windows and macOS, smoke configs that finish in seconds, a Streamlit control panel, a Dockerfile and an MkDocs site.
What the evidence shows, and what it doesn't
The base-model eval is the strongest result.
eval_base.pyreproduces EleutherAI's lm-evaluation-harness on GPT-2 124M exactly (ARC-Easy 43.81, ARC-Challenge 19.03, LAMBADA 32.56), with all 19,341 answer log-likelihoods within 0.0006. That is the kind of check that makes the rest of the code believable.Pretraining numbers are modest and honest. A 77M base on 2x L40 for 2,000 steps went from loss 11.14 to about 3.76 dev at 130 to 150K tokens/s.
Post-training results are thin. The reward model reaches 0.574 preference accuracy on 7,974 pairs, and DPO's implicit-reward accuracy is also 0.574, barely above chance. The README gives no GSM8K numbers for SFT, DPO, PPO or GRPO; it supplies the command and says you fill in the table yourself. At these model sizes you should expect GSM8K near zero, so the RL stages teach the mechanics, not a visible capability gain.
Unchecked side claims. The README says HellaSwag was dropped because its source repo was taken down by a DMCA notice in September 2026.
How it compares
| Train LLM From Scratch (R205) | MiniMind (R097) | nanochat (R120) | |
|---|---|---|---|
| Main aim | teach every objective on one backbone | a usable 64M model end to end | the fastest honest single-node pipeline |
| Post-training breadth | widest: SFT, RM, DPO family, PPO, GRPO family | wide: DPO, PPO, GRPO, CISPO, agent RL | narrow: SFT, a simple RL stage |
| Library use | none for training (no trl/peft/transformers) | some | minimal |
| Smallest run | 369K params, laptop CPU, about 5 min | 64M, one GPU | CPU script, then GPU speedrun |
| Evidence | exact lm-eval match; weak post-training numbers | README as lab notebook, stderr reported | public speedrun leaderboard |
Pick this one to read the objectives side by side; pick nanochat to learn what makes pretraining fast, and MiniMind for a model that actually does something after post-training.
Failure modes for a reader
Mistaking working mechanics for working methods. A 0.574 reward model means PPO with
--reward_source rmis optimizing mostly noise; use the verifier reward to see RL move anything.Comparing objectives without fixing data, seed and initialization. The shared backbone makes the controlled comparison possible but the scripts don't enforce it.
Two config systems (legacy
config/config.pyand the JSON stage configs) are easy to mix up.The billion-parameter presets need A100/4090-class memory; the GPU table's "max practical size" column is the author's estimate.
Review checklist if you adopt it for teaching
Run the laptop track first (three commands) and confirm the tiny model writes coherent stories.
Run
pytest -m "not slow"and one smoke config per stage before any real run.For an objectives lesson, hold the SFT checkpoint, preference data and seed fixed, then run
--loss_type dpo,orpo,kto,simpoand compare implicit-reward accuracy and sample outputs, not only loss curves.Use the verifier reward for PPO/GRPO demos; show the arithmetic curriculum's role by removing it once.
Pair
eval_base.pywith lm-evaluation-harness on GPT-2 as a lesson in validating your own eval code.
Related
R097 MiniMind and R120 nanochat (the other two from-scratch training repos), R098 Stanford CS329A (theory behind GRPO and verification), R126 (async GRPO used for real on a 27B model), R053 (small from-scratch models as a bench for poisoning studies).