Absolute position indices
×no parameterspositions_for(X, past_length)
Assigns an absolute position index to each token in the sequence.
- 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). counts positions already in the cache; a full-sequence pass has .
Codemodel.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)