Step 1: Setup and Running

First, let’s get the code running.
1.1 What You Need
All you need to run this project is uv. uv will automatically bring in Python itself and PyTorch as needed. No GPU is required; tiny-LLM finishes training in a few seconds.
Installing uv:
# macOS / Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows (PowerShell)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Homebrew (macOS)
brew install uv
After installing, if uv --version shows a version, you’re good to go.
Why use uv: it is faster than pip + venv, and it handles everything from Python version management to installing dependencies in a single tool. There is no need to create a virtual environment by hand, and no
source venv/bin/activateeither.
1.2 Getting the Code
git clone https://github.com/t-ishii66/tiny-llm.git
cd tiny-llm
These are the two main implementation files:
tiny-llm/
├── tiny_llm.py ← the whole implementation (~140 lines of real code)
├── tiny_llm_instruct.py ← for the instruction tuning used in Chapter 5
├── docs/
│ ├── ja/ ← Japanese documentation
│ │ ├── 01_data.md
│ │ ├── 02_transformer.md
│ │ ├── 03_training.md
│ │ ├── 03a_gradient.md
│ │ ├── 04_generation.md
│ │ ├── 05_instruction_tuning.md
│ │ └── tutorial/ ← this tutorial
│ └── en/ ← English documentation
│ └── ...
└── README.md
1.3 Running It
uv run --with torch tiny_llm.py
--with torch means “temporarily add PyTorch and run this script”.
The first time there is a PyTorch download, so you’ll wait a little, but from the second time on it starts instantly from the cache.
Even in an environment without Python installed, uv automatically fetches the required version.
Chapter 5’s
tiny_llm_instruct.pycan be run the same way:uv run --with torch tiny_llm_instruct.py
You will see output like the following (the numbers vary slightly from run to run):
vocab size: 10
vocab: {'<pad>': 0, 'the': 1, 'cat': 2, 'sat': 3, 'on': 4, 'mat': 5, '.': 6, 'dog': 7, 'log': 8, 'saw': 9}
training samples: 28, seq_len: 12
epoch 20 loss=1.9469
epoch 40 loss=1.5257
epoch 60 loss=0.8140
epoch 80 loss=0.5469
epoch 100 loss=0.3880
epoch 120 loss=0.3099
epoch 140 loss=0.2568
epoch 160 loss=0.2227
epoch 180 loss=0.1862
epoch 200 loss=0.1147
--- Generation ---
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
prompt: "the dog saw"
output: the dog saw the cat . the cat sat on the log . the dog sat on the mat . the dog sat
1.4 How to Read the Output
The Course of Training
epoch 20 loss=1.9469 ← still a nonsense prediction
epoch 200 loss=0.1147 ← an almost perfect prediction
- epoch: how many times it has gone through the training data
- loss: how bad the prediction is (smaller is better). About 2.3 if random, 0 if perfect
The Generated Result
prompt: "the cat sat on"
output: the cat sat on the mat . the dog sat on the log ...
- prompt: the input text given to the model
- output: the text the model generated by predicting one word at a time
Natural English sentences in line with the training corpus are being generated.
Note: build the prompt you pass to
generate()using only words contained in the training corpus. If it contains a word outside the corpus (an out-of-vocabulary token), the current implementation raises an exception.
1.5 What Just Happened
In just a few seconds, the following was executed:
- Data preparation: convert the 40-word corpus into numbers and create the training data
- Model construction: initialize a Transformer with about 68,000 parameters
- Training: learn “next-word prediction” over 200 training loops
- Generation: generate text with the trained model
In the next step, we will check the contents of each of these stages with our own eyes.
(Supplement) Where uv Puts PyTorch
Only read this if you’re curious. The PyTorch installed by uv run --with torch ... is placed entirely in a cache directory dedicated to uv. It writes nothing into the project directory (./.venv/ etc.), the system Python, or the Homebrew Python.
| OS | Cache location |
|---|---|
| macOS | ~/Library/Caches/uv/ |
| Linux | ~/.cache/uv/ |
| Windows | %LocalAppData%\uv\cache\ |
- Check the location:
uv cache dir - Keep the size down:
uv cache prune(deletes only old entries) - Full reset:
uv cache clean