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.

Absolute position indices

×

no parameterspositions_for(X, past_length)

Assigns an absolute position index to each token in the sequence.

position_ids=past_length+arange⁡(T)\mathrm{position\_ids}=\mathrm{past\_length}+\operatorname{arange}(T)
in
[B batch, T positions]
out
[B batch, T positions]
Explanation

The helper reads only the batch size and the sequence length from the tokens, and tiles the same indices across the batch (model.py 141–144). past_length\mathrm{past\_length} counts positions already in the cache; a full-sequence pass has past_length=0\mathrm{past\_length}=0.

Codemodel.py 141–144
model.py 141–144
def positions_for(tokens, past_length):
    batch_size = tf.shape(tokens)[0]
    nsteps = tf.shape(tokens)[1]
    return expand_tile(past_length + tf.range(nsteps), batch_size)
Sources1

Direct evidence

The position helper constructs indices from sequence length and past length; token values do not enter the calculation.

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

Read code excerpt
def positions_for(tokens, past_length):
    batch_size = tf.shape(tokens)[0]
    nsteps = tf.shape(tokens)[1]
    return expand_tile(past_length + tf.range(nsteps), batch_size)