Row-wise softmax
×Normalize each query’s masked scores across key positions into attention probabilities. Each row sums to one before attention dropout.
- in
- [B batch, 12 heads, 1, 1001]
- out
- [B batch, 12 heads, 1, 1001]
Explanation
, , and index batch, head, query and key; runs over the keys.
The released code subtracts each row’s maximum before exponentiating (model.py 20–23). The shift cancels between numerator and denominator and keeps every exponent at or below zero, so cannot overflow.
Sources
In codesoftmax(w)
Direct evidence
The released implementation subtracts the row maximum for stability, exponentiates, and divides by the row sum on the final axis. Attention calls it after masking.
model.py 20–97 ↗ · openai/gpt-2 @ 9b63575ef427
Excerpt
def softmax(x, axis=-1):
x = x - tf.reduce_max(x, axis=axis, keepdims=True)
ex = tf.exp(x)
return ex / tf.reduce_sum(ex, axis=axis, keepdims=True)… lines 24–95 omitted …
w = mask_attn_weights(w)
w = softmax(w)