Hari Meyyappan
GPT-2, builtBuild an LLM
Parameters · GPT-2 small0

Build an LLM

Press ↓ to put GPT-2 together, one part at a time, and watch every number in it get made.

tokenizerembeddingsLayerNormattention ×12 headsresidualsMLP×12 blockssoftmaxsampler124,439,808 parameters
↓ or space to go on, ↑ to go back

The whole machine

I once trained an LSTM on about 1,000 Thirukkural couplets. It kept reusing words, and it lost the thread after a few lines.

This is the machine that fixed that. We'll build it top to bottom. Every cube you'll see is one number, and you can drag the model to look around.

Click any part to jump there. Parts you've visited turn solid.

After the machine:

One guess at a time

GPT-2 has one job: guess the next token, a word or a piece of one.

words→numbers→words talk→each thinks→×12→a guess

Tamil verse written by my 2018 LSTM
2018: my undergrad thesis, a small LSTM writing Tamil verse. Real words, the right rhythm. Then it repeats itself and loses the thread.

Same job as GPT-2: guess the next token. So what changed? Watch for three things:

  1. How it reads · parts 5–9
  2. How much it learns · part 18
  3. What it's taught after · part 19

Before it guesses, you do. The cat sat on the…

We'll check your pick against GPT-2 at the sampler.

Cutting text into tokens

The model can only do arithmetic, and “cat” isn't a number.

A rarer word gets cut into pieces, something like:

␣Thirukkural
More detail

The dictionary comes from byte-pair encoding. Start from single bytes and keep merging the most common pair. Common words end up as one token, rare ones as several.

A row for every token

An id is a locker number. It says nothing about the word.

How alike are two rows? Multiply slider by slider, then add:

animalyou sit on it
␣cat0.90.2
␣dog0.80.1
␣mat00.9

␣cat · ␣dog = 0.9×0.8 + 0.2×0.1 = 0.74 · ␣cat · ␣mat = 0.18

That sum is a dot product, a match score. Almost everything after this is built from it.

␣the's row so far: the word “the”

More detail

Real sliders have no names; training sets them, and most mix several traits.

The “so far” line is a story of what a trained model's row comes to hold.

This table alone is 50,257 × 768 = 38.6 million numbers, almost a third of GPT-2 small.

Where each token sits

The next part sees all five words at once, in a heap, with no order.

␣the's row so far: the word “the” · the last of five

My LSTM never needed this. It read one word at a time, so order came for free.

Keeping the numbers tame

Quick housekeeping before the main event.

More detail

After the reset, two learned numbers per slider, a gain and a shift, turn parts back up where that helps.

Without it, some numbers grow huge, others fade to nothing, and training gets shaky.

GPT-2 does this reset 25 times: twice in each of 12 blocks, and once at the end.

Asking, and being found

Now the main event. ␣the's row is the same in every sentence, so it has to look back.

More detail

The questions and labels are our story. Nothing tells the model what to put on its cards; training decides.

Who listens to whom

Every question meets every label.

More detail

In training the next word is the answer, so no word may look ahead. Blocking the future is called the causal mask.

Each score is divided by 8, the square root of a GPT-2 head's 64 numbers, so the scores don't get too big for the next step. The toy's heads have 4 numbers, so it divides by 2.

Taking a share of each

Scores become slices of a pie. Then each word mixes the notes.

␣the's row so far: the word “the” · the last of five · a cat sat on something just before me

What changed · 1 of 3 · how it readsMy LSTM lost the thread because it squeezed everything into one memory row. Here, ␣the just looks back at every word.

A trained “previous word” head, drawn by hand: ␣the puts 88% on ␣on. The toy's heads are untrained, so their shares look random.

Look at one head at a time:

Several heads at once

One question isn't enough.

Trained heads often look like these (drawn by hand):

More detail

Each head works on its own slice of the row. The toy has 3 heads of 4 numbers; GPT-2 has 12 heads of 64.

The drawings follow patterns people have found in real models.

The stream

Attention's note is added on top. Nothing is overwritten.

More detail

It also makes the model trainable. If each part overwrote the row, whatever it missed would be lost, and the “be less wrong” signal would fade on its way back through 12 blocks. Along the stream it travels straight back.

Thinking alone

Now each word thinks on its own, with what the model memorised. This part is the MLP.

768→check 3,072 patterns→keep the matches→768

␣the's row so far: … · a cat sat on something just before me · so the next word is probably a thing to sit on

More detail

Without the bend, the two tables would collapse into one.

About two-thirds of each block's numbers live here.

Researchers have found MLPs often act like this kind of pattern-to-note lookup. It's a useful picture, not the whole story.

That's one block

Everything so far is one transformer block.

Put it back together, top to bottom:

More detail

Attention itself dates from 2014. What was new in 2017 was a model built from attention alone, with no reading word by word.

Twelve of them

One block is one round of “talk, then think”.

␣the's row so far: … · a thing to sit on comes next · soft, flat things a cat sits on: mat, floor, bed

More detail

Roughly, early blocks handle nearby words and grammar, and later ones handle meaning. Real models are messier than that.

Back to words

␣the's row now describes the next word. Time to turn it into one.

More detail

First the row gets one last volume reset.

In training, every row is scored against its own next word, all at once. When writing, only the last row matters.

Scores into odds

A score of 5.2 doesn't say how sure the model is.

More detail

The toy is untrained, so its odds would be noise. These bars are made up to look like a trained GPT-2.

Rolling the dice

Always taking the biggest slice reads flat, and it repeats itself.

1.00
1.00

My LSTM at temperature 0.2
0.2 · safe, repeats itself
My LSTM at temperature 1.2
1.2 · invents words

My LSTM at two temperatures. 0.5 was the sweet spot.

More detail

Temperature divides every score before softmax: below 1 that stretches the gaps, above 1 it shrinks them.

Again, and again

One run gives one token. A reply needs hundreds.

The cat sat on the
More detail

The earlier words' keys and values don't change, so they're kept in a cache. Each lap only works out rows for the newest token.

My LSTM also wrote one word at a time. The difference is training: a transformer checks every position of a sentence at once, while an LSTM has to step through. That's what made huge training runs practical.

Where the numbers come from

Every number so far was random. That's why the toy picked .

step 0 · loss 10.82 · the real next token gets 1 in 50,000

My LSTM saw about 1,000 couplets. GPT-2 saw roughly 9 billion tokens of web pages.

More detail

Working out the right direction for every number at once is called backpropagation: the model's arithmetic run backwards, from the loss to each weight.

More of everything

GPT-3 is the same block, with more of everything.

GPT-2GPT-3
blocks1296
numbers per row76812,288
training text~9B tokens~300B tokens

What changed · 2 of 3 · how much it learnsMy biggest takeaway, going from LSTMs to LLMs, was data and compute. The transformer is the design that could use both.

From autocomplete to assistant

A base model continues documents. It doesn't answer you.

What changed · 3 of 3 · what it's taught afterMy LSTM only ever continued text. Nobody taught it to answer.

Ask “What is the capital of France?”

base modelWhat is the capital of Germany? What is the capital of…
after example chatsThe capital of France is Paris.
after preferencesPeople picked “Paris.” over a 300-word answer, so short and right wins.

All it can see

It has no memory between messages.

Slide to compare window sizes. Grey text is outside the window.

More detail

Double the text and attention does about four times the work.

Facts buried in the middle of a very long window often get missed.

You built GPT-2

That's 124,439,808 numbers doing eight kinds of arithmetic in a loop, and every one of them started out random. Underneath it all is one move used over and over: the dot product, a match score.

So what changed since my 2018 LSTM? Same job, guess the next token. Three things:

  1. How it reads: every word looks straight back at every earlier word
  2. How much it learns: billions of tokens, not 1,000 couplets
  3. What it's taught after: a text continuer becomes an assistant

One last check. Tap the parts in order, from text in to token out:

The whole thing on 20 cards

Say each part's job out loud, then tap the card to check.

Where the 124,439,808 numbers are

Token embedding38,597,376
Position embedding786,432
12 blocks × 7,087,87285,054,464
Final LayerNorm1,536
Unembedding0, it reuses the token table
Total124,439,808

Inside one block: attention 2,362,368 (queries, keys and values 1,771,776, the merge 590,592), MLP 4,722,432, two LayerNorms 3,072.

Eight moves and a loop

Look up a row, add two rows, normalise, take a dot product, softmax, mix by shares, bend, and pick. GPT-2 is those eight, repeated, inside a loop.

Today's models add longer windows, mixtures of experts, tools and reasoning steps. Under all of it is the same loop: guess the next token, append it, go again.

Inspired by Brendan Bycroft's LLM Visualization and Neal Agarwal's Space Elevator. Sizes: GPT-2 small (Radford et al., 2019), GPT-3 (Brown et al., 2020); the block is adapted from Vaswani et al. (2017). The 3D model is a real but tiny, untrained transformer (5 tokens × 12 numbers, 3 heads). Anything called made up or drawn by hand is not computed. Made with three.js. First draft · ← hmeyy.com