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.
Build decoding, SFT, LoRA, reward modeling, PPO, preference optimization, evaluation, and a model comparison interface.
Every source step is its own lesson with intuition, concepts, correctly rendered MathJax mathematics, implementation, tests, mistakes, and a checkpoint.
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.
Construct a synthetic instruction dataset and build the formatting, tokenization, label masking, padding, batching, and train/val splitting utilities needed for supervised fine-tuning.
Implement the optimization machinery for supervised fine-tuning: shifted next-token loss, AdamW, warmup, gradient clipping, accumulation, training and evaluation steps.
Add low-rank adapters on top of distilgpt2 by implementing the LoRA delta, forward pass, initialization, parameter freezing, counting, and merge-back routines.
Build a synthetic preference dataset and train a reward head with pairwise losses, BCE alternatives, and pairwise accuracy diagnostics.
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.
Implement modern reference-based and reference-free preference losses including DPO, IPO, KTO, ORPO, and SimPO as drop-in replacements for the PPO pipeline.
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.