Reviewed: 2026-10 · Prerequisites: multi-head attention, residuals
Transformer diagrams often give attention the prominent position and reduce the FFN to a small box. That box usually holds a large share of the parameters and compute. Rather than reading the context again, it applies a nonlinear transformation to each position's existing representation.
We will use one token and column vectors, omitting batch dimensions, biases, and normalization.
Why expand and contract the representation?
In one lineWithout an activation, the two matrices collapse into one linear map at any width. The FFN works per position; context came in through attention.
A two-layer FFN is
$$ z=W_{\mathrm{up}}x,\qquad y=W_{\mathrm{down}}\phi(z), $$
with $x\in\mathbb R^d$, $W_{\mathrm{up}}\in\mathbb R^{h\times d}$, and $W_{\mathrm{down}}\in\mathbb R^{d\times h}$. Expansion creates intermediate features, the nonlinearity changes their combination, and the final projection returns to residual-stream width.
Without $\phi$, the matrices compose into one linear map. Expanding the hidden dimension alone does not produce nonlinear expressiveness.
Work it through with a one-dimensional input and two intermediate features. Let $W_{\mathrm{up}}=[1,\,-1]^\top$, $W_{\mathrm{down}}=[1,\ 1]$, and $\phi=\operatorname{ReLU}$:
| Input $x$ | $z=W_{\mathrm{up}}x$ | $\operatorname{ReLU}(z)$ | Output $y$ | Output without ReLU |
|---|---|---|---|---|
| 2 | [2, −2] | [2, 0] | 2 | 0 |
| −3 | [−3, 3] | [0, 3] | 3 | 0 |
With ReLU, this FFN computes $\lvert x\rvert$: one intermediate feature handles the positive half-line, the other the negative half-line. Without ReLU, $W_{\mathrm{down}}W_{\mathrm{up}}=1-1=0$, so every input maps to 0; other weights still collapse to a single line through the origin, $y=ax$, which cannot make the corner in $\lvert x\rvert$.
Tokens share FFN weights but do not directly exchange information inside an ordinary dense FFN. Earlier attention has already made their representations contextual. Position-wise does not mean context-free.
SwiGLU adds an input-dependent multiplicative branch
In one lineSwiGLU's gate scales the content branch element by element. It is not a probability, can be negative or above 1, and does not pick experts.
A bias-free formulation is
$$ g=W_gx,\quad u=W_ux,\quad h=\operatorname{SiLU}(g)\odot u,\quad y=W_dh, $$
$$ \operatorname{SiLU}(z)=z\sigma(z). $$
Two projections create a gate branch and a content branch, which interact elementwise. The gate is not a probability or necessarily between zero and one: SiLU can be negative or exceed one. It is not discrete expert selection.
GLU Variants compares several gated FFNs, including SwiGLU, under specific training settings and budgets. When trying one in your own model, check that those settings and compute budgets are comparable. Changing the activation alone is not enough to expect the same improvement.
Work through two intermediate features
In one lineA zero gate zeroes that output and the content gradient, but the gate itself still gets a gradient, so the feature can switch back on.
Isolate the gate with projected vectors $g=[0,1]$ and $u=[3,2]$:
| Dimension | Gate branch | SiLU | Content branch | Product |
|---|---|---|---|---|
| 1 | 0 | 0 | 3 | 0 |
| 2 | 1 | 0.7311 | 2 | 1.4621 |
With $W_d=[1,-1]$, the scalar output is approximately -1.4621. We use output dimension one for this calculation; an actual block returns to $d$.
import math
def silu(value):
return value / (1 + math.exp(-value))
gate = [0.0, 1.0]
content = [3.0, 2.0]
hidden = [silu(gate_value) * content_value
for gate_value, content_value in zip(gate, content)]
output = hidden[0] - hidden[1]
assert math.isclose(output, -1.4621171572600098)
A zero output in the first dimension does not permanently cut off learning. With upstream gradient $\delta=\partial\mathcal L/\partial h$ arriving at $h$,
$$ \frac{\partial\mathcal L}{\partial u} =\delta\odot\operatorname{SiLU}(g),\qquad \frac{\partial\mathcal L}{\partial g} =\delta\odot u\odot\operatorname{SiLU}'(g). $$
Since $\operatorname{SiLU}'(0)=1/2$, setting $u=3,\delta=1$ gives gate gradient 1.5. The content gradient is zero at this point, but the gate branch can still update. A zero in this forward pass does not mean the feature will remain switched off.
Compare three matrices at a matched budget
In one lineSwiGLU has three matrices; a hidden width of 8/3 d matches an ordinary 4d FFN in parameters. Report width, parameters and compute when comparing.
Ignoring biases, an ordinary FFN has $2dh$ parameters; SwiGLU has $3dh_g$. Matching an ordinary width of $4d$ gives
$$ 2d(4d)=3dh_g\quad\Rightarrow\quad h_g=\frac83d. $$
For $d=12$, ordinary width 48 and gated width 32 both give 1,152 parameters. Real implementations may round widths for hardware alignment.
Three matrices do not necessarily increase total parameters. Keeping the intermediate width unchanged, however, does cost more. Report width, parameter count, and compute when comparing structures, so extra compute is not mistaken for an architectural advantage.
What changes again when an FFN becomes MoE?
In one lineSwiGLU modulates feature channels continuously; an MoE router picks which experts see a token, and experts can use SwiGLU inside.
SwiGLU continuously modulates feature channels. An MoE router chooses which expert FFNs process a token; those experts can themselves use SwiGLU. These are different levels of selection.
| Mechanism | Operation | Main constraint |
|---|---|---|
| Attention | Combine information across positions | Length, cache, and IO |
| Dense FFN / SwiGLU | Transform channels at a position | Intermediate activations and matrix work |
| MoE routing | Assign tokens to selected experts | Load balance, communication, stability |
Continue with the MoE series. For an FFN implementation, check shapes and nonlinearity placement, use numerical gradients on tiny inputs, then compare task quality and runtime at a matched budget.