Query-key dot product
×Each query position is compared with every key position in its head, including future positions. Causal masking follows this raw similarity calculation.
- in
- [B batch, 12 heads, T positions, 64 d_h] [B batch, 12 heads, T positions, 64 d_h]
- out
- [B batch, 12 heads, T queries, T keys]
Sources
In codetf.matmul(q, k, transpose_b=True)
Direct evidence
The attention kernel forms the per-head query–key matrix before scaling.
model.py 91–94 ↗ · openai/gpt-2 @ 9b63575ef427
Excerpt
def multihead_attn(q, k, v):… lines 92–92 omitted …
w = tf.matmul(q, k, transpose_b=True)
w = w * tf.rsqrt(tf.cast(v.shape[-1].value, w.dtype))