---
title: "Fine-tune Llama 2 with DPO"
description: "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."
url: "https://swe.quest/articles/fine-tune-llama-2-with-dpo-68d2e917aae1caca"
canonical: "https://huggingface.co/blog/dpo-trl"
source: "Hugging Face"
published: "2023-08-08"
difficulty: "hard"
kind: "How-to"
reading_minutes: 7
tags: ["model-training","llms","python"]
---

# Fine-tune Llama 2 with DPO

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2023-08-08, 7 min read, How-to. Original: [https://huggingface.co/blog/dpo-trl](https://huggingface.co/blog/dpo-trl)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- RLHF basics ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- LLM fine-tuning ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))
- Hugging Face Transformers
- LoRA adapters

## Keywords

DPO, TRL, Llama 2, QLoRA, PEFT, bitsandbytes, RLHF, PPO

## Related posts

- [Preference Tuning LLMs with Direct Preference Optimization Methods](https://swe.quest/articles/preference-tuning-llms-with-direct-preference-optimization-methods-6edec5434e6d7272.md): Hugging Face · 2024-01-18 · Hard · 9 min read · Model training, LLMs, Evaluation
  An 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.
- [StackLLaMA: A hands-on guide to train LLaMA with RLHF](https://swe.quest/articles/stackllama-a-hands-on-guide-to-train-llama-with-rlhf-9d42cc2a0ae4620b.md): Hugging Face · 2023-04-05 · Hard · 14 min read · Model training, LLMs, Machine learning, Python
  A 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.
- [Putting RL back in RLHF](https://swe.quest/articles/putting-rl-back-in-rlhf-dbdbc5d62840e65d.md): Hugging Face · 2024-06-12 · Hard · 11 min read · Model training, LLMs, Python
  RLOO (REINFORCE Leave-One-Out) is now available as a trainer in Hugging Face TRL.
- [Fine-tuning open LLM judges to outperform GPT-5.2](https://swe.quest/articles/fine-tuning-open-llm-judges-to-outperform-gpt-5-2-fdf1c14f4206aa6b.md): Together AI · 2026-02-02 · Medium · 10 min read · Evaluation, Model training, LLMs
  DPO 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.
- [Preference Optimization for Vision Language Models](https://swe.quest/articles/preference-optimization-for-vision-language-models-a84799e96613bf21.md): Hugging Face · 2024-07-10 · Hard · 11 min read · Model training, Computer vision, LLMs, Python
  TRL now supports direct preference optimization (DPO) for vision language models, trained on chosen/rejected answer pairs rather than fixed labels.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
