GPT From Scratch

October 10, 2026 (2w ago)

Stack: Python, PyTorch, pytest, mypy, GitHub Actions, semantic-release · Repo: Axeloooo/gpt-from-scratch

Problem

Every part of a GPT gets one clear home in this package: tokenizer, data loading, attention, transformer blocks, initialisation, optimizer parameter groups, learning-rate schedule, gradient accumulation and sampling.

python -m gpt_from_scratch trains a small character-level model on Tiny Shakespeare (defaults: 4 layers, 4 heads, 128 dimensions, block size 64). GPTConfig() defaults to the GPT-2 124M shape, and a test checks its parameter count. The repository states what it leaves out: the large-scale pretraining run, HellaSwag evaluation, Hugging Face weight loading, torch.compile and multi-GPU training.

Architecture diagram

  input.txt (packaged data, or --data path)
      |
      v
  load_text --> CharTokenizer --> split_tokens 90/10
                                      |
                  +-------------------+-------------------+
                  | train 90%                             | val 10%
                  v                                       |
          DataLoaderLite (sequential batches)             |
                  |                                       |
                  v                                       |
  train loop: grad accumulation, clip 1.0,                |
  warmup + cosine LR, AdamW            <-- GPT -->        |
                  |      wte + wpe, N pre-norm blocks,    |
                  |      ln_f, tied lm_head               |
                  v                                       v
          estimate_loss (random windows) <----------------+
          on train and val splits
                  |
  GPT.generate: temperature + top-k --> decoded sample

model.py defines GPTConfig and GPT; train.py runs the loop and CLI, then reports train and validation loss and prints a generated sample.

Key decisions

Challenges

Results

Links