LLM - 使用 LLaMA-Factory 微调 Qwen2-VL DPO(LoRA) 图像数据集 教程 (3)

欢迎关注我的CSDN:https://spike.blog.csdn.net/

本文地址:https://spike.blog.csdn.net/article/details/143725947

免责声明:本文来源于个人知识与公开资料,仅用于学术交流,欢迎讨论,不支持转载。


DPO(Direct Preference Optimization, 直接偏好优化) 是在 RLHF 阶段中使用的优化算法,通过直接利用人类的偏好数据来优化策略模型,无需定义明确的奖励函数或进行复杂的强化学习过程。DPO的优化目标是,增加偏好样本的对数概率与减小非偏好样本响应的对数概率,结合动态加权机制,以避免仅使用概率比目标时遇到的模型退化问题。

DPO 公式参考:大模型训练 RLHF 阶段的 PPO/DPO 策略公式与源码

相关文章:

  1. 使用 LLaMA-Factory 微调大模型 环境配置与训练推理
  2. 使用 LLaMA-Factory 微调 Qwen2-VL SFT(LoRA) 图像数据集
  3. 使用 LLaMA-Factory 微调 Qwen2-VL DPO(LoRA) 图像数据集

在 LlamaFactory 中的 DPO 训练脚本,参考 examples/train_lora/qwen2vl_lora_dpo.yaml

需要准备数据集:rlhf_v,参考 data/dataset_info.json 的数据集信息,即:

json 复制代码
"rlhf_v": {
  "hf_hub_url": "llamafactory/RLHF-V",
  "ranking": true,
  "formatting": "sharegpt",
  "columns": {
    "messages": "conversations",
    "chosen": "chosen",
    "rejected": "rejected",
    "images": "images"
  }
},

使用 HuggingFace 下载数据集 llamafactory/RLHF-V,即:

bash 复制代码
cd [your folder]/llm/datasets/
huggingface-cli download --token [your token] --repo-type dataset llamafactory/RLHF-V --local-dir llamafactory/RLHF-V

注意:huggingface-cli 的数据集参数 --repo-type dataset

配置 llama_factory 的 jupyter 环境,即:

bash 复制代码
pip install ipykernel
python -m ipykernel install --user --name llama_factory --display-name "llama_factory"

待训练的数据集是 rlhf-v.parquet,大约 348 M,包括 5733 条样本,显示样本信息,包括:

  • conversations,对话,也就是 LlamaFactory 的 messages
  • chosen,正样本
  • rejected,负样本
  • images,图像列表,注意是 bytes 格式
python 复制代码
import pandas as pd

# 读取 Parquet 文件
file_path = '[your folder]/llm/datasets/llamafactory/RLHF-V/rlhf-v.parquet'
df = pd.read_parquet(file_path)

first_row = df.iloc[0] 
print(f"[Info] first_row: \n{first_row}")

显示信息:

python 复制代码
print(f"[Info] first_row conversations: \n{first_row['conversations']}")
print(f"[Info] first_row chosen: \n{first_row['chosen']}")
print(f"[Info] first_row chosen: \n{first_row['rejected']}")
from IPython.display import display, Image
# 假设你有一个 bytes 形式的图像数据
image_bytes = first_row['images'][0]['bytes']
# 使用 IPython.display 显示图像
display(Image(data=image_bytes))

输出:

json 复制代码
[{'from': 'human', 'value': '<image>What are the key features you observe in the image?'}]
{'from': 'gpt', 'value': 'A young man standing on stage wearing a white shirt and black pants.'}
{'from': 'gpt', 'value': 'A young man standing on stage wearing white pants and shoes.'}

问题是描述图像的关键特征,正确是穿着白色T恤和黑色裤子,错误是穿着白色裤子和鞋,让模型辨识主体颜色,图像如下:

训练模型:

bash 复制代码
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 llamafactory-cli train [your folder]/llm/LLaMA-Factory/examples/train_lora/qwen2vl_lora_dpo_my20241112.yaml

A100 的显存是 81920M,第0卡显存占用最大,大约 58.122G (81920 * 1024 * 1024 * 67.66%),8 卡 A100 消耗 388G (58 + 48 *6 + 42)

在训练和验证指标中,除了 Loss 之外,包括 3 个部分,即 logits、rewards、logps,其中每个里面包括 chosen 和 rejected 两个部分。

  • rewards/chosen,chosen 的 policy_chosen_logps - reference_chosen_logps,值 0 ~ -12
  • rewards/rejected,rejected 的 policy_rejected_logps - reference_rejected_logps,值是 0 ~ -17
  • rewards/accuracies,正确率,0~1之间。
  • rewards/margins,差距的均值,逐渐增大,0~6之间。
  • logps/rejected,logps均值,-120~-250
  • logps/chosen,logps均值,-100~-280
  • logits/rejected,logits,-2.4 上下震荡
  • logits/chosen,logits,-2.4 上下震荡

源码:

python 复制代码
metrics[f"{prefix}rewards/chosen"] = chosen_rewards.mean().item()
metrics[f"{prefix}rewards/rejected"] = rejected_rewards.mean().item()
metrics[f"{prefix}rewards/accuracies"] = (chosen_rewards > rejected_rewards).float().mean().item()
metrics[f"{prefix}rewards/margins"] = (chosen_rewards - rejected_rewards).mean().item()
metrics[f"{prefix}logps/rejected"] = policy_chosen_logps.mean().item()
metrics[f"{prefix}logps/chosen"] = policy_rejected_logps.mean().item()
metrics[f"{prefix}logits/rejected"] = policy_chosen_logits.mean().item()
metrics[f"{prefix}logits/chosen"] = policy_rejected_logits.mean().item()

logits 定义: 在生成任务(如语言生成)中,模型在每一步的输出是一个 logits 向量,每个 logits 值对应于某个词汇(token)的预激活值。logits 经过 softmax 变换后,得到的是当前时刻每个词汇的概率分布。logps,即 Log Probabilities。

rewards 曲线:

logpslogits 曲线:

logitslogps 的计算公式,参考源码 src/llamafactory/train/trainer_utils.py

python 复制代码
def get_batch_logps(
    logits: "torch.Tensor", labels: "torch.Tensor", label_pad_token_id: int = IGNORE_INDEX
) -> Tuple["torch.Tensor", "torch.Tensor"]:
    r"""
    Computes the log probabilities of the given labels under the given logits.

    Returns:
        logps: A tensor of shape (batch_size,) containing the sum of log probabilities.
        valid_length: A tensor of shape (batch_size,) containing the number of non-masked tokens.
    """
    if logits.shape[:-1] != labels.shape:
        raise ValueError("Logits (batchsize x seqlen) and labels must have the same shape.")

    labels = labels[:, 1:].clone()
    logits = logits[:, :-1, :]
    loss_mask = labels != label_pad_token_id
    labels[labels == label_pad_token_id] = 0  # dummy token
    per_token_logps = torch.gather(logits.log_softmax(-1), dim=2, index=labels.unsqueeze(2)).squeeze(2)
    return (per_token_logps * loss_mask).sum(-1), loss_mask.sum(-1)

Loss 是下降的:

参考 TorchTune - DPO 源码

参考文章:

相关推荐
CV-杨帆4 小时前
复现 LLama Guard Llama-Prompt-Guard-2-86M / Llama-Prompt-Guard-2-22M
llama
学习是生活的调味剂4 小时前
LLaMA大模型家族发展介绍
人工智能·llama
love530love5 小时前
【笔记】解决 Stable Diffusion WebUI 启动 “找不到llama_cpp模块”
运维·windows·笔记·python·stable diffusion·github·llama
同学小张2 天前
【端侧AI 与 C++】1. llama.cpp源码编译与本地运行
开发语言·c++·aigc·llama·agi·ai-native
程序员与背包客_CoderZ7 天前
C/C++版LLM推理框架Llama.cpp——入门与编码实战
c语言·开发语言·网络·c++·人工智能·语言模型·llama
java_logo9 天前
LOBE-CHAT Docker 容器化部署指南
运维·docker·语言模型·容器·llama
AI大模型9 天前
手把手教你用LlamaIndex搭建RAG系统,让LLM告别“幻觉”,提升回答质量!
llm·agent·llama
开发者导航17 天前
【开发者导航】轻量可微调且开源的大语言模型家族:LLaMA
语言模型·开源·llama
缘友一世17 天前
借助LLama_Factory工具对大模型进行lora微调
llama
illuspas19 天前
MI50运算卡使用llama.cpp的ROCm后端运行Qwen3-Coder-30B-A3B的速度测试
人工智能·llama