Skip to main content

Subsection 2.3 Input Encoding

The initial input to a decoder is not just raw token embeddings. Alone, they would not tell the model where each token occurs in a sequence. That order matters: the same words in a different order can mean something very different. (Compare “He ate dinner with his friends” and “With his dinner he ate friends”!)
So, the token embedding is combined with a positional encoding that indicates the token’s position in the sequence. This way, ate at position 2 will have a different representation than ate at position 5.
The embedding for the start token is added to the encoding for position one, and the combined vector enters the decoder stack.
Figure 2.5. Sending a word to the first transformer block means looking up its embedding and adding up the positional encoding vector for position #1.
The embedding and positional encoding are both learned components of GPT-2. Part of the training process involves learning not just embeddings for each token, but also appropriate positional encodings for each position in a sequence. While the number of token embeddings depends on the vocabulary size of the model, the number of position encodings is determined by the maximum context length. GPT-2 medium can consider context windows of up to 1024 tokens, so it will need to learn 1024 different positional encodings.
A token-embedding table has one row for each of 50,257 tokens and a column for each dimension of the model’s embedding.
A positional-encoding table has one row for each of 1,024 positions and the same number of columns as the token embeddings.
Future diagrams will simplify the representation and show tokens passing directly into the first decoder block. You should assume that the position embedding has already been added to each token.