Chapter 0 · About 8 minutes
What a language model does
A language model does one thing over and over. It guesses what comes next.
You will learn
- Say in one sentence what a language model does
- See how guessing one piece at a time produces whole sentences
- Know the parts of a transformer the next chapters cover
It guesses the next piece of text
The sentence as token chips, followed by bars for the model's five most likely next tokens with their probabilities.
Story
When you type a message, your phone suggests the next word. A Language model A program that reads text and gives every possible next piece of text a probability. Open the glossary does the same thing, only much better. You give it some text, and it answers with every piece of text that could come next, each with a chance.
Numbers
Here is what that answer could look like after “Once upon a”. These numbers are made up to show the idea.
| Next piece | Chance |
|---|---|
| time | 0.92 |
| day | 0.03 |
| hill | 0.01 |
| everything else | 0.04 |
The chances, called Probability A number from 0 to 1 that says how likely something is. The probabilities of all options add up to 1. Open the glossary, always add up to 1, because something has to come next.
Formula
The model assigns a probability to each possible next token, given all the tokens before it.
For each position the probabilities over the whole vocabulary sum to 1.
Code
const trace = forward(model, tokenIds);
const lastRow = rowView(trace.logits, tokenIds.length - 1);
const chances = probabilities(lastRow, 1); // one number per token in the vocabularyWhich word is most likely to come after "The cat sat on the"?
Most people pick “mat”, and a model trained on lots of English text usually does too, because that phrase is common in its training text. The model has never seen a cat. It has seen patterns in text. The playground lets you check what our small model predicts.
All chances add up to 1, so what is left is 1 minus 0.9 minus 0.05, which is 0.05.
Then it does it again
Guesses made: 0
The sentence as token chips, with buttons to add one guess at a time or to play them automatically, and a count of guesses made so far.
Story
To write more than one word, the model picks one of the likely pieces, adds it to the text, and guesses again. Repeat this a few hundred times and you have a paragraph. Chatbots like ChatGPT write their answers this way, one piece at a time.
Numbers
Starting from “Once upon a”, three rounds could go like this.
- “Once upon a” gives “time” the highest chance. The text becomes “Once upon a time”.
- “Once upon a time” makes “,” likely. The text becomes “Once upon a time,”.
- “Once upon a time,” makes “there” likely. The text becomes “Once upon a time, there”.
Formula
The probability of a whole sequence is the product of the next-token probabilities, one position at a time.
Generating text means choosing each from that distribution and feeding it back in.
Code
const result = generate(model, promptIds, { maxNewTokens: 20, temperature: 0.8, seed: 1 });An old idea
In 1951 Claude Shannon (Shannon, 1951) asked people to guess the next letter of English sentences, one letter at a time, to measure how predictable English is. A language model plays the same game with pieces of words.
Inside, a transformer does the guessing
The transformer's stages listed from tokens to output, each with a one sentence explanation you can open.
Story
The model that makes these guesses is called a Transformer The design behind most modern language models. It passes text through layers of attention and feed-forward steps. Open the glossary (Vaswani et al., 2017). It turns the text into numbers, passes those numbers through a stack of layers that let each word gather information from the words before it, and ends with a score for every possible next piece. The next chapters open each of those steps.
Numbers
The small model on this site has a vocabulary of 4,096 pieces. It turns each piece into a list of 128 numbers, runs them through 4 layers, and has about 1.3 million numbers it learned during training. Large chat models follow the same plan with tens of thousands of times more numbers, or more.
Formula
A transformer computes scores for every token in the vocabulary, called logits, from the tokens so far. Softmax turns the logits into probabilities.
Here stands for all of the model’s learned parameters.
Code
const model = loadModel(
await (await fetch(modelUrl)).arrayBuffer(), // modelUrl is where the site keeps the file
);
console.log(model.config); // { vocabSize: 4096, dModel: 128, nLayers: 4, ... }Where the analogy breaks
Phone suggestions are a helpful picture, but the model does not look up its answer in a list of phrases it has seen. It computes each guess from numbers it learned, which is why it can continue sentences it has never seen before.
Recap
- A language model gives every possible next piece of text a probability.
- It writes by picking a likely piece, adding it to the text and guessing again.
- A transformer computes those probabilities from numbers it learned in training.
Go deeper
- Shannon, 1951. Prediction and Entropy of Printed English
- Bengio et al., 2003. A Neural Probabilistic Language Model
- Vaswani et al., 2017. Attention Is All You Need
- Radford et al., 2019. Language Models are Unsupervised Multitask Learners
- Eldan and Li, 2023. TinyStories: How Small Can Language Models Be and Still Speak Coherent English?