GPT-2

Masked multi-head self-attention

Retrieved values by head

×

Each head contains the attention-weighted combination of its value vectors, before merging.

shape
HiH_{i} head i [B batch, T positions, 64 d_h]HH all heads [B batch, 12 heads, T positions, 64 d_h]
Sources

Contextual evidence from Weighted value retrieval

The maintained eager attention path normalizes scores, applies training-dependent dropout, and multiplies the resulting weights by values.

modeling_gpt2.py 63–69 ↗ · huggingface/transformers @ 71c6f699ac9b

Excerpt
    attn_weights = nn.functional.softmax(attn_weights, dim=-1)

    # Downcast (if necessary) back to V's dtype (if in mixed-precision) -- No-Op otherwise
    attn_weights = attn_weights.type(value.dtype)
    attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training)

    attn_output = torch.matmul(attn_weights, value)

The normalized score matrix multiplies the value field inside each head.

model.py 91–99 ↗ · openai/gpt-2 @ 9b63575ef427

Excerpt
        w = mask_attn_weights(w)
        w = softmax(w)
        a = tf.matmul(w, v)
        return a

Contextual evidence from Merge attention heads

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