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:

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.

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:

Scoring proceeds independently in each head. The following diagram shows one head:

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:

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.

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).
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.


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.