What Is a Feed-Forward Network in an LLM? FFN and SwiGLU
By LatestLLM Editorial · Published · Facts verified · 9 min read
Also called: FFN, MLP, feed-forward layer, SwiGLU, GELU, activation function, GLU
Before this: Attention, Dimension
In one line
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.
Feed-forward network, simply and technically
In plain English
After attention has let each word gather information from the words around it, every word goes through a small private thinking step. This step is the feed-forward network. It takes the list of numbers that represents one word, stretches it into a much longer list, lets that longer list be reshaped by a simple rule that keeps some signals and suppresses others, and then squeezes it back to its original length.
No word looks at any other word during this step, which is the opposite of attention. It is still where much of a model's stored knowledge appears to sit: researchers found that these layers hold about two-thirds of the weights of a classic transformer and behave like a large memory of patterns picked up during training.
Think of it like this: If attention is the group meeting where everyone swaps notes, the feed-forward network is each person going back to their own desk to think about what they just heard. Every desk is identical and nobody talks to a neighbour while working at one. The picture is a little too tidy, because the contents of each desk are not rules written by hand but patterns the model absorbed during training.
Everyday example: A word thinks on its own
The model has just finished attention for the sentence “The capital of France is”, and its last word, “is”, now carries information about France and about capitals.
Input:
“is”, already enriched by attention
Output:
A refined list of numbers for “is”, the same length as before
What it shows: The feed-forward step looks only at that one enriched list of numbers. Researchers who study these layers find units that respond to patterns such as “a place is being asked for” and nudge the vector toward the kind of word that should come next. No other word is consulted during this step.
Technically
For a token vector x of width d, the classic feed-forward network is FFN(x) = W2 · act(W1 x + b1) + b2, where W1 widens d to d_ff and W2 narrows it back. The original transformer used d = 512 and d_ff = 2,048 with ReLU; GPT-2 small uses d = 768 and d_ff = 3,072 with GELU, a 4× expansion. Without the activation, two matrices would collapse into one, so the nonlinearity is what lets the layer compute more than a linear map.
Modern models use gated variants. In SwiGLU (Shazeer 2020) the layer computes (SiLU(x W_gate) ⊙ (x W_up)) W_down: one projection passes through the activation and acts as a gate on the other, giving three matrices instead of two. Gemma 4 31B has hidden size 5,376 and feed-forward width 21,504 (4×) with a gated GELU; gpt-oss uses clamped SwiGLU in experts of width 2,880; DeepSeek-V3's first three layers have a dense 18,432-wide network and later layers use experts of width 2,048. In mixture-of-experts models this is the block that is split into experts.
Technical example: How much of a layer is feed-forward?
Weights per layer are counted from published config.json values: GPT-2 small (two matrices, 4× width) and Gemma 4 31B (three matrices, local-layer attention shapes). Biases and norms are ignored.
Code (python):
# GPT-2 small: hidden 768, feed-forward 3072, 12 heads of 64
h, ffn, heads, hd = 768, 3072, 12, 64
attn = 4 * h * heads * hd # Q, K, V and output projections
mlp = 2 * h * ffn # up and down
print("GPT-2 small attention", attn, "feed-forward", mlp, "share", round(mlp / (attn + mlp), 3))
# Gemma 4 31B: hidden 5376, feed-forward 21504, 32 query heads, 16 KV heads of 256
h, ffn, heads, kv, hd = 5376, 21504, 32, 16, 256
attn = 2 * h * heads * hd + 2 * h * kv * hd # Q and O, then K and V
mlp = 3 * h * ffn # gate, up and down
print("Gemma 4 31B attention", attn, "feed-forward", mlp, "share", round(mlp / (attn + mlp), 3))
Output:
GPT-2 small attention 2359296 feed-forward 4718592 share 0.667
Gemma 4 31B attention 132120576 feed-forward 346816512 share 0.724
What it shows: In both models the feed-forward network holds well over half of a layer's weights: 67% in GPT-2 small, matching the two-thirds figure reported for transformers, and about 72% in Gemma 4 31B, where the gate adds a third matrix. Attention, despite getting most of the attention in explanations, is the smaller part.
Widen, transform, narrow
In plain English: The feed-forward network works in three moves. It first spreads a word's list of numbers out into a much longer list, which gives the model room to look at the word in many ways at once. It then applies a simple rule that keeps strong signals and mutes weak or negative ones. Finally it condenses the result back to the original length so the next layer can use it.
Technically:
With hidden width d and expansion width d_ff, the first matrix has d × d_ff weights and the second d_ff × d. Applying it to a sequence of n tokens is two matrix multiplications over an n × d matrix and costs the same per token regardless of position, which is why it parallelises trivially and scales linearly with sequence length, unlike attention.
Each of the d_ff hidden units can be read as a detector: its row in W1 is a pattern to look for in the token's vector, and its column in W2 is the change to make when the pattern is present. This reading is the basis of the key-value memory view described below.
What is an MLP?
A multilayer perceptron: two or more linear layers with a nonlinearity between them. The feed-forward network of a transformer is an MLP applied to each token.
Why widen first?
A wider hidden layer can hold more distinct patterns. The expansion is typically 4× the hidden size in classic models.
Activation functions: ReLU, GELU and SiLU
In plain English: The reshaping rule in the middle is called an activation function. The simplest one, ReLU, passes positive numbers through and replaces negative numbers with zero. Newer ones smooth the corner: they let small negative values leak through instead of cutting them off abruptly, which turns out to train slightly better.
Technically:
ReLU(x) = max(0, x). GELU(x) = x · Φ(x), where Φ is the standard normal cumulative distribution (Hendrycks and Gimpel 2016); GPT-2 and Gemma use its tanh approximation. SiLU, also called Swish with β = 1, is x · sigmoid(x) (Ramachandran et al. 2017). All three are near zero for large negative inputs and near x for large positive ones; they differ in the transition around zero, which the figure above plots.
Smooth activations have gradients almost everywhere and avoid ReLU's units that can get stuck at zero. Shazeer (2020) found that gated variants, which multiply two projections one of which goes through such a function, improve quality over plain ReLU or GELU in the feed-forward sublayers.
Gated feed-forward layers and SwiGLU
In plain English: Many recent models split the first step into two parallel paths. One path produces content and the other produces a gate that decides, number by number, how much content gets through. Multiplying the two lets the layer switch signals on and off smoothly.
Technically:
SwiGLU computes (SiLU(x W_gate) ⊙ (x W_up)) W_down with ⊙ the element-wise product. Compared with the classic layer it has one extra matrix, so many models narrow the hidden width to keep parameter counts comparable. gpt-oss uses a clamped SwiGLU in each expert; Gemma 4 uses the same gated structure with a tanh-GELU gate.
Because the gate multiplies the content, the network can learn near-zero outputs for most hidden units on most inputs. That sparsity is part of why mixture-of-experts models can drop whole groups of hidden units per token without losing much.
Go deeper: details for practitioners
- Clamping, as in gpt-oss, limits the gate and linear outputs to a fixed range to keep very large activations from destabilising training.
A memory of patterns
In plain English: When researchers look at which stored patterns light up for which text, the feed-forward layers look like a giant lookup table. Early layers respond to surface patterns such as particular phrases, and later layers respond to more abstract ones, and each pattern pushes the next-word guess toward words that tend to follow it.
Technically:
Geva et al. (2021) showed that feed-forward layers act as key-value memories: each key correlates with textual patterns in the training data and each value induces a distribution over output words, with lower layers capturing shallow patterns and upper layers more semantic ones. Together with the finding that these layers hold two-thirds of the parameters, it explains why they are the natural place to put mixture-of-experts routing.
The view is a useful model rather than a full explanation. Knowledge is distributed across many layers and attention heads, and editing a fact in a model by changing one feed-forward unit is not reliable in general.
Is a fact stored in one neuron?
Usually not. Facts appear spread across many units and layers, and a single unit often responds to several unrelated patterns.
Common misconceptions
Myth: The feed-forward network is where words look at each other.
Reality: Attention does that. The feed-forward network processes each token separately, with exactly the same weights for every position.
Myth: Most of a transformer's parameters are in attention.
Reality: In a classic layer about two-thirds of the weights are in the feed-forward network, and in mixture-of-experts models the experts dominate the total even more.
Myth: The activation function is a minor detail.
Reality: Without a nonlinearity the two matrices would collapse into one linear map. The specific choice, and gating, measurably changes quality.
Where you'll see this
- gpt-oss architecture: gpt-oss uses clamped SwiGLU inside each of its experts.
- Gemma 4 architecture: Gemma 4 uses a gated feed-forward layer with a tanh-GELU gate, 4× the hidden size in the dense models.
- DeepSeek-V3 architecture: DeepSeek-V3 starts with three dense 18,432-wide feed-forward layers and then switches to mixture-of-experts layers.
Key takeaways
- The feed-forward network is the per-token step of each transformer layer: widen the vector, apply a nonlinearity, narrow it back, with no communication between tokens.
- It holds most of a layer's weights: about 67% in GPT-2 small and 72% in Gemma 4 31B by a count from their configs.
- Gated variants such as SwiGLU use three matrices and a smooth gate; ReLU, GELU and SiLU differ mainly in how they treat negative inputs.
- In mixture-of-experts models the feed-forward block is the part that is replaced by many smaller experts.
Frequently asked questions
What does the feed-forward network do in a transformer?
It processes each token's vector separately after attention: a widening projection, a nonlinearity and a narrowing projection, adding the model's stored patterns to the token.
Why is it called feed-forward?
Information flows in one direction through it, from input to output, with no loops, and unlike attention it never mixes tokens.
What is SwiGLU?
A gated feed-forward design: one projection goes through the SiLU function and multiplies a second projection, and a third projection returns to the model's width.
How big is the feed-forward network compared with attention?
Larger. In GPT-2 small it holds 67% of a layer's weights by a count from its config, and about 72% in Gemma 4 31B.
What replaces the feed-forward network in mixture-of-experts models?
A set of smaller feed-forward networks called experts, plus a router that picks a few of them for each token.
Sources
Facts last verified November 23, 2026.