Chapter 03
📌 快速开始
📌 快速开始
- CPU: Intel(R) Core(TM) i9-10980XE CPU @ 3.00GHz
- RAM: 128 GB
- GPU: NVIDIA GeForce RTX 3090 (24GB) * 8
- Ubuntu==20.04
- CUDA==12.2
- Python==3.10.16
- requirements.txt
第0步
# 克隆仓库、安装依赖
git clone --depth 1 https://github.com/jingyaogong/minimind
cd minimind && pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simpleⅠ 🚀 模型推理
1' 下载模型
在项目根目录:
# 方式1
modelscope download --model gongjy/minimind-3 --local_dir ./minimind-3
# 方式2
git clone https://huggingface.co/jingyaogong/minimind-32' CLI 推理
# 方式1:使用 Transformers 格式模型
python eval_llm.py --load_from ./minimind-3
# 方式2:基于 PyTorch 模型(确保./out目录下有对应权重)
python eval_llm.py --load_from ./model --weight full_sft3'(可选)WebUI
# 可能需要`python>=3.10`,安装 `pip install streamlit`
# ⚠️ 须先将 transformers 格式模型文件夹复制到 ./scripts/ 目录下(例如:cp -r minimind-3 ./scripts/minimind-3),web_demo 脚本会自动扫描该目录下包含权重文件的子文件夹,如不存在则报错
cd scripts && streamlit run web_demo.py4'(可选)第三方推理框架
# ollama
ollama run jingyaogong/minimind-3
# vllm
vllm serve /path/to/model --served-model-name "minimind"Ⅱ 🛠️ 模型训练
import torch
print(torch.cuda.is_available())若你计划使用 CUDA 训练,建议先确认当前环境是否已正确识别 GPU。
若 cuda 不可用,也仍可根据自身设备选择 CPU 或 MPS 运行,但训练速度与兼容性会有非常大的差异。
如需安装或更换 PyTorch 版本,可参考 torch_stable 与链接
1' 下载数据
从下文提供的数据集下载链接 下载所需数据文件,并放入 ./dataset 目录
当前默认仅需下载
pretrain_t2t_mini.jsonl与sft_t2t_mini.jsonl,即可较快复现MiniMind Zero对话模型。 如有更多需求,下文提供多种搭配方案,可根据自身任务目标与 GPU 资源灵活选择。
2' 开始训练
所有训练脚本均支持检查点保存。添加 --from_resume 1 参数后,即可自动检测并恢复训练进度:
python train_pretrain.py --from_resume 1
python train_full_sft.py --from_resume 1
# ...断点续训说明:
- 训练过程会自动在
./checkpoints/目录保存完整检查点(模型、优化器、训练进度等) - 检查点文件命名:
<权重名>_<维度>_resume.pth(如:full_sft_512_resume.pth) - 支持跨不同 GPU 数量恢复(自动调整 step)
- 支持 wandb 训练记录连续性(自动恢复同一个 run)
适合长时间训练或不稳定环境,无需担心训练中断导致进度丢失
2.1 预训练(必须)
cd trainer && python train_pretrain.py训练后,将得到
out/pretrain_*.pth作为输出权重(其中*为模型 dimension,默认为768)
2.2 指令微调(必须)
cd trainer && python train_full_sft.py训练后,将得到
out/full_sft_*.pth作为输出权重(其中full表示全参数微调)
2.3 测试已训练模型(可选)
确保待测试的模型 *.pth 文件位于 ./out/ 目录下;也可直接前往此处下载我已训练好的 *.pth 权重。
python eval_llm.py --weight full_sft
--weight用于指定权重名称前缀,例如pretrain、full_sft等;更多参数可直接参考eval_llm.py
1、所有训练脚本均基于 PyTorch 原生实现,并支持多卡加速。
2、若你的设备有 N (N > 1) 张显卡,可通过以下方式启动单机 N 卡训练(DDP,也支持扩展到多机多卡):
torchrun --nproc_per_node N train_xxx.py3、可根据需要开启 wandb 记录训练过程。
... train_xxx.py --use_wandb2025 年 6 月后,国内网络环境通常无法直连 WandB。MiniMind 当前默认转为使用 SwanLab 作为训练可视化工具,其接口与 WandB 基本兼容;通常只需将 import wandb 替换为 import swanlab as wandb,其余调用方式基本无需改动。
