Skip to main content

MiniCPM-SALA

MiniCPM Text Series

Use your own data to make MiniCPM-SALA fit your target task better. You can customize the training workflow with the Transformers Trainer, or follow the LLaMA-Factory guide to prepare data and configs for full-parameter or LoRA training.

If you want the model to better suit your business, persona or a specific task, you can train it with the fine-tuning tools we support.

Two approaches are currently recommended:

  • LLaMA-Factory: better for beginners and general developers. Configuration is simple and it also provides a WebUI, making it suitable for quickly completing LoRA / QLoRA / SFT fine-tuning tasks.
  • Transformers Trainer: better for developers with training experience, making it easy to customize data processing, training parameters and distributed training workflows.

You can choose a training approach based on your hardware:

Training environmentSuitable scenariosRecommended approach
Single GPUQuick validation, small datasets, persona fine-tuning, LoRA / QLoRALLaMA-Factory
Single node, multiple GPUsLarger datasets, higher training efficiencyLLaMA-Factory / Transformers Trainer
Multi-node trainingLarge-scale training, research or production-grade training tasksTransformers Trainer + a distributed training solution

If this is your first time fine-tuning, we recommend starting with a single GPU + LLaMA-Factory + LoRA / QLoRA. Once you've confirmed the data format, training workflow and model results, consider multi-GPU or distributed training.


Hugging Face Transformers Trainer​

We provide official scripts that make it easy to adapt the pretrained MiniCPM-SALA when fine-tuning on downstream tasks. Our fine-tuning scripts use transformers.Trainer and DeepSpeed by default. Both full-parameter fine-tuning and LoRA fine-tuning are supported.

  • Full-parameter fine-tuning: full-parameter fine-tuning updates all of the LLM's parameters throughout training.
  • LoRA fine-tuning: LoRA updates only a small subset of parameters, enabling lightweight model tuning. We provide a LoRA implementation based on peft.

Prerequisites​

  1. Installation

Install all packages.

pip install -r requirements.txt
📎 Attachment: requirements.txt

  1. Multi-turn conversation format

The multi-turn conversation fine-tuning example follows a conversation format convention that assigns different loss_mask values to different roles, so the loss for multiple replies can be computed in a single forward pass.

Example dataset format:

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

Note: The fine-tuning code now includes a validation set. A complete dataset must contain a training set and a validation set (a test set is optional). The data format of a single example is as follows:

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

DeepSpeed ZeRO​

Full-parameter Fine-tuning​

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 Fine-tuning​

Simply add --use_lora to the script above.

Accelerate​

Full-parameter Fine-tuning​

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 Fine-tuning​

Simply add --use_lora to the script above.


LLaMA-Factory​

Our Hugging Face checkpoint openbmb/MiniCPM-SALA can be used directly in LLaMA-Factory for full-parameter and LoRA training.

We provide training guides for SFT, KTO, DPO and continued pretraining.

Prerequisites​

  1. Installation

Clone and install the LLaMA-Factory GitHub repository.

Bash
git clone https://github.com/hiyouga/LlamaFactory.git
cd LLaMA-Factory
pip install -r requirements.txt
  1. Data preparation

Since we support four training modes (pretrain, sft, dpo, kto), you need to prepare your data following the format provided in {mode}_demo.json. Then add your dataset information to LLaMA-Factory/data/dataset_info.json so that your dataset can be found. Example:

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"
}
}
}

Creating the Training Config YAML File​

Full-parameter Fine-tuning​

Create a full training config file named minicpm_sala_sft.yaml and place it in LLaMA-Factory/examples/minicpm_config/. For other configs, see 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 Fine-tuning​

Create a config file named minicpm_sala_lora_sft.yaml and place it in LLaMA-Factory/examples/minicpm_config/. Copy the contents of minicpm_sala_sft.yaml above and simply change finetuning_type from full to lora.

Model Training Commands​

Full Training​

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

LoRA Training (DeepSpeed)​

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

Developer Showcase

View all →