A curated, Markdown-based ML knowledge base of 923 papers, lectures and articles, plus a small NumPy-only custom classifier and regressor called APRL.
raiyanyahya/how-to-train-your-gpt
A 12-chapter, commented tutorial that walks you through building and training a small LLaMA-style GPT from scratch in Python and PyTorch.
- Stars
- 4,049
- Forks
- 505
- Open issues
- 5
- Last commit
- 2 months ago
What is it?
This is a learning project that teaches how a modern decoder-only language model works by building one from scratch. It covers the full pipeline: a BPE tokenizer, embeddings, rotary position encoding (RoPE), multi-head attention with a causal mask, transformer blocks using RMSNorm and SwiGLU, a training loop with AdamW and cosine warmup, and an inference engine with KV cache and sampling options.
The material is organized as 12 chapters meant to be read in order, plus about 28 standalone topic explainers and companion Jupyter notebooks. Each code section is annotated with what each line does and why it is there. The guide assumes only basic Python and does not require prior machine learning experience.
The default configuration is a small model (d_model=256, 4 layers) that trains on CPU in minutes. A GPT-2 scale configuration (about 151M parameters) is included but needs a GPU. The project is intended for learning rather than production use.
Key features
- 01Twelve sequential chapters covering tokenization through inference
- 02Line-by-line comments explaining what each piece of code does and why
- 03Implements RoPE, RMSNorm, SwiGLU, pre-norm blocks and weight tying
- 04Custom training loop with AdamW, cosine warmup and mixed precision
- 05Inference with KV cache, temperature, top-k/top-p sampling and beam search
- 06Runnable main.py with a small CPU-friendly default config
Use cases
- Python developers who want to understand how ChatGPT-style models work internally
- Students learning Transformer attention and training from the ground up
- Engineers comparing architectural choices such as RoPE vs learned positions or RMSNorm vs LayerNorm
- Teachers preparing a hands-on lab on building a small language model
Quick start
- 1
Clone the repository
git clone https://github.com/raiyanyahya/how-to-train-your-gpt.git - 2
Create and activate a virtual environment (Mac/Linux)
python -m venv gpt_env && source gpt_env/bin/activate - 3
Install dependencies from the requirements file
pip install -r requirements.txt - 4
Run the training script (tiny config by default)
python main.py
Things to know
- The default config is a tiny model, and the 151M-parameter GPT-2 scale configuration needs a GPU.
- This is a learning resource and is labelled for learning only, not for production models.
Topics
FAQ
- What is how-to-train-your-gpt?
- A 12-chapter, commented tutorial that walks you through building and training a small LLaMA-style GPT from scratch in Python and PyTorch. This is a learning project that teaches how a modern decoder-only language model works by building one from scratch. It covers the full pipeline: a BPE tokenizer, embeddings, rotary position encoding (RoPE), multi-head attention with a causal mask, transformer blocks using RMSNorm and SwiGLU, a training loop with AdamW and cosine warmup, and an inference engine with KV cache and sampling options.
- Is how-to-train-your-gpt free to use? What license does it have?
- how-to-train-your-gpt is open source under the MIT license. Check the license terms before using it in commercial products.
- Is how-to-train-your-gpt still maintained?
- Updated within 6 months. The last commit to how-to-train-your-gpt was on Aug 30, 2026.
- What can I use how-to-train-your-gpt for?
- Python developers who want to understand how ChatGPT-style models work internally Students learning Transformer attention and training from the ground up Engineers comparing architectural choices such as RoPE vs learned positions or RMSNorm vs LayerNorm Teachers preparing a hands-on lab on building a small language model
- How do I install how-to-train-your-gpt?
- Clone the repository: git clone https://github.com/raiyanyahya/how-to-train-your-gpt.git Create and activate a virtual environment (Mac/Linux): python -m venv gpt_env && source gpt_env/bin/activate Install dependencies from the requirements file: pip install -r requirements.txt Run the training script (tiny config by default): python main.py
- Where can I get how-to-train-your-gpt?
- The source code and installation instructions are on GitHub: https://github.com/raiyanyahya/how-to-train-your-gpt
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
A free, MIT-licensed course of 542 lessons that builds AI engineering concepts from scratch in Python, TypeScript, Rust and Julia.
A Chinese-language open tutorial that teaches LLM principles by building a small LLaMA2-style model from scratch, covering Transformer, pretraining, fine-tuning, RAG and agents.
A collection of 54 single-file, dependency-free Python scripts that implement modern AI algorithms from scratch so their internals are readable.
Model-definition framework for state-of-the-art text, vision, audio and multimodal machine learning models, for inference and training.
A 17-lesson Python tutorial that builds a Claude Code-like agent harness from scratch, starting with a single loop and bash.