minicpm5-finetune-llamafactory
它能做什么
这个技能指导你的 AI 使用 LLaMA-Factory 微调 MiniCPM5,LLaMA-Factory 是一款由配置文件驱动的热门训练工具。适合希望以文档完善、基于配置的方式训练模型的人员。
适用场景
- ✓你想使用 LLaMA-Factory 或 llamafactory-cli 微调 MiniCPM5 的时候。
- ✓你想用 YAML 配置文件而不是编写 Python 代码进行训练的时候。
- ✓你想使用 LLaMA-Factory 网页界面训练模型的时候。
技能链接
https://github.com/OpenBMB/MiniCPM/blob/main/skills/minicpm5-finetune-llamafactory/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我用我的聊天示例文件,通过 LLaMA-Factory 微调 MiniCPM5-2B。”
每周 5 个新技能,1 分钟读完。
工作原理
这个技能在独立环境中安装 LLaMA-Factory,注册你的数据集,编写 YAML 训练配置,然后开始训练。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Fine-tune MiniCPM5-1B and MiniCPM5-2B with LLaMA-Factory
YAML-driven SFT / DPO with WebUI. Most-documented community framework.
Required input
| Var | Example | Default |
|---|---|---|
BASE_MODEL |
openbmb/MiniCPM5-2B |
required; openbmb/MiniCPM5-1B also works |
DATA_DIR |
dir containing dataset_info.json + jsonl |
required |
DATASET_NAME |
name registered in dataset_info.json |
required |
OUTPUT_DIR |
./runs/minicpm5_lf |
required |
GPU_ID |
0 |
0 |
Steps
1. Install (once, in its own venv to avoid breaking vLLM)
python -m venv .venv-lf && source .venv-lf/bin/activate
# `template: minicpm5` landed after v0.9.5 (the latest PyPI release) — install from source.
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
pip install -e LLaMA-Factory
⚠️ LLaMA-Factory requires
transformers>=4.55.0,<=5.8.0(excluding 4.57.0 and 5.6.0), which can clash with a serving stack such as vLLM. Always install it into its own venv.
2. Register the dataset (sharegpt / messages format)
${DATA_DIR}/dataset_info.json:
{
"${DATASET_NAME}": {
"file_name": "your_data.jsonl",
"formatting": "sharegpt",
"columns": {"messages": "messages"},
"tags": {
"role_tag": "role", "content_tag": "content",
"user_tag": "user", "assistant_tag": "assistant", "system_tag": "system"
}
}
}
Each line of your_data.jsonl:
{"messages": [{"role":"system","content":"..."}, {"role":"user","content":"..."}, {"role":"assistant","content":"..."}]}
3. Write the training YAML
Save as ${OUTPUT_DIR}/lora_sft.yaml:
### model
model_name_or_path: ${BASE_MODEL}
trust_remote_code: false
### method
stage: sft
do_train: true
finetuning_type: lora
lora_rank: 16
lora_alpha: 32
lora_target: all # all linear layers
### dataset
dataset: ${DATASET_NAME}
dataset_dir: ${DATA_DIR}
template: minicpm5 # 🔑 MANDATORY for MiniCPM5 — ChatML + XML tool calling
cutoff_len: 4096
max_samples: 100000
overwrite_cache: true
preprocessing_num_workers: 8
### output
output_dir: ${OUTPUT_DIR}
logging_steps: 10
save_steps: 200
plot_loss: true
overwrite_output_dir: true
### train
per_device_train_batch_size: 4
gradient_accumulation_steps: 4
learning_rate: 2.0e-4
num_train_epochs: 2.0
lr_scheduler_type: cosine
warmup_ratio: 0.03
bf16: true
ddp_timeout: 180000000
🔑
template: minicpm5is MANDATORY. It reproduces the model's ownchat_template.jinjabyte-for-byte — ChatML with MiniCPM5-2B Think mode, MiniCPM5-1B Think / No-think modes, plus XML tool calling.Do NOT use
template: empty. LLaMA-Factory does not delegate to the tokenizer's jinja;emptyis a genuinely empty template (bare{{content}}slots, no role markers, no EOS replacement, ReAct-style tools) that trains on a layout the model has never seen.llama3/qwen/ etc. are equally wrong. Requires LLaMA-Factory from source — see step 1.
4. Train
CUDA_VISIBLE_DEVICES=${GPU_ID} llamafactory-cli train ${OUTPUT_DIR}/lora_sft.yaml
For multi-GPU: prepend FORCE_TORCHRUN=1 and set NPROC_PER_NODE=8.
5. Validate
Loss should decrease monotonically; you should see lines like:
{'loss': 4.19, 'learning_rate': 0.000192, 'epoch': 0.2}
{'loss': 3.62, 'learning_rate': 0.000001, 'epoch': 1.0}
After training, the LoRA adapter is at ${OUTPUT_DIR}/. Sanity-check inference:
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
base = AutoModelForCausalLM.from_pretrained("${BASE_MODEL}", torch_dtype=torch.bfloat16, device_map="auto").eval()
model = PeftModel.from_pretrained(base, "${OUTPUT_DIR}").eval()
tok = AutoTokenizer.from_pretrained("${BASE_MODEL}")
inputs = tok.apply_chat_template([{"role":"user","content":"1+1=?"}], add_generation_prompt=True, enable_thinking=True, return_tensors="pt", return_dict=True).to(model.device)
print(tok.decode(model.generate(**inputs, max_new_tokens=32, do_sample=False)[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True))
Coherent answer ⇒ ✅. Gibberish ⇒ check template: minicpm5 in the YAML. (A clean loss curve does not confirm the template is right — a mismatched template trains just as smoothly.)
Merge LoRA for serving
cat > ${OUTPUT_DIR}/merge.yaml <<EOF
model_name_or_path: ${BASE_MODEL}
adapter_name_or_path: ${OUTPUT_DIR}
template: minicpm5
finetuning_type: lora
export_di
常见问题
它需要什么数据格式?
它使用 sharegpt 或 messages 格式,每行保存一段对话。
为什么要使用单独的环境?
这个技能警告,LLaMA-Factory 可能与 vLLM 等服务工具冲突,因此应使用自己的环境。
它也支持 DPO 吗?
支持。这个技能说明 LLaMA-Factory 同时支持 SFT 和 DPO 训练。
详情
作者的原始描述
Fine-tune MiniCPM5-1B or MiniCPM5-2B with LLaMA-Factory (YAML-driven SFT / DPO / WebUI). Use when the user wants to fine-tune via LLaMA-Factory, llamafactory-cli, mentions YAML configs, WebUI, or asks for the most-documented community framework.
repo: OpenBMB/MiniCPM
热度
- GitHub 星标
- 12k
- 排名
- #11541
- 许可证
- 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 模型,实现快速的本地聊天和文本生成。