关于 simpo-training
SimPO 是一种无需参考模型的偏好优化训练方法,适用于使用偏好数据微调语言模型的研究人员和开发者。该项目提供训练脚本与配置,可用于从基础模型训练或微调指令模型。
主要功能
- 01无需参考模型的偏好优化
- 02使用选择与拒绝样本训练
- 03提供模型训练脚本
- 04通过配置调整训练参数
适用场景
- 01基于偏好数据微调模型
- 02训练指令微调模型
- 03针对数学或代码任务调整训练
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ orchestra-research/ ai-research-skills --skill simpo-training安装后在 Claude Code 中输入/simpo-training确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/datasets.mdhyperparameters.mdloss-functions.md详细介绍
SimPO - 简单偏好优化
快速开始
SimPO 是一种无需参考模型的无参考偏好优化方法,性能优于 DPO。
安装:
# Create environment
conda create -n simpo python=3.10 && conda activate simpo
# Install PyTorch 2.2.2
# Visit: https://pytorch.org/get-started/locally/
# Install alignment-handbook
git clone https://github.com/huggingface/alignment-handbook.git
cd alignment-handbook
python -m pip install .
# Install Flash Attention 2
python -m pip install flash-attn --no-build-isolation
训练(Mistral 7B):
ACCELERATE_LOG_LEVEL=info accelerate launch \
--config_file accelerate_configs/deepspeed_zero3.yaml \
scripts/run_simpo.py \
training_configs/mistral-7b-base-simpo.yaml
常见工作流
工作流 1:从基础模型开始训练(Mistral 7B)
配置(mistral-7b-base-simpo.yaml):
# Model
model_name_or_path: mistralai/Mistral-7B-v0.1
torch_dtype: bfloat16
# Dataset
dataset_mixer:
HuggingFaceH4/ultrafeedback_binarized: 1.0
dataset_splits:
- train_prefs
- test_prefs
# SimPO hyperparameters
beta: 2.0 # Reward scaling (2.0-10.0)
gamma_beta_ratio: 0.5 # Target margin (0-1)
loss_type: sigmoid # sigmoid or hinge
sft_weight: 0.0 # Optional SFT regularization
# Training
learning_rate: 5e-7 # Critical: 3e-7 to 1e-6
num_train_epochs: 1
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
# Output
output_dir: ./outputs/mistral-7b-simpo
启动训练:
accelerate launch --config_file accelerate_configs/deepspeed_zero3.yaml \
scripts/run_simpo.py training_configs/mistral-7b-base-simpo.yaml
工作流 2:微调指令模型(Llama 3 8B)
配置(llama3-8b-instruct-simpo.yaml):
model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct
dataset_mixer:
argilla/ultrafeedback-binarized-preferences-cleaned: 1.0
beta: 2.5
gamma_beta_ratio: 0.5
learning_rate: 5e-7
sft_weight: 0.1 # Add SFT loss to preserve capabilities
num_train_epochs: 1
per_device_train_batch_size: 2
gradient_accumulation_steps: 4
output_dir: ./outputs/llama3-8b-simpo
启动:
accelerate launch --config_file accelerate_configs/deepspeed_zero3.yaml \
scripts/run_simpo.py training_configs/llama3-8b-instruct-simpo.yaml
工作流 3:推理密集型任务(较低学习率)
用于数学/代码任务:
model_name_or_path: deepseek-ai/deepseek-math-7b-base
dataset_mixer:
argilla/distilabel-math-preference-dpo: 1.0
beta: 5.0 # Higher for stronger signal
gamma_beta_ratio: 0.7 # Larger margin
learning_rate: 3e-7 # Lower LR for reasoning
sft_weight: 0.0
num_train_epochs: 1
per_device_train_batch_size: 1
gradient_accumulation_steps: 16
何时使用 SimPO 或其他方案
在以下情况下使用 SimPO:
- 希望训练过程比 DPO 更简单(无需参考模型)
- 有偏好数据(chosen/rejected 对)
- 需要比 DPO 更好的性能
- 计算资源有限
- 单节点训练即可满足需求
算法选择:
- SimPO:最简单、性能最佳,无需参考模型
- DPO:需要参考模型基线,更保守
- PPO:控制能力最强,需要奖励模型,设置复杂
- GRPO:节省内存的强化学习,无需评论家模型
以下情况请改用其他方案:
- OpenRLHF:多节点分布式训练、PPO/GRPO
- TRL:需要在一个框架中使用多种方法
- DPO:需要成熟的基线进行对比
常见问题
问题:损失发散
降低学习率:
learning_rate: 3e-7 # Reduce from 5e-7
降低 beta:
beta: 1.0 # Reduce from 2.0
问题:模型遗忘原有能力
添加 SFT 正则化:
sft_weight: 0.1 # Add SFT loss component
问题:偏好区分度较差
增大 beta 和 margin:
beta: 5.0 # Increase from 2.0
gamma_beta_ratio: 0.8 # Increase from 0.5
问题:训练期间发生 OOM
减小批次大小:
per_device_train_batch_size: 1
gradient_accumulation_steps: 16 # Maintain effective batch
启用梯度检查点:
gradient_checkpointing: true
进阶主题
损失函数:请参阅 references/loss-functions.md,了解 sigmoid 与 hinge 损失、数学公式及各自的适用场景。
超参数调优:请参阅 references/hyperparameters.md,了解 beta、gamma 和学习率的选择指南,以及针对不同模型规模的建议。
数据集准备:请参阅 references/datasets.md,了解偏好数据格式、质量筛选和自定义数据集创建。
硬件要求
- GPU:推荐使用 NVIDIA A100/H100
- 显存:
- 7B 模型:1× A100 40GB(DeepSpeed ZeRO-3)
- 8B 模型:2× A100 40GB
- 70B 模型:8× A100 80GB
- 单节点:DeepSpeed ZeRO-3 即可满足需求
- 混合精度:推荐使用 BF16
内存优化:
- DeepSpeed ZeRO-3(默认配置)
- 梯度检查点
- Flash Attention 2
资源
- 论文:https://arxiv.org/abs/2405.14734(NeurIPS 2024)
- GitHub:https://github.com/princeton-nlp/SimPO
- 模型:https://huggingface.co/princeton-nlp
- Alignment Handbook:https://github.com/huggingface/alignment-handbook
评论
支持 Markdown