Prepare Q, K, V

Reshape into attention heads

×

Reshape and permute Q, K, and V independently into 12 heads of width 64.

[B,1,768][B,1,768]
→  [B,1,12,64]\to\;[B,1,12,64]
→  [B,12,1,64]\to\;[B,12,1,64]
in
QQ [B batch, 1, 768 d]KK [B batch, 1, 768 d]VV [B batch, 1, 768 d]
out
Q/K/VQ / K / V [B batch, 12 heads, 1, 64 d_h]
Sources

In codemap(split_heads, tf.split(c, 3, axis=2))

Open pinned code ↗

Direct evidence

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