This repository contains the code for developing, pretraining, and finetuning a GPT-like LLM and is the official code repository for the book [Build a Large Language Model (From Scratch)](http://mng.bz/orYv).
In [*Build a Large Language Model (From Scratch)*](http://mng.bz/orYv), you'll learn and understand how large language models (LLMs) work from the inside out by coding them from the ground up, step by step. In this book, I'll guide you through creating your own LLM, explaining each stage with clear text, diagrams, and examples.
The method described in this book for training and developing your own small-but-functional model for educational purposes mirrors the approach used in creating large-scale foundational models such as those behind ChatGPT. In addition, this book includes code for loading the weights of larger pretrained models for finetuning.
To download a copy of this repository, click on the [Download ZIP](https://github.com/rasbt/LLMs-from-scratch/archive/refs/heads/main.zip) button or execute the following command in your terminal:
(If you downloaded the code bundle from the Manning website, please consider visiting the official code repository on GitHub at [https://github.com/rasbt/LLMs-from-scratch](https://github.com/rasbt/LLMs-from-scratch) for the latest updates.)
Please note that this `README.md` file is a Markdown (`.md`) file. If you have downloaded this code bundle from the Manning website and are viewing it on your local computer, I recommend using a Markdown editor or previewer for proper viewing. If you haven't installed a Markdown editor yet, [MarkText](https://www.marktext.cc) is a good free option.
You can alternatively view this and other files on GitHub at [https://github.com/rasbt/LLMs-from-scratch](https://github.com/rasbt/LLMs-from-scratch) in your browser, which renders Markdown automatically.
> If you're seeking guidance on installing Python and Python packages and setting up your code environment, I suggest reading the [README.md](setup/README.md) file located in the [setup](setup) directory.
| Ch 2: Working with Text Data | - [ch02.ipynb](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/ch02.ipynb)<br/>- [dataloader.ipynb](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/dataloader.ipynb) (summary)<br/>- [exercise-solutions.ipynb](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch02/01_main-chapter-code/exercise-solutions.ipynb) | [./ch02](./ch02) |
| Appendix D: Adding Bells and Whistles to the Training Loop | - [appendix-D.ipynb](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/appendix-D/01_main-chapter-code/appendix-D.ipynb) | [./appendix-D](./appendix-D) |
The code in the main chapters of this book is designed to run on conventional laptops within a reasonable timeframe and does not require specialized hardware. This approach ensures that a wide audience can engage with the material. Additionally, the code automatically utilizes GPUs if they are available. (Please see the [setup](https://github.com/rasbt/LLMs-from-scratch/blob/main/setup/README.md) doc for additional recommendations.)
- [Comparing Various Byte Pair Encoding (BPE) Implementations](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch02/02_bonus_bytepair-encoder)
- [Understanding the Difference Between Embedding Layers and Linear Layers](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch02/03_bonus_embedding-vs-matmul)
- [Dataloader Intuition with Simple Numbers](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch02/04_bonus_dataloader-intuition)
- [Alternative Weight Loading from Hugging Face Model Hub using Transformers](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch05/02_alternative_weight_loading/weight-loading-hf-transformers.ipynb)
- [Pretraining GPT on the Project Gutenberg Dataset](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch05/03_bonus_pretraining_on_gutenberg)
- [Adding Bells and Whistles to the Training Loop](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch05/04_learning_rate_schedulers)
- [Optimizing Hyperparameters for Pretraining](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch05/05_bonus_hparam_tuning)
- [Additional experiments finetuning different layers and using larger models](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch06/02_bonus_additional-experiments)
- [Finetuning different models on 50k IMDB movie review dataset](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch06/03_bonus_imdb-classification)
- [Dataset Utilities for Finding Near Duplicates and Creating Passive Voice Entries](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch07/02_dataset-utilities)
- [Evaluating Instruction Responses Using the OpenAI API and Ollama](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch07/03_model-evaluation)
- [Generating a Dataset for Instruction Finetuning](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch07/05_dataset-generation)
- [Generating a Preference Dataset with Llama 3.1 70B and Ollama](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch07/04_preference-tuning-with-dpo/create-preference-data-ollama.ipynb)
- [Direct Preference Optimization (DPO) for LLM Alignment](https://nbviewer.org/github/rasbt/LLMs-from-scratch/blob/main/ch07/04_preference-tuning-with-dpo/dpo-from-scratch.ipynb)
I welcome all sorts of feedback, best shared via the [Manning Forum](https://livebook.manning.com/forum?product=raschka&page=1) or [GitHub Discussions](https://github.com/rasbt/LLMs-from-scratch/discussions). Likewise, if you have any questions or just want to bounce ideas off others, please don't hesitate to post these in the forum as well.
Please note that since this repository contains the code corresponding to a print book, I currently cannot accept contributions that would extend the contents of the main chapter code, as it would introduce deviations from the physical book. Keeping it consistent helps ensure a smooth experience for everyone.