Chapter 4 · About 12 minutes

Prediction

The last token's numbers become a score for every token, and the scores become chances to pick from.

You will learn

  • Say how the model turns its last list of numbers into one score per token
  • Turn scores into chances, and say what temperature does to them
  • Tell greedy, top-k and top-p sampling apart

One score for every token

The final list for space her starts 0.82, 2.46, -3.15, … and has 128 numbers.

TokenIts rowScore
space ball-0.01, -0.00, -0.07, …2.60
space friends-0.05, 0.06, -0.10, …2.48
space toy0.01, 0.02, -0.08, …2.29
space doll0.01, 0.09, -0.05, …2.23
space new-0.02, -0.09, -0.03, …1.99
Each score is the last list times that token's own row.

A table of the five likeliest next tokens. For each one it shows the first numbers of its embedding row and its score, the dot product of that row with the last token's final list.

Story

After attention, each token’s list also passes through a small network of its own. Each step adds to the list instead of replacing it, and the whole block repeats 4 times. A last norm tidies the list.

Then the last token’s list is compared with every row of the embedding table, using a Dot product A way to compare two lists of numbers. Multiply the numbers in matching places and add up the results. Open the glossary. That gives each of the 4,096 tokens a score, called a Logit The raw score a model gives one possible next token, before softmax turns the scores into probabilities. It can be any size, even below zero. Open the glossary.

Numbers

The final list for “·her” starts 0.82, 2.46 and -3.15. Its dot product with the row for “·ball” is 2.60, the highest of all 4,096 scores. “·friends” comes next at 2.48.

Using the same table twice is called tying the weights (Press and Wolf, 2017). In our model it saves 524,288 numbers. GPT-2 does it too, like the first GPT (Radford et al., 2018, Radford et al., 2019).

Formula

With u\mathbf{u} the last token’s list after the final norm, the scores are

z=u WE⊤,zt=WE[t]⋅u\mathbf{z} = \mathbf{u} \, W_E^\top, \qquad z_t = W_E[t] \cdot \mathbf{u}

so token tt gets the dot product of its own embedding row with u\mathbf{u}.

Code

const trace = forward(model, ids);
trace.logits; // 6 × 4,096, one row of scores per token
const scores = rowView(trace.logits, ids.length - 1); // the last row is the guess

After "Lily wanted to play with her", which word gets the highest chance?

Scores become chances

Which tokens stay in

space ball gets 11.5%.

  • space ball11.5%
  • space friends10.2%
  • space toy8.5%
  • space doll8.0%
  • space new6.3%
  • space toys5.2%
  • space friend5.1%
  • space mom3.3%
  • 4,088 other tokens41.9%

Press Sample to pick a token.

The real chances after the sentence.

Bars for the model's likeliest next tokens with their chances. A temperature slider and a choice of which tokens stay in reshape the bars. A Sample button picks one token at random and lists the picks so far.

Story

Scores are not chances yet. Softmax A way to turn any list of scores into numbers between 0 and 1 that add up to 1. Bigger scores get bigger shares. Open the glossary turns them into chances between 0 and 1 that add up to 1, just as it did inside attention.

Then a token is picked at random, using those chances. This is called Sampling Picking the next token at random, where each token's chance of being picked is its probability. Open the glossary. Press Sample a few times. The same chances can give a different word each time.

Numbers

“·ball” gets 11.5%, “·friends” 10.2% and “·toy” 8.5%. The 50 best tokens hold 80% of the chance together. The other 4,046 tokens share the last 20%, so a rarer word gets picked about one time in five.

Formula

P(t)=ezt∑sezsP(t) = \frac{e^{z_t}}{\sum_{s} e^{z_s}}

The sum runs over all 4,096 tokens.

Code

const probs = probabilities(scores, 1); // softmax at temperature 1
const next = sample(probs, createRng(1)); // one token id, picked at random

Turning the temperature up or down

Which tokens stay in

space ball gets 26.6%.

  • space ball26.6%
  • space friends21.0%
  • space toy14.4%
  • space doll12.8%
  • space new7.9%
  • space toys5.4%
  • space friend5.2%
  • space mom2.2%
  • 4,088 other tokens4.5%

Press Sample to pick a token.

Try 0.1, then 2.

Bars for the model's likeliest next tokens with their chances. A temperature slider and a choice of which tokens stay in reshape the bars. A Sample button picks one token at random and lists the picks so far.

Story

Before softmax, every score is divided by a number called the Temperature A number the scores are divided by before softmax. Low makes the top guess win more often, and high spreads the chances out. Open the glossary.

A low temperature makes the gaps between scores bigger, so the top guess wins more often and the text gets safer. A high temperature shrinks the gaps, so rare words get picked more and the text gets wilder. At 1, the chances are the model’s own.

Numbers

At temperature 0.1, “·ball” gets 72% and “·friends” 22%. At 0.5, “·ball” gets 27%. At 2, “·ball” gets only 1.5%, and the 50 best tokens hold just 22% together.

Formula

With temperature τ\tau,

Pτ(t)=ezt/τ∑sezs/τP_\tau(t) = \frac{e^{z_t / \tau}}{\sum_{s} e^{z_s / \tau}}

As τ\tau goes to 0, all the chance goes to the top score. As τ\tau grows, the chances move toward equal.

Code

probabilities(scores, 0.1); // sharp, "·ball" gets 0.72
probabilities(scores, 2); // flat, "·ball" gets 0.015

Greedy, top-k and top-p

Which tokens stay in

space ball gets 100.0%. Greedy always takes the top token, so temperature changes nothing.

  • space ball100.0%
  • space friendsout
  • space toyout
  • space dollout
  • space newout
  • space toysout
  • space friendout
  • space momout
  • Other tokensout

Press Sample to pick a token.

Switch between the rules and watch which tokens stay in.

Bars for the model's likeliest next tokens with their chances. A temperature slider and a choice of which tokens stay in reshape the bars. A Sample button picks one token at random and lists the picks so far.

Story

Greedy decoding Always picking the single most likely next token. It gives the same text every time. Open the glossary decoding skips the draw and always takes the top guess. It gives the same text every time, which often ends up dull and repeats itself (Holtzman et al., 2020 ).

Top-k sampling Sampling only from the k most likely tokens, after dropping all the others. Open the glossary picks only from the k likeliest tokens. Top-p sampling Sampling only from the fewest most likely tokens whose probabilities add up to at least p. Also called nucleus sampling. Open the glossary picks from the fewest likeliest tokens whose chances add up to p. Both stop very rare words from sneaking in.

Numbers

With top-k and k = 5, only “·ball”, “·friends”, “·toy”, “·doll” and “·new” stay in, and their chances are scaled up to add to 1.

With top-p and p = 0.9, the number kept depends on how sure the model is. At temperature 1 it keeps about 200 tokens. At temperature 0.5 it keeps only 7.

Formula

Top-p keeps the smallest set VpV_p of likeliest tokens with ∑t∈VpP(t)≥p\sum_{t \in V_p} P(t) \ge p, then rescales.

P′(t)=P(t)∑s∈VpP(s) for t∈Vp, and 0 otherwiseP'(t) = \frac{P(t)}{\sum_{s \in V_p} P(s)} \text{ for } t \in V_p, \text{ and } 0 \text{ otherwise}

Code

argmax(scores); // greedy, the id of "·ball"
nextTokenDistribution(scores, { temperature: 1, topK: 5 });
nextTokenDistribution(scores, { temperature: 1, topP: 0.9 });

Like a raffle

Each token holds raffle tickets, as many as its chance. Temperature changes how unevenly the tickets are handed out, and top-k and top-p take tickets away from the long tail. Where this breaks. The model only hands out the tickets. The drawing happens outside the model, in a few lines of code, and you can change it without retraining anything.

Recap

  • The last token's final list is compared with every row of the embedding table, giving 4,096 scores.
  • Softmax turns the scores into chances, and one token is picked at random by those chances.
  • Low temperature makes the top guess win more often. High temperature spreads the chances out.
  • Greedy always takes the top guess. Top-k and top-p pick only from the likeliest tokens.

Go deeper