Skip to the content.

English · 日本語

📖 For the best reading experience with properly rendered math and figures, see the GitHub Pages version: https://t-ishii66.github.io/tiny-llm/

tiny-LLM top image

tiny-LLM from scratch

A single-file Transformer implementation for learning, in the most concise Python code possible, the algorithms at the core of large language models (LLMs) like GPT — Self-Attention, Query/Key/Value, Multi-Head Attention, and next-token prediction.

About This Project

This is a GPT-style Transformer pared down to the bare minimum. The model body fits in one file (tiny_llm.py, about 140 lines of executable code; the instruction tuning covered in Chapter 5 adds about 100 more lines in tiny_llm_instruct.py), and it trains on a toy corpus in a few seconds. The forward pass is written by hand, and only the backward pass is left to PyTorch’s autograd.

"the cat sat on" → Transformer → "the" (predicting the next word)

What You Can Learn

Simplifications

This is a learning tool, not a production-grade model. The main simplifications are as follows.

Aspect tiny-LLM Production LLMs
Tokenizer whitespace split (word = token) BPE / SentencePiece (subword)
Vocabulary 10 words 50,000 to 200,000+ tokens
Number of parameters about 68,000 billions to trillions
Training data 40 tokens drawn from a 10-word vocabulary (a total count including repeats of the same word) trillions of tokens
Generation Greedy (argmax) sampling with temperature, top-k, top-p
Dropout / regularization none Dropout, weight decay, etc.
Core algorithm the same the same

Why It’s Still Useful

Even with all these simplifications, the core algorithms implemented here are used as-is in state-of-the-art models such as GPT and LLaMA. The difference is mainly one of scale, while the underlying structure is shared. Once you understand this code, Q/K/V projections, Scaled Dot-Product Attention, the Causal Mask, Residual Connections, Layer Normalization, and autoregressive generation — all of these carry over directly to practical Transformer implementations, so it becomes a foundation for reading real-world code.

Quick Start

uv run --with torch tiny_llm.py

If you don’t have uv installed, see the installation instructions in Tutorial Step 1.

Running the script displays the training progress and the generation results (the numbers vary slightly from run to run).

epoch   20  loss=1.9469
epoch   40  loss=1.5257
...
epoch  200  loss=0.1147

prompt: "the cat sat on"
output: the cat sat on the mat . the dog sat on the log .
        the cat saw the dog . the dog saw the

Documentation

Document Content
Chapter 1: Data Preparation Building the vocabulary, tokenization, and how to make training data
Chapter 2: Transformer Embedding, Self-Attention, FFN, and the whole Forward Pass
Chapter 3: Training Cross-Entropy Loss, backpropagation, and parameter updates
Chapter 3 Supplement: Gradient Math Derivatives, partial derivatives, and the chain rule explained with concrete numbers
Chapter 4: Text Generation Next-word prediction, Greedy Decoding, and comparison with real LLMs
Chapter 5: Instruction Tuning Alpaca format, Response masking, and how to build an instruction-following LLM

Tutorial

Tutorial Content
Step 1: Setup and Running Setting up the environment, running the code, checking the output
Step 2: Exploring the Data Check tokenization and the contents of the training data with your own eyes
Step 3: Peeking Inside the Transformer Visualize the attention weights and embedding vectors
Step 4: Experiments and Modifications Experiment by changing parameters and changing the corpus
Step 5: Try Instruction Tuning Run Alpaca-style Instruction Tuning in 3 stages and feel the limits of rote memorization

Credits

Copyright(C) 2026 t-ishii66. All rights reserved.