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

Once upon a time, there was a
  • little39%
  • big12%
  • boy4%
  • small2%
  • girl2%

Tap a guess to add it.

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.

P(next token∣previous tokens)P(\text{next token} \mid \text{previous tokens})

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 vocabulary

Which word is most likely to come after "The cat sat on the"?

Then it does it again

The cat sat on the
  • bench9%
  • swing7%
  • tree7%
  • couch4%
  • table3%

Guesses made: 0

Each new word is one more guess.

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.

  1. “Once upon a” gives “time” the highest chance. The text becomes “Once upon a time”.
  2. “Once upon a time” makes “,” likely. The text becomes “Once upon a time,”.
  3. “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.

P(t1,…,tn)=∏i=1nP(ti∣t1,…,ti−1)P(t_1, \dots, t_n) = \prod_{i=1}^{n} P(t_i \mid t_1, \dots, t_{i-1})

Generating text means choosing each tit_i 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

Every guess goes through these steps.

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.

logits=fθ(t1,…,ti−1),P(ti∣t<i)=softmax⁡(logits)\text{logits} = f_\theta(t_1, \dots, t_{i-1}), \qquad P(t_i \mid t_{<i}) = \operatorname{softmax}(\text{logits})

Here θ\theta 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