Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Inside a language model

Next-token prediction: why only the last column predicts

Every token gets an output. So why do we read only the last one when a language model writes?

How would we train it?

The last article ended with the next token predicted from the last column. To see why, think about how we would train that on text.

Take “the hare sat on the grass”, and train on one prediction at a time:

  1. Run the network on “the”, and let it predict the next token. The answer is “hare”.
  2. Run it on “the hare”. The answer is “sat”.
  3. Run it on “the hare sat”. The answer is “on”.
  4. And so on.

Each run gives us a prediction to compare with the next token. Press Play and watch the prefix grow.

For this comparison, hold the weights fixed while collecting the losses, then make one update. Updating after every run would be a different training procedure.

One run for every token

Interactive
0 runs so far

That is one full run for every token we want to predict. Modern context windows reach a million tokens, so one long text would take a million runs. That is very slow.

Why predict only one token per run?

Look at the runs again. Each one repeats the work of the one before: run 2 reads “the hare”, and run 1 already read “the”.

So let us predict everything in one run. Let every column predict the token that comes next:

  • the column for “the” predicts “hare”
  • the column for “hare” predicts “sat”
  • and so on

We know every answer, because it is the next token of the text.

There is one rule. A column must not receive information from the tokens after it, or it could read its answer off the next column.

One run for every token at once

Interactive
0 runs so far

Each column now has the same prefix as its separate run. Sum or average their losses, and one forward pass trains many positions.

This is teacher forcing: training supplies the actual preceding tokens, not the model’s guesses. The illustration leaves out dropout and other training details.

Writing: only the last prediction is new

Now give the model a prompt. The tokens in it are already known.

An earlier column predicts another token inside that prompt. The last column predicts the token after it: the one we do not have.

Read that prediction, choose a token, append it and repeat.

Writing: one new token at a time

Interactive
0 runs so far

That is why only the last column’s prediction is read: it is the column that has seen the whole prompt.

A rule to carry forward

We still have not said what goes inside a layer. Whatever it does, the state at a position may depend on that token and the tokens before it, but never on later tokens. Break that rule and the prediction can copy its training target instead of learning to predict it.

What goes inside a layer is the next story.

Sources and assumptions

All positions produce predictions during training. Ordinary next-token generation reads the final position because earlier next tokens are already known. The figures illustrate prefix access and target alignment, not equality between different optimiser update schedules.

← Browse all subjectsBack to the beginning ↑