简介
LLM 训练通常包含预训练、微调和 RLHF。DPO 是 RLHF 中的一种方法,相比 PPO 等主流方法,DPO 对硬件资源需求更低。
硬件与环境
示例硬件:4070 12g*2、64g 内存、Ubuntu 24.04。模型选择 QWEN-3vl-2B(若无需多模态可下载纯语言模型)。
注意:若使用分布式训练,需确认 LLaMA-Factory 对 DeepSpeed 版本的兼容性。
步骤 1:下载数据集
从 Hugging Face 获取医疗 DPO 数据集。
from datasets import load_dataset
ds = load_dataset("HANI-LAB/Med-REFL-DPO", 'reasoning_enhancement')
print(ds['train'][:1])
步骤 2:数据预处理
将 Arrow 格式转换为 LLaMA-Factory 识别的 JSON 格式。
import json
from datasets import load_dataset
import os
def convert_arrow_to_json(dataset_path, output_json_path):
# 加载数据集
if os.path.exists(dataset_path):
dataset = load_dataset('arrow', data_files=dataset_path)
else:
dataset = load_dataset(dataset_path, name='reasoning_enhancement')
train_dataset = dataset['train']
output_data = []
for item in train_dataset:
if 'instruction' in item and 'chosen' in item and 'rejected' in item:
json_item = {
"instruction": item['instruction'],
"input": item.get('input', ),
: item[],
: item[]
}
output_data.append(json_item)
(output_json_path, , encoding=) f:
json.dump(output_data, f, ensure_ascii=, indent=)
()
__name__ == :
arrow_file_path =
output_json_path =
convert_arrow_to_json(arrow_file_path, output_json_path)

