Feed-Forward Network (FFN) Sub-Layer (Transformer): Difference between revisions
AI-assisted (RonzzWikiCowriter) (via create-page on MediaWiki MCP Server) |
AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server) |
||
| Line 55: | Line 55: | ||
$$ | $$ | ||
The [[self-attention]] sub-layer of the same block holds $3d | The [[self-attention]] sub-layer of the same block holds $3d^{2}$ weights for $\mathbf{W}_Q,\mathbf{W}_K,\mathbf{W}_V$ and $d^{2}$ for the output projection, i.e. $4d^{2}$ whatever the number of heads: the $h$ heads share the width, $d_k=d/h$, so the $3h$ matrices of width $d_k$ hold | ||
$$ | |||
3h\,d\,d_k=3h\,d\,\frac{d}{h}=3d^{2} | |||
$$ | |||
weights in total. The FFN share of a block is therefore | |||
$$ | $$ | ||
\frac{\left|\theta_{\text{FFN}}\right|}{\left|\theta_{\text{attn}}\right|+\left|\theta_{\text{FFN}}\right|} | \frac{\left|\theta_{\text{FFN}}\right|}{\left|\theta_{\text{attn}}\right|+\left|\theta_{\text{FFN}}\right|} | ||
\approx\frac{8d^{2}}{8d^{2}+ | \approx\frac{8d^{2}}{8d^{2}+4d^{2}} | ||
= | =\frac{2}{3}. | ||
\ | |||
2 | |||
$$ | $$ | ||
For the base Transformer ($d=512$, $d_{ff}=2048 | For the base Transformer ($d=512$, $d_{ff}=2048$) the FFN holds | ||
$$ | $$ | ||
| Line 73: | Line 75: | ||
$$ | $$ | ||
parameters, against | |||
$$ | |||
4\cdot512^{2}+4\cdot512=1\,050\,624 | |||
$$ | |||
for attention, so 2/3 of the parameters of a block sit in its FFN. | |||
== Cost == | == Cost == | ||
Revision as of 10:11, 12 November 2026
The feed-forward network (FFN) sub-layer is the 2nd of the 2 sub-layers of a Transformer block: a 2-layer feedforward network applied to each position of the sequence separately, after self-attention.[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}) =\sigma\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 of 1s, so that $\mathbf{1}\vec b_1^{\top}$ repeats the bias on every row, and $\sigma$ is an activation function applied entrywise. The original model takes
$$ d_{ff}=4d, \qquad \sigma=\operatorname{ReLU}. $$
Position-wise operation
Row $t$ of the output depends on row $t$ of the input alone,
$$ \operatorname{FFN}(\mathbf{X})_t =\sigma\left(\vec x_t^{\top}\mathbf{W}_1+\vec b_1^{\top}\right)\mathbf{W}_2+\vec b_2^{\top}, $$
so 1 network is applied to all $T$ positions with shared weights. Permuting the positions permutes the output and changes nothing else:
$$ \begin{aligned} \operatorname{FFN}\left(\mathbf{P}\mathbf{X}\right) &=\sigma\left(\mathbf{P}\mathbf{X}\mathbf{W}_1+\mathbf{1}\vec b_1^{\top}\right)\mathbf{W}_2+\mathbf{1}\vec b_2^{\top}\\ &=\mathbf{P}\left(\sigma\left(\mathbf{X}\mathbf{W}_1+\mathbf{1}\vec b_1^{\top}\right)\mathbf{W}_2+\mathbf{1}\vec b_2^{\top}\right) =\mathbf{P}\operatorname{FFN}(\mathbf{X}), \end{aligned} $$
using $\mathbf{P}\mathbf{1}=\mathbf{1}$ and $\mathbf{P}\sigma(\cdot)=\sigma(\mathbf{P}\cdot)$ entrywise. Sharing also keeps the parameter count independent of $T$: a 1-token and a $10^{5}$-token sequence have the same $\theta$.
Parameter count
$$ \left|\theta_{\text{FFN}}\right| =d\,d_{ff}+d_{ff}+d_{ff}\,d+d =2dd_{ff}+d_{ff}+d, $$
which for $d_{ff}=4d$ is
$$ \left|\theta_{\text{FFN}}\right|=8d^{2}+5d . $$
The self-attention sub-layer of the same block holds $3d^{2}$ weights for $\mathbf{W}_Q,\mathbf{W}_K,\mathbf{W}_V$ and $d^{2}$ for the output projection, i.e. $4d^{2}$ whatever the number of heads: the $h$ heads share the width, $d_k=d/h$, so the $3h$ matrices of width $d_k$ hold
$$ 3h\,d\,d_k=3h\,d\,\frac{d}{h}=3d^{2} $$
weights in total. The FFN share of a block is therefore
$$ \frac{\left|\theta_{\text{FFN}}\right|}{\left|\theta_{\text{attn}}\right|+\left|\theta_{\text{FFN}}\right|} \approx\frac{8d^{2}}{8d^{2}+4d^{2}} =\frac{2}{3}. $$
For the base Transformer ($d=512$, $d_{ff}=2048$) the FFN holds
$$ 2\cdot512\cdot2048+2048+512=2\,099\,712 $$
parameters, against
$$ 4\cdot512^{2}+4\cdot512=1\,050\,624 $$
for attention, so 2/3 of the parameters of a block sit in its FFN.
Cost
Each position costs $d\,d_{ff}$ multiply-adds for $\mathbf{W}_1$ and $d_{ff}\,d$ for $\mathbf{W}_2$, hence
$$ \text{FFN: }2T\,d\,d_{ff} \qquad\text{versus}\qquad \text{attention: }2T^{2}d, $$
where the 2nd counts $\mathbf{Q}\mathbf{K}^{\top}$ and the weighted sum over $d$ channels in total across heads. The sub-layer therefore costs
$$ \mathcal{O}\left(T\,d\,d_{ff}\right)=\mathcal{O}\left(Td^{2}\right) $$
time, against $\mathcal{O}(T^{2}d)$ for attention, and the 2 terms cross at
$$ 2T\,d\,d_{ff}=2T^{2}d \iff T=d_{ff}. $$
With $d_{ff}=4d$ the FFN dominates for contexts shorter than $4d$ tokens (e.g. $16384$ tokens at $d=4096$) and attention dominates beyond it.
Placement in the block
With the Pre-LN arrangement of residual connections,
$$ \begin{cases} \mathbf{X}'=\mathbf{X}+\operatorname{Attn}\left(\operatorname{LN}\left(\mathbf{X}\right)\right),\\ \mathbf{X}''=\mathbf{X}'+\operatorname{FFN}\left(\operatorname{LN}\left(\mathbf{X}'\right)\right), \end{cases} $$
so a block mixes positions once, in the attention sub-layer, and transforms each position once, in the FFN sub-layer, both inside a residual connection and a normalisation.
Activation choice
- the rectifier $\operatorname{ReLU}(z)=\max(0,z)$, in the original model.
- the Gaussian error linear unit $\operatorname{GELU}(z)=z\,\Phi(z)=\frac{z}{2}\left(1+\operatorname{erf}\left(z/\sqrt{2}\right)\right)$, a smooth version of the rectifier.
A gated linear unit replaces the elementwise $\sigma$ by the product of 2 projections, of which 1 is gated,[2]
$$ \operatorname{GLU}(\mathbf{X}) =\left(\mathbf{X}\mathbf{W}_1+\mathbf{1}\vec b_1^{\top}\right) \odot\sigma\left(\mathbf{X}\mathbf{W}_3+\mathbf{1}\vec b_3^{\top}\right), \qquad \operatorname{FFN}_{\text{GLU}}(\mathbf{X})=\operatorname{GLU}(\mathbf{X})\,\mathbf{W}_2+\mathbf{1}\vec b_2^{\top}, $$
with ReLU, GELU or the sigmoid in place of $\sigma$ in the gate. Such a variant holds 3 matrices of size $d\times d_{ff}$ instead of 2, so it shrinks the hidden width by the same factor to match the parameter count of the ungated form,
$$ 3d\,d_{ff}'=2d\cdot4d \iff d_{ff}'=\frac{2}{3}\cdot4d . $$
At matched parameter count, the gated variants improve over ReLU and GELU in the FFN sub-layer of the Transformer.[2]