๐ง MiniMind
๐ก What is this?
MiniMind is a minimalist open-source project that lets you train a GPT-style language model entirely from scratch. Think of it as a teaching hospital for LLMs: small enough to understand every layer, yet complete enough to cover the full training lifecycle โ from BPE tokenizer training through pretraining, supervised fine-tuning (SFT), LoRA adaptation, and reinforcement learning from human feedback (DPO, PPO, GRPO, SPO). The smallest config (26M params) trains on a single consumer GPU, making it ideal for learning how transformers really work.
Architecture Overview
The system is organised into four sub-modules. model/ defines the transformer backbone, trainer/ orchestrates every training recipe, dataset/ provides data loaders, and scripts/ handles deployment and inference.
flowchart LR
subgraph model["model/"]
MC[MiniMindConfig]
MM[MiniMindModel
RoPE + GQA]
MCL[MiniMindForCausalLM
GenerationMixin]
ATT[Attention
Flash + KV Cache]
FF[FeedForward
SwiGLU]
MOE[MOEFeedForward
MoEGate + SharedExpert]
RMS[RMSNorm]
LORA[LoRA Module]
MC --> MM
MM --> MCL
ATT --> MM
FF --> MM
MOE --> MM
RMS --> MM
LORA -.-> MCL
end
subgraph trainer["trainer/"]
PT[pretrain.py]
SFT[train_sft.py]
TLORA[train_lora.py]
DPO[train_dpo.py]
PPO[train_ppo.py]
GRPO[train_grpo.py]
SPO[train_spo.py]
KD[train_distillation.py]
TR[train_reasoning.py]
TOK[train_tokenizer.py]
TU[trainer_utils.py]
end
subgraph dataset["dataset/"]
PD[PretrainDataset]
SD[SFTDataset]
DD[DPODataset]
RD[RLAIFDataset]
end
subgraph scripts["scripts/"]
API[serve_api.py
FastAPI]
WEB[web_demo.py
Streamlit]
CVT[convert_model.py]
CLI[api_client.py]
end
dataset --> trainer
model --> trainer
model --> scripts
Training Data Flow
Every training recipe follows a similar pipeline. Raw text is tokenised, batched by the dataset classes, passed through the model, and gradients flow back through the optimizer.
sequenceDiagram
participant T as Tokenizer
(BPE, vocab=6400)
participant D as Dataset
(PretrainDataset / SFTDataset)
participant M as MiniMindForCausalLM
(Transformer + MoE)
participant L as Loss
(CE / DPO / PPO)
participant O as Optimizer
(AdamW)
T->>D: Encode raw text โ token IDs
D->>M: Batched input_ids + labels
M->>M: Embedding โ Nร(RMSNorm โ Attention โ RMSNorm โ FFN) โ LM Head
M->>L: logits + labels โ loss
L->>O: loss.backward()
O->>M: parameter update (lr=5e-4, weight_decay=0.1)
Note over M,L: MoE adds aux_loss for load balancing
Narrated Walkthrough
๐ถ Following the Full Training Pipeline
Step 1 โ Tokenizer Training (trainer/train_tokenizer.py): Train a BPE tokenizer on your corpus to produce a 6,400-token vocabulary. This creates the tokenizer.model file consumed by all downstream stages.
Step 2 โ Pretraining (trainer/pretrain.py): Feed raw text through PretrainDataset into MiniMindForCausalLM with standard cross-entropy loss. The model learns general language patterns over ~10B tokens. Distributed training via DDP is supported.
Step 3 โ Supervised Fine-Tuning (trainer/train_sft.py): Switch to SFTDataset with instruction/response pairs. The loss mask ignores prompt tokens so the model only learns to generate responses.
Step 4 โ LoRA Adaptation (trainer/train_lora.py): Freeze the base model, inject LoRA low-rank matrices into Q/V projections, and fine-tune on domain data with ~0.1% trainable params.
Step 5 โ Preference Alignment (trainer/train_dpo.py): Use DPODataset to learn from chosen/rejected response pairs. DPO directly optimises the policy without a separate reward model.
Step 6 โ GRPO Reinforcement (trainer/train_grpo.py): Group Relative Policy Optimization samples multiple completions per prompt, ranks them, and updates the policy using relative advantages โ no critic network needed.
Step 7 โ Deployment (scripts/serve_api.py): Wrap the trained model in an OpenAI-compatible FastAPI server. The scripts/web_demo.py Streamlit app provides a chat interface for interactive testing.
Key Components
| Class / Function | File | Role |
|---|---|---|
MiniMindConfig | model/model_minimind.py | HuggingFace-compatible config holding hidden_size, num_layers, MoE params, RoPE theta |
MiniMindModel | model/model_minimind.py | Core transformer stack: token embedding โ N ร MiniMindBlock โ RMSNorm |
MiniMindForCausalLM | model/model_minimind.py | Full causal LM wrapping MiniMindModel + LM head + loss; inherits GenerationMixin |
Attention | model/model_minimind.py | Grouped Query Attention (GQA) with KV cache, RoPE, optional Flash Attention |
FeedForward | model/model_minimind.py | SwiGLU feed-forward: gate_proj ยท up_proj โ SiLU โ down_proj |
MoEGate | model/model_minimind.py | Top-k softmax gating with auxiliary load-balancing loss |
MOEFeedForward | model/model_minimind.py | Mixture of Experts layer dispatching tokens to top-k experts + shared expert |
RMSNorm | model/model_minimind.py | Root Mean Square Layer Normalization (no centering, faster than LayerNorm) |
LoRA | model/model_lora.py | Low-Rank Adaptation module injected into attention Q/V projections |
PretrainDataset | dataset/lm_dataset.py | Streams tokenised raw-text chunks for next-token pretraining |
SFTDataset | dataset/lm_dataset.py | Instruction-response pairs with prompt masking for supervised fine-tuning |
DPODataset | dataset/lm_dataset.py | Chosen/rejected pairs for Direct Preference Optimization |
RLAIFDataset | dataset/lm_dataset.py | Prompt dataset for RL-based training (PPO, GRPO, SPO) |
CriticModel | trainer/train_ppo.py | PPO value network extending MiniMindForCausalLM with a scalar value head |
AutoAdaptiveValueTracker | trainer/train_spo.py | SPO adaptive baseline tracker for reward normalisation |
Deep-Dive Topics
Focused explorations of key architectural decisions and training strategies.
๐๏ธ Transformer Architecture
RoPE positional encoding, Grouped Query Attention with KV cache, RMSNorm, and SwiGLU feed-forward โ how MiniMind assembles a GPT-style decoder.
model/model_minimind.py๐ Training Pipeline Flow
From pretrain through SFT, DPO, PPO, GRPO, and SPO โ the complete lifecycle of training a language model with MiniMind.
trainer/*.py๐ MoE Routing
MoEGate softmax scoring, top-k expert selection, shared expert path, and auxiliary load-balancing loss for stable training.
model/model_minimind.py โ MoEGate๐ฏ LoRA Fine-tuning
Low-rank decomposition of weight updates, injection into Q/V projections, rank selection, and merging for efficient adaptation.
model/model_lora.py โ LoRADirectory Map
Model Configuration (MiniMindConfig)
Key knobs exposed via MiniMindConfig(PretrainedConfig). The three standard model sizes are Small-26M, MoE-145M, and Base-104M.
| Parameter | Small-26M | MoE-145M | Base-104M | Description |
|---|---|---|---|---|
hidden_size | 512 | 640 | 768 | Transformer hidden dimension |
num_hidden_layers | 8 | 8 | 16 | Number of transformer blocks |
num_attention_heads | 8 | 8 | 8 | Query heads in multi-head attention |
num_key_value_heads | 2 | 2 | 2 | KV heads for Grouped Query Attention |
vocab_size | 6400 | BPE tokenizer vocabulary size | ||
max_position_embeddings | 32768 | Maximum sequence length (with YaRN RoPE scaling) | ||
rope_theta | 1000000.0 | RoPE base frequency for long-context extrapolation | ||
use_moe | false | true | false | Enable Mixture of Experts layers |
num_experts | โ | 8 | โ | Total expert count per MoE layer |
num_experts_per_tok | โ | 2 | โ | Top-k experts activated per token |
n_shared_experts | โ | 1 | โ | Always-on shared experts alongside routed ones |
flash_attn | true (if available) | Use Flash Attention 2 for memory-efficient attention | ||
Sub-Module Index
Drill down into each sub-module for class-level documentation, diagrams, and code walkthroughs.
๐ฆ model/
Core transformer architecture โ MiniMindForCausalLM, Attention with GQA + RoPE, MoE routing, LoRA adaptation, RMSNorm, SwiGLU FFN.
3 files ยท model_minimind.py ยท model_lora.py๐๏ธ trainer/
Complete training pipeline โ pretrain, SFT, LoRA, DPO, PPO, GRPO, SPO, knowledge distillation, reasoning, tokenizer training, shared utilities.
11 files ยท pretrain.py โฆ trainer_utils.py๐ dataset/
Data loading and preprocessing โ PretrainDataset for raw text, SFTDataset for instructions, DPODataset for preferences, RLAIFDataset for RL prompts.
2 files ยท lm_dataset.py๐ scripts/
Deployment and tooling โ OpenAI-compatible FastAPI server, Streamlit web demo, model format converter, API test client.
4 files ยท serve_api.py ยท web_demo.pyCross-Module Dependencies
flowchart TD dataset["dataset/
lm_dataset.py"] model_mm["model/
model_minimind.py"] model_lora["model/
model_lora.py"] trainer["trainer/*
(11 training scripts)"] scripts["scripts/*
(API, demo, converter)"] eval["eval_llm.py"] utils["trainer/
trainer_utils.py"] dataset -->|"PretrainDataset, SFTDataset, DPODataset"| trainer model_mm -->|"MiniMindForCausalLM"| trainer model_mm -->|"MiniMindForCausalLM"| scripts model_lora -->|"LoRA, apply_lora"| scripts model_mm -->|"MiniMindForCausalLM"| eval model_lora -->|"LoRA"| eval utils -->|"lr_scheduler, logging"| eval style dataset fill:#1a3a2a,stroke:#3fb950,color:#c9d1d9 style model_mm fill:#1a2a3a,stroke:#58a6ff,color:#c9d1d9 style model_lora fill:#1a2a3a,stroke:#58a6ff,color:#c9d1d9 style trainer fill:#3a2a1a,stroke:#d29922,color:#c9d1d9 style scripts fill:#3a1a1a,stroke:#f85149,color:#c9d1d9