minicpm5-finetune-xtuner
它能做什么
这个技能指导你的 AI 使用 xtuner 训练 MiniCPM5-1B,xtuner 是一款使用 Python 配置文件的工具包。适合使用 mmengine 风格训练设置的人员。
适用场景
- ✓你想用 xtuner 微调 MiniCPM5-1B 的时候。
- ✓你更喜欢用 Python 配置文件而不是 YAML 进行训练的时候。
- ✓你的团队已经使用 mmengine,并希望以同样的方式训练 MiniCPM5 的时候。
技能链接
https://github.com/OpenBMB/MiniCPM/blob/main/skills/minicpm5-finetune-xtuner/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我设置一份 xtuner 训练配置,用我的数据集文件微调 MiniCPM5-1B。”
每周 5 个新技能,1 分钟读完。
工作原理
这个技能安装 xtuner 和无界面版的 OpenCV 软件包,编写用于 LoRA 训练的 Python 配置,并使用 xtuner 工具运行训练。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Fine-tune MiniCPM5-1B with xtuner
mmengine config-driven SFT. Uses Python config files (not YAML) and integrates with mmengine's runner / hook system.
Required input
| Var | Example | Default |
|---|---|---|
BASE_MODEL |
openbmb/MiniCPM5-1B |
required |
DATA |
path to messages-format jsonl | required |
WORK_DIR |
./runs/minicpm5_xtuner |
required |
Steps
1. Install (once)
pip install "xtuner==0.2.0"
# Replace opencv-python with the headless variant (avoids libGL linkage)
pip install --force-reinstall opencv-python-headless
pip uninstall -y opencv-python
2. Save the config — ${WORK_DIR}/minicpm5_lora.py
import torch
from datasets import load_dataset
from mmengine.dataset import DefaultSampler
from mmengine.hooks import (CheckpointHook, DistSamplerSeedHook, IterTimerHook,
LoggerHook, ParamSchedulerHook)
from mmengine.optim import AmpOptimWrapper, CosineAnnealingLR, LinearLR
from peft import LoraConfig
from torch.optim import AdamW
from transformers import AutoModelForCausalLM, AutoTokenizer
from xtuner.dataset import process_hf_dataset
from xtuner.dataset.collate_fns import default_collate_fn
from xtuner.dataset.map_fns import openai_map_fn, template_map_fn_factory
from xtuner.engine.hooks import DatasetInfoHook
from xtuner.engine.runner import TrainLoop
from xtuner.model import SupervisedFinetune
from xtuner.utils import PROMPT_TEMPLATE
pretrained_model_name_or_path = "${BASE_MODEL}" # ← replace
data_path = "${DATA}" # ← replace
prompt_template = PROMPT_TEMPLATE.qwen_chat # 🔑 ChatML — DO NOT use llama3_chat
max_length = 2048
batch_size = 4
accumulative_counts = 4
max_epochs = 2
lr = 2e-4
warmup_ratio = 0.03
tokenizer = dict(type=AutoTokenizer.from_pretrained,
pretrained_model_name_or_path=pretrained_model_name_or_path,
trust_remote_code=False, padding_side="right")
model = dict(
type=SupervisedFinetune, use_varlen_attn=False,
llm=dict(type=AutoModelForCausalLM.from_pretrained,
pretrained_model_name_or_path=pretrained_model_name_or_path,
trust_remote_code=False, torch_dtype=torch.bfloat16),
lora=dict(type=LoraConfig, r=16, lora_alpha=32, lora_dropout=0.05,
bias="none", task_type="CAUSAL_LM",
target_modules=["q_proj","k_proj","v_proj","o_proj","gate_proj","up_proj","down_proj"]),
)
train_dataset = dict(
type=process_hf_dataset,
dataset=dict(type=load_dataset, path="json", data_files=dict(train=data_path)),
tokenizer=tokenizer, max_length=max_length,
dataset_map_fn=openai_map_fn, # 🔑 messages format
template_map_fn=dict(type=template_map_fn_factory, template=prompt_template),
remove_unused_columns=True, shuffle_before_pack=True,
pack_to_max_length=False, use_varlen_attn=False,
)
train_dataloader = dict(
batch_size=batch_size, num_workers=2,
dataset=train_dataset,
sampler=dict(type=DefaultSampler, shuffle=True),
collate_fn=dict(type=default_collate_fn, use_varlen_attn=False),
)
optim_wrapper = dict(
type=AmpOptimWrapper,
optimizer=dict(type=AdamW, lr=lr, betas=(0.9, 0.999), weight_decay=0),
clip_grad=dict(max_norm=1, error_if_nonfinite=False),
accumulative_counts=accumulative_counts, loss_scale="dynamic", dtype="bfloat16",
)
param_scheduler = [
dict(type=LinearLR, start_factor=1e-2, # 🔑 use 1e-2 not 1e-5 (default is too small)
by_epoch=True, begin=0, end=warmup_ratio * max_epochs, convert_to_iter_based=True),
dict(type=CosineAnnealingLR, eta_min=0.0, by_epoch=True,
begin=warmup_ratio * max_epochs, end=max_epochs, convert_to_iter_based=True),
]
train_cfg = dict(type=TrainLoop, max_epochs=max_epochs)
default_hooks = dict(
timer=dict(type=IterTimerHook),
logger=dict(type=LoggerHook, log_metric_by_epoch=False, interval=10),
param_scheduler=dict(type=ParamSchedulerHook),
checkpoint=dict(type=CheckpointHook, by_epoch=False, interval=200, max_keep_ckpts=2),
sampler_seed=dict(type=DistSamplerSeedHook),
)
custom_hooks = [dict(type=DatasetInfoHook, tokenizer=tokenizer)]
env_cfg = dict(cudnn_benchmark=False, mp_cfg=dict(mp_start_method="fork"), dist_cfg=dict(backend="nccl"))
log_level = "INFO"
load_from = None
resume = False
randomness = dict(seed=42, deterministic=False)
log_processor = dict(by_epoch=False)
🔑 **`PROMPT
常见问题
为什么要替换 OpenCV 软件包?
这个技能将 opencv-python 替换为无界面版本,以避免 libGL 链接问题。
这份指南适用于哪个模型?
这个技能是为 MiniCPM5-1B 编写的。
它需要什么数据?
它需要一个 messages 格式的文件,每行一段对话。
详情
作者的原始描述
Fine-tune MiniCPM5-1B with xtuner (mmengine config-driven SFT). Use when the user mentions "xtuner", "mmengine", InternLM's training framework, or wants config-file-driven training.
repo: OpenBMB/MiniCPM
热度
- GitHub 星标
- 12k
- 排名
- #11545
- 许可证
- 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 模型,实现快速的本地聊天和文本生成。