Plain-PyTorch codebase to pretrain and post-train your own small language models
open sourceMITfree
Train LLM From Scratch is an MIT-licensed Python codebase for training your own language model end to end in plain PyTorch, from raw text and a BPE tokenizer through pretraining to SFT, a reward model, PPO, DPO, ORPO, KTO and GRPO, then evaluation and chat. It installs with pip in editable mode, runs each stage from scripts driven by per-stage JSON configs whose fields can be overridden on the command line, saves checkpoints between stages, and offers a classic and a modern Transformer (RoPE, RMSNorm, SwiGLU, grouped-query attention, Mixture of Experts). Presets range from a 369K-parameter model that trains on a laptop CPU in minutes to billion-parameter configs on GPUs, with a Streamlit control panel and a docs site. It began as a pretraining tutorial and its README still explains each step.
Problems it solves
- train my own small language model from raw text without Hugging Face libraries
- run SFT, DPO and GRPO on a model I pretrained myself to see each stage work
- train a tiny language model on my laptop CPU to learn how pretraining behaves
Features
- pretraining and post-training stages (SFT, reward model, PPO, DPO, ORPO, KTO, GRPO) in plain PyTorch
- per-stage JSON configs with command-line overrides
- presets from a CPU-sized 369K-parameter model to billion-parameter configs
- classic and modern Transformer architectures, including Mixture of Experts
- Streamlit control panel
Limitations
- billion-parameter configs need a large GPU; a free Colab or Kaggle T4 fits the 13M model but not a billion-parameter one1
Alternatives
The same job
These do what Train LLM From Scratch does.
- PaddleFormers A Transformers library for PaddlePaddle that trains large language and vision-language models
