Skip to main content

Subsection 2.6 From Decoders to Output

Finally, we are ready to follow a token through the entire GPT-2 model and consider how the final output is generated.

Subsubsection 2.6.1 Multiple Blocks

We have focused on the operation of a single transformer block. In the full GPT-2 model, this process is repeated across multiple blocks. (That number depends on the specific variant of GPT-2 being used. GPT-2 small has 12 blocks, while GPT-2 large has 48 blocks.) Each block performs the same kinds of operations, but with its own learned parameters. As a token passes from block to block, it is processed by each block in turn, with the output of each block serving as the input to the next.
A token vector passes upward through a stack of decoder blocks; the expanded first block shows masked self-attention followed by a feed-forward network.
What is each block responsible for? Again, it is difficult to say exactly. Studies of GPT-2 suggest a loose progression through its blocks. Attention in middle blocks often follows nearby grammatical relationships, while attention in the deepest blocks tends to reach farther back. Researchers can take individual sentences and pass them through the models to observe how the attention patterns evolve across different blocks, but they are not able to describe specific rules that describe how attention will work for some new sentence.

Subsubsection 2.6.2 Model Output

When the top block produces its output vector, GPT-2 multiplies it by the transposed token-embedding matrix to turn the vector back into a set of raw scores for each token in the vocabulary. These raw scores are called logits and they correspond to how reasonable each token is as a candidate for the next word.
From these logits, the model can produce a probability distribution over the vocabulary by applying a softmax function. The softmax function converts the raw scores into probabilities that sum to 1. The model can then select the next token based on these probabilities.
The final decoder vector is multiplied by token embeddings to produce scores for vocabulary tokens, followed by an arrow to the selected token β€œThe”.
Models can be tuned to be more creative or more predictable by adjusting how many tokens are considered (\(top_k\)) and how the logits are weighted when turned into probabilities (the temperature). To make a model more creative, you can tell it to consider more of the top tokens (say \(top_k\) = 40 meaning it considers the 40 highest-scoring tokens) and set the temperature to a higher value which makes tokens with a higher logit score only slightly more likely to be selected. To make a model more predictable, you can limit the number of tokens considered (say \(top_k\) = 5) and set the temperature to a lower value which makes the highest-scoring tokens much more likely to be selected.
As an example, consider the context β€œthe dog sat”. Given a \(top_k\) of 5 and a temperature of 0.5, the model might produce the following probabilities for the next token:
Table 2.10. Predicted words and probabilities for small top_k and low temperature
Word Percent
on 61.31%
in 28.39%
down 5.91%
there 2.39%
at 2.01%
The model would then select one of these five words based on their probabilities. In this case, it is most likely to select on, but it could also select in, down, there, or at with lower probabilities. No other words would be selected.
Given the same context, but a higher \(top_k\) of 15 and a much higher temperature of 10.0, the model might produce the following probabilities for the next token:
Table 2.11. Predicted words and probabilities with top_k of 15 and high temperature
Word Percent
on 8.09%
in 7.79%
down 7.20%
there 6.88%
at 6.82%
up 6.81%
quietly 6.56%
with 6.52%
still 6.45%
next 6.42%
and 6.41%
, 6.19%
for 6.16%
silently 5.89%
upright 5.80%
Notice that the top 5 words are still the same, but their probabilities are much closer together. The high temperature reduces the importance of the different logit values, making the model less likely to stick to the most probable word. There also now 10 new words that are being considered. Because the temperature is so high, even the least probable word is being given a significant probability to get selected.
If we kept \(top_k\) at 15 but reduced the temperature to 0.5, the model would consider those same 15 words, but would be much more likely to select the one with the highest logit score:
Table 2.12. Predicted words and probabilities
Word Percent
on 57.68%
in 26.71%
down 5.56%
there 2.25%
at 1.89%
up 1.84%
quietly <0.01%
with <0.01%
still <0.01%
next <0.01%
and <0.01%
, <0.01%
for <0.01%
silently <0.01%
upright <0.01%
With that, the model has completed an iteration resulting in outputting a single word. The model would then start over to generate the next output token, including the newly generated token in the context.