Pre-MLP LayerNorm
×Center and scale each position’s features, then apply learned gain and bias.
- in
- [B batch, T positions, 768 d]
- out
- [B batch, T positions, 768 d]
Parameters1,536 × 12 layers
1,536 parameters per instance.
| Component | Parameters |
|---|---|
| Learned gain | 768 |
| Learned bias | 768 |
| Per-instance total | 1,536 |
One in each of the 12 layers, each with its own weights.
Not checked against a checkpoint.
Sources
In codenorm(x, 'ln_2')
Direct evidence
The second normalization appears directly inside the MLP call.
model.py 28–128 ↗ · openai/gpt-2 @ 9b63575ef427
Excerpt
def norm(x, scope, *, axis=-1, epsilon=1e-5):
"""Normalize to mean = 0, std = 1, then do a diagonal affine transform."""
with tf.variable_scope(scope):
n_state = x.shape[-1].value
g = tf.get_variable('g', [n_state], initializer=tf.constant_initializer(1))
b = tf.get_variable('b', [n_state], initializer=tf.constant_initializer(0))
u = tf.reduce_mean(x, axis=axis, keepdims=True)
s = tf.reduce_mean(tf.square(x-u), axis=axis, keepdims=True)
x = (x - u) * tf.rsqrt(s + epsilon)
x = x*g + b
return x… lines 39–127 omitted …
m = mlp(norm(x, 'ln_2'), 'mlp', nx*4, hparams=hparams)A second nn.LayerNorm instance precedes the MLP.
modeling_gpt2.py 254–305 ↗ · huggingface/transformers @ 71c6f699ac9b
Excerpt
self.ln_2 = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon)… lines 255–302 omitted …
residual = hidden_states
hidden_states = self.ln_2(hidden_states)
feed_forward_hidden_states = self.mlp(hidden_states)