📈 GH Trending Explorer 🎲 Surprise me

← all repos

FareedKhan-dev / train-llm-from-scratch

git clone https://github.com/FareedKhan-dev/train-llm-from-scratch.git

A straightforward method for training your LLM, from downloading data to generating text.

+7
stars, 24h · #2,064
+~49
stars, 7 days · #2,579
+~210
stars, 30 days · #2,521
12k
★ total · +0% this week
1.7k
forks · 2 open issues

Star history (as recorded here)

2026-10-20 · 12,391 ★ 2026-10-22 · 12,418 ★
LanguagePython
LicenseMIT
Created2025-01-12 (1y ago)
Last push20d ago
Watchers91
OwnerUser

GitHub topics

Links

README excerpt
# Train LLM From Scratch
**I am Looking for a PhD position in AI**. GitHub
I implemented a transformer model from scratch using PyTorch, based on the paper Attention is All You Need. You can use my scripts to train your own **billion** or **million** parameter LLM using a single GPU, or a small one on a laptop CPU with no GPU at all.
This started as a pretraining tutorial. It now goes all the way from raw text to an aligned, reasoning style model, with every algorithm hand written in plain PyTorch (no `trl`, no `peft`, no `transformers`). The whole journey is one idea repeated: turn text into numbers, predict the next token, then keep changing the data and the loss until the model does what we want.
Next to the original model there is now a modern one, built the way current open models are (rotary positions, RMSNorm, SwiGLU, grouped-query and latent attention, Mixture of Experts, a KV cache), and every stage can train either. The newer training tricks are here too, each written out by hand: the Muon optimizer, LoRA, IPO and SimPO, Dr. GRPO, DAPO and GSPO, speculative decoding and int8 weights.
Here is the path we will walk, end to end:
 [code] 
Below is the output of a trained 13 million parameter LLM, just so you can see where the small end of this starts:
 [code] 
## Table of Contents
- Who this is for
- Prerequisites and Training Time
- Setup
- No GPU? Train on Your Laptop
- Code Structure
- Step 1: Preparing the Data
- Step 2: The Model, Built From Small Pieces
 - Multi Layer Perceptron (MLP)
 - Single Head Attention
 - Multi Head Attention
 - The Transformer Block
 - The Full Transformer
 - The Modern Version of the Same Model
- Step 3: Pretraining the Base Model
- Step 4: Generating Text
- Step 5: Post-Training, Turning a Base Model Into an Assistant
 - SFT (Supervised Fine-Tuning)
 - The Reward Model
 - DPO, ORPO and KTO
 - PPO
 - GRPO / RLVR
- Step 6: Evaluation
- Step 7: Talking to the Model
- The Streamlit Control Panel
- The Documentation Site
- Run the Whole Thing
- Type Safety and Tests
- What's Next
## Who this is for
I tried to write this so one page works for very different readers:
- If you are a **student**, read top to bottom. Every block of code comes after a plain explanation of what it does and why, and most blocks are followed by the output you should expect.
- If you are a **developer**, the commands and file paths are all here. You can copy, run, and read the referenced source files directly.
- If you are a **researcher**, the post-training half is the interesting part: SFT, a Bradley-Terry reward model, PPO with GAE, DPO/ORPO/KTO, and GRPO, all from scratch on the same small Transformer, trained on real public datasets.
Every diagram in this README is colored the same way, so the colors mean something:
- green is raw data
- teal is stored, tokenized data on disk
- blue is a plain processing step
- yellow is the model or a training step
- orange is the reinforcement learning and reward parts
- red is a loss
- grey is a sa