StackMap
Subscribe
Explore / train-llm-from-scratch
FareedKhan-dev

train-llm-from-scratch

The full LLM pipeline hand-written in plain PyTorch — tokens, transformer, pretraining, then SFT, reward model, PPO, DPO, GRPO. No trl, no peft: read every algorithm, train on one GPU.

8,486 1,171 Python MITupdated 28 days ago
Curator's take

The textbook that runs: every stage from raw text to an aligned reasoning-style model, each algorithm implemented by hand in readable PyTorch — including the post-training alphabet (SFT → reward model → PPO/DPO → GRPO) that most tutorials wave at. If you want to understand what TRL actually does under its trainer classes, this is the fastest honest path, and it fits on a single GPU at the 13M–1B scale. NOT for production: hand-rolled training code at toy scale is the point, not the product — when you're done learning, ship with TRL or LLaMA-Factory. From the author of all-agentic-architectures, same runnable-textbook DNA.

Mapped by ShipWithAI editors · links verified
README.md

main image

Train LLM From Scratch

Python License Contributions Docs

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.

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.

From raw text to an aligned reasoning model

Here is the path we will walk, end to end:

raw text  ->  tokens  ->  a Transformer  ->  next-token loss  ->  a base model
base model  ->  SFT  ->  Reward Model  ->  {PPO, DPO}  ->  GRPO  ->  evaluation and chat

Below is the output of a trained 13 million parameter LLM, just so you can see where the small end of this starts:

In ***1978, The park was returned to the factory-plate that
the public share to the lower of the electronic fence that
follow from the Station's cities. The Canal of ancient Western
nations were confined to the city spot. The villages were directly
linked to cities in China that revolt that the US budget and in
Odambinais is uncertain and fortune established in rural areas.

Table of Contents

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.

Ev

Continue your stack

What teams reach for next — and why each earns a place beside train-llm-from-scratch. Ranked by curator confidence.