Pre-attention 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_1')
Direct evidence
Original LayerNorm implementation and ε = 10⁻⁵.
model.py 28–38 ↗ · 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 xPyTorch delegates the operation to nn.LayerNorm with the checkpoint epsilon.
modeling_gpt2.py 252–273 ↗ · huggingface/transformers @ 71c6f699ac9b
Excerpt
self.ln_1 = nn.LayerNorm(hidden_size, eps=config.layer_norm_epsilon)… lines 253–271 omitted …
residual = hidden_states
hidden_states = self.ln_1(hidden_states)