Fine-Grained Experts and Shared Experts in Mixture-of-Experts Models

    This article explains two design changes for Mixture-of-Experts (MoE) models: splitting experts into finer units and adding shared experts that every token uses. It covers why standard MoE designs run into redundancy, weak specialization, and unstable routing, how these two methods address the first two problems, and the trade-offs they introduce, including routing cost, hardware utilization, and communication overhead. Code examples show how to split an expert and how to combine shared and routed expert outputs.

    1. Background: Why Parameter Scale and Compute Cost Need to Be Decoupled

    In a standard dense Transformer, every token passes through all feed-forward network parameters. If the model’s parameter count doubles, the compute for each inference or training step roughly doubles too. Once a model reaches tens of billions of parameters, training cost and inference latency usually become the main bottlenecks.

    Mixture of Experts (MoE) offers a different approach. It splits the feed-forward network into multiple “experts,” and a router selects a small subset of them for each token. The total parameter count can be very large, but the compute each token uses depends only on the parameters that are activated. In this way, the total parameter count and the compute cost are partially decoupled.

    Early MoE approaches often ran into three problems in engineering practice:

    • Knowledge redundancy: Different experts all need to learn basic abilities such as grammar and common sense, so parameters are repeatedly used for the same knowledge.
    • Insufficient specialization: The boundaries between experts are blurry, making it hard for them to form clear divisions of labor.
    • Unstable routing: During training, some experts stay idle for long periods while others become overloaded, which leads to “expert collapse.”

    For the first two problems, two common directions for improvement are fine-grained expert segmentation and shared expert isolation. The following sections cover each one.

    2. Fine-Grained Expert Segmentation: Trading Many Small Experts for More Flexible Combinations

    2.1 Basic Approach

    The traditional approach sets up N relatively large experts, and each token selects K of them. Fine-grained segmentation splits each large expert into m parts along the intermediate dimension. The total number of experts becomes mN, and the number of experts each token selects becomes mK. Each small expert handles only 1/m of the original expert’s width, so the number of parameters and the compute activated per step stay roughly the same.

    2.2 Why It Works

    The number of possible expert combinations grows quickly as the number of experts increases. For example, suppose there are originally 16 experts and each token selects 2. The number of possible combinations is C(16,2) = 120. If each expert is split into 4 parts, there are 64 small experts, and each token selects 8. The number of combinations is C(64,8), about 4.4 billion. With a much larger combination space, different tokens can more easily find expert subsets that match their semantics, and the division of labor among experts becomes more precise.

    2.3 Costs and Considerations

    • The router’s scoring compute grows with the number of experts, so its overhead needs to be evaluated separately.
    • Smaller experts mean finer-grained matrix operations, which can lower hardware utilization. Batching and operator fusion are needed to compensate.
    • As the number of experts grows, communication and memory management become more complex.

    2.4 Example Implementation

    1. def split_ffn_expert(w_up, w_down, m):
    2. """Split one feed-forward expert into m smaller experts along the intermediate dimension.
    3. w_up: shape [hidden, inter]
    4. w_down: shape [inter, hidden]
    5. """
    6. inter = w_up.shape[1]
    7. assert inter % m == 0, "The intermediate dimension must be divisible by m"
    8. step = inter // m
    9. experts = []
    10. for i in range(m):
    11. s, e = i * step, (i + 1) * step
    12. experts.append((w_up[:, s:e], w_down[s:e, :]))
    13. return experts

    After splitting, the combined output of all small experts is mathematically equivalent to the original expert. The difference is that each small expert can be routed independently.

    3. Shared Expert Isolation: Storing General Knowledge in One Place

    3.1 Basic Approach

    In addition to the routed experts, the model includes a small number of shared experts. Every token passes through the shared experts regardless of routing results. The routed experts can then focus on knowledge from specific domains or patterns.

    3.2 Main Benefits

    • Less redundancy: General knowledge is concentrated in the shared experts, so routed experts do not each need to learn it again.
    • Better specialization: Routed experts cover a narrower range of knowledge, so their division of labor is clearer.
    • More stable training: The shared path provides a stable base output, which reduces the effect of routing fluctuations on the final result.

    3.3 Merging the Outputs

    1. def moe_forward(x, shared_experts, routed_experts, router, top_k):
    2. # Shared experts: every token goes through these
    3. shared_out = sum(expert(x) for expert in shared_experts)
    4. # Routed experts: select the top_k experts by gating scores
    5. scores = router(x) # shape [num_routed]
    6. top_idx = topk_indices(scores, top_k)
    7. weights = normalize(scores[top_idx]) # normalized weights
    8. routed_out = sum(w * routed_experts[i](x) for w, i in zip(weights, top_idx))
    9. # The sum of both parts is the feed-forward output of this layer. The residual connection is handled by the outer layer.
    10. return shared_out + routed_out

    4. Routing Mechanism and Load Balancing

    4.1 Gating and Top-K Selection

    The router computes a score for each routed expert for every token, selects the top K experts with the highest scores, and normalizes their weights. A common approach applies softmax first and then takes the top-k.

    4.2 Load Balancing Methods