大模型训练总结

概念

RLHF(Reinforcement Learning from Human Feedback)

基于人类反馈的强化学习

RLHF 是 SFT 的后续阶段,其核心是通过强化学习的方式进一步优化模型,使其输出更符合人类偏好。

示例:

阿里云-RLHF标注

RLHF工程框架

微软 DeepSpeed
https://github.com/microsoft/DeepSpeed

豆包 HybridFlow
HybridFlow: A Flexible and Efficient RLHF Framework https://arxiv.org/html/2409.19256v2
豆包大模型团队开源RLHF框架,破解强化学习训练部署难题
代码链接: https://github.com/volcengine/veRL

OpenRLHF
https://github.com/OpenRLHF/OpenRLHF
OpenRLHF 是一个基于 Ray、DeepSpeed 和 HF Transformers 构建的高性能 RLHF 框架:

SFT(Supervised Fine-Tuning)

SFT 是一种通过监督学习对预训练模型进行微调的方法。其核心思想是利用标注数据(输入-输出对)调整模型参数,使其适应特定任务。

SFT 的广义上包括所有基于监督学习的微调方法,而狭义上通常指 Full Fine-Tuning

P-tuning、LoRA 和 Full Fine-Tuning 都属于SFT的范畴,但它们在实现方式、参数更新策略和适用场景上有所不同。以下是它们的详细对比和关系分析:

Full Fine-Tuning(全量微调)

LoRA(Low-Rank Adaptation)

P-tuning

三者的关系

共同点:

区别:

方法 参数更新范围 计算成本 性能 适用场景
Full Fine-Tuning 更新所有参数 最强 数据充足、计算资源丰富
LoRA 更新少量参数(低秩矩阵) 较强 资源有限、数据较少
P-tuning 不更新模型参数,优化提示词 最低 依赖提示词设计 少样本学习、零样本学习

SFT最佳实践

SFT总结

DeepMind 的一篇研究 SFT Scaling law 的论文中探讨了不同数据量下不同训练方式的效果及其对泛化性的影响。该研究指出,当数据量仅在几千条时,P-tuning 是最佳选择;数据量在几千至万条之间时,Lora 更为适合;而当数据量达到百万级别时,Full-tunning 效果最佳。此外,使用 Full-tunning 会导致训练后的模型泛化性不如 Lora。

SFT的目的

激发预训练知识:虽然 SFT 不能直接学的新知识,但需要靠它激发模型在预训练中已学到的知识。

稳定格式输出:通过 SFT,我们可以训练模型以稳定的格式输出结果,便于线上的稳定。

更遵循具体任务:如多标签多分类时,模型老输出一些不在标签体系的任务。

学习业务逻辑:SFT 能够教导模型特定的业务规则,如让他习得 “买了 20 万以上的车算有钱人”。

SFT的局限性

pretrain 是在背书,纯粹的学习知识;sft 则是在做题,学习的是指令 follow 能力。切勿在 sft 阶段强行给模型做知识注入,比如训个 50W 条的 code 数据,所有的知识注入工作应该采用 continue-pretrain 的思路进行,否则都会使得模型的通用能力掉点明显(sft 做知识注入基本上是 100% 某个知识,但 continue-pretrain 做知识注入会控制在 10% ~ 20% 左右的比例)。

SFT数据格式

单轮对话

{"prompt": "What is the capital of France?", "completion": "Paris"}

指令跟随格式

{"instruc": "Translate the following sentence into English: 你好,世界。", "input": "你好,世界。", "output": "Hello, world."}

Alpaca数据格式

{
    "role": "system",
    "content": "You are a customer support agent for Xbox, mainly responsible for helping users solve problems they encounter with their Xbox devices. You are friendly and concise. You only provide factual answers related to Xbox issues.",
    "user": "Is Xbox better than PlayStation?",
    "assistant": "I'm sorry, but I can't provide a personal opinion. My main task is to assist you with any issues related to your Xbox device. Do you have any specific questions about Xbox?"
}

[推荐]多轮对话格式

{
    "messages": [
        {"role": "system", "content": "You are a customer support agent for Xbox, mainly responsible for helping users solve problems they encounter with their Xbox devices."},
        {"role": "user", "content": "Is Xbox better than PlayStation?"},
        {"role": "assistant", "content": "I'm sorry, but I can't provide a personal opinion. My main task is to assist you with any issues related to your Xbox device."},
        {"role": "user", "content": "I have a problem with my Xbox. Can you help me?"},
        {"role": "assistant", "content": "Of course, I'd be happy to help you with any issues related to your Xbox device. Please describe the problem in detail."}
    ]
}

SFT 工程框架

https://github.com/hiyouga/LLaMA-Factory

部署

多Lora部署

多-LoRA 服务让我们仅需部署一个基础模型。由于 LoRA 适配器很小,所以可以加载多个适配器,不用担心内存问题。最终效果相当于在一次部署中支持了多个经过微调的模型。

LoRA 权重的大小依秩和量化方法的不同而不同,但它们通常都非常小。直观印象: predibase/magicoder 为 13.6MB,不到 mistralai/Mistral-7B-v0.1 尺寸 (14.48GB) 的 1/1000。相对而言,将 30 个适配器加载到 RAM 中只会让 VRAM 增加 3%,这对于大多数部署来说都不成问题。

参考:

https://huggingface.co/blog/zh/multi-lora-serving

https://docs.infini-ai.com/fundamentals/guide/llm-multi-lora-deploy.html

参考资料

深度对比_ SFT、ReFT、RHLF、RLAIF、DPO、PPO

SFT RLHF DPO