Masked multi-head self-attention

Merge and project

×

Concatenates the per-head outputs into one feature axis and projects it into attention’s residual update.

Δattn=merge_heads⁡(H) WO+bO\begin{aligned}{\htmlData{light=attn-update attn-proj}{\Delta_{\mathrm{attn}}}}&=\operatorname{merge\_heads}({\htmlData{light=head-mix attn-mix}{H}})\,W_O+b_O\end{aligned}
in
HH [B batch, 12 heads, 1, 64 d_h]
out
Δattn\Delta_{\mathrm{attn}} [B batch, 1, 768 d]

In training, the maintained code applies resid_dropout to the projected update inside the module (modeling_gpt2.py 224).

Parameters590,592 × 12 layers

590,592 parameters per layer, across its learned components

ComponentCalculationParameters
Merge attention heads—0
Attention output projection768 × 768 + 768590,592

One in each of the 12 layers, each with its own weights.

The group references these owners; it adds no weights of its own.

Explanation

Merging moves the head axis beside the feature axis and concatenates 12 × 64 = 768 features into HH. The output projection then combines information across heads at every position: Δattn=HWO+bO\Delta_{\mathrm{attn}}=HW_O+b_O. Only the projection owns parameters.

Intermediate states
Merged attention values
Sources

In codeattn · model.py 69–112

Open pinned code ↗

Evidence for the operations this group contains. The group itself is a presentation and has no implementation of its own.

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

Contextual evidence from Attention output projection

The released projection helper owns a learned matrix and output bias and applies them to the last feature axis.

model.py 50–56 ↗ · openai/gpt-2 @ 9b63575ef427

Excerpt
def conv1d(x, scope, nf, *, w_init_stdev=0.02):
    with tf.variable_scope(scope):
        *start, nx = shape_list(x)
        w = tf.get_variable('w', [1, nx, nf], initializer=tf.random_normal_initializer(stddev=w_init_stdev))
        b = tf.get_variable('b', [nf], initializer=tf.constant_initializer(0))
        c = tf.reshape(tf.matmul(tf.reshape(x, [-1, nx]), tf.reshape(w, [-1, nf]))+b, start+[nf])
        return c

The attention output merges its head axis and passes through c_proj before returning to the residual stream.

model.py 109–112 ↗ · openai/gpt-2 @ 9b63575ef427

Excerpt
        a = multihead_attn(q, k, v)
        a = merge_heads(a)
        a = conv1d(a, 'c_proj', n_state)
        return a, present

The maintained attention path merges heads, applies c_proj, and returns the residual update.

modeling_gpt2.py 222–226 ↗ · huggingface/transformers @ 71c6f699ac9b

Excerpt
        attn_output = attn_output.reshape(*attn_output.shape[:-2], -1).contiguous()
        attn_output = self.c_proj(attn_output)
        attn_output = self.resid_dropout(attn_output)

        return attn_output, attn_weights