Train a tiny LLM. Understand the whole machine.
A visual deep dive into jingyaogong/minimind: an end-to-end project for building, training, aligning and serving a small language model from scratch.
A visual deep dive into jingyaogong/minimind: an end-to-end project for building, training, aligning and serving a small language model from scratch.
It keeps the model close enough to modern practice to be useful, but small enough to train, inspect and modify on personal hardware.
Move beyond “call an API” or “attach a LoRA” and see what pretraining, SFT, RL and serving actually do.
Change the model, data, tokenizer, reward or runtime and observe the consequences.
This is an educational and experimental platform, not a frontier-model replacement.
The black box gets smaller when you can read the whole path.
The real value is not benchmark theatre. It is a concrete mental model of how a language model becomes a system.
Tokenisation, causal attention, training loops, fine-tuning and evaluation.
Explore domain behaviour when a frontier API is too opaque, large or expensive.
Change one ingredient at a time: architecture, context, reward or tools.
Use a compact sandbox for low-latency or on-device assistant concepts.
MiniMind is best understood as a sequence of increasingly opinionated loops.
Config, Transformer blocks, attention, RMSNorm, RoPE, SwiGLU, MoE and tokenizer assets.
Pretraining, SFT, LoRA, DPO, PPO, GRPO, distillation and agentic training.
JSONL-oriented loaders and templates for text, conversations and tool-use examples.
Conversion, tool-call evaluation, an OpenAI-compatible server and Streamlit demo.
Translation note: trainer means the executable learning pipeline, not a high-level framework that hides the loop.
Start with next-token prediction. Then teach interaction, preference, tools and multi-step behaviour.
The model is repeatedly shaped by data, objectives and feedback.
Data format and chat templates are part of the model interface.
Capability is weights + prompt format + tools + runtime.
8 layers, 768 hidden size, 8 query heads and 4 key/value heads.
Four experts with top-1 routing. More capacity, plus routing and kernel overhead.
A compact vocabulary reduces embedding and output-layer cost. The trade-off is less mature tokenisation across languages.
Pretraining uses text → next token. SFT uses conversation templates, including roles, thinking markers and tool calls.
Important: max_seq_len means tokens, while dataset names may refer to character length. Padding and truncation are efficiency-versus-context decisions.
The model includes several design choices you will meet in larger systems.
Pre-NormRMSNormSwiGLUGrouped-query attentionRoPEKV cacheYaRN scalingMore query heads than key/value heads can reduce the memory footprint of cached keys and values during generation.
The config advertises a 32,768-position limit and includes an inference-time YaRN option. Treat long-context gains as something to measure, not assume.
Past key/value states avoid recomputing the entire prefix on every step.
Preference pairs shift the policy towards the preferred answer without a separate reward-model training phase.
trainer/train_dpo.py
The policy generates candidates, receives a reward and updates towards higher-scoring outputs.
trainer/train_ppo.py · train_grpo.py
The agent can call tools over multiple turns, observe results and receive delayed task-level rewards.
trainer/train_agent.py
Reported in the repository README, not reproduced here. The project describes the comparison as rough because its data scale and training mix differ from larger models.
Inference → mini training → controlled extension. That order keeps feedback fast and failure modes legible.
Download a checkpoint. Use the CLI, Streamlit demo or API.
No trainingBest first hourUse the mini JSONL datasets to reproduce pretraining and SFT.
GPU recommendedBest for learningAdd LoRA, DPO, RL, tools, agentic rollouts or distillation.
Experiment designBest for buildersThe README lists Python 3.10.16, CUDA 12.2, Ubuntu 20.04 and an RTX 3090-based reference setup. Your environment may differ.
Use the dependency file in a fresh environment.
Use ModelScope or Hugging Face for a released model.
Load it with eval_llm.py and test a few prompts.
Launch Streamlit or the API after the baseline works.
Keep one known-good checkpoint and command. Every experiment should return to that baseline.
Place pretrain_t2t_mini.jsonl in ./dataset, then train the base checkpoint.
Add sft_t2t_mini.jsonl to teach roles, instruction following, thinking markers and basic tool-call patterns.
The repository documents checkpoint saving and resume support. For a long run, use --from_resume 1 and keep checkpoints with experiment logs.
Swap in a small, licensed dataset with a held-out evaluation set.
Compare capacity, active parameters, throughput and quality. Don’t assume fewer active parameters means lower wall-clock cost.
Start with a narrow task, explicit success criteria and an inspectable reward or preference signal.
Inspect generations, thinking display, tool selection and multi-turn calls.
Expose a familiar interface to a chat UI or application, with reasoning content and tool-call support.
Convert or serve through Transformers, vLLM, Ollama or llama.cpp where the format and versions support it.
Treat tool schemas, timeouts, refusal behaviour, logging and provenance as application code.
This is the difference between a nice demo and an evidence-bearing learning project.
Start with inference, reproduce the mini training path, then run one controlled experiment. Treat the repo as a laboratory, not a magic box.
Claims and numbers are translated or synthesised from the repository as inspected on 19 September 2026. Timings, costs and benchmark figures are repository claims; this deck does not reproduce the runs.
The next useful experiment is probably one dataset, one objective and one metric.