本页面介绍如何从 0 开始训练 MiniMind 语言模型。
从 ModelScope 或 HuggingFace 下载数据集。
创建 ./dataset 目录并放入数据文件:
./dataset/
├── pretrain_hq.jsonl (1.6GB, ✨推荐)
├── sft_mini_512.jsonl (1.2GB, ✨推荐)
├── sft_512.jsonl (7.5GB)
├── sft_1024.jsonl (5.6GB)
├── sft_2048.jsonl (9GB)
├── dpo.jsonl (909MB)
├── r1_mix_1024.jsonl (340MB)
└── lora_*.jsonl
!!! tip "推荐组合"
最快速度复现:`pretrain_hq.jsonl` + `sft_mini_512.jsonl`
**单卡 3090 仅需 2 小时 + 3 块钱!**
预训练数据 (pretrain_hq.jsonl):
{"text": "如何才能摆脱拖延症?治愈拖延症并不容易..."}
SFT 数据 (sft_*.jsonl):
{
"conversations": [
{"role": "user", "content": "你好"},
{"role": "assistant", "content": "你好!"}
]
}
所有训练脚本位于 ./trainer 目录。
预训练阶段让模型学习基础知识,目标是学会词语接龙。
cd trainer
python train_pretrain.py
# 多卡训练
torchrun --nproc_per_node 2 train_pretrain.py
输出权重:./out/pretrain_*.pth
!!! info "训练时长"
- MiniMind2-Small (26M): ~1.1h (单卡 3090)
- MiniMind2 (104M): ~3.9h (单卡 3090)
SFT 阶段让模型学习对话方式,适应聊天模板。
python train_full_sft.py
# 多卡训练
torchrun --nproc_per_node 2 train_full_sft.py
输出权重:./out/full_sft_*.pth
!!! info "训练时长"
- MiniMind2-Small: ~1h (使用 sft_mini_512)
- MiniMind2: ~3.3h (使用 sft_mini_512)
LoRA 是一种参数高效的微调方法,适合领域适配。
python train_lora.py
应用场景:
lora_medical.jsonllora_identity.jsonl输出权重:./out/lora/lora_*.pth
DPO 用于优化模型回复质量,使其更符合人类偏好。
python train_dpo.py
输出权重:./out/rlhf_*.pth
蒸馏 DeepSeek-R1 的推理能力。
python train_distill_reason.py
输出权重:./out/reason_*.pth
MiniMind 使用 Transformer Decoder-Only 结构(类似 Llama3):
| Model Name | params | d_model | n_layers | kv_heads | q_heads |
|---|---|---|---|---|---|
| MiniMind2-Small | 26M | 512 | 8 | 2 | 8 |
| MiniMind2-MoE | 145M | 640 | 8 | 2 | 8 |
| MiniMind2 | 104M | 768 | 16 | 2 | 8 |
# model_mode: 0=pretrain, 1=sft, 2=rlhf, 3=reason
python eval_model.py --model_mode 1
# 测试 LoRA 模型
python eval_model.py --lora_name 'lora_medical' --model_mode 2
torchrun --nproc_per_node N train_xxx.py
deepspeed --master_port 29500 --num_gpus=N train_xxx.py
# 需要先登录
wandb login
# 启用 wandb
torchrun --nproc_per_node N train_xxx.py --use_wandb
基于单卡 NVIDIA 3090:
| 数据集组合 | 时长 | 成本 | 效果 |
|---|---|---|---|
| pretrain_hq + sft_mini_512 | 2.1h | ≈2.73¥ | 😊😊 基础对话 |
| 完整数据集 (MiniMind2-Small) | 38h | ≈49.61¥ | 😊😊😊😊😊😊 完整能力 |
| 完整数据集 (MiniMind2) | 122h | ≈158.6¥ | 😊😊😊😊😊😊😊😊 最强性能 |
!!! success "极速复现"
使用 `pretrain_hq` + `sft_mini_512`,单卡 3090 仅需 **2 小时 + 3 块钱**即可训练出能对话的 ChatBot!
batch_size 或使用 DeepSpeed