fine-tuning-with-trl
Agent skill fromOrchestra-Research/AI-Research-SKILLs
Fine-tune LLMs using reinforcement learning with TRL - SFT for instruction tuning, DPO for preference alignment, PPO/GRPO for reward optimization, and reward model training. Use when need RLHF, align model with preferences, or train from human feedback. Works with HuggingFace Transformers.
How to install fine-tuning-with-trl
install fine-tuning-with-trl
# install the AI-Research-SKILLs pack with the skills CLI
npx skills add Orchestra-Research/AI-Research-SKILLs# or add only this skill to Claude Code: copy the folder
06-post-training/trl-fine-tuning/
# into
~/.claude/skills/trl-fine-tuning/ (all projects)
.claude/skills/trl-fine-tuning/ (one project)
1# TRL - Transformer Reinforcement Learning23## Quick start45TRL provides post-training methods for aligning language models with human preferences.67**Installation**:8```bash9pip install trl transformers datasets peft accelerate10```1112**Supervised Fine-Tuning** (instruction tuning):13```python14from trl import SFTTrainer1516trainer = SFTTrainer(17 model="Qwen/Qwen2.5-0.5B",18 train_dataset=dataset, # Prompt-completion pairs19)20trainer.train()21```2223**DPO** (align with preferences):24```python25from trl import DPOTrainer, DPOConfig2627config = DPOConfig(output_dir="model-dpo", beta=0.1)28trainer = DPOTrainer(29 model=model,30 args=config,31 train_dataset=preference_dataset, # chosen/rejected pairs32 processing_class=tokenizer33)34trainer.train()35```3637## Common workflows3839### Workflow 1: Full RLHF pipeline (SFT → Reward Model → PPO)4041Complete pipeline from base model to human-aligned model.4243Copy this checklist:4445```46RLHF Training:47- [ ] Step 1: Supervised fine-tuning (SFT)48- [ ] Step 2: Train reward model49- [ ] Step 3: PPO reinforcement learning50- [ ] Step 4: Evaluate aligned model51```5253**Step 1: Supervised fine-tuning**5455Train base model on instruction-following data:5657```python58from transformers import AutoModelForCausalLM, AutoTokenizer59from trl import SFTTrainer, SFTConfig60from datasets import load_dataset6162# Load model63model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B")64tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B")65…
Excerpt shown under the repository's MIT license.
