PaddleNLP 中 BLOOM 大模型配置与微调量化实战指南
2026/9/24 8:52:28 网站建设 项目流程
  • 人工智能
  • 大模型
  • 预训练
  • 微调
  • LoRA
  • RLHF
  • 强化学习
  • 分布式训练

【免费下载链接】PaddleNLP

Easy-to-use and powerful LLM and SLM library with awesome model zoo.

项目地址:https://gitcode.com/gh_mirrors/pa/PaddleNLP
点击查看免费下载

导读

本文以 PaddleNLP 仓库中 BLOOM 模型配置文档(docs/en/llm/config/bloom/README.md)为核心骨架,系统讲解 BLOOM 系列大语言模型在 PaddleNLP 中的落地方式。你将掌握:BLOOM/BloomZ 模型的特性与仓库支持的权重列表、五种开箱即用的训练与量化配置文件(SFT、LoRA、Prefix Tuning、PTQ、GPTQ)的逐项参数含义,以及如何结合 llm/config/bloom/ 目录下的配置与 llm/run_finetune.py、llm/run_quantization.py 启动微调和量化实验。

1. BLOOM 模型介绍

BLOOM 是一个自回归(autoregressive)大型语言模型(LLM),在大量文本数据上训练,用于生成目标文本。其核心能力如下:

  • 多语言支持:支持 46 种语言和 13 种编程语言的文本交互;
  • 核心任务:主要基于文本生成任务训练,可以很好地完成文本续写(text continuation)任务;
  • BloomZ 变体:BloomZ 系列模型在 BLOOM 基础上加入了Instruction Tuning(指令微调),能够更好地理解并执行以指令形式给出的任务。

在 PaddleNLP 中,BLOOM 的完整实现位于 paddlenlp/transformers/bloom/,包含:

  • modeling.py:自回归语言模型(CausalLM)核心实现;
  • configuration.py:模型配置类,定义了vocab_sizehidden_sizen_layern_headhidden_dropoutattention_dropout等关键超参数;
  • tokenizer.py 与 tokenizer_fast.py:分词器(含 Fast 加速版);
  • processor.py:多模态/多输入处理器。

2. 支持的模型权重

配置文档中明确列出了 PaddleNLP 支持的 BLOOM / BloomZ 系列预训练权重(模型名即 Hugging Face 权重标识,可直接作为model_name_or_path使用):

Model
bigscience/bloom-560m
bigscience/bloom-560m-bf16
bigscience/bloom-1b1
bigscience/bloom-3b
bigscience/bloom-7b1
bigscience/bloomz-560m
bigscience/bloomz-1b1
bigscience/bloomz-3b
bigscience/bloomz-7b1-mt
bigscience/bloomz-7b1-p3
bigscience/bloomz-7b1
bellegroup/belle-7b-2m

其中bloomz-*为经过指令微调的 BloomZ 系列,belle-7b-2m为社区中文化微调变体。在仓库的各配置文件中,默认使用的权重均为bigscience/bloomz-7b1-mt,即支持多语言指令的 7B 级 BloomZ 模型。

3. 微调配置:SFT 全量微调

llm/config/bloom/sft_argument.json 提供了 BLOOM 全量监督微调(SFT)的标准配置,完整内容如下:

{ "model_name_or_path": "bigscience/bloomz-7b1-mt", "dataset_name_or_path": "./data", "output_dir": "./checkpoints/sft_ckpts", "per_device_train_batch_size": 4, "gradient_accumulation_steps": 4, "per_device_eval_batch_size": 8, "eval_accumulation_steps":16, "num_train_epochs": 3, "learning_rate": 3e-05, "warmup_steps": 30, "logging_steps": 1, "evaluation_strategy": "epoch", "save_strategy": "epoch", "src_length": 1024, "max_length": 2048, "fp16": true, "fp16_opt_level": "O2", "do_train": true, "do_eval": true, "disable_tqdm": true, "load_best_model_at_end": true, "eval_with_do_generation": false, "metric_for_best_model": "accuracy", "recompute": true, "save_total_limit": 1, "tensor_parallel_degree": 4, "pipeline_parallel_degree": 1, "zero_padding": false, "unified_checkpoint": true, "use_flash_attention": false }

关键参数逐项解读:

参数取值含义与建议
model_name_or_pathbigscience/bloomz-7b1-mt预训练权重标识,可替换为上表任意支持权重
dataset_name_or_path./data训练数据路径(本地数据目录)
output_dir./checkpoints/sft_ckpts模型与检查点输出目录
per_device_train_batch_size4单卡训练 batch size,7B 模型建议配合梯度累积使用
gradient_accumulation_steps4梯度累积步数,等效 batch size = 4 × 4 = 16
learning_rate3e-05全量微调通常使用较小的学习率
num_train_epochs3训练轮数
src_length/max_length1024 / 2048输入上下文长度与最大序列长度
fp16+fp16_opt_leveltrue /O2混合精度训练,O2 为保留算子精度等级的优化策略
recomputetrue开启重计算(activation checkpointing)以节省显存
tensor_parallel_degree4张量并行度,7B 级模型在 SFT 中默认按 4 卡切分
pipeline_parallel_degree1流水线并行度
unified_checkpointtrue使用统一检查点格式,便于跨并行策略复用权重
use_flash_attentionfalse是否启用 FlashAttention 加速注意力计算

3.1 如何启动 SFT

使用 llm/run_finetune.py 入口脚本,将上述 JSON 作为--arguments传入即可:

python -m paddle.distributed.launch \ --gpus "0,1,2,3" \ llm/run_finetune.py \ llm/config/bloom/sft_argument.json

从源码看,llm/run_finetune.py 通过PdArgumentParser解析 JSON 参数,并通过AutoModelForCausalLM/AutoModelForCausalLMPipe自动加载对应规模的 BLOOM 模型;当pipeline_parallel_degree > 1时会自动切换到AutoModelForCausalLMPipe流水线并行版本。数据侧通过 llm/utils/data.py 的get_convert_example完成 prompt 与 response 的拼接格式化。

4. LoRA 高效微调

llm/config/bloom/lora_argument.json 提供 LoRA 低秩适配微调配置:

{ "model_name_or_path": "bigscience/bloomz-7b1-mt", "dataset_name_or_path": "./data", "output_dir": "./checkpoints/lora_ckpts", "per_device_train_batch_size": 4, "gradient_accumulation_steps": 4, "per_device_eval_batch_size": 8, "eval_accumulation_steps":16, "num_train_epochs": 3, "learning_rate": 3e-04, "warmup_steps": 30, "logging_steps": 1, "evaluation_strategy": "epoch", "save_strategy": "epoch", "src_length": 1024, "max_length": 2048, "fp16": true, "fp16_opt_level": "O2", "do_train": true, "do_eval": true, "disable_tqdm": true, "load_best_model_at_end": true, "eval_with_do_generation": false, "metric_for_best_model": "accuracy", "recompute": true, "save_total_limit": 1, "tensor_parallel_degree": 1, "pipeline_parallel_degree": 1, "lora": true, "zero_padding": false, "unified_checkpoint": true, "use_flash_attention": false }

与 SFT 配置相比,LoRA 配置的关键差异:

  • "lora": true:开启 LoRA 适配,仅训练注入的低秩矩阵,冻结原模型参数,显著降低显存占用与训练成本;
  • learning_rate: 3e-04:LoRA 训练使用比全量微调(3e-05)高一个数量级的学习率;
  • tensor_parallel_degree: 1:LoRA 训练默认单卡即可进行,说明该方法对显存需求更友好。

LoRA 底层由 paddlenlp/peft/lora/ 中的LoRAConfig/LoRAModel实现,llm/run_finetune.py 会依据lora=True自动调用get_lora_target_modules确定注入目标模块。LoRA 训练产出的检查点可通过 llm/tools/merge_lora_params.py 合并回原模型权重。

5. Prefix Tuning(P-Tuning)前缀微调

llm/config/bloom/pt_argument.json 提供 Prefix Tuning 前缀微调配置:

{ "model_name_or_path": "bigscience/bloomz-7b1-mt", "dataset_name_or_path": "./data", "output_dir": "./checkpoints/pt_ckpts", "per_device_train_batch_size": 4, "gradient_accumulation_steps": 4, "per_device_eval_batch_size": 8, "eval_accumulation_steps":16, "num_train_epochs": 3, "learning_rate": 3e-02, "warmup_steps": 30, "logging_steps": 1, "evaluation_strategy": "epoch", "save_strategy": "epoch", "src_length": 1024, "max_length": 2048, "fp16": true, "fp16_opt_level": "O2", "do_train": true, "do_eval": true, "disable_tqdm": true, "load_best_model_at_end": true, "eval_with_do_generation": false, "metric_for_best_model": "accuracy", "recompute": true, "save_total_limit": 1, "tensor_parallel_degree": 1, "pipeline_parallel_degree": 1, "prefix_tuning": true, "zero_padding": false, "unified_checkpoint": true, "use_flash_attention": false }

关键差异:

  • "prefix_tuning": true:开启前缀微调,在每一 Transformer 层的 KV 前拼接可学习的前缀向量;
  • learning_rate: 3e-02:前缀向量是可学习的少量新参数,因此使用更高的学习率(相比 SFT 高三个数量级)也能稳定收敛;
  • 同样保持tensor_parallel_degree: 1,单卡即可运行。

Prefix Tuning 由 paddlenlp/peft/prefix/ 中的PrefixConfig/PrefixModelForCausalLM实现,llm/run_finetune.py 会调用get_prefix_tuning_params构造前缀参数。

6. 量化配置:PTQ 与 GPTQ

BLOOM 配置目录还提供了两种后训练量化(Post-Training Quantization)方案,用于降低推理显存与加速部署,对应入口脚本为 llm/run_quantization.py。

6.1 PTQ(含 SmoothQuant 平滑量化)

llm/config/bloom/ptq_argument.json:

{ "model_name_or_path": "bigscience/bloomz-7b1-mt", "per_device_train_batch_size": 8, "per_device_eval_batch_size": 8, "eval_accumulation_steps":16, "src_length": 1024, "max_length": 2048, "fp16": true, "fp16_opt_level": "O2", "dataset_name_or_path": "./data", "output_dir": "./checkpoints/ptq_ckpts", "do_eval": true, "eval_with_do_generation": false, "do_ptq": true, "ptq_step": 16, "smooth": true, "smooth_step": 16, "smooth_all_linears": true, "smooth_piecewise_search": true, "smooth_k_piece": 3, "unified_checkpoint": true, "smooth_search_piece": true }

PTQ 参数说明:

参数取值含义
do_ptqtrue开启 PTQ 后训练量化
ptq_step16量化校准(calibration)时使用的样本步数
smoothtrue启用 SmoothQuant 激活平滑,缓解激活值离群导致的量化误差
smooth_step16平滑系数搜索步数
smooth_all_linearstrue对所有 Linear 层执行平滑处理
smooth_piecewise_search/smooth_search_piecetrue分段搜索平滑策略
smooth_k_piece3分段搜索的段数

6.2 GPTQ

llm/config/bloom/gptq_argument.json:

{ "model_name_or_path": "bigscience/bloomz-7b1-mt", "per_device_train_batch_size": 8, "per_device_eval_batch_size": 8, "eval_accumulation_steps":16, "src_length": 1024, "max_length": 2048, "fp16": true, "fp16_opt_level": "O2", "dataset_name_or_path": "./data", "output_dir": "./checkpoints/gptq_ckpts", "do_eval": true, "eval_with_do_generation": false, "do_gptq": true, "unified_checkpoint": true, "gptq_step": 8 }

GPTQ 参数说明:

  • do_gptq: true:开启 GPTQ 逐层权重量化;
  • gptq_step: 8:量化校准步数(较 PTQ 的 16 步更少,GPTQ 依赖二阶 Hessian 信息补偿量化误差)。

6.3 启动量化

python -m paddle.distributed.launch \ --gpus "0" \ llm/run_quantization.py \ llm/config/bloom/ptq_argument.json # 或 python -m paddle.distributed.launch \ --gpus "0" \ llm/run_quantization.py \ llm/config/bloom/gptq_argument.json

量化后的检查点配合 llm/predict/export_model.py 可导出为推理模型。更完整的量化背景可参考 llm/docs/quantization.md 与 llm/docs/quantization_tutorial.md。

7. 三种微调方案对比与选择建议

结合以上配置,可归纳 BLOOM 在 PaddleNLP 中的微调选型参考:

方案关键开关学习率并行度默认值适用场景
SFT 全量微调无(默认)3e-05TP=4, PP=1数据充足、追求最优效果、显存充裕
LoRAlora: true3e-04TP=1, PP=1单卡可训、快速迭代、低成本适配
Prefix Tuningprefix_tuning: true3e-02TP=1, PP=1参数量最少,适合快速尝试验证

启动微调的统一入口为 llm/run_finetune.py,只需将--arguments指向对应 JSON;训练结束后,LoRA/Prefix 检查点可通过 llm/tools/merge_lora_params.py 等合并工具还原为完整权重。数据准备与 SFT 全流程细节可继续阅读 llm/docs/finetune.md 和 llm/docs/peft.md。

8. 总结

PaddleNLP 为 BLOOM / BloomZ 系列提供了从模型实现(paddlenlp/transformers/bloom/)到训练(llm/run_finetune.py)、量化(llm/run_quantization.py)的完整链路。本文基于 llm/config/bloom/ 下的五份标准配置,逐项解读了 SFT、LoRA、Prefix Tuning、PTQ 与 GPTQ 的参数设计思路——其中学习率与并行度的差异化取值,正体现了全量微调、参数高效微调与后训练量化在资源占用和收敛特性上的本质区别。读者可直接复用这些配置,将任意支持的 BLOOM 权重快速接入自己的微调与部署流程。

  • 人工智能
  • 大模型
  • 预训练
  • 微调
  • LoRA
  • RLHF
  • 强化学习
  • 分布式训练

【免费下载链接】PaddleNLP

Easy-to-use and powerful LLM and SLM library with awesome model zoo.

项目地址:https://gitcode.com/gh_mirrors/pa/PaddleNLP
点击查看免费下载

相关推荐

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询