1 Text becomes tokens
GPT-2 never sees letters. Text is first converted to bytes and then cut into pieces from a fixed vocabulary of 50,257 tokens. The vocabulary was built by byte-pair encoding: start from the 256 possible bytes and repeatedly merge the most frequent adjacent pair in the training text into a new token, 50,000 times. Common words become one token. Rare words are spelled out in pieces. Type below to see how text gets split.
A leading space is part of the token (shown as ·), so " the" and "the" are different tokens. Tokens marked ◆ hold only part of a multi-byte character, such as one byte of an emoji. They mean nothing on their own.
2 Tokens it never learned to use
Each token gets an embedding, a vector of 768 numbers that the model learns during training. Every vector starts out random, and training only moves the vectors of tokens that actually occur in the training text. A token that almost never appears keeps its starting value. That makes it sit close to the average of all embeddings, apart from the tokens that were used.
The likely cause is a mismatch: the vocabulary was fitted to a larger, rawer pile of scraped text than the filtered pages the model then trained on. Strings that were frequent in the first but almost absent from the second got their own token, and almost no training. They read like a list of the web's back rooms:
externalToEVA(the closest token of all) comes from configuration files of Kerbal Space Program, a spaceflight game whose part files are full of fields likeexternalToEVAOnlyandguiActiveUnfocused.TheNitromeandRandomRedditorare pieces of the usernames TheNitromeFan and RandomRedditorWithNo, two of the most prolific posters in r/counting, a subreddit that does nothing but count upwards, one number per comment.StreamerBotis part of TPPStreamerBot, the bot account of Twitch Plays Pokémon.rawdownload,embedreportprintandreportprintare pieces ofrawdownloadcloneembedreportprint: the row of buttons on every Pastebin page (raw, download, clone, embed, report, print), run together by a scraper. The whole string is one of the 30 longest tokens.InstoreAndOnline,quickShipandActionCodelook like fields from shopping-site markup.- Most of the rest are raw control bytes (tab, carriage return,
\x00…) andÃÂÃÂ, the mojibake that UTF-8 text turns into when it's decoded with the wrong encoding over and over. That happened often enough to earn tokens up to 64 characters long.
3 One guess at a time
The model reads up to 128 tokens here (1,024 in the original) and, at every position, outputs a probability for each of the 50,257 possible next tokens. Each token's vector goes through 12 identical blocks. In each block, attention lets every position copy information from earlier positions, and an MLP transforms each position on its own. Both add their result to a running vector, the residual stream. At the end, the stream is compared with every token's embedding to score the candidates.
Layer by layer
The text, coloured by how expected each token was
Colour is the probability the model gave each token before seeing it: bright means expected, dark means surprising. Click a token to inspect the prediction made there.
Next token after
The logit lens reads the residual stream after each block as if the model had to answer right there: the stream goes through the final layer norm and the unembedding early. Each cell shows the top guess and its probability for the token after that column. In the lower rows the guess is often just a copy of the token itself. Higher up, the model's answer forms. Shown: the 16 positions up to the selected token.
Row i shows where position i looks: how much of each earlier token it copies from. Beside it, the selected token's row. Try Repeated random tokens above with head 5.5 or 6.9. These are induction heads: in the second copy, each token looks at the token that followed its first occurrence, and the model then predicts that token. It's the simplest form of in-context learning, and it shows as a bright diagonal stripe.
Which earlier tokens did the prediction at the selected position depend on? This is gradient × input: the gradient of log p(top guess) with respect to each token's input embedding, dotted with that embedding. The gradient comes from a graph exported with PyTorch's autodiff and runs here in onnxruntime-web. Orange pushed the guess up, blue pushed it down.
4 What it memorised
A language model is a compression of its training text, and the text that compresses best is the text that occurred thousands of times. Pick a passage below and look at the colours. A token the model saw coming is bright. A passage it has memorised is bright from start to finish.
What GPT-2 small remembers word for word is not what is famous. It is what was copied: software licences pasted into thousands of repositories, the wire-service footer under every Associated Press story, the share buttons of every WordPress blog. Hamlet's soliloquy and the digits of π are famous, yet they score barely better than a sentence made up for this page. The model is a record of how often text appeared on the web, not of what mattered.
From nothing
Every document in training ended with the special token <|endoftext|>. Start
from that token alone and the model writes the start of a typical WebText document. These
are samples at temperature 0.9 (first one: greedy):
Live samples are unfiltered. They read like WebText, which includes crime reporting, slurs and conspiracy theories, so expect some to be unpleasant.