The Feed-Forward Network in a Transformer

Half of every transformer layer is not attention — it is a small per-token network holding most of the model's parameters. What it computes and why factual knowledge appears to live there.

On this page

Attention gets the attention. But in a transformer layer, self-attention is only half the block — the other half is a feed-forward network, and it holds roughly two-thirds of the model’s parameters. If attention is where tokens exchange information, the feed-forward network is where each token does its private thinking. Skipping it leaves you understanding half the machine.

Attention moves, the FFN processes

The cleanest way to hold the two halves apart:

  • Attention mixes across positions. Every token pulls in information from other tokens. It is fundamentally about relationships between positions.
  • The feed-forward network works within a position. After a token has gathered context from attention, it passes through the FFN alone — no interaction with other tokens at all.

The same feed-forward network is applied to every position independently, with identical weights. Token 3 and token 300 go through the exact same transformation, separately. Attention is where the sequence talks to itself; the FFN is where each enriched token is transformed on its own.

What it actually computes

The feed-forward network is small and its shape is fixed across nearly every model. It is two linear layers with a nonlinearity between them:

  1. Expand. Project the token’s vector up to a larger dimension — typically about 4× the model’s width. A 4,096-dimension vector becomes roughly 16,384.
  2. Activate. Apply a nonlinear function elementwise. This is the only nonlinearity in the block, and it is what lets the layer represent more than a straight-line transformation.
  3. Contract. Project back down to the original dimension so the output can slot into the next layer.

That expand-then-contract shape is the whole design. The wide middle gives the layer room to compute many intermediate features; the contraction brings it back to a size the rest of the stack can consume. The two projection matrices are where most of the parameter count sits.

Why the nonlinearity is the point

Strip out the activation function and the two linear layers collapse into one — a stack of linear operations is just another linear operation, no matter how many you chain. The whole model would be unable to represent anything but straight-line relationships between input and output, which is nowhere near enough for language.

The nonlinearity in the middle is what breaks that. It lets the FFN represent things like “if this feature and that feature are both present, produce this output” — conditional, curved relationships that no linear map can express. Every layer’s FFN adds another round of this nonlinear processing, and the stacking is a large part of why depth helps. The activation function itself (its exact shape is a design detail that has evolved over time) matters less than the fact that some nonlinearity is there.

Where the knowledge seems to live

Here is the finding that makes the FFN interesting beyond bookkeeping. When researchers probe where a model “stores” facts — that Paris is in France, that water boils at 100°C — the evidence points at the feed-forward layers, not attention.

The picture that has emerged is that the FFN acts something like a key-value memory. The expand step matches the incoming token pattern against many learned patterns; the contract step writes the associated information back into the token’s vector. Editing individual facts in a model turns out to be doable by adjusting specific FFN weights, which is direct evidence that this is where a lot of factual association is held.

This reframes the parameter count. The FFN dominates the parameter budget not by accident but because that is where the model’s stored knowledge has to fit. Attention decides what to look at; the FFN holds much of what the model knows.

Why its share of parameters matters

Because the FFN is the bulk of the weights, it is also the bulk of the compute during a forward pass and a primary target for efficiency work. Techniques that shrink or sparsify these layers — only activating part of the FFN per token, for instance — attack the biggest block of the model. The mixture-of-experts design is exactly this idea: replace one large feed-forward network with many smaller ones and route each token to only a few, so the parameter count can grow without every token paying for all of it.

So when a model is described as having a certain parameter count, most of that number is feed-forward weights, and most efficiency levers pull on them.

What to remember

  • Every transformer layer pairs attention with a feed-forward network; the FFN holds roughly two-thirds of the parameters.
  • Attention mixes information across positions; the FFN processes each position independently with shared weights.
  • Its shape is expand → nonlinear activation → contract, and the nonlinearity is what lets it represent non-linear relationships.
  • Evidence suggests much of the model’s factual knowledge is stored in FFN weights, like a learned key-value memory.
  • Because it dominates the parameter count, it is the main target for efficiency techniques like mixture-of-experts.

Next: Normalization Layers — the other machinery wrapped around every block.