minicpm5-finetune-ms-swift
它能做什么
这个技能指导你的 AI 使用 ms-swift 训练 MiniCPM5,ms-swift 是一套训练工具包。适合希望使用 swift 命令进行微调或偏好训练的人员。
适用场景
- ✓你想在 MiniCPM5 模型上运行 swift sft 或 swift rlhf 的时候。
- ✓你在训练 MiniCPM5-2B,需要其特定的 ms-swift 版本和模板的时候。
- ✓你想使用 Megatron-SWIFT 进行训练的时候。
技能链接
https://github.com/OpenBMB/MiniCPM/blob/main/skills/minicpm5-finetune-ms-swift/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请使用 ms-swift 对我的 messages 文件进行 LoRA 训练,训练 MiniCPM5-1B。”
每周 5 个新技能,1 分钟读完。
工作原理
这个技能为你的模型选择正确的 ms-swift 版本和模板,安装它,并用 swift 命令开始 LoRA 训练。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Fine-tune MiniCPM5-1B and MiniCPM5-2B with ms-swift
Select the version and template that match the model:
| Model | ms-swift version | Arguments |
|---|---|---|
| MiniCPM5-1B | PyPI 4.5.3 |
--model_type llama --template minicpm5 |
| MiniCPM5-2B | >=4.6.0.dev0 |
--model_type llama --template minicpm5_2b |
Both models require transformers>=5.6.
ms-swift 4.x renamed
--train_typeto--tuner_type. Use--tuner_type lorafor LoRA training.
Required input
| Var | Example | Default |
|---|---|---|
BASE_MODEL |
openbmb/MiniCPM5-2B |
required; openbmb/MiniCPM5-1B is also supported |
TEMPLATE |
minicpm5_2b |
use minicpm5 for MiniCPM5-1B |
DATA |
path to messages-format jsonl | required |
OUTPUT_DIR |
./runs/minicpm5_swift |
required |
GPU_ID |
0 |
0 |
Each line of DATA: {"messages": [{"role":"...","content":"..."}, ...]}.
Steps
1. Install (once)
For MiniCPM5-1B:
pip install "ms-swift==4.5.3" "transformers>=5.6"
For MiniCPM5-2B:
git clone https://github.com/modelscope/ms-swift.git
cd ms-swift
git checkout 654e24f17b5d9f40ed4b9ee4c56a723320244db5
pip install -e .
pip install "transformers>=5.6,<5.17"
For Megatron-SWIFT:
pip install mcore-bridge==1.6.4
2. Train (LoRA SFT)
CUDA_VISIBLE_DEVICES=${GPU_ID} swift sft \
--model "${BASE_MODEL}" \
--model_type llama \
--template "${TEMPLATE}" \
--tuner_type lora \
--dataset "${DATA}" \
--output_dir "${OUTPUT_DIR}" \
--num_train_epochs 2 \
--per_device_train_batch_size 4 \
--gradient_accumulation_steps 4 \
--learning_rate 2e-4 \
--lora_rank 16 --lora_alpha 32 --lora_dropout 0.05 \
--target_modules q_proj k_proj v_proj o_proj gate_proj up_proj down_proj \
--max_length 4096 \
--warmup_ratio 0.03 \
--bf16 true \
--logging_steps 10 \
--save_steps 200
Set TEMPLATE=minicpm5 for MiniCPM5-1B or TEMPLATE=minicpm5_2b for MiniCPM5-2B.
3. Validate
Loss should decrease over the first few hundred steps:
{'loss': 4.52, 'token_acc': 0.26, 'epoch': 0.04}
{'loss': 3.57, 'token_acc': 0.35, 'epoch': 1.00}
Adapter is at ${OUTPUT_DIR}/v0-${TIMESTAMP}/checkpoint-${STEP}/.
Merge for serving
swift export \
--model "${BASE_MODEL}" \
--adapters "${OUTPUT_DIR}/v0-${TIMESTAMP}/checkpoint-${STEP}" \
--merge_lora true \
--output_dir ./minicpm5-swift-merged
The merged model is a regular LlamaForCausalLM and serves with any minicpm5-deploy-* skill.
Full SFT / DPO / RLHF
Same flag surface, just swap the trainer:
# Full SFT
swift sft --tuner_type full ...
# DPO
swift rlhf --rlhf_type dpo \
--model "${BASE_MODEL}" --model_type llama --template "${TEMPLATE}" \
--dataset preference.jsonl \
--output_dir ${OUTPUT_DIR} ...
# Megatron-SWIFT SFT
megatron sft \
--model "${BASE_MODEL}" \
--model_type llama \
--template "${TEMPLATE}" \
--dataset "${DATA}" \
--finetune true \
--output_dir "${OUTPUT_DIR}"
Set TEMPLATE=minicpm5 for MiniCPM5-1B or TEMPLATE=minicpm5_2b for MiniCPM5-2B.
For complete training options and configuration details, see the ms-swift command-line parameters and the Megatron-SWIFT quick start.
Multi-GPU
NPROC_PER_NODE=8 swift sft \
--model "${BASE_MODEL}" --model_type llama --template "${TEMPLATE}" \
--tuner_type lora --deepspeed default-zero2 \
...
Common pitfalls
Failed to automatically match model_type: add--model_type llama.Failed to automatically match template_type: use--template minicpm5for MiniCPM5-1B or--template minicpm5_2bfor MiniCPM5-2B.minicpm5_2b is not registered: the active Python environment is using an older ms-swift release. Installms-swift>=4.6.0.dev0; the source installation above pins a known revision for reproducibility.- Conflict with LLaMA-Factory in same env: LLaMA-Factory pins
transformers==4.52, ms-swift wants the latest (currently transformers ≥5.6). Use separate venvs, or setPYTHONNOUSERSITE=1to ignore user-sitetransformers.
Reference
常见问题
哪个模板对应哪个模型?
这个技能对 MiniCPM5-1B 使用 minicpm5 模板,对 MiniCPM5-2B 使用 minicpm5_2b 模板。
参数有哪些变化?
这个技能说明,ms-swift 4.x 使用 --tuner_type lora,而旧版本使用 --train_type。
使用什么数据格式?
每行保存一个 messages 列表,其中包含 role 和 content 字段。
详情
作者的原始描述
Fine-tune MiniCPM5-1B or MiniCPM5-2B with ms-swift or Megatron-SWIFT. Use when the user mentions "ms-swift", "swift sft", "swift rlhf", or "megatron sft". MiniCPM5-1B uses the PyPI release with `--template minicpm5`; MiniCPM5-2B requires `ms-swift>=4.6.0.dev0` with `--template minicpm5_2b`. Both use `--model_type llama`.
repo: OpenBMB/MiniCPM
热度
- GitHub 星标
- 12k
- 排名
- #11542
- 许可证
- Apache-2.0
- 更新时间
- 2026/9/21
评论
- …
同一仓库的更多技能
minicpm5-deploy-litert
12k帮你的智能体使用谷歌的 LiteRT-LM 运行时,在电脑或 Android 手机上运行 MiniCPM5 模型,只需一个模型文件。
minicpm5-deploy-llama-cpp
12k向你的智能体展示如何使用 llama.cpp,借助现成的 GGUF 文件,在 CPU 或家用显卡上运行 MiniCPM5 模型。
minicpm5-deploy-lmstudio
12k帮你的智能体在 LM Studio 中运行 MiniCPM5 模型,这是一款带聊天窗口的桌面应用,并启动其本地 OpenAI 风格服务器。
minicpm5-deploy-mlx
12k帮你的智能体使用苹果的 MLX 框架,在 Apple 芯片 Mac 上原生运行 MiniCPM5 模型,实现快速的本地聊天和文本生成。