Token embedding

×

Look up a learned feature vector for each token ID.

Et=WTE[input_ids]{\htmlData{light=token-embedding wte}{E_t}}=\mathrm{WTE}[\mathrm{input\_ids}]
in
[B batch, T positions]
out
EtE_t [B batch, T positions, 768 d]
Parameters38,597,376

38,597,376 parameters per instance.

ComponentCalculationParameters
Token embedding50,257 × 76838,597,376

The vocabulary projection reuses this table’s transpose; its parameters are counted here.

Not checked against a checkpoint.

Sources

In codetf.gather(wte, X)

Open pinned code ↗

Direct evidence

The original TensorFlow graph creates both tables and adds their gathered rows.

model.py 152–157 ↗ · openai/gpt-2 @ 9b63575ef427

Excerpt
        wpe = tf.get_variable('wpe', [hparams.n_ctx, hparams.n_embd],

… lines 153–153 omitted …

        wte = tf.get_variable('wte', [hparams.n_vocab, hparams.n_embd],

… lines 155–156 omitted …

        h = tf.gather(wte, X) + tf.gather(wpe, positions_for(X, past_length))

Transformers names the tables wte and wpe, matching the released checkpoint keys.

modeling_gpt2.py 492–577 ↗ · huggingface/transformers @ 71c6f699ac9b

Excerpt
        self.wte = nn.Embedding(config.vocab_size, self.embed_dim)
        self.wpe = nn.Embedding(config.max_position_embeddings, self.embed_dim)

… lines 494–568 omitted …

            inputs_embeds = self.wte(input_ids)

… lines 570–575 omitted …

        position_embeds = self.wpe(position_ids)
        hidden_states = inputs_embeds + position_embeds.to(inputs_embeds.device)