Prepare Q, K, V

Reference code openai/gpt-2 @ 9b63575, huggingface/transformers @ 71c6f69, configuration openai-community/gpt2 @ 607a30d · checkpoint tensors not independently counted · How to read

Transformer block › Attention

Query field

×

tensorq, k, v = map(split_heads, tf.split(c, 3, axis=2))

Queries represent each position for comparison with the keys when calculating retrieval weights.

shape
QQ [B batch, 1, 768 d]
Sources

Contextual evidence from Q, K, V split

The fused field is split three ways and each result is reshaped into the head axis.

openai · openai/gpt-2 / src/model.py
Pinned revision 9b63575ef427 ↗ · lines 75–103

Read code excerpt
    def split_heads(x):

… lines 76–76 omitted …

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

… lines 78–102 omitted …

        q, k, v = map(split_heads, tf.split(c, 3, axis=2))

Contextual evidence from Reshape into attention heads

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