Position-wise feed-forward network
×Transforms each position’s features independently to produce an update for the residual stream.
- in
- [B batch, T positions, 768 d]
- out
- [B batch, T positions, 768 d]
Parameters4,722,432 × 12 layers
4,722,432 parameters per instance, across its learned components
| Component | Calculation | Parameters |
|---|---|---|
| Feature expansion | 768 × 3,072 + 3,072 | 2,362,368 |
| Feature projection | 3,072 × 768 + 768 | 2,360,064 |
| Per-instance total | 4,722,432 | |
One in each of the 12 layers, each with its own weights.
Not checked against a checkpoint.
Sources
In codemlp(norm(x, 'ln_2'), 'mlp', nx*4, hparams=hparams)
Direct evidence
Original two projections with a four-times expansion.
model.py 115–128 ↗ · openai/gpt-2 @ 9b63575ef427
Excerpt
def mlp(x, scope, n_state, *, hparams):
with tf.variable_scope(scope):
nx = x.shape[-1].value
h = gelu(conv1d(x, 'c_fc', n_state))
h2 = conv1d(h, 'c_proj', nx)
return h2… lines 121–127 omitted …
m = mlp(norm(x, 'ln_2'), 'mlp', nx*4, hparams=hparams)Transformers resolves the activation from the checkpoint configuration.
modeling_gpt2.py 232–260 ↗ · huggingface/transformers @ 71c6f699ac9b
Excerpt
embed_dim = config.hidden_size
self.c_fc = Conv1D(intermediate_size, embed_dim)
self.c_proj = Conv1D(embed_dim, intermediate_size)
self.act = ACT2FN[config.activation_function]
self.dropout = nn.Dropout(config.resid_pdrop)
def forward(self, hidden_states: tuple[torch.FloatTensor] | None) -> torch.FloatTensor:
hidden_states = self.c_fc(hidden_states)
hidden_states = self.act(hidden_states)
hidden_states = self.c_proj(hidden_states)
hidden_states = self.dropout(hidden_states)
return hidden_states… lines 244–248 omitted …
hidden_size = config.hidden_size
inner_dim = config.n_inner if config.n_inner is not None else 4 * hidden_size… lines 251–259 omitted …
self.mlp = GPT2MLP(inner_dim, config)