Transformer block › Attention
Reshape into attention heads
×no parametersmap(split_heads, tf.split(c, 3, axis=2))
Reshape and permute Q, K, and V independently into 12 heads of width 64.
- in
- [B batch, T positions, 768 d] [B batch, T positions, 768 d] [B batch, T positions, 768 d]
- out
- [B batch, 12 heads, T positions, 64 d_h]
Codemodel.py 75, 77
def split_heads(x): return tf.transpose(split_states(x, hparams.n_head), [0, 2, 1, 3])Sources1
Direct evidence
The released split_heads function introduces a head axis and moves it before the position axis.
openai · openai/gpt-2 / src/model.py
Pinned revision 9b63575ef427 ↗ · lines 75–79
Read code excerpt
def split_heads(x):… lines 76–76 omitted …
return tf.transpose(split_states(x, hparams.n_head), [0, 2, 1, 3])