Fine-tune Llama 2 with DPO
Direct Preference Optimization (DPO) replaces the reward-model and RL steps of RLHF with a simple binary cross-entropy loss over preference pairs, using a frozen reference model to provide an implicit reward. With TRL's DPOTrainer, data is formatted as prompt/chosen/rejected triples and training is controlled by a beta temperature, typically 0.1 to 0.5. The walkthrough fine-tunes Llama 2 7B on the stack-exchange preference dataset: first SFT with QLoRA (4-bit NF4 plus LoRA adapters), then DPO using the SFT model as both policy and reference. Progress is monitored with logged reward metrics (chosen, rejected, accuracies, margins), where margins should grow and accuracies should approach 1.0.
Related posts
- Preference Tuning LLMs with Direct Preference Optimization Methods
Hugging Face hardMasterAn empirical comparison of three RL-free preference alignment methods (DPO, IPO and KTO) on two SFT'd 7B models, Zephyr-7b-beta-sft and OpenHermes-2.5-Mistral-7B, sweeping β from 0.01 to 0.9 with TRL's DPOTrainer and scoring the results on MT-Bench. After a fix to TRL's IPO implementation, which now averages rather than sums log-likelihoods, DPO and IPO perform comparably and both beat KTO in the paired-preference setting. The best β varies sharply by model and algorithm: 0.01 works best for Zephyr, while OpenHermes favours 0.6, 0.3 and 0.01. That makes β tuning essential. KTO remains attractive because it only needs thumbs-up/down labels rather than paired preferences, and configs and sweep scripts are published in the alignment-handbook.
- StackLLaMA: A hands-on guide to train LLaMA with RLHF
Hugging Face hardMasterA 7B LLaMA model is trained to answer Stack Exchange questions with the full RLHF pipeline: supervised fine-tuning on packed Stack Exchange text, a pairwise reward model trained on upvote-derived preference scores, and PPO with a KL penalty against a frozen reference model. Memory is kept manageable by loading the model in 8-bit and training only LoRA adapters via peft, which needs roughly 1.2-1.4GB per billion parameters. The same frozen base serves as both policy and reference model, and data parallelism through accelerate or torchrun speeds up training. The reward model reaches 67% accuracy, and the RL run takes about 20 hours on 24 A100s. The post closes with practical notes on reward hacking and training instabilities.
- Putting RL back in RLHF
Hugging Face hardMasterRLOO (REINFORCE Leave-One-Out) is now available as a trainer in Hugging Face TRL. It is an online RLHF algorithm that drops PPO's value model. It treats the whole completion as a single action, uses the REINFORCE loss, and computes each sample's baseline as the mean reward of the other k−1 completions for the same prompt. That cuts vRAM by roughly 50-70% and makes training 2-3x faster than PPO on Pythia 1B and 6.9B, while matching PPO's GPT-4-judged win rate (78.7% for 6.9B on TL;DR) and beating DPO. Code walkthroughs show the advantage computation and that REINFORCE is a special case of the PPO loss. There is a caveat: bf16 numerical mismatches between generation and training logprobs hurt RLOO more than PPO.
- Fine-tuning open LLM judges to outperform GPT-5.2
Together AI mediumAdeptDPO fine-tuning open-weight LLM judges on RewardBench 2 preference data lets them match or beat GPT-5.2 at agreeing with human preference labels. GPT-OSS 120B was trained with LoRA on 5,407 preference pairs built from ~1,500 examples (beta 0.1, LR 5e-6, 3 epochs, ~1.5 hours) and rose from 57.91% to 62.63% accuracy, against GPT-5.2's 61.62%, at about 15x lower cost and 14x higher throughput. Qwen3 235B already beat GPT-5.2 at baseline (62.63%) but dropped slightly to 61.28% after DPO. Category analysis shows Safety and Factuality are easy for judges while Focus and precise instruction following remain hard, and high tie counts point to positional bias.
- Preference Optimization for Vision Language Models
Hugging Face hardMasterTRL now supports direct preference optimization (DPO) for vision language models, trained on chosen/rejected answer pairs rather than fixed labels. A walkthrough fine-tunes Idefics2-8b on the RLAIF-V dataset, formatting image-plus-question prompts with the processor's chat template. Full float32 DPO training would need about 160 GB (policy, reference model, gradients, AdamW states), but bfloat16 plus LoRA cuts trainable parameters to 55M and memory to roughly 32 GB. Batch size 2 with 32 gradient accumulation steps and gradient checkpointing fits activations, and training is judged by rising accuracy and reward margin.