The Core Role of Transformer Feed-Forward Networks and Engineering Practices
This article explains what the feed-forward network (FFN) does inside a Transformer block and how to engineer it in practice. It covers the division of labor between self-attention and the FFN, the expand-and-compress structure and its information bottleneck, the evolution of activation functions toward gated designs such as SwiGLU, parameter-count estimation and the choice of the intermediate dimension (4x versus 8/3x), and the parallelization and kernel-fusion techniques—tensor parallelism, expert parallelism, and operator fusion—that determine whether those parameters can be used efficiently.
1. Where the FFN Fits in the Overall Architecture
To understand the value of the feed-forward network (FFN), place it back inside the full Transformer block. A standard block has two sub-layers: self-attention and the feed-forward network. Each is wrapped with a residual connection and a normalization layer, so sub-layers stack as “sub-layer output + residual.”
Self-attention solves cross-position information aggregation: every position in the sequence pulls information from other positions based on relevance, and the output is a combined representation of all positions. The FFN solves per-position information processing: it takes the mixed representation produced by attention and applies the same set of nonlinear transformations to each position independently, with no further interaction between positions.
A pipeline is a useful analogy. The attention layer works like a sorting stage that gathers materials scattered across the line to the current station according to demand; the FFN works like a processing shop that cuts, melts, and shapes the collected material into structured intermediate products. The key point is that the lines inside the shop are independent of one another—how one token is processed does not affect any other token.
This division of labor is necessary from an expressiveness standpoint. Attention is essentially a weighted sum, which is a linear operation. If stacked layers kept only linear transformations, the whole network could still be collapsed into a single linear mapping, limiting both model capacity and nonlinear expressiveness. The FFN introduces per-position nonlinearity, allowing the network to fit complex functional relationships and providing a high-dimensional space for knowledge storage. The industry generally treats the FFN as the main component that carries factual knowledge and pattern mappings, while attention takes on routing and scheduling duties.
2. The Expand-Compress Structure and the Information Bottleneck
A classic FFN consists of two linear transformations with a nonlinear activation in between: the first maps the hidden dimension d to a larger intermediate dimension d_ff, and after activation the second maps it back to d. Expanding first and compressing afterward is equivalent to projecting the representation into a higher-dimensional space, making nonlinear decisions in that wider space, and then compressing back to the main dimension so the residual path keeps a consistent shape.
Expansion brings two direct benefits. First, the decision space is more relaxed—similar to making data linearly separable in a higher-dimensional space and then projecting back to the original one. Second, it provides capacity for knowledge encoding: many factual associations are distributed across the intermediate-layer weights in the form of approximate key-value mappings. Compression keeps the module stackable, keeps residuals aligned, and controls overall compute and memory-access cost.
Note that the FFN uses exactly the same weights for every position. This is similar to weight sharing in convolutions, except the scope is the entire representation vector rather than a local window. As a result, FFN parameters grow quadratically with the hidden dimension, making the FFN the largest parameter share within a single block—typically about two-thirds of the total.
3. Activation Function Evolution and Gating Mechanisms
The activation function determines the quality of the FFN’s nonlinearity. Early designs used ReLU, which is cheap to compute but suffers from dying neurons and truncation. Smooth approximations that followed mitigate this: they are differentiable around zero and do not zero out the negative range entirely, which tends to help optimization in deep networks. Gaussian error linear unit (GELU) style activations multiply the input by a Gaussian cumulative distribution, combining smoothness with a probabilistic interpretation; Swish-style activations introduce a learnable or fixed scaling coefficient, producing a self-gating effect.
The more mainstream approach in today’s large models is the gated linear unit structure, with SwiGLU as the typical representative: the expanded representation is split into two paths, one passed through an activation function to serve as a gate signal and the other kept linear, and the two are multiplied element-wise before compression. Compared with the two-layer structure, it adds one more set of expansion weights, but the gate provides stronger feature selection and often performs better under the same parameter budget.
Illustrative pseudocode:
# Classic two-layer FFNh = linear_up(x) # [B, S, d] -> [B, S, d_ff]h = activation(h)y = linear_down(h) # [B, S, d_ff] -> [B, S, d]# Gated structure (SwiGLU-style)gate = activation(linear_gate(x))up = linear_up(x)y = linear_down(gate * up)
One engineering caveat: the gated structure has more parameters, so the intermediate dimension must be reduced accordingly to keep the total parameter count unchanged.
4. Estimating Parameter Count and Choosing the Intermediate Dimension
With hidden dimension d and intermediate dimension d_ff, a classic layer has roughly 2·d·d_ff parameters, while a gated layer has roughly 3·d·d_ff. For d=4096 and d_ff=16384, a single classic FFN layer has about 134 million parameters and a gated one about 200 million, which shows how large a share it takes.
The intermediate dimension is commonly set to 4d. This ratio comes from empirical trade-offs: too small leaves insufficient nonlinear capacity, while too large yields diminishing returns as memory-access and communication costs rise. With gated structures, the industry often sets the intermediate dimension to about 8d/3 (≈2.67d) to match the parameter budget, so that 3·d·(8d/3)=8d² equals the classic 2·d·4d=8d². This explains why configuration tables show both 4x and 8/3x values: they are not arbitrary, but equivalent conversions under the same parameter budget. Actual selection should also account for sequence length, batch size, and the compute-to-bandwidth ratio of the hardware, so the intermediate layer does not become a memory-access bottleneck.
5. Parallel Sharding and Operator Fusion
Tensor parallelism is the most FFN-friendly form of parallelism. Because the FFN is two matrix multiplications in series, the first layer can be sharded by column and the second by row: each device completes expansion and activation locally, and a single reduction is performed after compression, so communication volume depends only on the hidden dimension. Gated structures require sharding both the gate and expansion paths in the same way to avoid extra communication.
Expert parallelism is another path: the FFN as a whole is replaced by multiple experts with identical structure, and each token activates only a few experts through routing, expanding the parameter scale without significantly increasing per-token compute. The engineering focus is load-balanced routing, capacity factor settings, and control of cross-device dispatch overhead, avoiding hotspot experts that skew device load.
Operator fusion is a single-machine optimization. If the expansion matrix multiplication, activation, element-wise multiplication, and compression matrix multiplication in the FFN each run independently, the activation tensors are read and written repeatedly and become bandwidth-bound. Fusing the activation and element-wise multiplication into the epilogue of the matrix multiplication substantially reduces intermediate tensors spilling to memory; combined with a suitable precision strategy and efficient memory layout, it improves actual throughput. In cloud deployments, general capabilities such as container platforms, distributed training frameworks, and monitoring and alerting can handle scaling and performance observation.
6. Summary
The FFN complements attention’s linear aggregation with per-position nonlinear processing, and it is the main body of model capacity and knowledge storage. The expand-compress structure together with activation and gating design determines its representational efficiency; 4x and 8/3x are simply different conversions under the same parameter budget; and tensor parallelism, expert parallelism, and operator fusion determine whether those parameters can be used efficiently. Understanding this chain from structure to hardware is what enables sound trade-offs in model design and deployment tuning.
