Position embedding table
×learnedtf.gather(wpe, positions_for(X, past_length))
One learned row per absolute position, 0 through 1,023.
- in
- [B batch, T positions]
- out
- [B batch, T positions, 768 d]
- parameters
- 786,432
Parameters
786,432 parameters owned by this component
| Component | Calculation | Parameters |
|---|---|---|
| table | 786,432 | |
| Per-instance total | 786,432 | |
Explanation
The table has one row per position, so a sequence, cached positions included, holds at most 1,024 tokens (model.py 152).
Codemodel.py 152, 157 · modeling_gpt2.py 493, 576
wpe = tf.get_variable('wpe', [hparams.n_ctx, hparams.n_embd], h = tf.gather(wte, X) + tf.gather(wpe, positions_for(X, past_length)) self.wpe = nn.Embedding(config.max_position_embeddings, self.embed_dim) position_embeds = self.wpe(position_ids)Sources2
Direct evidence
wpe is created alongside wte and gathered by position index.
openai · openai/gpt-2 / src/model.py
Pinned revision 9b63575ef427 ↗ · lines 152–157
Read code excerpt
wpe = tf.get_variable('wpe', [hparams.n_ctx, hparams.n_embd],… lines 153–156 omitted …
h = tf.gather(wte, X) + tf.gather(wpe, positions_for(X, past_length))Direct evidence
The constructor sets the position-table row count from max_position_embeddings; the lookup selects rows by position ID. The model configuration supplies the numeric count.
transformers · huggingface/transformers / src/transformers/models/gpt2/modeling_gpt2.py
Pinned revision 71c6f699ac9b ↗ · lines 492–577
Read code excerpt
self.wpe = nn.Embedding(config.max_position_embeddings, self.embed_dim)… lines 494–575 omitted …
position_embeds = self.wpe(position_ids)