Layer

Attention residual merge

×

Add the mechanism’s update to the residual stream element by element.

xn′=xn+Dresid(Δattn){\htmlData{light=x-attn attn-add}{x'_{n}}}={\htmlData{light=x-in}{x_{n}}}+D_{\mathrm{resid}}({\htmlData{light=attention}{\Delta_{\mathrm{attn}}}})
in
xnx_{n} [B batch, T positions, 768 d]Dresid(Δattn)D_{\mathrm{resid}}(\Delta_{\mathrm{attn}}) [B batch, T positions, 768 d]
out
xn′x'_{n} [B batch, T positions, 768 d]

DresidD_{\mathrm{resid}} is dropout during training and the identity at evaluation. The maintained code applies it inside the attention module (modeling_gpt2.py 224); the released code adds aa as it is.

Sources

Direct evidence

First branch of the original GPT-2 layer.

model.py 123–130 ↗ · openai/gpt-2 @ 9b63575ef427

Excerpt
        a, present = attn(norm(x, 'ln_1'), 'attn', nx, past=past, hparams=hparams)
        x = x + a

The PyTorch layer saves residual before normalization and adds it after attention.

modeling_gpt2.py 272–282 ↗ · huggingface/transformers @ 71c6f699ac9b

Excerpt
        residual = hidden_states
        hidden_states = self.ln_1(hidden_states)
        attn_output, _ = self.attn(

… lines 275–281 omitted …

        hidden_states = attn_output + residual

Incoming transform evidence

The maintained training path applies residual dropout after c_proj and before the residual write; evaluation makes dropout the identity.

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

Excerpt
        attn_output = self.c_proj(attn_output)
        attn_output = self.resid_dropout(attn_output)

        return attn_output, attn_weights