Layer

Post-attention residual state

×

The residual features after the attention update, feeding the MLP branch and its bypass.

shape
xn′x'_{n} [B batch, T positions, 768 d]
Sources

Contextual evidence from Attention residual merge

First branch of the original GPT-2 layer.

model.py 123–130 ↗ · openai/gpt-2 @ 9b63575ef427

Excerpt
        a, present = attn(norm(x, 'ln_1'), 'attn', nx, past=past, hparams=hparams)
        x = x + a

The PyTorch layer saves residual before normalization and adds it after attention.

modeling_gpt2.py 272–282 ↗ · huggingface/transformers @ 71c6f699ac9b

Excerpt
        residual = hidden_states
        hidden_states = self.ln_1(hidden_states)
        attn_output, _ = self.attn(

… lines 275–281 omitted …

        hidden_states = attn_output + residual

Contextual evidence from Pre-MLP LayerNorm

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)

Contextual evidence from MLP residual merge

Second branch and layer return in the reference graph.

model.py 123–130 ↗ · openai/gpt-2 @ 9b63575ef427

Excerpt
        m = mlp(norm(x, 'ln_2'), 'mlp', nx*4, hparams=hparams)
        x = x + m
        return x, present

The same residual contract in the maintained PyTorch implementation.

modeling_gpt2.py 303–309 ↗ · huggingface/transformers @ 71c6f699ac9b

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_states