A positional embedding is a learned vector for a place in a sequence. A Transformer processes tokens in parallel, so token embeddings alone do not tell it their order. Adding a position vector to each token vector gives self-attention an input that carries both token and position information. section 11.1.2 section 9.1.2
The nearby alternative is fixed positional encoding: sine and cosine functions produce vectors that do not change during training. With learned positional embeddings, an embedding layer holds position vectors whose weights are refined during training. Both approaches add vectors with the same width as the token embeddings. section 10.2.2 section 11.1.2
Four-token GPT-2 prompt
For “this is a prompt,” GPT-2 assigns positions 0 through 3. In GPT-2XL, each position selects a learned 1,600-value vector from a layer covering up to 1,024 positions. The resulting 4 × 1,600 position matrix is added to the 4 × 1,600 token-embedding matrix. section 11.1.2
Figure 11.2 GPT-2 first represents each token in a sequence with a 50,276-value one-hot vector. The token representation of the sequence goes through a word embedding layer to compress it into an embedding with a dimension of 1,600. GPT-2 also represents each position in a sequence with a 1,024-value one-hot vector. The positional representation of the sequence goes through a positional encoding layer to compress it into an embedding also with a dimension of 1,600. The word embedding and positional encoding are added together to form the input embedding.
Follow the token and position paths separately, then see their equal-width vectors added to form the input embedding. section 11.1.2
In a PyTorch decoder-only Transformer:
- Create a position table with `nn.Embedding(config.block_size, config.n_embd)`: one available position per row, with the same vector width as the token embeddings. section 12.3.3 section 12.2.2
- Look up vectors for the sequence positions, add them to the token embeddings, and pass the sum into the decoder blocks. section 11.2.4
- Set the table dimensions for the task: one text generator uses 128 positions and 256-value vectors; a music Transformer uses 2,048 positions and 512-value vectors. section 12.3.3 section 14.3.2











