Skip to main content

MiniCPM4.1 & MiniCPM4

MiniCPM Text Series

Choose between the official code and the LLaMA-Factory route based on your training goal, and learn about data preparation, training configuration, and methods such as LoRA / QLoRA. Resource requirements differ across models and training configurations, so check the target version, your hardware and the sample parameters first.

Hardware requirements​

Model sizeMinimum GPUs for full fine-tuningMinimum GPUs for LoRA fine-tuning
0.5BAt least one 20-series GPU with more than 12GB of VRAMAt least one 20-series GPU with 8GB of VRAM
8BAt least four GPUs with 24GB of VRAMAt least two 20-series GPUs with 24GB of VRAM

Device requirements: at least one GPU with 24GB of VRAM, 20-series or newer; for QLoRA you can try a 12GB GPU

Get the official code with git​

Bash
git clone https://github.com/OpenBMB/MiniCPM

Prepare the dataset​

For dialogue datasets, convert them into the following JSON format, where each entry is one messages list

The system field highlighted in red below is optional:

JSON
[
{
"messages": [
{
"role": "system",
"content": "<system prompt text>"
},
{
"role": "user",
"content": "<user prompt text>"
},
{
"role": "assistant",
"content": "<assistant response text>"
},
// ... Muti Turn,多轮对话
{
"role": "user",
"content": "<user prompt text>"
},
{
"role": "assistant",
"content": "<assistant response text>"
}
]
}
]

Edit the MiniCPM/finetune/lora_finetune_ocnli.sh file​

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

#4090添加这两行代码
export NCCL_P2P_DISABLE=1
export NCCL_IB_DISABLE=1

deepspeed --include localhost:1 --master_port 19888 finetune.py \
--model_name_or_path openbmb/MiniCPM3-4B \ # 可以修改为本地模型目录
--output_dir output/OCNLILoRA/$formatted_time/ \ # 可以修改为其他用来保存输出模型的地址
--train_data_path data/ocnli_public_chatml/train.json \ # 这里写按照第二步处理好的训练集地址
--eval_data_path data/ocnli_public_chatml/dev.json \ # 这里写按照第二步处理好的验证集
--learning_rate 5e-5 \ # 学习率
--per_device_train_batch_size 16 \ # 每张卡训练时的batch_size
--per_device_eval_batch_size 128 \ # 每张卡测试时的batch_size
--model_max_length 1024 \ # 模型训练时最大token数,超出将截断
--bf16 \ # 是否使用bf16数据格式,如果不是改为false
--use_lora \ # 是否使用lora
--gradient_accumulation_steps 1 \ # 梯度累计次数
--warmup_steps 100 \ # 预热步数
--max_steps 1000 \ # 最大训练步数,到达后停止训练
--weight_decay 0.01 \ # 权重正则化值
--evaluation_strategy steps \ # 测试方法,可以改为epoch
--eval_steps 500 \ # 与evaluation_strategy steps一起起作用,500个step测试一一次
--save_strategy steps \ # 模型保存策略,可以改为epoch即每个epoch保存一次
--save_steps 500 \ # save_strategy steps一起起作用,代表500步保存一次
--seed 42 \ # 随机种子
--log_level info --logging_strategy steps --logging_steps 10 \ # logging的设置
--deepspeed configs/ds_config_zero2_offload.json # deepspeed配置文件设置,如果显存充足可以改为configs/ds_config_zero2_offload.json

Train with LoRA / QLoRA (optional)​

lora​

Add the use_lora parameter to the MiniCPM/finetune/lora_finetune_ocnli.sh file.

If you're unsure where, add the following line just before the --deepspeed configs/ds_config_zero2_offload.json line:

Bash
--use_lora \

qlora​

Add the use_lora and qlora parameters to the MiniCPM/finetune/lora_finetune_ocnli.sh file.

If you're unsure where, add the following lines just before the --deepspeed configs/ds_config_zero2_offload.json line:

Bash
--use_lora \
--qlora \

After editing the bash file above, run the following commands to start training:​

Bash
cd MiniCPM/finetune
bash lora_finetune_ocnli.sh

Usage is essentially the same as in [Fine-tuning Tutorial] MiniCPM2.0. If you are not training tool-calling capability, you only need to change template: cpm3. For training tool-calling capability, stay tuned for the PR we are submitting to llama-factory.

First, install the Llama_Factory dependencies​

Bash
git clone https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[torch,metrics]"

Process the data​

Convert your dataset into the format used in the Minicpm/finetune/llama_factory_example/llama_factory_data folder (the examples cover three fine-tuning methods: DPO, KTO and SFT), and place it in the llama_factory/data directory.

DPO data format​

Below is an example of DPO data for constructing multi-turn dialogues in Llama_Factory, where:

JSON
{
"instruction": "你是一个科技百科小助手,能够回复大部分科技领域问题",
"input": "我想知道 MiniCPM4 到底好不好呀?",
"chosen": "亲呀,MiniCPM4 真的很不错呢!它运用了 NSA 注意力机制呀,这使得它在处理信息等方面有着很出色的表现,能够更加精准地理解和生成内容,无论是文本创作还是辅助学习等场景,都可以发挥挺不错的作用呢,很多用户使用后都反馈体验挺好的哦。",
"rejected": "哎呀,MiniCPM4 嘛,就是一般般啦。它虽然有一些功能,但是在实际使用中,也没有特别突出的亮点呀,和其他同类产品比起来,优势也不是特别明显呢,反正就是能用,但也没达到让人惊艳的程度啦。",
"history": [
[
"我听说 MiniCPM4 挺智能的,是真的吗?",
"亲,那确实呀,MiniCPM4 挺智能的呢,它有着独特的技术加持,在很多任务上都能展现出不错的智能水平,能帮大家解决不少问题哦。"
],
[
"那它在语言理解方面表现如何呢?",
"亲,它在语言理解方面的表现可圈可点哦,靠着相关的技术,像 NSA 注意力机制助力,对各种语句的理解还是挺准确的呢,能较好地把握语义呀。"
],
]
}

KTO data format​

  1. Each messages represents one data entry, and label indicates whether the reply in that entry is reasonable
  2. Unlike DPO, you don't need different answers to the same question; instead, give a rating to each question-answer pair
  3. Each question and reply can be rated True (good) or False (bad)
  4. The places you need to modify are highlighted in red
JSON
{
"messages": [
{
"content": "minicpm4.0哪里最强?",
"role": "user"
},
{
"content": "哪里都强,六边形战士好吗",
"role": "assistant"
}
],
"label": true
},
{
"messages": [
{
"content": "minicpm3.0 有哪些优点。",
"role": "user"
},
{
"content": "1.长文本能力。2.function call能力 3.其他所有能力",
"role": "assistant"
},
{
"content": "细说长文本能力",
"role": "user"
},
{
"content": "反正很强,细说就去我们github吧。https://github.com/OpenBMB/MiniCPM.git",
"role": "assistant"
}
],
"label": false
}
]

The SFT data format is as follows; each dictionary forms one data entry​

alpaca format:

JSON
[
{
"instruction": "你现在比牛顿还牛的科学家",
"input": "识别并解释给定列表中的两个科学理论:细胞理论和日心说。",
"output": "细胞理论是生物科学的一个理论,它认为所有生命体都是由微小的基本单元——细胞所构成。这是生物学的一个基础理论,认为细胞是所有生物的基本结构和功能单位,所有的生物都是由一个或多个细胞组成,细胞只能通过细胞分裂产生新的细胞。这一理论由薛定谔、施瓦内和雪莱于1839年首次提出。\n\n日心说是指太阳是太阳系的中心,也就是说,行星围绕太阳旋转的理论。这个理论打破了传统的地心说观点,认为地球并不是宇宙的中心。日心说的提出者是尼古拉·哥白尼,他在16世纪初发表了他的著作《天体运行论》,阐述了太阳系行星围绕太阳运行的模型,为天文学的发展做出了巨大贡献。"
}
]

chatml format:

JSON
[
{
"messages": [
{
"content": "Who are they?",
"role": "user"
},
{
"content": "They're Kane and Gretzka from Bayern Munich.",
"role": "assistant"
},
{
"content": "What are they doing?<image>",
"role": "user"
},
{
"content": "They are celebrating on the soccer field.",
"role": "assistant"
}
]
}
]

Add the dataset information to dataset_info.json​

Add your dataset information to llama_factory/data/dataset_info.json so that your dataset can be found there, as in the example below:

  1. file_name is the file name of the dataset you created
  2. For example, "sft_zh_demo" is the dataset name, which will be used in the yaml configuration file later
  3. Only two things need to be changed: the key name of the dataset, e.g. "sft_zh_demo", and the data file name for file_name, e.g. alpaca_zh_demo.json
  4. All places that need to be modified are highlighted in red
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",
"columns":{
"prompt":"instruction",
"query":"input",
"chosen":"chosen",
"rejected":"rejected",
"history": "history"
}
}
}

Set up the training script​

Copy the files from MiniCPM/finetune/llama_factory_example into the LLaMA-Factory/examples/minicpm directory​

Bash
cd LLaMA-Factory/examples
mkdir minicpm
#以下代码中的/your/path要改成你的MiniCPM代码和LLaMA-Factory路径
cp -r /your/path/MiniCPM/finetune/llama_factory_example/* /your/path/LLaMA-Factory/examples/minicpm

Depending on the fine-tuning method you need (DPO as an example), the configuration parameters in LLaMA-Factory/examples/minicpm/minicpm_dpo.yaml that must be modified are as follows​

YAML
model_name_or_path: openbmb/MiniCPM3-4B # 或者你本地保存的地址
template: cpm3 #注意如果微调minicpm3或者minicpm4就写cpm3,如果是minicpm2则写cpm
dataset: dpo_en_demo # 这里写dataset_info.json中的键名
output_dir: your/finetune_minicpm/save/path # 你微调后模型的保存地址
bf16: true # 如果你的设备支持bf16,否则false
deepspeed: examples/deepspeed/ds_z2_config.json # 如果显存不够可以改成ds_z3_config.json

If you need LoRA fine-tuning, change the following in the yaml file:​

Bash
finetuning_type: lora
lora_target: all

Start training​

Bash
cd LLaMA-Factory
FORCE_TORCHRUN=1 llamafactory-cli train examples/train_full/MiniCPM4.yaml

Developer Showcase

View all →