LLM Basics
Lesson 11 of 20Assembling the Transformer

Residuals, Normalization & the Feed-Forward Layer

With position injected, attention has everything it needs to mix information across tokens. Two more pieces turn that into an actual transformer block, the repeating unit stacked to build every modern LLM: a way to keep gradients flowing through deep stacks, and a layer that lets each token "think" on its own.

First: residual connections. Recall from the backpropagation lesson that gradients are products of local derivatives chained together, stack enough layers and that product can shrink toward zero (or blow up), making deep networks hard to train. The fix is almost embarrassingly simple: instead of replacing a vector with f(x), compute x + f(x). The +x gives gradients a direct shortcut path backward through every layer, no matter how deep the stack gets.

Two more pieces round out a block:

  • Layer norm rescales a vector to have zero mean and unit variance before the next sublayer, keeping activations in a well-behaved range as they pass through many stacked blocks.
  • A feed-forward network, a small two-layer network (expand, ReLU, contract) applied independently to each token's vector. Where attention mixes information across tokens, the feed-forward layer lets each token "think" about what it just gathered. This sublayer alone holds a large fraction of a transformer's total parameters.

Step one token through a full transformer block

Token embedding

[1.40, 0.30]

+ Positional encoding

Self-attention output

Add residual

withPos + attention output

Layer norm

Feed-forward output

Add residual

norm output + feed-forward output

Layer norm (block output)

× N blocks, stacked — same structure, different learned weights each time

That's a complete transformer block: embed → add position → attend → residual → norm → feed-forward → residual → norm. A real model stacks dozens of these, each with its own learned weights, and every stack refines the same token vectors a little further before a final layer turns them into next-token predictions.

Here's the entire block in plain Python, reusing the exact same sentence and attention math from the self-attention lesson, continuing with the token "it":

Python

One thing this block glossed over: the attention step above only used a single set of Q/K/V projections. Real transformers run several in parallel, next.