A minimal, hackable harness that covers the full LLM pipeline from tokenization and pretraining to finetuning, evaluation and chat, runnable on a single GPU node.
rasbt/LLMs-from-scratch
Companion code for the book Build a Large Language Model (From Scratch), implementing a GPT-like LLM in PyTorch step by step.
- Stars
- 106,975
- Forks
- 16,469
- Open issues
- 4
- Last commit
- 19 days ago
What is it?
This repository contains the code for developing, pretraining, and finetuning a GPT-like large language model in PyTorch, without relying on external LLM libraries. It is organized by book chapter, covering text data preparation and tokenization, attention mechanisms, building a GPT model, pretraining on unlabeled data, finetuning for classification, and instruction finetuning.
The code is designed to run on a conventional laptop within a reasonable time, and it uses a GPU automatically when one is available. Beyond the main chapters, bonus folders explore topics such as BPE tokenizers, KV cache, grouped-query and multi-head latent attention, mixture-of-experts, DPO preference tuning, LoRA, and from-scratch implementations of architectures like Llama 3.2, Qwen3, and Gemma 3. Loading weights of larger pretrained models for finetuning is also included.
The project is the official code companion to the Manning book, and a video course and a sequel on reasoning models are also available.
Key features
- 01Step-by-step implementation of a GPT-like model in PyTorch without external LLM libraries
- 02Covers tokenization, attention, pretraining, classification finetuning and instruction finetuning
- 03Runs on conventional laptops and uses a GPU automatically when available
- 04Bonus material on BPE, KV cache, MoE, DPO and LoRA
- 05From-scratch code for architectures such as Llama 3.2, Qwen3 and Gemma 3
Use cases
- Learning how LLMs work internally by writing the code yourself
- Following along with the Build a Large Language Model book or its video course
- Experimenting with pretraining a small GPT model on a laptop for educational purposes
- Studying modern attention variants and architectures through readable PyTorch code
Quick start
- 1
Clone the repository with a shallow copy
git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git - 2
Follow the setup guide in the setup folder to install Python and the required packages
- 3
Open the main chapter notebooks, such as ch02/01_main-chapter-code/ch02.ipynb
Things to know
- The metadata lists the license as NOASSERTION, so the license terms are unclear and should be checked before reuse.
- This is companion code for a book, so it is a set of learning notebooks and scripts rather than a packaged library or application.
Topics
FAQ
- What is LLMs-from-scratch?
- Companion code for the book Build a Large Language Model (From Scratch), implementing a GPT-like LLM in PyTorch step by step. This repository contains the code for developing, pretraining, and finetuning a GPT-like large language model in PyTorch, without relying on external LLM libraries. It is organized by book chapter, covering text data preparation and tokenization, attention mechanisms, building a GPT model, pretraining on unlabeled data, finetuning for classification, and instruction finetuning.
- Is LLMs-from-scratch free to use? What license does it have?
- LLMs-from-scratch is open source under the Other license. Check the license terms before using it in commercial products.
- Is LLMs-from-scratch still maintained?
- Actively maintained. The last commit to LLMs-from-scratch was on Oct 2, 2026.
- What can I use LLMs-from-scratch for?
- Learning how LLMs work internally by writing the code yourself Following along with the Build a Large Language Model book or its video course Experimenting with pretraining a small GPT model on a laptop for educational purposes Studying modern attention variants and architectures through readable PyTorch code
- How do I install LLMs-from-scratch?
- Clone the repository with a shallow copy: git clone --depth 1 https://github.com/rasbt/LLMs-from-scratch.git Follow the setup guide in the setup folder to install Python and the required packages Open the main chapter notebooks, such as ch02/01_main-chapter-code/ch02.ipynb
- Where can I get LLMs-from-scratch?
- The source code and installation instructions are on GitHub: https://github.com/rasbt/LLMs-from-scratch
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
A 12-chapter, commented tutorial that walks you through building and training a small LLaMA-style GPT from scratch in Python and PyTorch.
A free, MIT-licensed course of 542 lessons that builds AI engineering concepts from scratch in Python, TypeScript, Rust and Julia.
A 12-week, 24-lesson beginner AI curriculum from Microsoft covering symbolic AI, neural networks, computer vision, NLP and ethics, with notebooks in PyTorch and TensorFlow.
A collection of 54 single-file, dependency-free Python scripts that implement modern AI algorithms from scratch so their internals are readable.
A curated, Markdown-based ML knowledge base of 923 papers, lectures and articles, plus a small NumPy-only custom classifier and regressor called APRL.