Absolute position indices

×

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)
position_ids=past_length+arange⁡(T)\begin{aligned}\mathrm{position\_ids}={}&\mathrm{past\_length}\\&+\operatorname{arange}(T)\end{aligned}
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.

Sources

In codepositions_for(X, past_length)

Open pinned code ↗

Direct evidence

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

model.py 141–144 ↗ · openai/gpt-2 @ 9b63575ef427

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)