Skip to main content

Subsection 2.5 Inside a Decoder Block

Now that we know how self-attention works, we can return to focusing on how the entire decoder works. The diagrams below show processing it within the string of tokens <s> a robot must obey the orders given it (where <s> is a start-of-sequence token that just marks the beginning of a new sequence):

Subsubsection 2.5.1 Creating queries, keys, and values

Suppose the model is processing it. At the bottom block, the input is its token embedding plus the positional encoding for slot #9:
The representation of β€œit,” the ninth token, enters a GPT-2 decoder block at the step that creates its query, key, and value vectors.
Each transformer block has its own learned weights. The first weight matrix projects the input into query, key, and value vectors.The multiplication results in a vector that’s basically a concatenation of the query, key, and value vectors for the current token.
A 768-dimensional input vector for β€œit” is multiplied by a learned matrix and split into query, key, and value vectors.
Figure 2.7. Multiplying the input vector by the attention weights vector (and adding a bias vector aftwards) results in the key, value, and query vectors for this token.
So far, this is the process illustrated in the previous section. But models like GPT-2 have another trick: they use multiple attention heads to capture different types of relationships between tokens.

Subsubsection 2.5.2 Multiple heads

Each head is a separate self-attention mechanism. In GPT-2 small, there are 12 attention heads, each with its own set of keys, queries, and values. Although we can’t clearly point to one specific head and say what it captures, each head does focus on different aspects of the input. One head might focus more on the grammatical structure (what words are verbs, nouns, etc.), while another might focus more on the semantic relationships between words.
These heads each have their own learned weight matrices for queries, keys, and values. While producing output, each head’s calculations are performed independently. Then, the outputs of the heads are combined to produce a single output that takes into account the contributions of all the heads.
The earlier examples follow one attention head. The following diagram illustrates three of the twelve heads:
The query, key, and value vectors for β€œit” are divided into separate portions for multiple attention heads; three heads are shown.
Scoring proceeds independently in each head. The following diagram shows one head:
In one attention head, the query for β€œit” gives the largest illustrated weights to β€œrobot” at 50 percent and β€œa” at 30 percent.
Although this particular head devotes attention for it to a and robot, other heads will likely focus on different aspects of the input. Perhaps one head will link it to the start of sentence marker <s>, while another head will focus on the relationship between it and given. After each head produces its own output vector, the scaled outputs of all the attention heads are concatenated into one vector:
Outputs from separate attention heads, each 64 dimensions wide, are concatenated into one 768-dimensional vector for β€œit”.
This combined output vector is transformed one more time by another set of learned weights to produce the final 768 bit representation that passes out from the attention layer.

Subsubsection 2.5.3 Feed-Forward Neural Network

The attention network is the core part of each decoder block. The output of the attention network is passed to a feed-forward neural network as shown in the diagram below.
While processing β€œit” in β€œa robot must obey the orders given it,” masked self-attention gives the most weight to β€œrobot” and β€œa” before passing the result to a feed-forward network.
A feed-forward neural network is a simple neural network where each input is connected to each hidden unit and each hidden unit is connected to each output unit. Key to this particular network, the internal layer is much wider than the input and output layers. In GPT-2 small, the internal layer is four times the size of the input and output layers (it is 3072 elements wide while the input and output layers are 768 elements wide).
Four input nodes connect to 16 hidden nodes, which connect to four output nodes; an upward arrow shows the flow of information.
Like other neural networks in GPT-2, this network is implemented as a series of large matrix multiplications. One multiplication expands the input vector into a larger intermediate space, and another multiplication projects it back down to the output dimension.
The first feed-forward matrix multiplies a 768-dimensional vector by weights that expand it to four times its original width.
Figure 2.8. (Not shown: A bias vector)
The second feed-forward matrix multiplies the expanded vector by weights that project it back to 768 dimensions.
Figure 2.9. (Not shown: A bias vector)
So what does this layer do?
Self-attention has added clues from earlier tokens into the current token’s representation. Expanding that representation into a much larger space effectively gives the model room to respond to more patterns at once. For instance, in river bank, some responses might reflect clues about a shoreline, while others reflect the token’s grammatical role.
However, we can’t describe exactly how those meanings are incorporated and transformed. Researchers have identified that the feed-forward network is a key part of the model’s ability to learn and represent complex patterns. But they have not fully described how that improved representation is formed in detail. Although they can clearly see the individual mathematical operations that are happening, making sense of the overall behavior produced by all of those calculations is extraordinarily challenging.
Why does GPT-2 use a network that expands the representation by 4 times? Because that is what seemed to work. Researchers experimented with different sizes and found that this size produced the best balance of increased ability to generate text and computational efficiency.