Feedforward Blocks and Gated Linear Units
Attention mixes information between tokens; the feedforward block that follows it in every transformer layer is where each token's representation is actually processed, and swapping a plain ReLU for a gated linear unit is a small change that reliably improves it.
Prerequisites: Scaled Dot-Product Attention and the √d Factor, The Residual Stream View of Transformers
Attention is the transformer's mechanism for mixing information between tokens — deciding what each token should borrow from every other token. But attention alone never processes a single token's content in isolation; every transformer layer follows its attention step with a feedforward block that runs independently, with the same weights, on every token's vector one at a time, with no mixing across positions at all. This is where most of a transformer's actual per-token "thinking" happens, and roughly two-thirds of a typical transformer's parameters live in these feedforward blocks, not in attention.
The analogy: a shared meeting followed by private desk work
Attention is like a meeting where everyone briefly shares information and each person walks away having incorporated relevant input from others. The feedforward block is what happens next: everyone returns to their own desk and processes what they learned privately, using the exact same standard procedure (the same weights) everyone else also uses, with no further cross-talk. A gated linear unit changes that private procedure slightly: each person also decides, based on what they learned, how much of a second processing pathway to let through — a built-in, content-dependent volume knob, rather than a rigid, one-size-fits-all rule.
The mechanics: plain feedforward versus a gated variant
A standard transformer feedforward block is two linear layers with a nonlinearity between them, applied identically to every token:
In words: project the token's vector up to a wider intermediate dimension with , apply a nonlinearity (originally ReLU, later often GELU) elementwise, then project back down to the original dimension with — a simple expand-then-compress computation, applied independently to every position in the sequence.
A gated linear unit (GLU) variant instead computes two separate linear projections of the same input, uses one of them (passed through a nonlinearity) as a gate that multiplies elementwise into the other:
In words: one projection (, passed through ) decides, dimension by dimension, how much of the other projection () to let through, via elementwise multiplication — some dimensions of the intermediate representation get mostly shut off (gate value near 0), others pass through almost fully (gate value near 1), and this gating pattern is different for every input token, learned rather than fixed. This costs a third weight matrix compared to plain feedforward, so gated variants are typically built with a somewhat narrower intermediate dimension to keep total parameter count comparable.
Worked example 1: gating in action on two token types
Suppose, for a punctuation-mark token, the gate projection outputs values close to across three intermediate dimensions, while the content projection outputs . The gated output is elementwise: — dimension 2 passes through almost fully, the others are mostly suppressed. For a content-bearing noun token, the same gate projection might instead output , flipping which dimensions dominate: . The same fixed weights produce a completely different effective computation depending on the input, because the gate itself is a function of that input.
Worked example 2: parameter budget comparison
A plain feedforward block with model dimension and intermediate dimension has two weight matrices of size each, for M parameters. A gated variant typically uses a smaller intermediate dimension, say , with three matrices of size : M — deliberately shrinking width so the extra gating matrix doesn't blow past the plain version's parameter count.
The gate's sigmoid-shaped nonlinearity squashes values toward 0 or 1 — near-off or near-on — exactly the smooth switching behavior that lets a gated unit act like a soft, learned, per-dimension volume control rather than a rigid on/off rule.
What this means in practice
Feedforward blocks account for the bulk of a transformer's parameters and computation, so their design choices matter for both quality and cost. Gated linear unit variants (SwiGLU, GeGLU — named for which nonlinearity gates which projection) are now the standard choice in most modern large language models, replacing the plain ReLU feedforward of the original transformer, because they reliably improve downstream task performance at roughly matched parameter count, at the cost of a slightly more involved implementation with three matrices instead of two.
The feedforward block processes each token's vector independently, with the same weights reused at every position — it's where most of a transformer's parameters and per-token computation live. Gated linear units add a learned, per-dimension, per-token gate that multiplies into part of that computation, letting the network selectively suppress or amplify different intermediate features depending on the input, which empirically outperforms a plain fixed nonlinearity.
Practice
- Write out the plain feedforward equation and identify which matrix expands the dimension and which compresses it back.
- In the punctuation-token example, explain what it means for the gate value at a dimension to be close to 0.
- Why do gated feedforward variants typically use a narrower intermediate dimension than plain feedforward blocks at the same total parameter budget?
The common confusion is thinking the feedforward block does the "cross-token" work that attention already handles. It does the opposite: the feedforward block is applied to every position completely independently, with zero information exchange between tokens at this step — all cross-token mixing happens exclusively in attention. Mixing this up leads people to expect the feedforward block to somehow "know about" neighboring tokens, when in fact any token-to-token information it uses was already incorporated into that token's own vector by the attention sub-layer that ran immediately before it.
Related concepts
Practice in interviews
Further reading
- Vaswani et al., Attention Is All You Need (2017)
- Shazeer, GLU Variants Improve Transformer (2020)