AI Grimoire
Sheet
statusstandard
difficultyintroductory
timeO(n·d²)
memoryO(n·d_ff)
described2017
revisedtoday

Feed-Forward Networks

Attention moves information between positions. The feed-forward block is where anything is done with it — and it is where two thirds of the parameters are.

[ffn][core]Current standard

Standing

Current standardPart of the default recipe. Present in most frontier models trained today, and the thing a new design departs from rather than argues for.

Half of every transformer block ever built. The gated variant replaced the exact form; the role it plays has not changed since 2017.

judged as of 2026-09 · what the labels mean

Theory

A transformer block is two things: attention, and this. Attention is the part everyone studies and the part the papers are named after. The feed-forward block is where the parameters are.

FFN(x)=W2ϕ(W1x+b1)+b2,W1Rdff×d,    W2Rd×dff\mathrm{FFN}(x) = W_2\,\phi(W_1 x + b_1) + b_2, \qquad W_1 \in \mathbb{R}^{d_{ff} \times d},\;\; W_2 \in \mathbb{R}^{d \times d_{ff}}
eq. 1 — up, nonlinearity, down

Project up to a wider space, apply a nonlinearity, project back. The conventional width is dff=4dd_{ff} = 4d, which gives 8d28d^2 parameters against attention’s 4d24d^2 — hence two thirds of the model, in the half of the block that gets a third of the attention.

Position-wise

The same W1W_1 and W2W_2 are applied to every token, independently. Nothing in the block sees a neighbour. This is the division of labour that makes the transformer work: attention is the only operation that mixes positions, and the feed-forward block is the only one with the capacity to transform what it finds there.

Why 4×

The ratio is inherited from the original paper and has never been strongly justified. What evidence exists says the block is not very sensitive to it — between roughly 2d2d and 8d8d, quality tracks parameter count rather than the ratio itself, so the choice is a question of how you would rather spend a parameter budget.

Two things have moved it. Gated variants use three matrices instead of two, so the width is scaled by 23\tfrac{2}{3} to hold the parameter count fixed — dff83dd_{ff} \approx \tfrac{8}{3}d, usually rounded to a multiple of 256 for the hardware. And mixture-of-experts replaces the single block with many, at which point the per-expert width becomes a granularity question rather than a capacity one.

Implementation

python · torch
from torch import Tensor, nn


class FeedForward(nn.Module):
    def __init__(self, dim: int, hidden: int | None = None, bias: bool = False):
        super().__init__()
        hidden = hidden or 4 * dim
        self.up = nn.Linear(dim, hidden, bias=bias)
        self.down = nn.Linear(hidden, dim, bias=bias)
        self.act = nn.GELU()

    def forward(self, x: Tensor) -> Tensor:                   # [B, T, D]
        return self.down(self.act(self.up(x)))

Biases are gone from most modern implementations. They are the smallest tensors in the model and removing them measurably improves training stability at scale — one of several places where a parameter that costs nothing turns out to be worth less than nothing.

What it is doing

The mechanistic reading is that W1W_1‘s rows are pattern detectors and W2W_2‘s columns are what gets written back when a pattern fires — a key-value memory, with dffd_{ff} entries per layer. That view is well supported and it explains a great deal: why factual knowledge localises to feed-forward blocks, why editing a fact means editing W2W_2, and why the block’s activations are extremely sparse in practice even though nothing asked them to be.

Attention params
67 M
FFN params
135 M
Share of total
≈ 67%
Llama-2-7B, per layer

Related

References

[1]Vaswani et al. — Attention Is All You Need (2017)arXiv:1706.03762
[2]Geva et al. — Transformer Feed-Forward Layers Are Key-Value Memories (2020)arXiv:2012.14913
[3]Shazeer — GLU Variants Improve Transformer (2020)arXiv:2002.05202