Functions and operators

FFN⁡(x)=max⁡(0,xW1+b1)W2+b2\operatorname{FFN}(\mathbf{x}) = \max(0, \mathbf{x} W_1 + \mathbf{b}_1) W_2 + \mathbf{b}_2 Feed-forward network (FFN, MLP)

A small network applied to each position: a linear layer, an activation, and another linear layer.

In ML papers

FFN⁡\operatorname{FFN} or MLP⁡\operatorname{MLP} in a formula stands for a whole sub-network, usually defined once in the paper: two linear layers with an activation between them. In the Transformer (Vaswani et al. 2017) it is max⁡(0,xW1+b1)W2+b2\max(0, \mathbf{x} W_1 + \mathbf{b}_1) W_2 + \mathbf{b}_2, applied to each token's vector separately and identically.

The inner width is usually about 4 times the model width (dmodel=512d_{\text{model}} = 512, dff=2048d_{ff} = 2048 in the original). Newer models swap ReLU for GELU or use a gated form such as SwiGLU (SiLU / Swish (and SwiGLU)). When a paper writes MLP⁡\operatorname{MLP} without defining it, assume this shape with some activation.

How to type it

LaTeXShows
\operatorname{FFN}(\mathbf{x})FFN⁡(x)\operatorname{FFN}(\mathbf{x})
\operatorname{MLP}(\mathbf{x})MLP⁡(x)\operatorname{MLP}(\mathbf{x})

A worked example

x∈R512  →  R2048  →  R512\mathbf{x} \in \mathbb{R}^{512} \;\to\; \mathbb{R}^{2048} \;\to\; \mathbb{R}^{512}

Each token goes up to 2048 features, through the activation, and back down to 512.

Common confusions

Related

ReLU⁡(x)=max⁡(0,x)\operatorname{ReLU}(x) = \max(0, x)ReLUW∈Rm×n\mathbf{W} \in \mathbb{R}^{m \times n}Bold uppercase (matrix)SiLU⁡(x)=x σ(x)\operatorname{SiLU}(x) = x \, \sigma(x)SiLU / Swish (and SwiGLU)X∈RB×T×dX \in \mathbb{R}^{B \times T \times d}Tensor shapes

Decode this example →