LLM internals: how transformers work
Tokens in, probabilities out. Build the whole machine once, from the tokenizer to the sampler, so you can reason about every behaviour you meet later.
You will implement byte-pair encoding, causal self-attention and a full GPT, train it on a small corpus, and learn how scale, data and decoding choices change what a model does. Everything here is deliberately small enough to run on a laptop or a free GPU.
By the end: You can explain a decoder-only transformer end to end, implement one in about 150 lines, and estimate the memory and compute a model needs.
7 topics · about 36 hours
Do these first
This stage is the one most people skip, and the one that pays back the longest. When an agent loops, a model refuses to follow a format, or latency doubles at long context, the explanation is almost always in one of these seven topics.
01
Tokenization and byte-pair encoding
4 h
02
Embeddings and self-attention
6 h
03
The transformer block and the full GPT
4 h
04
Build and train a GPT from scratch
8 h
05
Pre-training, data and scaling laws
5 h
06
Decoding and sampling
3 h
07
Modern architecture upgrades: RoPE, GQA, FlashAttention, MoE
6 h
Build this to prove it
Your own GPT, trained and sampled
Train a small GPT on a text corpus you care about (code, a language, a book collection). Add a KV cache to your sampler and compare perplexity as you vary model width and context length.
- A GPT implementation with causal attention, pre-norm blocks and weight tying
- A BPE tokenizer you trained yourself, with a vocabulary-size experiment
- Loss curves for at least three model sizes and a note on what changed
- A sampler with temperature, top-k, top-p and a working KV cache