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 源码

参考文章:

相关推荐
临街的小孩2 天前
Docker 容器访问宿主机 Ollama 服务配置教程
llama·argflow
鸿蒙小白龙2 天前
OpenHarmony平台大语言模型本地推理:llama深度适配与部署技术详解
人工智能·语言模型·harmonyos·鸿蒙·鸿蒙系统·llama·open harmony
AI大模型5 天前
轻松搞定百个大模型微调!LLaMA-Factory:你的AI模型量产神器
程序员·llm·llama
fly五行9 天前
大模型基础入门与 RAG 实战:从理论到 llama-index 项目搭建(有具体代码示例)
python·ai·llama·llamaindex
德育处主任Pro13 天前
前端玩转大模型,DeepSeek-R1 蒸馏 Llama 模型的 Bedrock 部署
前端·llama
relis13 天前
AVX-512深度实现分析:从原理到LLaMA.cpp的性能优化艺术
性能优化·llama
relis15 天前
llama.cpp RMSNorm CUDA 优化分析报告
算法·llama
云雾J视界15 天前
开源革命下的研发突围:Meta Llama系列模型的知识整合实践与启示
meta·开源·llama·知识管理·知识整合·知识迭代·知识共享
丁学文武16 天前
大模型原理与实践:第三章-预训练语言模型详解_第3部分-Decoder-Only(GPT、LLama、GLM)
人工智能·gpt·语言模型·自然语言处理·大模型·llama·glm
余衫马17 天前
llama.cpp:本地大模型推理的高性能 C++ 框架
c++·人工智能·llm·llama·大模型部署