jingyaogong/minimind · 72.3k stars · Intermediate · 20 min · 2026-11-28

Train a 64M LLM from scratch with MiniMind

MiniMind trains a roughly 64M-parameter language model from scratch in plain PyTorch; you run pretraining, then SFT, then chat with the result.

TL;DR

MiniMind is an open-source (Apache-2.0) project for training a small language model, about 64M parameters, from scratch with native PyTorch code. You download two mini jsonl datasets (about 2.8GB in total per the README), run train_pretrain.py, then train_full_sft.py, and chat with the result using eval_llm.py.

Why this repo matters

Per the README, libraries such as transformers, trl and peft wrap training in a few abstract lines, which hides how an LLM actually works. MiniMind implements the model, the training loops and the post-training methods (SFT, LoRA, distillation, DPO, PPO, GRPO and an agent trainer, each with its own script under trainer/) in readable PyTorch. The README says the "2 hours" figure is one SFT epoch on a single NVIDIA 3090 and "RMB 3" is the matching GPU rental cost.

Best practices

  • Start with the mini datasets. The README says you only need pretrain_t2t_mini.jsonl and sft_t2t_mini.jsonl to reproduce the MiniMind Zero dialogue model.
  • Run scripts from trainer/. The README states all training scripts are executed from the ./trainer directory.
  • Use --from_resume 1 for long runs. Checkpoints (model, optimizer, progress) go in ./checkpoints/ and the run resumes automatically.
  • Scale out with torchrun. For N GPUs the README uses torchrun --nproc_per_node N train_xxx.py.
  • Use LoRA for a private domain. The README positions it for adapting the model to a vertical scenario while keeping general ability.
  • Match --weight to what you trained. eval_llm.py --weight takes the weight-name prefix such as pretrain or full_sft.

How-to

Where do I start?

1. Install and fetch the data

What do I need first? Clone the repo and install the pinned requirements (the README gives this, with a mirror flag you can drop):

git clone --depth 1 https://github.com/jingyaogong/minimind
cd minimind && pip install -r requirements.txt

Then download pretrain_t2t_mini.jsonl and sft_t2t_mini.jsonl from the ModelScope or Hugging Face dataset page into ./dataset.

Next: Pretrain

Try it

Run pretrain then SFT on the mini datasets, then ask the same question with --weight pretrain and --weight full_sft. Write down how the answers differ.

Pitfalls

  • Weight names must line up. The README warns the --weight for LoRA evaluation must match the base model used in train_lora.
  • The WebUI needs a copied model folder. The README says to copy the transformers-format model into ./scripts/ first, or the demo errors out.
  • Timing claims are narrow. "2 hours" is one SFT epoch on one 3090 according to the README, not the whole pipeline on any hardware.
  • Pin your environment. requirements.txt pins versions (for example transformers==4.57.6), so use a fresh virtualenv.

Next steps