This guide covers training models using Direct Preference Optimization (DPO) on three different datasets with varying diversity criteria.
# Install dependencies (if not already installed)
pip install tinker datasets pandas
# Set your Tinker API key
export TINKER_API_KEY="your-api-key-here"
# (Optional) Login to WandB for tracking
wandb loginMake sure you have generated your DPO datasets:
scripts/dataset1_embedding_diversity.parquet✓scripts/dataset2_quality_based.parquet✓scripts/dataset3_llm_judge_diversity.parquet(when ready)
Note: Training scripts use key=value syntax (no -- dashes).
# Train Qwen 8B on Dataset 1
python3 scripts/train_dpo.py model_name=Qwen/Qwen3-8B dataset=1 wandb_project=cs329x-dposcripts/
├── train_dpo.py # Main training script
├── train_dpo_examples.sh # Example commands
├── inference_example.py # How to use trained models
├── dataset1_embedding_diversity.parquet
├── dataset2_quality_based.parquet
└── dataset3_llm_judge_diversity.parquet (when ready)
experiments/ # Auto-generated during training
└── dataset1_qwen34b_lr1e-05_beta0.1_2024-12-05/
├── checkpoints.jsonl # Contains tinker:// paths to weights
├── metrics.jsonl # Training metrics
└── config.json # Training configuration
model_name: Base model to fine-tuneQwen/Qwen3-4B-Instruct-2507Qwen/Qwen3-8BQwen/Qwen3-30B-A3B-Instruct-2507
dataset: Dataset number (1, 2, or 3)
learning_rate: Default1e-5dpo_beta: Default0.1(controls preference strength)batch_size: Default256num_epochs: Default1lora_rank: Default32
log_path: Auto-generated asexperiments/dataset{N}_{model}_{lr}_{beta}_{date}/wandb_project: WandB project name (e.g.,cs329x-dpo)wandb_name: Auto-generated ifwandb_projectis set
# Train on Dataset 1 with Qwen 8B
python3 scripts/train_dpo.py model_name=Qwen/Qwen3-8B dataset=1 wandb_project=cs329x-dpoOutput:
- Experiment folder:
experiments/dataset1_qwen34b_lr1e-05_beta0.1_2024-12-05/ - WandB run:
dataset1_qwen34b_lr1e-05_beta0.1_2024-12-05 - Weights saved to Tinker cloud (paths in
checkpoints.jsonl)
# Try different DPO betas
python3 scripts/train_dpo.py model_name=Qwen/Qwen3-8B dataset=1 dpo_beta=0.1 wandb_project=cs329x-dpo
python3 scripts/train_dpo.py model_name=Qwen/Qwen3-8B dataset=1 dpo_beta=0.2 wandb_project=cs329x-dpo
python3 scripts/train_dpo.py model_name=Qwen/Qwen3-8B dataset=1 dpo_beta=0.05 wandb_project=cs329x-dpo# Run all combinations
for dataset in 1 2 3; do
for model in "Qwen/Qwen3-4B-Instruct-2507" "Qwen/Qwen3-8B" "Qwen/Qwen3-30B-A3B-Instruct-2507"; do
python3 scripts/train_dpo.py model_name=$model dataset=$dataset wandb_project=cs329x-dpo
done
doneAfter training, your model weights are saved to Tinker's cloud. To find them:
# Look at the checkpoints file
cat experiments/dataset1_qwen34b_lr1e-05_beta0.1_2024-12-05/checkpoints.jsonlYou'll see output like:
{
"name": "final",
"epoch": 1,
"batch": 150,
"state_path": "tinker://abc123.../state",
"sampler_path": "tinker://abc123.../sampler_weights"
}The sampler_path is what you need for inference!
from tinker_cookbook.completers import TinkerSampler
sampler = TinkerSampler(
model_name="Qwen/Qwen3-4B-Instruct-2507",
model_path="tinker://...", # From checkpoints.jsonl
temperature=0.7,
max_tokens=2048,
)
response = sampler.complete("Your prompt here")
print(response)# Edit inference_example.py to point to your experiment folder
python scripts/inference_example.py- Create a WandB team (free tier supports this)
- All team members join
- Use team name:
--wandb_project=team-name/cs329x-dpo
- One person creates project:
--wandb_project=cs329x-dpo - Share access with teammates
- Loss curves (DPO loss, accuracy)
- Learning rate schedule
- Preference margins
- Chosen vs rejected rewards
# View metrics
cat experiments/dataset1_qwen34b_lr1e-05_beta0.1_2024-12-05/metrics.jsonl | tail -20FileNotFoundError: Dataset file not found: scripts/dataset1_embedding_diversity.parquet
Solution: Generate the dataset first using pairs_creation.ipynb
# Try smaller batch size
python scripts/train_dpo.py ... batch_size=128# Check checkpoints file
cat experiments/your-experiment/checkpoints.jsonl- DPO Loss: Should decrease (indicates learning)
- Accuracy: % of times chosen > rejected (should increase)
- Margin: Difference between chosen and rejected rewards (should increase)
- Chosen Reward: Should increase
- Rejected Reward: Should decrease
- Evaluate diversity: Run inference on prompts with varying creativity scores
- Compare datasets: Does Dataset 1 (embedding) vs Dataset 2 (quality) vs Dataset 3 (LLM judge) produce different behaviors?
- Scaling analysis: Compare Qwen 4B vs 8B vs 30B - does model size affect diversity adaptation?
Check:
train_dpo_examples.shfor more command examplesinference_example.pyfor inference code- Tinker docs: https://tinker-docs.thinkingmachines.ai/