GGiriBioAI

/ module 05 · Inside the model

Transformer Walkthrough

GPT, Llama, Claude and Gemini are all stacks of the same building block: the transformer. Here is one block shrunk to 4 numbers per token, so you can follow every multiplication from words to the predicted next word.

By the end you can
  • Name the steps inside one transformer block, in order
  • Explain queries, keys, values and the causal mask
  • Say what residual connections and the MLP add

Try this first: Press 'Next step' seven times, reading one sentence per step.

about 20 min hover ? for help

architecture

The block in one breath

Each token becomes a vector (embedding) plus a hint about its position. Inside the block, attention lets tokens share information, then a per-token MLP transforms it. Residual connections add each result back onto the running vector and layer norm keeps the numbers in range. At the end, the last token's vector is compared with every vocabulary embedding to score the next word.

GPT-2 small does this with 768 numbers per token, 12 attention heads and 12 stacked blocks. This toy uses 4 numbers, 1 head and 1 block.

positional encoding
A vector added to each token embedding so the model knows word order.
query, key, value
Three projections of each token: the question it asks, the label it offers and the content it shares.
residual connection
Adding a layer's input back to its output, so information and gradients flow through deep stacks.
Live lab · Step through a GPT block
  1. input words
  2. token embed + position
  3. layer norm
  4. Q · K · V
  5. masked softmax
  6. Σ weights × V (+ residual)
  7. layer norm → MLP (+ residual)
  8. layer norm → unembed → softmax
  9. GPT-2 small repeats the middle part 12 times.
1 · Tokens to IDs
<bos>
id 0
the
id 1
cat
id 3
sat
id 5
on
id 8

A special <bos> (beginning of sequence) token is added first. Each word is looked up in the vocabulary to get an integer ID. Real models use subword tokens, covered in the Tokens module.

positive negativedarker = larger magnitude

scale

Toy vs. real

settingthis toyGPT-2 smallLlama 3 8B
vocabulary1350,257128,256
numbers per token (d)47684,096
attention heads11232 (8 KV)
blocks (layers)11232
MLP width83,07214,336
context length81,0248,192
parameters~200124 M8 B
layer norm
Rescales each token vector to zero mean and unit variance to keep training stable.
MLP / feed-forward
The per-token two-layer network inside each block that stores much of the model's knowledge.
unembedding
The final matrix that turns a token's vector into one logit per vocabulary entry.

Check yourself

Tick each one you could explain to a friend. If you can't yet, scroll back up and replay the lab.

Finished exploring?
Progress is saved only in this browser.