LLM Feed-Forward Networks: From Position-Wise Transforms to Gated Designs

    This article explains what feed-forward networks do inside Transformer layers, how a standard FFN expands and then projects hidden states, and why its parameter count is often large. It then contrasts scalar activations such as ReLU, GELU, and SiLU with gated designs such as GLU, GEGLU, and SwiGLU, derives the common intermediate width of about \(8d/3\), and outlines how to compare these choices fairly.

    1. What does an FFN do in a large model?

    In a typical Transformer layer, attention and the feed-forward network (FFN) have different jobs. Attention moves information across sequence positions: the current position updates its representation using other visible positions. The FFN reads a representation that already contains context and applies a nonlinear transform along its feature dimension. Both components are usually paired with residual connections and normalization, although the exact placement of normalization depends on the architecture.

    The key distinction is between an input that already contains context and an operator that itself interacts across positions. In the sentence “copy the file to the target directory,” attention can bring information from “file” and “directory” into the representation at “copy.” The FFN then transforms that fused vector. It does not reread other positions during its own computation. An FFN can therefore change its output according to context, but that context was supplied by earlier computation.

    In tensor form, if the input is (X\in\mathbb{R}^{B\times T\times d}), where (B) is the batch size, (T) is the sequence length, and (d) is the hidden size, one FFN layer applies the same function to all (B\times T) vectors. Positions are computed independently while sharing weights. In practice, the batch and sequence dimensions can be merged so the work is executed as batched matrix multiplication rather than a token-by-token loop.

    2. The standard FFN: expand, then apply a nonlinearity

    For a single position’s row vector (x\in\mathbb{R}^{1\times d}), the standard FFN is:

    [
    \operatorname{FFN}(x)=\phi(xW{\mathrm{up}}+b{\mathrm{up}})W{\mathrm{down}}+b{\mathrm{down}}
    ]

    Here, (W{\mathrm{up}}\in\mathbb{R}^{d\times m}) projects the representation to an intermediate width (m), (\phi) is an element-wise activation, and (W{\mathrm{down}}\in\mathbb{R}^{m\times d}) projects the result back to the hidden size (d). After returning to the original dimension, the FFN output can be added to the residual branch.

    Expansion gives one input more learnable feature channels, but expansion alone is not nonlinear. Without the activation, the two affine transforms can be merged into one affine transform. The activation is what lets different inputs follow different response patterns: tokens that share the same weights can still respond strongly on different channels.

    Why is the parameter count often large?

    Ignoring biases, the two projections in a standard FFN contain (2dm) weights. With the common width example (m=4d), that is (8d^2) weights. By comparison, a standard multi-head attention block whose query, key, value, and output projections are each (d\times d) uses (4d^2) weights across those four projections. Comparing only the FFN and the attention projections in this configuration, the FFN accounts for about two-thirds of their combined weights. That ratio does not apply directly to a whole model: embeddings, the output layer, and different attention projection designs all change the parameter mix.

    What does the “key–value memory” view mean?

    Ignoring biases, the standard FFN output can be written as a weighted sum of vectors:

    [
    \operatorname{FFN}(x)=\sum_{j=1}^{m}\phi(xk_j)\,v_j
    ]

    Here, (k_j) is column (j) of the up-projection and (v_j) is row (j) of the down-projection. After activation, the match between the input and (k_j) controls how much (v_j) contributes to the output. From this angle, an FFN resembles a token reading a set of learnable “keys” and “values.” That is only an interpretation of the computation: it is not explicit database retrieval, and it does not mean that each channel stores one independently addressable fact.

    3. From activation functions to gated structures

    ReLU, GELU, and Swish are scalar activations. GLU, GEGLU, and SwiGLU are gated structures with two projection branches. The two groups are often placed in the same comparison table, but they cannot all be implemented as a drop-in replacement of a single activation.

    ReLU: hard truncation of negative values

    [
    \operatorname{ReLU}(z)=\max(0,z)
    ]

    ReLU sets negative inputs to zero and keeps positive inputs unchanged. It is simple to compute and is a useful baseline for a non-gated FFN. It is not smooth at zero, and its gradient is zero on the negative side. Even if the activation tensor contains many zeros, ordinary dense matrix multiplication does not automatically skip those values, so activation sparsity should not be treated as inference speedup by itself.

    GELU: a smooth response that depends on input magnitude

    [
    \operatorname{GELU}(z)=z\Phi(z)
    ]

    where (\Phi) is the cumulative distribution function of the standard normal distribution. GELU multiplies the input by a weight that increases as the input grows, rather than cutting off at zero. A probabilistic intuition is that this is the expected value of keeping (z) with probability (\Phi(z)); the actual computation is deterministic and does not sample. A common hyperbolic-tangent approximation is:

    [
    \operatorname{GELU}(z)\approx
    \frac{z}{2}\left[1+\tanh\left(\sqrt{\frac{2}{\pi}}\left(z+0.044715z^3\right)\right)\right]
    ]

    Swish and SiLU: smooth scaling with a sigmoid

    [
    \operatorname{Swish}_{\beta}(z)=z\sigma(\beta z),\qquad
    \sigma(z)=\frac{1}{1+e^{-z}}
    ]

    (\beta) can be fixed, or made learnable in designs that support that. With (\beta=1), the result is the function usually called SiLU, (z\sigma(z)). Like GELU, SiLU lets some negative values through and does not change slope abruptly at zero. Placing SiLU inside a standard FFN still yields a two-projection structure; that is not the same as SwiGLU.

    GLU, GEGLU, and SwiGLU: multiplying two branches

    Ignoring biases, a gated FFN can be written as:

    [
    \operatorname{FFN}{\mathrm{gated}}(x)=
    \left[(xW
    {\mathrm{value}})\odot g(xW{\mathrm{gate}})\right]W{\mathrm{down}}
    ]

    Here (\odot) is element-wise multiplication. The value branch and the gate branch both start from the same input and produce intermediate vectors. Corresponding channels are multiplied, and the result is projected back down. Classical GLU uses a sigmoid for (g), GEGLU uses GELU, and the common SwiGLU uses Swish with its parameter fixed at 1, which is SiLU.

    The point of the two branches is that the signal a channel passes onward depends not only on the value projection, but also on a modulation computed from the same input by another set of weights. Only the sigmoid output naturally lies between 0 and 1. GELU and SiLU outputs can be negative or greater than 1. Calling them “gates” describes this modulating role; it does not mean they output probabilities or binary switches.

    4. Why is the SwiGLU intermediate width often about (8d/3)?

    Let the standard FFN intermediate width be (m) and the gated FFN width be (h). Ignoring biases, the standard form has two weight matrices and (2dm) parameters. The gated form has value, gate, and output projections, for (3dh) parameters. To keep the projection-parameter budgets similar:

    [
    3dh\approx 2dm
    \quad\Longrightarrow\quad
    h\approx \frac{2m}{3}
    ]

    Using (m=4d) as the baseline gives (h\approx 8d/3). So (8d/3) comes from converting width under a matched projection-parameter budget. It is not part of the definition of SwiGLU, and it is not guaranteed to be the best width for every model. Practical settings may also round (h) to a multiple that suits matrix multiplication and parallel partitioning, then recompute the budget from that rounded width.

    Similar parameter counts do not imply the same speed. At matched parameter counts, the theoretical multiply-add volume of the matrix multiplications is similar, but the gated form also performs an element-wise product and must handle both branches before the down-projection. The real cost depends on whether intermediate tensors are written back to memory, whether operators can be fused, and whether the matrix shapes suit the underlying kernels. In engineering practice, the two input projections can be concatenated into one larger matrix multiplication and then split by channel. That changes execution, not the gating formula.

    5. How should different FFNs be compared fairly?

    If the goal is to compare scalar activations, keep the same two-projection structure and width, and change only ReLU, GELU, or SiLU. If the goal is to compare a standard structure with a gated structure, the budget must be stated first. Fixing the width gives the gated version more projection parameters. Fixing the projection-parameter count requires a smaller intermediate width for the gated version. Both experiments are valid, but they answer different questions. Gains from extra capacity should not be attributed directly to gating.

    For language models, perplexity is a quantifiable quality metric. With natural logarithms, averaged over the same evaluation positions:

    [
    \log\operatorname{PPL}
    =-\frac{1}{N}\sum{i=1}^{N}\log p(x_i\mid x{<i})
    ]

    where (N) is the number of target tokens included in the evaluation. A lower value means the model assigns higher probability overall to those true next tokens. Comparisons should use the same evaluation data, tokenization, and scoring rules, and should control training data volume, training steps, and other architectural differences as far as possible. The original comparison curves are unavailable, so the gaps between options and the experimental conditions cannot be verified. No generally valid ranking of activation functions can be claimed from them.

    Deployment also needs a separate look at speed and resources. Prefill on long inputs and token-by-token decoding have different matrix shapes and memory-access patterns, so the same FFN can behave differently in the two stages. At the target batch size, sequence length, and numeric precision, record prefill throughput, per-token decoding latency, and peak memory, then judge a gated design together with validation loss.

    A practical selection can start from a clearly specified non-gated baseline, then build a SwiGLU counterpart with a similar projection-parameter count. First decide whether the comparison is about the activation, the structure, or the budget, then verify the gain on the target workload. That is more reliable than choosing an FFN design from a single perplexity ranking.