Transformer block
MLP residual merge
×no parametersx + m
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 MLP module (modeling_gpt2.py 242); the released code adds as it is.
Codemodel.py 128–130 · modeling_gpt2.py 303–305, 307
m = mlp(norm(x, 'ln_2'), 'mlp', nx*4, hparams=hparams)
x = x + m
return x, present residual = hidden_states
hidden_states = self.ln_2(hidden_states)
feed_forward_hidden_states = self.mlp(hidden_states) hidden_states = residual + feed_forward_hidden_statesSources2
Direct evidence
Second branch and block return in the reference graph.
openai · openai/gpt-2 / src/model.py
Pinned revision 9b63575ef427 ↗ · lines 123–130
Read code excerpt
m = mlp(norm(x, 'ln_2'), 'mlp', nx*4, hparams=hparams)
x = x + m
return x, presentDirect evidence
The same residual contract in the maintained PyTorch implementation.
transformers · huggingface/transformers / src/transformers/models/gpt2/modeling_gpt2.py
Pinned revision 71c6f699ac9b ↗ · lines 303–309
Read code excerpt
residual = hidden_states
hidden_states = self.ln_2(hidden_states)
feed_forward_hidden_states = self.mlp(hidden_states)… lines 306–306 omitted …
hidden_states = residual + feed_forward_hidden_statesIncoming transform evidence
The maintained training path applies residual dropout after the MLP projection and before the residual write; evaluation makes dropout the identity.
MLP residual dropout · huggingface/transformers / src/transformers/models/gpt2/modeling_gpt2.py
Pinned revision 71c6f699ac9b ↗ · lines 238–243
Read code excerpt
hidden_states = self.c_proj(hidden_states)
hidden_states = self.dropout(hidden_states)
return hidden_states