Transformer block
Attention residual merge
×no parametersx + a
Add the mechanism’s update to the residual stream element by element.
- in
- [B batch, T positions, 768 d] [B batch, T positions, 768 d]
- out
- [B batch, T positions, 768 d]
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 as it is.
Codemodel.py 126–127 · modeling_gpt2.py 272–274, 282
a, present = attn(norm(x, 'ln_1'), 'attn', nx, past=past, hparams=hparams)
x = x + a residual = hidden_states
hidden_states = self.ln_1(hidden_states)
attn_output, _ = self.attn( hidden_states = attn_output + residualSources2
Direct evidence
First branch of the original GPT-2 block.
openai · openai/gpt-2 / src/model.py
Pinned revision 9b63575ef427 ↗ · lines 123–130
Read code excerpt
a, present = attn(norm(x, 'ln_1'), 'attn', nx, past=past, hparams=hparams)
x = x + aDirect evidence
The PyTorch block saves residual before normalization and adds it after attention.
transformers · huggingface/transformers / src/transformers/models/gpt2/modeling_gpt2.py
Pinned revision 71c6f699ac9b ↗ · lines 272–282
Read code excerpt
residual = hidden_states
hidden_states = self.ln_1(hidden_states)
attn_output, _ = self.attn(… lines 275–281 omitted …
hidden_states = attn_output + residualIncoming transform evidence
The maintained training path applies residual dropout after c_proj and before the residual write; evaluation makes dropout the identity.
Attention residual dropout · huggingface/transformers / src/transformers/models/gpt2/modeling_gpt2.py
Pinned revision 71c6f699ac9b ↗ · lines 222–226
Read code excerpt
attn_output = self.c_proj(attn_output)
attn_output = self.resid_dropout(attn_output)
return attn_output, attn_weights