跳到主要内容
返回

MiniCPM-SALA

MiniCPM 文本系列

使用自己的数据,让 MiniCPM-SALA 更贴合目标任务。你可以通过 Transformers Trainer 定制训练流程,也可以按 LLaMA-Factory 指南准备数据与配置,开展全参数或 LoRA 训练。

如果你希望让模型更适合自己的业务、角色设定或特定任务,可以使用我们支持的微调工具进行训练。

目前推荐两种方式:

  • LLaMA-Factory:更适合新手和普通开发者,配置简单,也提供 WebUI,适合快速完成 LoRA / QLoRA / SFT 等微调任务。
  • Transformers Trainer:更适合有训练经验的开发者,方便自定义数据处理、训练参数和分布式训练流程。

你可以根据自己的设备选择不同的训练方式:

训练环境适合场景推荐方式
单张 GPU快速验证、小规模数据、人设微调、LoRA / QLoRALLaMA-Factory
单节点多 GPU更大数据量、更高训练效率LLaMA-Factory / Transformers Trainer
多节点训练大规模训练、研究或生产级训练任务Transformers Trainer + 分布式训练方案

如果你是第一次微调,建议先从 单 GPU + LLaMA-Factory + LoRA / QLoRA 开始。等你确认数据格式、训练流程和模型效果后,再考虑多卡或分布式训练。


Hugging Face Transformers Trainer​

我们提供官方脚本,以便在微调下游任务时轻松调整预训练的 MiniCPM-SALA。我们的微调脚本默认使用 transformers.Trainer 和 DeepSpeed。我们支持全参数微调和 LoRA 微调。

  • 全参数微调:全参数微调需要在整个训练过程中更新 LLM 的所有参数。
  • LoRA 微调:LoRA 允许仅更新一小部分参数,实现轻量级的模型调优。我们提供了基于 peft 的 LoRA 实现。

前置要求​

  1. 安装

安装所有软件包。

pip install -r requirements.txt
📎 附件:requirements.txt

  1. 多轮对话格式

多轮对话微调示例采用了对话格式约定,为不同角色添加不同的 loss_mask 值,从而能够在单次前向传播中计算多个回复的损失。

示例数据集格式:

JSON
[
{
"messages": [
{ "role": "system", "content": "<系统提示文本>" },
{ "role": "user", "content": "<用户提示文本>" },
{ "role": "assistant", "content": "<助手回复文本>" },
{ "role": "user", "content": "<用户提示文本>" },
{ "role": "assistant", "content": "<助手回复文本>" }
]
}
]

注意: 微调代码现在包含验证集。一个完整的数据集必须包含训练集和验证集(测试集是可选的)。以下是单个示例的数据格式:

JSON
{
"messages": [
{
"role": "user",
"content": "类型#裙*裙长#半身裙"
},
{
"role": "assistant",
"content": "这款百搭时尚的仙女半身裙,整体设计非常的飘逸随性,穿上之后每个女孩子都能瞬间变成小仙女啦。料子非常的轻盈,透气性也很好,穿到夏天也很舒适。"
}
]
}

DeepSpeed ZeRO​

全参数微调​

Bash
formatted_time=$(date +"%Y%m%d%H%M%S")

deepspeed --include localhost:0,1,2,3,4,5,6,7 finetune.py \
--model_name_or_path openbmb/MiniCPM-SALA \
--output_dir output/AdvertiseGenSFT/$formatted_time/ \
--train_data_path data/AdvertiseGenChatML/train.json \
--eval_data_path data/AdvertiseGenChatML/dev.json \
--learning_rate 5e-5 \
--per_device_train_batch_size 2 \
--per_device_eval_batch_size 32 \
--bf16 \
--gradient_accumulation_steps 4 \
--warmup_steps 100 \
--max_steps 3000 \
--weight_decay 0.01 \
--eval_steps 100 \
--save_strategy steps \
--save_steps 500 \
--seed 42 \
--log_level info \
--logging_strategy steps \
--logging_steps 10 \
--deepspeed configs/ds_config_zero3.json

LoRA 微调​

只需在上述脚本中添加 --use_lora。

Accelerate​

全参数微调​

Bash
formatted_time=$(date +"%Y%m%d%H%M%S")

accelerate launch --config_file configs/accelerate/fsdp_config.yaml \
finetune.py \
--model_name_or_path openbmb/MiniCPM-SALA \
--output_dir output/AdvertiseGenSFT/$formatted_time/ \
--train_data_path data/AdvertiseGenChatML/train.json \
--eval_data_path data/AdvertiseGenChatML/dev.json \
--learning_rate 5e-5 \
--per_device_train_batch_size 2 \
--per_device_eval_batch_size 32 \
--bf16 \
--gradient_accumulation_steps 4 \
--warmup_steps 100 \
--max_steps 3000 \
--weight_decay 0.01 \
--eval_steps 100 \
--save_strategy steps \
--save_steps 500 \
--seed 42 \
--log_level info \
--logging_strategy steps \
--logging_steps 10

LoRA 微调​

只需在上述脚本中添加 --use_lora。


LLaMA-Factory​

我们的 Hugging Face 检查点 openbmb/MiniCPM-SALA 可以直接在 LLaMA-Factory 中用于全参数和 LoRA 训练。

我们提供 SFT、KTO、DPO 和持续预训练的训练指南。

前置要求​

  1. 安装

克隆并安装 LLaMA-Factory GitHub 仓库。

Bash
git clone https://github.com/hiyouga/LlamaFactory.git
cd LLaMA-Factory
pip install -r requirements.txt
  1. 数据准备

由于我们支持四种训练模式(pretrain、sft、dpo、kto),您需要根据 {mode}_demo.json 中提供的格式准备数据。然后,将数据集信息添加到 LLaMA-Factory/data/dataset_info.json 中,确保可以找到您的数据集。示例:

JSON
{
"identity": {
"file_name": "identity.json"
},
"sft_zh_demo": {
"file_name": "alpaca_zh_demo.json"
},
"kto_en_demo": {
"file_name": "kto_en_demo.json",
"formatting": "sharegpt",
"columns": {
"messages": "messages",
"kto_tag": "label"
},
"tags": {
"role_tag": "role",
"content_tag": "content",
"user_tag": "user",
"assistant_tag": "assistant"
}
},
"dpo_en_demo": {
"file_name": "dpo_en_demo.json",
"ranking": true,
"formatting": "sharegpt",
"columns": {
"messages": "conversations",
"chosen": "chosen",
"rejected": "rejected"
}
}
}

创建训练配置 YAML 文件​

全参数微调​

创建一个名为 minicpm_sala_sft.yaml 的完整训练配置文件,并将其放在 LLaMA-Factory/examples/minicpm_config/ 中。对于其他配置,请参考 llama_factory/configs。

YAML
### model
model_name_or_path: openbmb/MiniCPM-SALA
trust_remote_code: true

### method
stage: sft
do_train: true
finetuning_type: full

### ddp
ddp_timeout: 180000000
deepspeed: examples/deepspeed/ds_z3_config.json

### dataset
dataset: sft_zh_demo
template: cpm4
cutoff_len: 1800
max_samples: 500000
overwrite_cache: true
preprocessing_num_workers: 16

### output
output_dir: saves/minicpm/minicpm_sala_full
logging_steps: 10
save_strategy: epoch
plot_loss: true
overwrite_output_dir: true

### train
per_device_train_batch_size: 2
gradient_accumulation_steps: 4
learning_rate: 0.0001
num_train_epochs: 3.0
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true

### eval
val_size: 0.1
per_device_eval_batch_size: 4
eval_steps: 500

LoRA 微调​

创建一个名为 minicpm_sala_lora_sft.yaml 的配置文件,并将其放在 LLaMA-Factory/examples/minicpm_config/ 中。复制上面的 minicpm_sala_sft.yaml 的内容,只需将 finetuning_type 从 full 更改为 lora。

模型训练命令​

完整训练​

  1. DeepSpeed
Bash
llamafactory-cli train examples/minicpm_config/minicpm_sala_sft.yaml
  1. Accelerate FSDP 多节点
Bash
accelerate launch \
--config_file examples/accelerate/fsdp_config_multiple_nodes.yaml \
src/train.py examples/minicpm_config/minicpm_sala_sft.yaml

LoRA 训练 (DeepSpeed)​

Bash
llamafactory-cli train examples/minicpm_config/minicpm_sala_lora_sft.yaml

开发者精选

查看全部 →
MiniCPM 文本VoxCPM 语音ESP32 塔台雷达:AI 语音播报航班圆屏塔台雷达,用克隆管制员音色播报航班MiniCPM 文本猫娘大模型的自进化:OPSD 涌现推理能力不请教师、不换底座,让猫娘在自我蒸馏中涌现数学推理MiniCPM 文本ReJev:轻量决策模型后训练LoRA 后训练 MiniCPM5-2B,留出集准确率 51.1% → 80.5%MiniCPM 文本BMB Agent:由 MiniCPM 驱动的个人助手让 2B 端侧模型接管日程与待办,.funcall 协议实战与踩坑记录MiniCPM 文本MetaCog:小裁判引导推理推理时元认知循环,多路径打分择优作答MiniCPM 文本MiniCPM News Desk:本地新闻简报本地小模型挑选原文段落,生成每日新闻汇总MiniCPM 文本WebGPU Pi:浏览器本地编程 AgentMiniCPM5-2B 经 WebGPU 在浏览器本地运行的编程 AgentMiniCPM 文本MiniCPM5-1B Tool-Use:离线函数调用SFT + GRPO 的 1B 函数调用模型,CPU 离线运行MiniCPM 文本deku:本地可信任务助手代码负责规划、模型只做简短补全的本地问答 AgentMiniCPM 文本MiniCPM5-1B:端侧部署与多语评测INT8 LiteRT-LM 端侧包,附西语等四种语言评测MiniCPM 文本MiniCPM-V 多模态MiniCPM Agent Gateway:端云协同网关本地 MiniCPM 判断数据策略,敏感请求不出本机MiniCPM 文本ToneBridge:情境化中文纠错按场景语气小幅修改中文句子,附拼音与发音MiniCPM 文本MiniCPM5-1B FP8:GPU 推理加速RTX 3060 解码 248 tok/s,比 BF16 快 1.5 倍MiniCPM 文本AUGURY:从杂草读懂土壤通过杂草物种推断土壤状况,可在手机端运行MiniCPM 文本gmgn-minicpm:本地链上研究 Agent1B 模型本地驱动的只读 GMGN 链上研究助手MiniCPM 文本MiniCPM-V 多模态Lost & Found Desk:小模型失物招领MiniCPM-V 描述物品、MiniCPM5 引导问询,人工确认归还MiniCPM 文本MiniCPM-V 多模态VoxCPM 语音CoreAI Model Zoo:MiniCPM 原生上 iPhoneMiniCPM5、MiniCPM-V 4.6 转换为 Core AI 原生运行MiniCPM 文本MiniCPM-V 多模态Viva Mais:WhatsApp 本地 CRM把旅行社的 WhatsApp 聊天整理成本地 CRMMiniCPM 文本天问:1B 模型解八字周易用 1B 模型把八字与周易变成自我反思MiniCPM 文本WanderLust:按心情规划步行按心情规划从 A 到 B 的探索式步行路线MiniCPM 文本NIGHTWAVE:1B 模型深夜电台由 1B 模型主持、可现场来电的复古电台MiniCPM 文本6ixPulse:多伦多租房地图多智能体比较多伦多街区,结论都标注数据来源MiniCPM 文本华尔街 AI:小模型交易公司四个小模型智能体在像素办公室里自主炒股MiniCPM 文本Hackathon Advisor:选题地图顾问用创意地图评估黑客松选题的原创性MiniCPM 文本LLM Cinema:AI 导演 ASCII 电影小模型自编自导 ASCII 动画短片MiniCPM 文本tiny-llm:纯 Rust 推理引擎纯 Rust 手写、仅用 CPU 的 MiniCPM5-1B 推理引擎MiniCPM 文本Document Field Agent:文档字段抽取批量文档 OCR 与结构化字段抽取工作台MiniCPM 文本MiniCPM-V 多模态当猫娘学会思考,手机也能部署这个模型难道真的是为了猫娘而生的吗?端侧部署实测MiniCPM 文本MiniCPM-V 多模态让 1B 小模型当好数学 AgentTeXada 的 MiniCPM 端侧实践与方法沉淀MiniCPM 文本ORG-2:Coding Agent 工作台统一编排与管理多个编程 Agent 的组织工作台MiniCPM 文本EdgeHome Harness用 Rust 为 1B 端侧小模型构建可回放、可评测的本地控制链路MiniCPM 文本榨干 NAS!接入 WorkBuddyN150 CPU 跑本地 AI,分享一些真实用法和踩坑经验