Masked multi-head self-attention

Queries by head

×

The feature axis is partitioned into 12 heads of width 64; positions remain separate.

shape
QiQ_{i} head i [B batch, 1, 64 d_h]QQ all heads [B batch, 12 heads, 1, 64 d_h]
Sources

Contextual evidence from Reshape into attention heads

The released split_heads function introduces a head axis and moves it before the position axis.

model.py 75–79 ↗ · openai/gpt-2 @ 9b63575ef427

Excerpt
    def split_heads(x):

… lines 76–76 omitted …

        return tf.transpose(split_states(x, hparams.n_head), [0, 2, 1, 3])

Contextual evidence from Query-key dot product

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))