How ydeven757/llms-from-scratch works
Purpose and Entry Points
This is the companion repository for *Build a Large Language Model (From Scratch)*: an educational, Python/PyTorch codebase that builds GPT-like models, pretrains them, and adapts them for classification and instruction following. Primary entry points are chapter notebooks and runnable scripts, not a unified application: ch04/01_main-chapter-code/gpt.py, ch05/01_main-chapter-code/gpt_train.py, ch06/01_main-chapter-code/gpt_class_finetune.py, and ch07/01_main-chapter-code/gpt_instruction_finetuning.py.
Reusable Package Boundary
pkg/llms_from_scratch is the consolidated importable implementation/test surface, while chapter directories preserve book-oriented, self-contained examples. Chapter modules cover each pipeline stage: pkg/llms_from_scratch/ch02.py, pkg/llms_from_scratch/ch04.py, and pkg/llms_from_scratch/ch07.py. Consumers should treat duplicated previous_chapters.py files in chapters as pedagogical snapshots rather than a stable shared library.
Text Data and Tokenization
The pipeline begins with raw text, tokenization, and next-token training examples. Chapter 2 implements data loading and BPE exploration in ch02/01_main-chapter-code/ch02.ipynb and ch02/02_bonus_bytepair-encoder/bpe_openai_gpt2.py; the reusable layer is pkg/llms_from_scratch/ch02.py. Token IDs and context windows form the boundary passed into embedding and transformer execution.
Transformer Model Construction
Attention mechanisms lead into the baseline decoder-only GPT model: ch03/01_main-chapter-code/ch03.ipynb, ch04/01_main-chapter-code/gpt.py, and pkg/llms_from_scratch/ch04.py. The model converts token sequences through embeddings, causal multi-head attention, feed-forward blocks, residual/normalization structure, and a vocabulary-logit output head. Optional architecture branches compare KV caching, GQA/MLA/SWA, MoE, and KV sharing under ch04/03_kv-cache, ch04/04_gqa, and ch04/07_moe; they are alternatives rather than dependencies of the main path.
Pretraining and Generation
Pretraining consumes unlabeled token batches, computes next-token loss, and updates the GPT model with PyTorch training loops. Core scripts are ch05/01_main-chapter-code/gpt_train.py and ch05/01_main-chapter-code/gpt_generate.py; Gutenberg preparation is separated in ch05/03_bonus_pretraining_on_gutenberg/prepare_dataset.py. Model downloading/loading is an external-weight boundary in ch05/01_main-chapter-code/gpt_download.py, supporting use of pretrained weights alongside scratch training.
Adaptation, Evaluation, and Serving Examples
Classification fine-tuning adapts the pretrained GPT output for labeled text through ch06/01_main-chapter-code/gpt_class_finetune.py. Instruction fine-tuning consumes JSON instruction/response datasets and produces response-generating models via ch07/01_main-chapter-code/gpt_instruction_finetuning.py and ch07/01_main-chapter-code/instruction-data-with-response.json. Evaluation and dataset-generation examples integrate local Ollama and the OpenAI API: ch07/01_main-chapter-code/ollama_evaluate.py, ch07/03_model-evaluation, and ch07/05_dataset-generation. Lightweight interactive UIs are optional demos, e.g. ch05/06_user_interface/app_own.py, ch06/04_user_interface/app.py, and ch07/06_user_interface/app.py.
Runtime and Infrastructure
The material runtime is Python with PyTorch, designed for laptop execution while using GPUs when available. Distributed training examples use PyTorch DDP/torchrun in appendix-A/01_main-chapter-code/DDP-script-torchrun.py. Dependency and package metadata reside in pyproject.toml, requirements.txt, and pixi.toml; optional Docker development support is under setup/03_optional-docker-environment/.devcontainer/Dockerfile. GitHub Actions provide cross-platform Python/PyTorch validation.
Main entry points: gpt_train.py Scratch pretraining · ch02.py Tokenization & windows · gpt_instruction_finetuning.py Instruction fine-tuning · ch04.py Reusable GPT implementation
Components 18
Data Pipeline
- Raw text corpora
- Tokenization & windows
pkg/llms_from_scratch/ch02.py - BPE exploration
ch02/02_bonus_bytepair-encoder/bpe_openai_gpt2.py - Gutenberg preparation
ch05/03_bonus_pretraining_on_gutenberg/prepare_dataset.py - Instruction responses
ch07/01_main-chapter-code/instruction-data-with-response.json
Modeling
- Reusable GPT implementation · Consolidated importable model implementation.
pkg/llms_from_scratch/ch04.py - Baseline GPT model
ch04/01_main-chapter-code/gpt.py - Attention foundations
ch03/01_main-chapter-code/ch03.ipynb - GPT architecture variants · KV cache, GQA, MLA, SWA, MoE, and KV-sharing alternatives.
ch04/03_kv-cache
Training & Adaptation
- Scratch pretraining
ch05/01_main-chapter-code/gpt_train.py - Text generation
ch05/01_main-chapter-code/gpt_generate.py - Pretrained weight loading
ch05/01_main-chapter-code/gpt_download.py - Classification fine-tuning
ch06/01_main-chapter-code/gpt_class_finetune.py - Instruction fine-tuning
ch07/01_main-chapter-code/gpt_instruction_finetuning.py
Evaluation & Runtime
- LLM evaluation · Uses local Ollama and OpenAI API integrations.
ch07/03_model-evaluation - Interactive UI demos
ch07/06_user_interface/app.py - Distributed training
appendix-A/01_main-chapter-code/DDP-script-torchrun.py - Python / PyTorch runtime · Laptop-first execution with optional GPU acceleration.
Connections 20
- Raw text corpora → Tokenization & windows: tokenize
- BPE exploration → Tokenization & windows: explores BPE
- Gutenberg preparation → Raw text corpora: prepares corpus
- Tokenization & windows → Reusable GPT implementation: token ID windows
- Attention foundations → Baseline GPT model: builds toward
- Baseline GPT model → Reusable GPT implementation: chapter counterpart
- GPT architecture variants → Baseline GPT model: alternative designs
- Tokenization & windows → Scratch pretraining: training batches
- Reusable GPT implementation → Scratch pretraining: model and logits
- Pretrained weight loading → Scratch pretraining: initializes from weights
- Scratch pretraining → Text generation: trained model
- Scratch pretraining → Classification fine-tuning: pretrained model
- Scratch pretraining → Instruction fine-tuning: pretrained model
- Instruction responses → Instruction fine-tuning: supervision
- Instruction fine-tuning → LLM evaluation: responses
- Text generation → Interactive UI demos: generation backend
- Instruction fine-tuning → Interactive UI demos: instruction model
- Scratch pretraining → Distributed training: scales training loop
- Python / PyTorch runtime → Reusable GPT implementation: executes
- Python / PyTorch runtime → Scratch pretraining: trains on