#2323rd of 65 in Learning and cookbooks

train-llm-from-scratch

Hand-written PyTorch pipeline from raw text to a pretrained, aligned LLM

Stars
13.1k
License
MIT
Last commit
Oct 2026
Language
Python
Stars, last 30 weeks+12,541
4 Apr 2026: 536 stars11 Apr 2026: 537 stars18 Apr 2026: 542 stars25 Apr 2026: 548 stars2 May 2026: 552 stars9 May 2026: 560 stars16 May 2026: 567 stars23 May 2026: 1,536 stars30 May 2026: 2,352 stars6 Jun 2026: 4,275 stars13 Jun 2026: 5,954 stars20 Jun 2026: 7,019 stars27 Jun 2026: 7,461 stars4 Jul 2026: 7,962 stars11 Jul 2026: 8,171 stars18 Jul 2026: 8,316 stars25 Jul 2026: 8,589 stars1 Aug 2026: 8,788 stars8 Aug 2026: 8,907 stars15 Aug 2026: 9,057 stars22 Aug 2026: 9,291 stars29 Aug 2026: 9,431 stars5 Sept 2026: 9,493 stars12 Sept 2026: 9,560 stars19 Sept 2026: 9,923 stars26 Sept 2026: 11,249 stars3 Oct 2026: 11,930 stars10 Oct 2026: 12,041 stars17 Oct 2026: 12,166 stars24 Oct 2026: 12,494 stars31 Oct 2026: 13,077 stars

536 on 4 Apr 202613,077 on 31 Oct 2026

From GitHub's daily star counts.

Checked on GitHub.

Overview

Tutorial repo that implements a transformer in plain PyTorch and trains it from raw text through SFT, reward model, PPO, DPO and GRPO, without trl, peft or transformers. It has a classic GPT-style model and a modern one (RoPE, RMSNorm, SwiGLU, GQA/MLA, MoE, KV cache), plus a 369K-parameter preset that trains on a laptop CPU. A Streamlit control panel and an MkDocs site are included.

Who it is for: Students and engineers learning how LLMs are pretrained and aligned

Strengths

  • Pretraining and post-training (SFT, reward, PPO, DPO/ORPO/KTO, GRPO) all hand-written in PyTorch
  • Tiny preset trains on a laptop CPU in about five minutes
  • Typed JSON configs per stage, smoke configs that finish in seconds, and pytest coverage
  • Modern model option with Muon, LoRA, speculative decoding and int8 weights

Weaknesses

  • Educational scale: the sample output from the 13M model is largely incoherent
  • Billion-parameter training needs a 24 GB+ GPU; larger configs are marked TBD on RTX 5090
  • Two config systems (legacy constants and JSON) add confusion
  • No release tags and no Docker setup

What it needs

  • GPU optional
  • Needs PyTorch, tiktoken, Hugging Face datasets, Streamlit, wandb, HDF5
  • Models: Custom transformer (classic and modern), PyTorch checkpoints

Quick answers

Is train-llm-from-scratch open source?

Yes. Its code is on GitHub under the MIT license.

Does train-llm-from-scratch need a GPU?

No. It runs on a CPU and can use a GPU when you have one.

Also in Learning and cookbooks

See all 65
Also in Learning and cookbooks
RankProjectScore
1ai-engineering-from-scratchHands-on AI engineering curriculum with 542 lessons across 20 phases76.2k starsMIT89 out of 100
2generative-ai-for-beginners21-lesson course on building generative AI apps in Python and TypeScript122.7k starsMIT81 out of 100
3rag_techniquesCollection of 42 runnable notebooks implementing RAG techniques30.2k starscustom license79 out of 100
#4cs249r_bookOpen textbook and course materials for machine learning systems engineering29.8k starscustom license79 out of 100
#5ai-agents-for-beginners18-lesson course on building AI agents with Microsoft Agent Framework78.6k starsMIT78 out of 100