Feed-forward network (FFN) is the position-wise multi-layer perceptron that, together with self-attention, makes up each block of a Transformer. Where attention moves information between positions, the feed-forward network processes every token independently through the same pair of linear transformations, and it holds the majority of a modern large language model's parameters.1

Role in the network

Each Transformer block alternates two sublayers: an attention sublayer that mixes information across positions, and a feed-forward sublayer that transforms the representation at every position in isolation. The same two weight matrices are applied to every token, so the layer parallelizes trivially across the sequence, and no information flows between positions inside it. Like every sublayer, the FFN sits inside a residual connection followed by normalization, and the 2017 design applied dropout to each sublayer output.1

Because the FFN's hidden dimension is several times wider than the model dimension, it dwarfs the attention projections in parameter count: in the standard configuration roughly two thirds of a block's parameters sit in the feed-forward layers, which is why they became the main target of sparsity schemes such as mixture-of-experts and of weight-compression methods generally.5

The mathematics

The original Transformer applied, independently at every position, a linear expansion from the model dimension to a hidden dimension four times larger, a ReLU nonlinearity, and a projection back to the model dimension.1 With \(x \in \mathbb{R}^{d_{\text{model}}}\) the sublayer input at one position:

$$\mathrm{FFN}(x) = W_2\,\sigma(W_1 x + b_1) + b_2$$
Symbol Shape Meaning
\(x\) \(d_{\text{model}}\) input representation at one position
\(W_1, b_1\) \((d_{\text{ff}} \times d_{\text{model}})\), \(d_{\text{ff}}\) expansion projection and bias
\(\sigma\) applied elementwise nonlinearity (ReLU in 2017; GELU or SwiGLU later)
\(W_2, b_2\) \((d_{\text{model}} \times d_{\text{ff}})\), \(d_{\text{model}}\) contraction projection and bias
\(d_{\text{ff}}\) scalar hidden width; \(4\,d_{\text{model}}\) in Vaswani et al.

The nonlinearities that have shipped in language models are, in order of adoption:

$$\mathrm{ReLU}(z) = \max(0, z), \qquad \mathrm{GELU}(z) = z\,\Phi(z) \approx 0.5z\left(1 + \tanh\!\sqrt{\tfrac{2}{\pi}}\,(z + 0.044715\,z^3)\right), \qquad \mathrm{Swish}_{\beta}(z) = z\,\mathrm{sigmoid}(\beta z)$$

where \(\Phi\) is the standard normal cumulative distribution function. GELU, introduced by Hendrycks and Gimpel in 2016, weights inputs by their Gaussian percentile rather than thresholding them;8 Swish, found by automated search in 2017, is the smooth \(\beta\)-parameterized analogue.10

The gated form that dominates open-weight models since 2023 replaces the plain nonlinearity with an elementwise product of two projections, one of them gated:

$$\mathrm{FFN}_{\mathrm{SwiGLU}}(x) = W_2\left(\mathrm{Swish}_{1}(W_1 x) \odot W_g x\right)$$

with \(W_g\) a third learned matrix of the same shape as \(W_1\) and \(\odot\) the elementwise product. The gating branch multiplies the activation coordinate-wise, so each hidden unit's output is modulated by a learned function of the same input rather than passing through a fixed curve. Because the layer now carries three matrices, the hidden width is typically shrunk to keep the parameter budget comparable: LLaMA uses \(d_{\text{model}} = 4096\) and \(d_{\text{ff}} = 11008\), about \(2.7\,d_{\text{model}}\) rather than 4.6

Worked example: parameter share

For one LLaMA-class layer with \(d_{\text{model}} = 4096\) and \(d_{\text{ff}} = 11008\), the three SwiGLU matrices contribute \(3 \times 4096 \times 11008 \approx 1.35 \times 10^{8}\) parameters. Across 32 layers that is roughly \(4.3 \times 10^{9}\) of the model's \(6.7 \times 10^{9}\) parameters, near two thirds, matching the classic dense-Transformer split in which the feed-forward layers hold the bulk of the weights.6

Worked example: one token forward

Take \(d_{\text{model}} = 2\), \(d_{\text{ff}} = 3\), ReLU, zero biases, and

$$x = \begin{pmatrix} 1 \\ -1 \end{pmatrix}, \quad W_1 = \begin{pmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{pmatrix}, \quad W_2 = \begin{pmatrix} 1 & 1 & 0 \\ 0 & 1 & 1 \end{pmatrix}$$

The hidden pre-activation is \(W_1 x = (1,\, -1,\, 2)\); ReLU zeroes the negative coordinate to give \(h = (1,\, 0,\, 2)\); and the output is \(W_2 h = (1,\, 2)\). Every position in the sequence passes through exactly these two matrix multiplies, which is the sense in which the layer is position-wise.

Intuition

Attention decides where information travels; the FFN is where most per-token computation happens. The expansion to a wider hidden space lets the network represent each token as a large collection of independently switchable features, compute on them in parallel, and write the result back into the residual stream. Two-layer networks of this form are universal function approximators in the limit of width, which is the classical justification for the expand-contract shape.11

Interpretability work has read the two matrices as key-value memories: rows of the first matrix detect patterns in the input, and columns of the second write vocabulary-space associations back out, an analysis that later motivated knowledge-editing methods targeting specific FFN weights.4 The reading is not complete (activations superpose features rather than storing them one per neuron), but it explains why the FFN, and not attention, is where much factual recall appears to live.4

Activation variants

The GPT line replaced ReLU with GELU starting with GPT-1 in 2018, and encoder models such as BERT followed.2 The dominant choice since 2023 is the gated SwiGLU family, a descendant of the gated linear unit introduced by Dauphin et al. for convolutional language modeling:9 Shazeer's comparison of gated linear unit variants reported consistent gains from gating, and LLaMA's 2023 adoption of SwiGLU, alongside RMSNorm and rotary position embeddings, standardized it across open-weight models.36

Sparse feed-forward layers

The sparsely-gated mixture-of-experts layer of Shazeer et al. (2017) replaces the dense FFN with many expert FFNs and a learned router that activates a few of them per token, decoupling parameter count from per-token compute.5 After Mixtral 8x7B and DeepSeek-V3 demonstrated the design at frontier scale, MoE feed-forward layers became the default in large open models, with dense FFNs retained mainly in smaller ones. The routing arithmetic and its load-balancing losses are treated at mixture of experts.

Adoption

The dense FFN is universal in deployed Transformers; what varies is the nonlinearity and width. ReLU survives chiefly in 2017-era reproductions; GELU is standard in the GPT and BERT lines and their descendants; SwiGLU is the default in open-weight families including Llama, Mistral, and Qwen. Sparse expert FFNs ship in Mixtral, the DeepSeek line, and, reportedly, most frontier models of 2025 and 2026.5

Limitations and critiques

The layer's strength is also its cost. Holding most of a model's parameters, the FFN dominates weight memory and, at roughly \(4\,d_{\text{model}}\,d_{\text{ff}}\) multiply-adds per token for a dense SwiGLU layer, a large share of inference FLOPs; this is the economic argument behind expert sparsity, and it makes FFN weights the first target of quantization and pruning pipelines. The key-value memory reading cuts both ways: if FFN rows store factual associations, they also store memorized training text, which is where much of the privacy and copyright exposure of a trained model is believed to sit.4 Finally, the classic ReLU form suffers dead units that never activate after a large negative bias update, one of the practical motivations for the smooth GELU and Swish replacements.8

History

Multi-layer perceptrons of the expand-contract form became the standard trainable architecture after Rumelhart, Hinton, and Williams popularized backpropagation in 1986.7 Their appointment as one half of the Transformer block dates to Vaswani et al. in June 2017, who paired each attention sublayer with a position-wise ReLU network of hidden width \(4\,d_{\text{model}}\).1 The gated-linear-unit idea arrived the same year from convolutional sequence modeling,9 GELU slightly earlier,8 and the sparsely-gated expert construction in January 2017.5 Shazeer's 2020 ablation made gated FFNs the stronger default,3 Geva et al.'s 2020 key-value analysis gave the layer its interpretability research program,4 and the 2023 LLaMA stack (SwiGLU, RMSNorm, rotary embeddings) fixed the configuration that open-weight models have largely kept since.6

See also

References


  1. Vaswani, A., et al., "Attention Is All You Need," arXiv:1706.03762, June 2017. ↩↩↩↩

  2. Radford, A., et al., "Improving Language Understanding by Generative Pre-Training," OpenAI, June 2018. ↩

  3. Shazeer, N., "GLU Variants Improve Transformer," arXiv:2002.05202, February 2020. ↩↩

  4. Geva, M., Schuster, R., Berant, J., and Levy, O., "Transformer Feed-Forward Layers Are Key-Value Memories," arXiv:2012.14913, December 2020. ↩↩↩↩

  5. Shazeer, N., et al., "Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer," arXiv:1701.06538, January 2017. ↩↩↩↩

  6. Touvron, H., et al., "LLaMA: Open and Efficient Foundation Language Models," arXiv:2302.13971, February 2023. ↩↩↩↩

  7. Rumelhart, D. E., Hinton, G. E., and Williams, R. J., "Learning representations by back-propagating errors," Nature 323, October 1986. ↩

  8. Hendrycks, D., and Gimpel, K., "Gaussian Error Linear Units (GELUs)," arXiv:1606.08415, June 2016. ↩↩↩

  9. Dauphin, Y. N., Fan, A., Auli, M., and Grangier, D., "Language Modeling with Gated Convolutional Networks," arXiv:1612.08083, December 2016. ↩↩

  10. Ramachandran, P., Zoph, B., and Le, Q. V., "Searching for Activation Functions," arXiv:1710.05941, October 2017. ↩

  11. Hornik, K., Stinchcombe, M., and White, H., "Multilayer feedforward networks are universal approximators," Neural Networks 2(5), 1989. ↩