Expert Networks › Experts as Feed-Forward Networks
Language AI HandbookAll chapters Expert Networks › Experts as Feed-Forward NetworksOne section · Sections in this chapter3 / 12 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The most common implementation uses standard feed-forward networks as experts. This choice is not arbitrary; it stems from a basic property of transformer FFN layers. Understanding this property reveals why FFN layers are uniquely suited for expert-based sparsification, and why attention layers, by contrast, are almost never sparsified through expert routing.
Think of the FFN layer in a transformer as a per-token function applier. It takes each token's vector representation, runs it through a two-layer nonlinear network, and returns a transformed vector, completely ignoring all other token positions. This is sometimes called "position-wise" processing, because the same function is applied independently at each position. The attention mechanism, which comes before the FFN in a standard transformer block, handles the cross-position communication: it allows token representations to incorporate information from neighboring positions. Once that communication has occurred, the FFN's job is to transform each resulting representation locally, without any further inter-token interaction.
This position-independence property is exactly what makes FFN layers good candidates for sparsification. If token at position 3 is processed entirely independently of token at position 7 during the FFN phase, then there is no reason these two tokens must use the same FFN weights. We can send token 3 to expert and token 7 to expert , and neither computation interferes with the other. The same logic does not apply to attention: because attention computes a weighted sum over all positions simultaneously, you cannot route different positions to different "attention experts" without fundamentally changing the information flow. FFN layers are structurally decoupled from one another across positions, and that decoupling is the key technical reason they become the natural home for MoE routing.
The Position-Independence Property
Recall that the feed-forward network in a transformer block operates identically on each position in the sequence, processing each token in isolation. This property, sometimes called "position-wise" or "token-wise" processing, means that the FFN applies the exact same transformation to every token without any awareness of neighboring tokens. The computation for any single token is self-contained. Mathematically, the FFN applies a point-wise transformation:
We can write this transformation for a single token's vector as:
where:
- : the input vector corresponding to a specific token position, with dimension
- : the weight matrix for the first linear transformation, mapping from to
- : the bias vector for the first linear layer, with dimension
- : the projection into the intermediate expansion dimension, before any nonlinearity
- : the non-linear activation function, typically ReLU or GELU, applied element-wise
- : the activated intermediate representation, with each dimension independently passed through the nonlinearity
- : the weight matrix for the second linear transformation, mapping from back to
- : the bias vector for the output layer, with dimension
Let's trace through this computation to build intuition. The input vector first undergoes a linear transformation through , which projects it from the model dimension into a higher-dimensional intermediate space (typically four times larger). This expansion gives the network room to represent more complex combinations of the input features: in a model with a standard four-times expansion, the intermediate representation has 4096 dimensions. The bias term shifts this projection, and the activation function introduces nonlinearity by selectively suppressing or passing through different components of the intermediate representation. The choice of activation matters here: ReLU zeroes out negative values entirely, creating hard sparsity in the intermediate activations, while GELU provides a smoother transition that most modern models prefer. Finally, projects the result back to the original dimensionality, with giving a final bias adjustment.
Why does this formula make sense? Notice that the two-layer structure is the minimal architecture that can represent arbitrary nonlinear functions given enough hidden units, a result dating back to the universal approximation theorem. The expansion-then-contraction shape (wide in the middle, narrow at the edges) is a practical implementation of this principle: the wide middle layer provides the capacity for complex representations, and the contraction back to the model dimension ensures compatibility with the residual stream. The position-independence falls directly out of the fact that , , , and are shared parameters applied identically to each position: nowhere in this formula does the position index appear.
This position-independence is important for MoE. Because each token passes through the FFN independently, we can route different tokens to different FFN-style experts without disrupting the computational flow. If the FFN for token at position 3 has no interaction with the FFN for token at position 7, then there is no reason they must use the same FFN weights. Attention layers, by contrast, require all positions to interact simultaneously through the query-key-value mechanism. This makes them poor candidates for expert-based sparsification.
Expert FFN Architecture
Each expert in an MoE layer typically mirrors the standard transformer FFN structure, but with its own independent set of parameters. Building on the gated architectures covered in Part XV, most modern expert implementations use the SwiGLU variant. This architecture splits the input into two parallel paths: one for gating and one for features. The gate acts as a dynamic filter that modulates which information from the feature path is allowed to flow through to the output projection. This gating mechanism is inspired by long short-term memory (LSTM) cells, where gates selectively retain or forget information, but applied to the feed-forward context rather than recurrence.
To understand the motivation for gated FFNs over the simpler two-layer variant, consider what happens with ReLU activation. The ReLU activation sets negative intermediate values to zero, effectively performing hard binary gating: a neuron either fully activates or is entirely silent. SwiGLU generalizes this: instead of a fixed zero threshold, the network learns a soft, continuous gate for each dimension based on the current input. This learned gating makes the expert's behavior more adaptive and context-sensitive, which is particularly valuable in the sparse MoE setting where each expert sees only a subset of the data distribution and must be maximally expressive within its assigned domain.
The SwiGLU expert architecture computes its output through the following sequence of operations. The input token representation is projected into a higher-dimensional space along two parallel paths, and their element-wise product forms the pre-output representation:
where:
- : the input token representation vector, with dimension
- : the gating projection weights for expert , with shape
- : the feature up-projection weights for expert , with the same shape
- : the Sigmoid Linear Unit activation function, defined as where is the sigmoid function
- : the gating mask values computed by the first path, with dimension
- : the candidate features computed by the second path, also with dimension
- : the element-wise (Hadamard) multiplication operator, which filters features using the gate values
- : the down-projection weights for expert , with shape , transforming back to the model dimension
To understand why this gated structure is valuable, consider what each component contributes. The up-projection generates candidate features by expanding the input into a higher-dimensional space where the model can represent more complex patterns. Meanwhile, the gating path computes a vector of values that can range smoothly above and below zero (since SiLU is not bounded like sigmoid), acting as a soft, content-dependent filter. Each element of this gating vector indicates how much the corresponding candidate feature should be emphasized or suppressed.
The element-wise product () then filters the candidates with this gate, letting the network suppress or preserve specific features before the final projection . This gating mechanism gives the expert fine-grained, input-dependent control over which information flows through. Some dimensions might be nearly silenced (gate value close to zero) while others pass through strongly (large positive gate value); the decision is dynamic for each input rather than fixed by the weight matrices alone. This selective filtering allows experts to develop specialized behaviors for different input types during training.
Why does this formula make sense? Notice that the two separate projections and serve entirely different purposes despite having the same shape. The gate path learns to recognize which features in the input are relevant for the current transformation, while the up path learns what the actual feature values should be. The product of these two paths means that only features deemed relevant by the gate path are allowed to influence the output. This is fundamentally more expressive than a simple two-layer FFN where the activation function applies the same threshold rule to every input uniformly.
The key parameters defining an expert's architecture include:
- Input/output dimension (): Matches the transformer's hidden dimension, typically 2048 to 8192 in large models. This must be identical for all experts since they all receive the same token representations from the attention layer.
- Intermediate dimension (): The expansion dimension within the expert. For standard FFNs this is often . For SwiGLU variants, a common choice is , which preserves the parameter count of the original FFN despite having three weight matrices instead of two (since each of the three matrices is proportionally smaller).
- Activation function: SiLU (also called Swish) for gated variants, or GELU or ReLU for standard two-layer FFNs.
Each expert maintains its own weight matrices, completely independent of other experts. This independence is what enables specialization: expert can learn entirely different transformations than expert . During training, the gradient updates for each expert depend only on the tokens routed to that expert, letting different experts to develop distinct computational roles. If expert only ever sees tokens from formal academic writing, its weights will be pushed by gradients shaped entirely by that sub-distribution, naturally developing transformations that work well for that register.
Parameter Counting
To evaluate the memory footprint of an MoE layer, we sum the parameters across all experts. Understanding this calculation helps you anticipate memory requirements and make informed architectural decisions. The math is straightforward, but tracing through it carefully reveals the core efficiency tradeoff of MoE.
Consider an MoE layer with experts, where each expert is a standard two-layer FFN with intermediate dimension . For a single expert, the parameter count breaks down into the two weight matrices and their associated bias vectors. We can compute this systematically:
where:
- : the total parameter count for a single expert
- : the parameter count for the weight matrices alone
- : the parameter count for the bias vectors
- : the hidden dimension of the model
- : the intermediate dimension inside the expert FFN
- : parameters for the two weight matrices ( and ), each of size
- : parameters for the bias vectors ( of size and of size )
Let's trace through why the weight matrix formula has the factor of 2. The first weight matrix has shape , mapping from the model dimension to the intermediate dimension, contributing parameters. The second weight matrix has shape , mapping back from the intermediate dimension to the model dimension, contributing another parameters. Together, these two matrices account for the term. The bias vectors add a relatively small number of additional parameters: for and for .
For experts, the total MoE layer parameters are approximately . A model with 8 experts in each MoE layer has roughly 8 times the FFN parameters of an equivalent dense model. However, if only 2 experts activate per token (the "top-2" routing scheme used by Mixtral), the compute per forward pass remains comparable to a dense model with 2 times the FFN size, which is still much less than 8 times. This is the basic tradeoff of MoE: we store many more parameters than we use for any single input, gaining model capacity without proportional computational cost. The price we pay is in memory, not arithmetic.
