RLHF from Scratch
ML JOURNEY / FULL WALKTHROUGH

RLHF from Scratch

Build decoding, SFT, LoRA, reward modeling, PPO, preference optimization, evaluation, and a model comparison interface.

8 parts65 individual lessons5.4 estimated hours325 XP available
RLHF from Scratch project artwork
0%0 of 65 complete
Start walkthrough
No compressed chapters.

Every source step is its own lesson with intuition, concepts, correctly rendered MathJax mathematics, implementation, tests, mistakes, and a checkpoint.

01
PART 1

Model Setup and Decoding Strategies

Load distilgpt2 and its tokenizer, sanity-check generation, and implement the core decoding primitives (greedy, temperature, top-k, top-p) used throughout the rest of the project.

0/8
001define model setup and decoding strategies+5 XP002derive model setup and decoding strategies+5 XP003prepare model setup and decoding strategies+5 XP004implement model setup and decoding strategies+5 XP005connect model setup and decoding strategies+5 XP006validate model setup and decoding strategies+5 XP007debug model setup and decoding strategies+5 XP008optimize model setup and decoding strategies+5 XP
02
PART 2

SFT Data Pipeline

Construct a synthetic instruction dataset and build the formatting, tokenization, label masking, padding, batching, and train/val splitting utilities needed for supervised fine-tuning.

0/11
009define sft data pipeline+5 XP010derive sft data pipeline+5 XP011prepare sft data pipeline+5 XP012implement sft data pipeline+5 XP013connect sft data pipeline+5 XP014validate sft data pipeline+5 XP015debug sft data pipeline+5 XP016optimize sft data pipeline+5 XP017benchmark sft data pipeline+5 XP018integrate sft data pipeline+5 XP019define sft data pipeline+5 XP
06
PART 6

PPO-Based RLHF

Implement the PPO ingredients required to optimize the policy against the learned reward: log-probs, KL penalties, returns, GAE, clipped surrogates, value loss, and entropy bonus.

0/11
041define ppo based rlhf+5 XP042derive ppo based rlhf+5 XP043prepare ppo based rlhf+5 XP044implement ppo based rlhf+5 XP045connect ppo based rlhf+5 XP046validate ppo based rlhf+5 XP047debug ppo based rlhf+5 XP048optimize ppo based rlhf+5 XP049benchmark ppo based rlhf+5 XP050integrate ppo based rlhf+5 XP051define ppo based rlhf+5 XP
07
PART 7

Preference Optimization Alternatives

Implement modern reference-based and reference-free preference losses including DPO, IPO, KTO, ORPO, and SimPO as drop-in replacements for the PPO pipeline.

0/7
052define preference optimization alternatives+5 XP053derive preference optimization alternatives+5 XP054prepare preference optimization alternatives+5 XP055implement preference optimization alternatives+5 XP056connect preference optimization alternatives+5 XP057validate preference optimization alternatives+5 XP058debug preference optimization alternatives+5 XP
08
PART 8

Evaluation and Chat Interface

Generate completions on a held-out prompt set, score and compare models via win rate, and wrap everything in a streaming chat function with stop-token handling.

0/7
059define evaluation and chat interface+5 XP060derive evaluation and chat interface+5 XP061prepare evaluation and chat interface+5 XP062implement evaluation and chat interface+5 XP063connect evaluation and chat interface+5 XP064validate evaluation and chat interface+5 XP065debug evaluation and chat interface+5 XP