Layer Norm and Residual Connections in Transformers Explained

By LatestLLM Editorial · Published · Facts verified · 9 min read

Also called: LayerNorm, RMSNorm, layer normalization, residual connection, skip connection, residual stream, pre-norm

Before this: Attention, Dimension

In one line

Residual connections add each layer's output to its input, and normalization rescales the numbers, so dozens of transformer layers can be trained without the signal fading or exploding.

Normalization and residual connections, simply and technically

In plain English

A transformer stacks dozens of layers. Stacking looks easy, but a long chain is fragile: a signal passed through many steps can fade to nothing or grow out of control, and then the model stops learning. Two inexpensive habits solve this. The first is to keep a running copy of each word's information and let every layer add its changes to that copy instead of replacing it. The second is to rescale the numbers before each step so they stay in a sensible range.

These habits are invisible to people who use a chatbot, but modern models could not be built without them. They are why a transformer is often described as a conveyor belt of information: every layer reads the belt, adds a note and passes it along.

Think of it like this: Think of a shared document that every editor may only add comments to, never delete from. Each layer is an editor who reads the document so far and adds a note, and normalization is a rule that every note must be written at a readable size. The original text can never be lost, because it is still there under all the notes. In a real model the notes do interfere with one another to some degree, since they are numbers that can add up or cancel out.

Everyday example: Keeping the original while adding to it

A word enters the first layer as the list of numbers that stands for “bank”. Attention notices “river” nearby and produces a correction.

Input:

“bank” next to “river”

Output:

The numbers for “bank” plus an adjustment toward “riverside”

What it shows: Instead of replacing the word's numbers with the correction, the layer adds the correction to them. The original meaning stays and the new information sits on top of it. After dozens of layers the original numbers are still part of the sum, so early information is never forced to survive a long chain of rewrites.

Technically

A residual connection computes x_(l+1) = x_l + f(x_l), where f is an attention or feed-forward block. The running vector, often called the residual stream, is read by every block and added to by every block, so layers communicate through it (Elhage et al. 2021). He et al. (2015) introduced residual learning for image networks and trained up to 152 layers. Because the identity path is untouched, gradients reach early layers directly, which is what makes very deep stacks trainable.

Normalization controls scale. LayerNorm computes (x − mean) / sqrt(var + ε) over the d numbers of one token and then applies a learned gain and bias. RMSNorm (Zhang and Sennrich 2019) drops the mean subtraction and the bias and divides by sqrt(mean(x²) + ε); it is cheaper and used by gpt-oss, Gemma 4 and DeepSeek. Placement matters: the original transformer normalised after each residual addition (post-norm), which Xiong et al. (2020) showed gives large gradients near the output and needs learning-rate warm-up. Pre-norm, x + f(norm(x)), is well behaved at initialisation and is what gpt-oss uses; Gemma 4 normalises before and after each block and also normalises queries and keys.

Technical example: LayerNorm versus RMSNorm on real numbers

The first 8 of the 384 numbers all-MiniLM-L6-v2 gives for “The cat sat on the mat.” are normalised with each method, with and without a constant offset of 0.5 added to every number. A toy block output is then added with a residual connection.

Code (python):

import math

x = [0.1302, -0.0158, -0.0367, 0.058, -0.0598, 0.0331, 0.0301, 0.0289]
mean = lambda v: sum(v) / len(v)
rms = lambda v: math.sqrt(sum(a * a for a in v) / len(v))

def layer_norm(v, eps=1e-5):
    m, var = mean(v), sum((a - mean(v)) ** 2 for a in v) / len(v)
    return [(a - m) / math.sqrt(var + eps) for a in v]

def rms_norm(v, eps=1e-6):
    r = math.sqrt(sum(a * a for a in v) / len(v) + eps)
    return [a / r for a in v]

for name, v in [("input", x), ("input + 0.5", [a + 0.5 for a in x])]:
    ln, rn = layer_norm(v), rms_norm(v)
    print(f"{name:12s} mean {mean(v):.4f}  rms {rms(v):.4f}")
    print(f"  LayerNorm  mean {mean(ln):.4f}  rms {rms(ln):.4f}")
    print(f"  RMSNorm    mean {mean(rn):.4f}  rms {rms(rn):.4f}")

block_output = [0.5 * a for a in x]        # a toy f(x)
print("residual x + f(x):", [round(a + b, 4) for a, b in zip(x, block_output)])

Output:

input        mean 0.0210  rms 0.0595
  LayerNorm  mean 0.0000  rms 0.9984
  RMSNorm    mean 0.3529  rms 0.9999
input + 0.5  mean 0.5210  rms 0.5240
  LayerNorm  mean -0.0000  rms 0.9984
  RMSNorm    mean 0.9943  rms 1.0000
residual x + f(x): [0.1953, -0.0237, -0.0551, 0.087, -0.0897, 0.0496, 0.0451, 0.0433]

What it shows: Both methods give the output unit RMS. LayerNorm also forces the mean to zero, so the +0.5 offset vanishes, while RMSNorm leaves the mean (about 0.99 after the offset) in place. The residual line shows the original numbers surviving inside the sum.

Normalising a real vector. Bars show the first 40 of 384 numbers of a real sentence vector from all-MiniLM-L6-v2, before and after normalisation. Choose LayerNorm or RMSNorm, pick one of three vectors and add an offset to every number: LayerNorm removes the offset and RMSNorm keeps it. The readout gives the mean and RMS before and after. Vectors are real all-MiniLM-L6-v2 sentence embeddings used as example numbers; unit gain and no bias.

Why deep stacks are fragile

In plain English: Imagine whispering a message down a line of fifty people. By the end it is either inaudible or garbled. Passing numbers through fifty layers has the same problem: each layer multiplies the signal, and small changes compound until values shrink to nothing or blow up. Learning needs a clear signal to flow in both directions.

Technically:

In training, gradients flow backward through every layer. If each layer scales them by a factor slightly below one they vanish; slightly above, they explode. Before residual connections, networks deeper than about twenty layers were hard to optimise and sometimes got worse as they got deeper. He et al. reported that residual networks were easier to optimise and gained accuracy from increased depth.

Language models add a second difficulty: activations grow with depth and some dimensions become much larger than the rest, so without normalization the largest values dominate every dot product and the softmax in attention saturates.

What is a vanishing gradient?

A learning signal that shrinks as it moves backward through layers until early layers receive almost none.

Residual connections and the residual stream

In plain English: A residual connection changes what a layer is asked to do. Instead of rewriting the word's numbers from scratch, it only has to say what to add. If there is nothing useful to add, it can output close to zero and the word passes through unchanged.

Technically:

Writing each block as x + f(x) makes the identity the default behaviour. Every attention head and every feed-forward block reads from the stream and writes back into it by addition, so the stream accumulates contributions: after L layers it is the embedding plus 2L additions (one attention and one feed-forward block per layer). Elhage et al. describe the stream as a shared communication channel, with blocks free to read what earlier blocks wrote.

The stream has the model's hidden width, 2,880 in gpt-oss, 5,376 in Gemma 4 31B and 7,168 in DeepSeek-V3, so all information the model keeps about a token must fit in that many numbers. The final norm and output projection read the stream after the last block to produce next-token scores.

Go deeper: details for practitioners
  • Reading the stream as a sum of block outputs lets researchers attribute a prediction to individual heads and layers, which is the basis of much interpretability work.

LayerNorm and RMSNorm

In plain English: Normalization is a volume control. Before a block looks at a word's numbers, the numbers are rescaled so their typical size is fixed, whatever happened in earlier layers. The block then always sees values in a comfortable range.

Technically:

LayerNorm (Ba et al. 2016) normalises over the feature dimension of a single token, so it needs no batch statistics and works at any batch size and during generation. It subtracts the mean, divides by the standard deviation and applies learned per-dimension gain and bias. RMSNorm divides only by the root mean square and keeps just a learned gain, saving the mean computation; Zhang and Sennrich argued that re-centering is dispensable and that RMSNorm is simpler and more efficient.

Both are applied per token and cost little next to attention and feed-forward layers. gpt-oss and DeepSeek-V3 use RMSNorm before each block; Gemma 4 also normalises the queries and keys inside attention (QK-norm) to keep attention scores in range.

What is RMS?

Root mean square: square every number, average the squares, take the square root. It measures the typical size of the numbers regardless of sign.

Where the normalization goes

In plain English: The order of the steps changes how easy the model is to train. Putting the volume control before each block keeps the main path clean, and that turns out to be much easier to train than putting it after.

Technically:

The original transformer applied LayerNorm after adding the block output, LayerNorm(x + f(x)), called post-norm. Xiong et al. (2020) proved that at initialisation this gives large expected gradients near the output, which forces a learning-rate warm-up, while pre-norm, x + f(LayerNorm(x)), has well-behaved gradients so warm-up can be removed. Almost all current LLMs use pre-norm or a variant.

Gemma 4 uses a sandwich pattern: RMSNorm before and after each attention and feed-forward block. The extra norm bounds each block's contribution before it joins the stream, which helps when the stream's values become large in deep stacks.

Common misconceptions

  • Myth: A layer replaces the word's numbers with its own output.

    Reality: With residual connections each layer adds to the running vector. The original embedding is still part of the sum at the last layer.

  • Myth: Normalization makes all numbers equal or between 0 and 1.

    Reality: It sets the typical size of a token's numbers, with unit RMS in the figure, and keeps their pattern. Negative values and different sizes remain.

  • Myth: LayerNorm and RMSNorm are interchangeable and nothing differs.

    Reality: LayerNorm removes the mean as well, so a constant offset vanishes; RMSNorm keeps it. Most recent LLMs choose RMSNorm because it is cheaper and works as well.

Where you'll see this

  • gpt-oss architecture: gpt-oss uses plain pre-norm RMSNorm with residual connections around attention and the expert layers.
  • Gemma 4 architecture: Gemma 4 applies RMSNorm before and after each block and also normalises queries and keys.
  • DeepSeek-V3 architecture: DeepSeek-V3 uses RMSNorm and residual connections in each of its 61 layers.

Key takeaways

  • A residual connection adds a block's output to its input, so the original signal always has a clear path through the whole stack.
  • Normalization rescales each token's numbers so they stay in a stable range; RMSNorm does this without subtracting the mean and is common in current models.
  • Where the normalization sits matters: pre-norm, used by gpt-oss, trains stably without a long warm-up, and Gemma 4 adds norms after the blocks as well.
  • Together they are why modern models can have 30 to 60 or more layers and still be trained.

Frequently asked questions

What is a residual connection?

A path that adds a block's input to its output, so each layer only has to learn a change rather than a whole new representation.

What is the residual stream?

The running vector of a token that every attention and feed-forward block reads from and adds to; its width equals the model's hidden size.

What is the difference between LayerNorm and RMSNorm?

LayerNorm subtracts the mean and divides by the standard deviation; RMSNorm skips the mean subtraction and divides by the root mean square, which is cheaper.

What is pre-norm?

Normalising the input of each block rather than its output. It keeps gradients well behaved and lets models train without a long warm-up.

Why do transformers need normalization?

Without it activations can grow or shrink with depth, saturating the softmax and destabilising training.

Sources

Facts last verified November 23, 2026.

Next concept: Transformer

Learn next

  • Transformer (Intermediate): A transformer is a neural network built from a stack of identical blocks, each mixing information between tokens with attention and then refining each token on its own.
  • Feed-forward network (Intermediate): A feed-forward network is the step in each transformer layer that processes every token on its own: it widens the token's vector, applies a nonlinearity and shrinks it back.
  • Positional encoding (Intermediate): Positional encoding is the signal that tells a transformer where each token sits in the text, because attention alone cannot tell “dog bites man” from “man bites dog”.