Views
No views yet
A professional, modular pipeline to fine-tune Llama-3 8B using Direct Preference Optimization (DPO) — from training to inference to benchmarking.
| Concept | What We Did |
|---|---|
| Base Model | unsloth/llama-3-8b-Instruct-bnb-4bit |
| Alignment Technique | Direct Preference Optimization (DPO) |
| Dataset | Intel/orca_dpo_pairs (1,000 samples) |
| Speed Optimization | Unsloth (2x faster training) |
| Memory Optimization | 4-bit quantization + Gradient Checkpointing |
| Environment | Kaggle T4 x2 GPU (Free Tier) |
| Trained Adapter | 🤗 Karan6124/llama3-8b-dpo-orca-adapter |
llama3-dpo-alignment-pipeline/
├── 📁 configs/
│ ├── dpo_config.yaml # All DPO training hyperparameters
│ └── benchmark_config.yaml # Test prompts & generation settings
├── 📁 scripts/
│ └── train_dpo.py # The main training engine (reads from configs/)
├── 📁 inference/
│ └── inference.py # Load adapter and run interactive inference
├── 📁 evaluation/
│ └── benchmark.py # Compare Base vs. Aligned model side-by-side
├── 📁 training/
│ └── training-llama3-dpo.ipynb # The original Kaggle notebook
├── 📁 models/ # (gitignored) Local adapter weights live here
├── pyproject.toml # Dependency management with uv
└── README.mduv1# Windows
2powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"1git clone https://github.com/Edge-Explorer/llama3-dpo-alignment-pipeline.git
2cd llama3-dpo-alignment-pipeline
3uv syncNote: Unsloth requires an NVIDIA GPU to import. For local development, you can write and review code without a GPU. Use Kaggle or Google Colab to actually run the scripts.
configs/dpo_config.yaml. You can tweak learning rates, batch sizes, and sequence lengths without touching any Python code.1# On Kaggle or a GPU machine:
2uv run python scripts/train_dpo.pybeta: 0.1 (DPO temperature)learning_rate: 5e-6per_device_train_batch_size: 1gradient_accumulation_steps: 8max_length: 7681# Make sure the adapter is in models/llama3_dpo_adapter/
2uv run python inference/inference.py1from unsloth import FastLanguageModel
2
3model, tokenizer = FastLanguageModel.from_pretrained(
4 model_name = "Karan6124/llama3-8b-dpo-orca-adapter",
5 max_seq_length = 2048,
6 load_in_4bit = True,
7)
8FastLanguageModel.for_inference(model)
9
10messages = [{"role": "user", "content": "Why use DPO over SFT?"}]
11inputs = tokenizer.apply_chat_template(
12 messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
13).to("cuda")
14
15outputs = model.generate(**inputs, max_new_tokens=256)
16print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])uv run python evaluation/benchmark.pyevaluation/benchmark_report.txt.| Problem | Solution |
|---|---|
transformers 5.0.0 broke trl in Colab | Switched to Kaggle's stable environment |
DPOConfig not found in old trl | Pinned to trl>=0.12.0 |
OutOfMemoryError on T4 GPU | Reduced batch size to 1, enabled gradient checkpointing |
| Slow training | Unsloth's PatchDPOTrainer gave ~2x speedup |
| Messy notebook workflow | Refactored into reusable scripts + YAML configs |