Engineered for simplicity, complete transparency, and high-performance edge deployment: covering end-to-end pretraining, alignment (SFT/LoRA/DPO/RL/Distillation) for 26M ~ 145M models, elastic cloud training workflows, and a zero-dependency pure C++ native inference engine.
MiniMind-Next is a compact Chinese LLM laboratory designed for both research and engineering. It focuses on accessible, reproducible, and end-to-end workflows for model pretraining, multi-stage alignment, and high-performance edge deployment on modest consumer hardware.
The repository features:
- Unified Staged Training Pipeline: Seamlessly run Pretraining, Full SFT, LoRA, DPO, Reinforcement Learning (GRPO / PPO / SPO), and Knowledge Distillation.
- Pure Native C++ Inference Engine: A zero-dependency, modular C++ implementation featuring Grouped Query Attention (GQA), KV Cache, RoPE (
rotate_half), and OpenMP multi-core acceleration. - Cloud & Automation Workflows: Millisecond code hot-sync, automatic Conda environment setup, and power-off protection for pay-as-you-go GPU instances.
- Bilingual Documentation: Comprehensive user guides and model resource guides in both English and Chinese.
- ⚡ Modular Architecture: Fully decoupled with object-oriented design across training dispatchers, model definitions, tokenizers, and inference engines.
- 🎯 Full Training Pipeline:
- Self-Supervised Pretraining
- Supervised Fine-Tuning (Full SFT)
- Parameter-Efficient Tuning (LoRA)
- Direct Preference Optimization (DPO)
- Reinforcement Learning Alignment (GRPO / PPO / SPO)
- Knowledge Distillation & Reasoning SFT
- 🚀 Hardware Acceleration: Deeply integrated with
torch.compileJIT kernel fusion and mixed precision. Training SFT on an RTX 4090 takes only ~10 minutes. - ☁️ Cloud Training Workflow: Tailored for pay-as-you-go GPU instances with millisecond code hot-sync, automatic Conda environment activation, and
--shutdownauto power-off. - 💻 Pure Native C++ Inference:
- Modular, zero-dependency modern C++ inference engine (under src/).
- Native support for GQA (Grouped Query Attention), KV Cache, RoPE (rotate_half), and SwiGLU.
- OpenMP Multi-Core Parallelism, achieving 13+ tok/s on CPU with stream buffer handling UTF-8 Chinese characters seamlessly.
- Built with modern CMake Presets.
.
├── CMakeLists.txt # C++ root build configuration (outputs to bin/)
├── CMakePresets.json # Modern CMake presets (default / debug)
├── requirements.txt # Python dependencies
├── bin/ # Compiled executable binaries (gitignored)
├── models/ # Model weights directory (gitignored)
├── checkpoints/ # Resume checkpoints & process snapshots (gitignored)
├── resource/ # Weights and datasets directory (refer to docs/model_download.md)
│ ├── MiniMind2-PyTorch/ # Native PyTorch weights (.pth)
│ ├── MiniMind2/ # HuggingFace format model (safetensors / config.json)
│ └── minimind_dataset/ # Pretrain/SFT/DPO/RL datasets (.jsonl)
├── src/ # Pure C++ native inference engine
│ ├── ops/ # High-performance kernels (RMSNorm, RoPE, MatMul, Softmax)
│ ├── tokenizer/ # Tokenizer & stream decoding (BPE, ChatML template)
│ ├── sampler/ # Sampler (Temperature, Top-P, Greedy)
│ ├── model/ # Transformer network & KV Cache management
│ └── main.cpp # CLI interactive chat entry point
├── scripts/
│ ├── Deploy/ # Python inference & serving (chat_llm, serve_openai_api, chat_openai_api)
│ ├── Trainer/ # Training pipelines (train.py --stage ...) & RL algorithms
│ │ ├── stages/ # Pretrain, SFT, LoRA, DPO, Reason, Distillation implementations
│ │ └── train_common.py # TrainCtx, resume state, and multi-GPU DDP utilities
│ ├── Tools/ # Toolings (export_cpp_bin, cloud_train, sync_data, convert_model)
│ ├── Model/ # Model architecture & Tokenizer
│ └── Dataset/ # Dataset loader (lm_dataset.py)
└── docs/ # Project documentation
├── guide.md # Full user & training guide (English)
├── guide_zh.md # Full user & training guide (Chinese)
├── model_download.md # Weights & dataset download guide (English)
└── model_download_zh.md # Weights & dataset download guide (Chinese)
# Clone the repository
git clone https://github.com/whiteode/MiniMind-Next.git
cd MiniMind-Next
# Install dependencies
pip install -r requirements.txt💡 Resource Preparation: Please refer to docs/model_download.md to download pretrained weights and datasets into
resource/.
# Option 1: Native PyTorch weights terminal chat (with sliding KV cache)
python scripts/Deploy/chat_llm.py --save_dir resource/MiniMind2-PyTorch --weight full_sft --hidden_size 512
# Option 2: Load HuggingFace format directory
python scripts/Deploy/chat_llm.py --format hf --load_from resource/MiniMind2
# Option 3: Launch OpenAI-compatible API server (listening on 0.0.0.0:8998)
python scripts/Deploy/serve_openai_api.py --save_dir resource/MiniMind2-PyTorch --weight full_sft --hidden_size 512# Step 1: Export HuggingFace model into binary format
python scripts/Tools/export_cpp_bin.py --model_dir resource/MiniMind2 --output models/minimind2.bin
# Step 2: Build with CMake Presets (executable generated in bin/)
cmake --preset default
cmake --build --preset default
# Step 3: Launch interactive terminal chat
./bin/minimind_cpp models/minimind2.binAll training commands are executed from the project root directory via train.py --stage <stage>:
python scripts/Trainer/train.py --stage pretrain \
--data_path resource/minimind_dataset/pretrain_t2t_mini.jsonl \
--batch_size 80 --accumulation_steps 4 --use_compile 1python scripts/Trainer/train.py --stage full_sft --batch_size 64 --use_compile 1# LoRA fine-tuning
python scripts/Trainer/train.py --stage lora --batch_size 64 --use_compile 1
# DPO alignment
python scripts/Trainer/train.py --stage dpo --batch_size 8 --use_compile 1
# Knowledge distillation (Student 512 + Teacher 768)
python scripts/Trainer/train.py --stage distillation --batch_size 32 --use_compile 1
# Reinforcement Learning (GRPO / PPO / SPO)
python scripts/Trainer/train_grpo.py
python scripts/Trainer/train_ppo.py
python scripts/Trainer/train_spo.py💡 Resume Training: Append
--from_resume 1to any stage to seamlessly resume fromcheckpoints/.
# Sync code -> Activate conda env on remote GPU -> Run SFT -> Auto-shutdown after training
python scripts/Tools/cloud_train.py run minimind python scripts/Trainer/train.py \
--stage full_sft --batch_size 224 --use_compile 1 --shutdown| VRAM Specification | 512 (26M Small, Tested) | 768 (104M Base) | 640-MoE (145M) | RTX 4090 Estimated Time |
|---|---|---|---|---|
| 12GB (3060) | batch 80~96 |
batch 32~48 |
batch 32~48 |
- |
| 24GB (3090/4090) | batch 224 |
batch 96~128 |
batch 128~160 |
Full SFT takes ~10-15 mins |
| 80GB (A100/H100) | batch 768 |
batch 384~512 |
batch 512~640 |
Seconds / Minutes |
Inspired by and evolved from MiniMind, extensively refactored with modular C++ inference and full-stack enhancements.
Special thanks to jingyaogong for the inspiring MiniMind project. This project builds upon its foundation with object-oriented refactoring, training acceleration, checkpoint closures, and native C++ inference engine extensions.
This project is licensed under the Apache 2.0 License.