Now, let's build your very own AI.
Use inside this , including real files and processes. Type the three checks in the — no pip, account, or needed.
Environment
The runs through . is already installed. This course uses only its : no installation, account, or is required.
is the that handles math and for large . accelerates PyTorch on supported Apple computers; it is not available in this emulated . can run Python calculations, but ordinary listening and background services are constrained. We therefore complete every required step in this Alpine terminal. We write a small differentiation engine ourselves.
Only the Python standard library — modules used and what they do
installs nothing with pip. It uses only the standard library that comes with Python.
- : saves and reads settings, the and checkpoints as text files.
- math: math such as exp, log and sqrt.
- random: random numbers for initial , picking windows and . The same seed gives the same result.
- collections.Counter: counts adjacent pairs in .
- pathlib: reads and writes files.
- argparse: accepts command-line options such as --steps.
- hashlib: makes a fingerprint () of the to detect whether it changed.
- os: swaps in a checkpoint safely ().
- and urllib: the and the .
With no installation, there are no conflicts or failed installs. Instead, you must write yourself what matrix libraries (, PyTorch) would do. That is exactly the purpose of this course.
Linux inside the browser — what runs where
The terminal on the right is not connected to a server. Real Linux runs inside this .
- v86: an that imitates an . It is built with and runs in the browser.
- Alpine Linux 32bit: a small Linux that runs on that virtual CPU. It has 512MB of .
- , , , , : preinstalled in this Linux.
Your computer’s CPU runs the through the emulator. No is sent to a server, so anything you run is safe. But because it is emulated, the same Python code runs much slower than directly on your computer. That is why XSLM’s default size and number of are small.
Files in ~/xslm are saved in this browser and survive a reload. Running programs (such as the Runner) stop when you reload, so start them again.
What tools are used to build bigger models
XSLM uses only the Python standard library. Bigger models are usually built with the tools below.
- PyTorch: does number-array (tensor) math and automatic differentiation fast on the GPU. It does the job of engine.py in XSLM.
- tokenizers: a fast BPE tokenizer library. It is written in and is about 100 times faster than pure Python. It does the job of tokenizer.py in XSLM.
- numpy and tqdm: number arrays and a progress bar.
- : manages Python projects and packages. You install with commands like uv add torch.
- Device: where the computation runs. mps for an Apple GPU, for an GPU, otherwise cpu.
The tools differ, but the principles are the same as in XSLM. The tools only do the same calculations faster.
Why is it slow — the cost of computing one number at a time
All XSLM math is additions and multiplications of individual Value objects. Count what happens when it trains once on a 12-token window.
- Each position has seven matrix multiplications — query, key, value, project, expand, contract and head — and one is tens to hundreds of multiplications and additions.
- Every operation creates one Value object that is recorded in the . With the default settings, one training step creates about 32,000 of them.
- backward walks through all these objects once more in reverse.
PyTorch groups the same math into whole matrices and processes it at once in or on the GPU, so it is hundreds to thousands of times faster. We chose the slow road on purpose to see the principles. If training takes long, you can stop it with ; everything up to the last completed step stays saved.
We timed the default 20 training steps (python3 train.py --steps 20) in this browser Linux: about 23 seconds on a fast desktop (Apple M5 Max), about 2 min 13 s with the CPU slowed 4×, and about 4 min 5 s slowed 6×. Generation (generate.py) took about 3, 17 and 33 seconds. On a slow computer, run it in parts such as --steps 5. Training continues from the checkpoint, so the result is the same as doing it all at once.
Python and terminal basics for this course
Here is just what you need so you don’t get stuck while typing the sources.
- micro filename.py: opens a Python file to edit. +S saves; Ctrl+Q closes. Enter carries the to the next line, so press Backspace if a line goes deeper than the source.
- nano filename: opens any other file, such as settings.json or raw.txt. Ctrl+O then Enter saves; Ctrl+X closes.
- python3 filename.py: runs a program. Typing only python3 opens interactive Python () for trying one line at a time; exit() leaves it.
- Indentation: Python separates blocks with spaces. This course uses 4 spaces per level. Mixing Tab and spaces causes .
- : from model import takes GPT from model.py in the same folder. That is why every command runs in ~/xslm.
- if __name__ == '__main__': the code under it runs only when you run the file directly. It does not run when another file imports it.
- , message: stops with that message if the condition is false. XSLM uses assert to catch wrong or settings early.
When an error occurs, read the from the bottom line up. The last line is the error type and message, and the File … line number above it is where the problem happened.
If you want to build bigger on your own computer
If you want to train a bigger model after finishing XSLM, set up a PyTorch environment on your own computer (optional).
- Install Python 3.10 or later.
- Install uv: curl -LsSf https://astral.sh/uv/install.sh | (on , install uv also works).
- Create a project with uv init my-gpt, then my-gpt and install with uv add torch tokenizers tqdm numpy.
- On an Apple Silicon Mac, check that uv run python -c "import torch; (torch.backends.mps.is_available())" prints True. For an NVIDIA GPU, check torch.cuda.is_available().
Apple Silicon trains with MPS, NVIDIA GPUs on Linux or with CUDA, and anything else with the CPU. You can train on the CPU too, but it is slow. The names you learned in XSLM (, , , , ) appear unchanged in PyTorch code.
Type and try
Type the three checks. Look for the Python version, a 32bit x86 architecture and ready. All later commands belong in this terminal, not your computer’s terminal.
python3 --version
uname -m
python3 -c "import json, math, random, http.server; print('ready')"
Practice in the terminal
Check your understanding
Explain why importing torch or enabling MPS is unnecessary here. Python performs the training on the CPU emulated by your browser.
If you get stuck
Do not try to fix a missing torch package with pip install torch: this lesson does not import it. If the terminal will not load, check the connection and retry. A simulated success message is not a working Python environment.
Frequently asked questions
Will pip install torch make it faster?
Don’t do it in this terminal. PyTorch has no package for 32bit Linux, and even if it did, it would hardly run in an emulator with 512MB of memory. The XSLM sources do not import torch, so you don’t need it. Install PyTorch separately on your own computer.
Why not run Python in the browser with Pyodide?
Pyodide can do Python math in the browser, so the tokenizer and training would work. But ordinary socket servers and background processes are constrained, so you could not start the Runner (an server) and have another program (XChat) connect to it. That is why every exercise runs on real Linux (Alpine).
What stays and what disappears when I reload?
.py, .json, .txt and .md files in ~/xslm (up to 100 files / 2MB) are saved in this browser when a command finishes and every 5 seconds, and come back when you reopen the page. Running programs, other file types and files in other folders disappear. This saving lives only in this browser, so another browser or computer starts empty.