Feed-Forward Network (FFN) Sub-Layer (Transformer)
The feed-forward network (FFN) sub-layer is the 2nd sub-layer of a typical Transformer layer.[1]
Definition
Let $\mathbf{X}\in\mathbb{R}^{T\times d}$, and let $\mathbf{W}_1\in\mathbb{R}^{d\times d_{ff}}$, $\vec b_1\in\mathbb{R}^{d_{ff}}$, $\mathbf{W}_2\in\mathbb{R}^{d_{ff}\times d}$, $\vec b_2\in\mathbb{R}^{d}$. The sub-layer is
$$ \operatorname{FFN}(\mathbf{X}) =f\left(\mathbf{X}\mathbf{W}_1+\mathbf{1}\vec b_1^{\top}\right)\mathbf{W}_2+\mathbf{1}\vec b_2^{\top}, $$
where $\mathbf{1}\in\mathbb{R}^{T}$ is a column vector of $1$s, so $\mathbf{1}\vec b_1^{\top}$ forms a $T \times d_{ff}$ matrix with the bias vector repeating over every row, and $f$ is an activation function.
Properties
Row-wise operation
Unlike self-attention, $\operatorname{FFN}$ is row-wise: row $t$ of the output depends on row $t$ of the input alone.
Compute
The compute for each row of $\mathbf{X}$ is dominated by $d\,d_{ff}$ multiplications with $\mathbf{W}_1$ and $d_{ff}\,d$ multiplications with $\mathbf{W}_2$ (since multiplications dominate over addition), hence
$$ \operatorname{FFN} \in O\left(T\,d\,d_{ff}\right) $$
where $O(x)$ is the big O notation.