Reference code openai/gpt-2 @ 9b63575, huggingface/transformers @ 71c6f69, configuration openai-community/gpt2 @ 607a30d · checkpoint tensors not independently counted · How to read

GPT-2 model overview

  1. UTF-8 text passes through byte-level BPE, which is configured by fixed vocabulary and merge assets.
  2. Token IDs cross the preprocessing boundary and select rows from the learned token embedding table WTE.
  3. The token sequence shape determines absolute position IDs, independently of token values, which select rows from WPE on a lower positional branch.
  4. Token and position embeddings are added, then training-only embedding dropout produces x zero. Evaluation leaves the sum unchanged.
  5. Exactly 12 transformer blocks transform x zero into x_12.
  6. Final LayerNorm acts across the 768-dimensional feature axis with epsilon equal to 10 to the minus 5.
  7. The transpose view of the shared WTE matrix projects the final states to logits without adding parameters.

Embedding addition

×

no parameterstf.gather(wte, X) + tf.gather(wpe, positions_for(X, past_length))

Add token and position vectors element-wise before training-only embedding dropout initializes the residual stream.

E=Wtoken[input_ids]+Wposition[position_ids]{\htmlData{light=embed-add}{E}}=W_{\mathrm{token}}[\mathrm{input\_ids}]+W_{\mathrm{position}}[\mathrm{position\_ids}]
in
EtE_{t} [B batch, T positions, 768 d]EposE_{\mathrm{pos}} [B batch, T positions, 768 d]
out
EE [B batch, T positions, 768 d]

During training the maintained code applies dropout to EE before the first block (modeling_gpt2.py 604): x0=Dropout⁡(E;pembd)x_0=\operatorname{Dropout}(E;p_{\mathrm{embd}}). At evaluation, and in the released code, x0=Ex_0=E.

Codemodeling_gpt2.py 577–608 · model.py 157
modeling_gpt2.py 577
        hidden_states = inputs_embeds + position_embeds.to(inputs_embeds.device)
modeling_gpt2.py 604
        hidden_states = self.drop(hidden_states)
modeling_gpt2.py 608
        for i, block in enumerate(self.h):
model.py 157
        h = tf.gather(wte, X) + tf.gather(wpe, positions_for(X, past_length))
Sources2

Direct evidence

For the base GPT-2 text path, embedding dropout follows token-plus-position addition and precedes the block loop; optional token-type embeddings are outside the base text path.

transformers · huggingface/transformers / src/transformers/models/gpt2/modeling_gpt2.py
Pinned revision 71c6f699ac9b ↗ · lines 576–608

Read code excerpt
        hidden_states = inputs_embeds + position_embeds.to(inputs_embeds.device)

… lines 578–603 omitted …

        hidden_states = self.drop(hidden_states)

… lines 605–607 omitted …

        for i, block in enumerate(self.h):

Direct evidence

The original graph gathers the two tables and adds them into the initial stream.

openai · openai/gpt-2 / src/model.py
Pinned revision 9b63575ef427 ↗ · lines 152–157

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