Merge and project

Merge attention heads

×

Move the head axis beside the feature axis and concatenate 12 × 64 features into width 768.

[B,12,T,64][B,12,T,64]
→  [B,T,12,64]\to\;[B,T,12,64]
→  [B,T,768]\to\;[B,T,768]
in
HH [B batch, 12 heads, T positions, 64 d_h]
out
HH [B batch, T positions, 768 d]
Explanation

Heads are concatenated in order: head ii fills features 64i,…,64i+63{64i},\dots,{64i+63}.

Sources

In codemerge_heads(a)

Open pinned code ↗

Direct evidence

The released merge_heads function reverses the head permutation and merges the final two axes.

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

Excerpt
    def merge_heads(x):

… lines 80–80 omitted …

        return merge_states(tf.transpose(x, [0, 2, 1, 3]))