This repo collects post-training methods for Large Language Models (LLMs) with small, focused implementations and runnable examples. The goal is to make alignment and reinforcement post-training practical, understandable, and reproducible.
- Post-training methods that start from a pretrained model.
- Minimal, readable implementations over full-scale training stacks.
- RL fundamentals in Gymnasium to build intuition for later LLM alignment.
- Two step-by-step blog series: build a tiny LLM, then post-train one (see Learning Path).
python3 -m venv .venv
. .venv/bin/activate
python -m pip install -U pip
pip install -r requirements.txtpython -m pytest tests/ -vgymnasium/: RL foundations (CartPole examples).chess/: Toy chess Q-learning (KQ vs K).tests/: unit and smoke tests (pytest).blog/: blog post drafts for the learning series.requirements.txt: Python dependencies.README.md: learning path and run instructions.
Runs a single random rollout to verify environment setup.
Code: gymnasium/cartpole_random.py
python gymnasium/cartpole_random.pyTrains a discretized Q-learning agent and evaluates it.
Code: gymnasium/cartpole_q_learning.py
python gymnasium/cartpole_q_learning.pyEvaluation renders by default.
Trains a deep Q-network with replay and a target network.
Code: gymnasium/cartpole_dqn.py
python gymnasium/cartpole_dqn.pyEvaluation renders by default.
Trains an actor-critic with the PPO clipped objective and GAE.
Code: gymnasium/cartpole_ppo.py
python gymnasium/cartpole_ppo.pyEvaluation renders by default.
Trains a critic-free policy using group-normalized episode returns as advantages, the same mechanism GRPO uses for LLM post-training.
Code: gymnasium/cartpole_grpo.py
python gymnasium/cartpole_grpo.pyEvaluation renders by default.
Trains a Q-learning agent on a toy chess endgame (King + Queen vs King).
Code: chess/chess_q_learning.py
python chess/chess_q_learning.pyTwo build-from-scratch blog series, taken in order. They build up slowly: no post uses a concept that an earlier post has not taught, and every method is implemented from scratch in PyTorch first, then with a library.
%%{init: {'theme': 'neutral'}}%%
flowchart LR
A["Build a Tiny LLM from Scratch<br/>10 posts"] --> B["LLM Post-Training from Scratch<br/>22 posts"]
A -.-> C["Architectures in Code<br/>(later)"]
The concepts behind the architecture posts are covered separately in the LLM Model Architectures Deep Dive series; the posts here build what that series explains.
The shared foundation. Ends with a working tiny base model.
- Tensors and Matrix Multiplication: The Linear Algebra a Transformer Actually Uses
- Gradients by Hand, Then by Autograd
- Probability for Language Models: Softmax, Log-Probs, and Sampling
- Cross-Entropy, KL Divergence, and Entropy from Scratch
- Training Loops: AdamW, Learning Rate Schedules, and Reading Loss Curves
- Tokenizers, Special Tokens, and Chat Templates
- Embeddings, Attention, and the Causal Mask
- The Transformer Block: Residuals, LayerNorm, MLP, and the Next-Token Loss
- Pre-Training in Miniature: Training a Tiny Base Model
- Generating Text: Decoding, the KV Cache, and Stopping on EOS vs Max Length
One running project ties this series together: a Wordle-playing agent that starts as the raw Qwen2.5-0.5B base model. Each post-training method is applied to the same agent, and every post reports the same scoreboard (win rate, average guesses, illegal-move rate) on a fixed set of target words.
Opener
- Base Model vs Chat Model: Loading Qwen and Measuring What Post-Training Changes
RL basics
- RL in One Loop: States, Actions, Rewards, Q-Learning, and DQN
- Monte Carlo Estimates and the Log-Derivative Trick: Why RL Gradients Are Noisy
- REINFORCE from Scratch, and the Baseline That Tames Variance
- Actor-Critic: Value Functions, Advantages, GAE, and Importance Sampling
- Before You Touch an LLM: PPO and GRPO on CartPole
- From CartPole to Tokens: Text Generation as an RL Problem
Supervised fine-tuning
- SFT from Scratch: Instruction Data and Loss Masking on Your Tiny Model
- LoRA from Scratch, Then with PEFT: Fitting Qwen-0.5B on a Free GPU
- Evaluating a Fine-Tune: Held-Out Loss, Win Rates, and Regressions
Preferences
- Reward Models: Bradley-Terry Loss on Preference Pairs
- RLHF with PPO: The KL Penalty and the Reference Model
- Reward Hacking: Watching a Policy Game Its Reward Model
- DPO from Scratch: Deriving It from the RLHF Objective
- The DPO Family: IPO, KTO, ORPO, and SimPO on One Dataset
Verifiable rewards and reasoning
- RLVR: Verifiers, Best-of-N, and Rejection Sampling on Math Problems
- GRPO for LLMs from Scratch
- Building an RL Environment: A Sandboxed Verifier Harness
- Scaling RL for Reasoning: Response Length, Entropy Collapse, and GRPO Fixes
Wrap-up
- RLAIF and Constitutional AI: An LLM Judge as the Labeler
- Distillation: Teaching a Small Model from a Post-Trained One
- Capstone: SFT, DPO, and GRPO on One Small Model with One Eval Harness