LLMs-from-scratch: step-by-step code for building a GPT-style LLM in PyTorch
It is the companion code for Sebastian Raschka's book on coding a GPT-style large language model from the ground up, with each chapter building one stage of it.
The repository contains code for Sebastian Raschka's book Build a Large Language Model (From Scratch). According to the README, the book shows how to code a GPT-like model step by step. The chapters cover text data, attention mechanisms, a GPT model, pretraining on unlabeled data, finetuning for text classification, and finetuning to follow instructions. An appendix covers LoRA. The README says the code uses PyTorch without any external LLM libraries. The main chapter code is designed to run on conventional laptops, and it uses a GPU when one is available.
The bonus folders add a byte pair encoding tokenizer written from scratch, a KV cache, Grouped-Query Attention, Mixture-of-Experts, direct preference optimization, and from-scratch versions of Llama 3.2, Qwen3, Gemma 3 and Olmo 3. Setup is covered in a separate guide. The README also points to a 17-hour video course that codes through the chapters, and to an evaluation script that uses Ollama.
The README says the book includes code for loading the weights of larger pretrained models for finetuning. It also states that the main chapter code does not accept outside contributions that extend it, because the repository matches the print book. The text provided does not state a licence.
In the author's words
This repository contains the code for developing, pretraining, and finetuning a GPT-like LLM and is the official code repository for the book
From the README or the author's post. We checked that the sentence is there, not that it is true.
When to reach for it
A reader who wants to understand how a GPT-style model works can follow the chapters in order and rebuild each piece, rather than relying only on papers. The bonus folders show how the same from-scratch approach extends to newer architectures.
Works with
See it
AI wrote this entry from the project's own README or its author's post. What the project does, and that AI built it where the entry says so, are the author's statements, quoted here. Nobody at IKANGAI ran or tested it. How this list is made