Sparse Models › Efficiency Analysis: Decoupling Parameters from Compute

Michael BrenndoerferUpdated December 31, 20259 min read
Language AI HandbookAll chapters
Part 33 of 67Mixture of Experts
Chapter 239Sparse Models › Efficiency Analysis: Decoupling Parameters from Compute
Reading section 4 of 10 · Efficiency Analysis: Decoupling Parameters from Compute · 9 minSection 4 of 10 · 9 min

Book contents

Section 4 of 10 · 9 min read

Sparse Models › Efficiency Analysis: Decoupling Parameters from ComputeOne section · Sections in this chapter4 / 10 ↓
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 power of sparse models becomes clear when we quantify the efficiency gains. We can compare a dense model to a sparse model with equivalent computational cost to see the capacity advantage. This analysis reveals the mathematical foundation for why sparse models can reach better performance per unit of compute than their dense counterparts.

Consider a dense FFN with intermediate dimension dffd_{\text{ff}}. This layer has two weight matrices: W1∈Rdff×dmodelW_1 \in \mathbb{R}^{d_{\text{ff}} \times d_{\text{model}}} and W2∈Rdmodel×dffW_2 \in \mathbb{R}^{d_{\text{model}} \times d_{\text{ff}}}. These matrices define the expansion into a higher-dimensional space and the subsequent projection back to the model dimension.

The dense FFN has the following computational characteristics:

  • Total parameters: 2×dmodel×dff2 \times d_{\text{model}} \times d_{\text{ff}}. This count comes from two weight matrices: W1∈Rdff×dmodelW_1 \in \mathbb{R}^{d_{\text{ff}} \times d_{\text{model}}} (containing dff×dmodeld_{\text{ff}} \times d_{\text{model}} parameters) and W2∈Rdmodel×dffW_2 \in \mathbb{R}^{d_{\text{model}} \times d_{\text{ff}}} (containing dmodel×dffd_{\text{model}} \times d_{\text{ff}} parameters)
  • FLOPs per token: 4×dmodel×dff4 \times d_{\text{model}} \times d_{\text{ff}}, because each of the two matrix multiplications requires approximately 2×dmodel×dff2 \times d_{\text{model}} \times d_{\text{ff}} FLOPs (standard approximation that a matrix multiplication of dimensions m×nm \times n by n×pn \times p requires 2mnp2mnp FLOPs).

where:

  • dmodeld_{\text{model}}: the dimension of the model's hidden representations (the size of token embeddings throughout the network)
  • dffd_{\text{ff}}: the intermediate dimension of the feed-forward network, typically set to 4×dmodel4 \times d_{\text{model}}
  • 2mnp2mnp: the FLOP count for matrix multiplication comes from computing m×pm \times p output elements, each requiring nn multiplications and nn additions, giving approximately 2mnp2mnp FLOPs

Now consider a sparse layer with NN experts, where each expert has intermediate dimension dff/Nd_{\text{ff}}/N, using top-2 routing. This configuration divides the total parameters across multiple smaller experts, each specialized for different types of inputs. The top-2 routing means each token will be processed by exactly two experts, combining their outputs with learned weights.

For this sparse configuration, we can compute the total parameter count, active parameters per token, and FLOPs per token. These derivations illuminate exactly where the efficiency gains originate.

Total parameters: Each expert is a smaller FFN with intermediate dimension dff/Nd_{\text{ff}}/N instead of the full dffd_{\text{ff}}. Since each expert has two weight matrices, it contains 2×dmodel×(dff/N)2 \times d_{\text{model}} \times (d_{\text{ff}}/N) parameters. The expert's reduced size means it specializes in a narrower range of computations, but collectively the experts cover the same representational space as the original dense layer. Summing across all NN experts:

Total sparse parameters=N×2×dmodel×(dff/N)(sum over all experts)=2×dmodel×dff×NN(rearrange)=2×dmodel×dff(cancel N)\begin{aligned} \text{Total sparse parameters} &= N \times 2 \times d_{\text{model}} \times (d_{\text{ff}}/N) && \text{(sum over all experts)} \\ &= 2 \times d_{\text{model}} \times d_{\text{ff}} \times \frac{N}{N} && \text{(rearrange)} \\ &= 2 \times d_{\text{model}} \times d_{\text{ff}} && (\text{cancel } N) \end{aligned}

This matches the dense model exactly, since the NN terms cancel. This result is important because it shows that we haven't reduced capacity: the total number of learnable parameters remains the same. The parameters are simply distributed across multiple experts rather than concentrated in a single network.

Active parameters per token: The key efficiency gain comes from activating only a subset of experts. With top-2 routing, only 2 out of NN experts activate for any given token. Each active expert contributes its 2×dmodel×(dff/N)2 \times d_{\text{model}} \times (d_{\text{ff}}/N) parameters to the computation. This selective activation is the heart of sparse efficiency:

Active parameters=top-k×2×dmodel×(dff/N)(k active experts)=2×2×dmodel×(dff/N)(substitute top-k = 2)=4×dmodel×dffN(simplify)\begin{aligned} \text{Active parameters} &= \text{top-k} \times 2 \times d_{\text{model}} \times (d_{\text{ff}}/N) && \text{(k active experts)} \\ &= 2 \times 2 \times d_{\text{model}} \times (d_{\text{ff}}/N) && \text{(substitute top-k = 2)} \\ &= \frac{4 \times d_{\text{model}} \times d_{\text{ff}}}{N} && \text{(simplify)} \end{aligned}

FLOPs per token: The computational cost follows the active parameters, since we only perform computations for the experts that are activated. Each active expert must perform two matrix multiplications (expansion and contraction), costing 4×dmodel×(dff/N)4 \times d_{\text{model}} \times (d_{\text{ff}}/N) FLOPs. With 2 active experts:

Sparse FLOPs=top-k×4×dmodel×(dff/N)(k active experts, each costs 4×dmodel×(dff/N) FLOPs)=2×4×dmodel×(dff/N)(substitute top-k = 2)=8×dmodel×dffN(simplify)\begin{aligned} \text{Sparse FLOPs} &= \text{top-k} \times 4 \times d_{\text{model}} \times (d_{\text{ff}}/N) && \text{(k active experts, each costs } 4 \times d_{\text{model}} \times (d_{\text{ff}}/N) \text{ FLOPs)} \\ &= 2 \times 4 \times d_{\text{model}} \times (d_{\text{ff}}/N) && \text{(substitute top-k = 2)} \\ &= \frac{8 \times d_{\text{model}} \times d_{\text{ff}}}{N} && \text{(simplify)} \end{aligned}

With N=8N = 8 experts, the sparse model uses only 1/41/4 the FLOPs per token while maintaining the same total parameter count. This is a remarkable result: we get the same capacity at a fraction of the computational cost. We can verify this by computing the ratio:

Sparse FLOPs ratio=Sparse FLOPs per tokenDense FLOPs per token(definition)=8×dmodel×dff/N4×dmodel×dff(substitute formulas)=8/N4(cancel dmodel×dff)=2N(simplify)=28=14(substitute N=8)\begin{aligned} \text{Sparse FLOPs ratio} &= \frac{\text{Sparse FLOPs per token}}{\text{Dense FLOPs per token}} && \text{(definition)} \\ &= \frac{8 \times d_{\text{model}} \times d_{\text{ff}}/N}{4 \times d_{\text{model}} \times d_{\text{ff}}} && \text{(substitute formulas)} \\ &= \frac{8/N}{4} && (\text{cancel } d_{\text{model}} \times d_{\text{ff}}) \\ &= \frac{2}{N} && \text{(simplify)} \\ &= \frac{2}{8} = \frac{1}{4} && \text{(substitute } N = 8\text{)} \end{aligned}

This 1/41/4 ratio reveals the key insight: with 8 experts and top-2 routing, we use only 25% of the dense model's compute per token. The general formula 2/N2/N shows that computational cost decreases linearly with the number of experts, while total model capacity (parameter count) increases linearly with NN. This inverse relationship between experts and compute cost is the mathematical foundation for sparse model efficiency.

A significant advantage of sparse models is the ability to increase total parameters without increasing compute. Rather than keeping the same total parameter count as the dense model, we can make each expert the same size as the original dense FFN, giving us NN times more parameters while only paying k/Nk/N times the compute. This is the real power of sparse architectures: they let us scale capacity independently of computational cost.

In[9]:
Code
def compare_dense_sparse(
    d_model: int = 4096,
    d_ff_dense: int = 16384,
    n_experts: int = 8,
    top_k: int = 2,
):
    """Compare dense and sparse model efficiency."""

    # Dense model statistics
    dense_params = 2 * d_model * d_ff_dense
    dense_flops = 4 * d_model * d_ff_dense  # 2 matmuls, each costs 2*in*out

    # Sparse model: each expert has same size as dense FFN
    # This gives us N times more total parameters
    expert_ff_dim = d_ff_dense  # Each expert is full-sized
    sparse_total_params = n_experts * 2 * d_model * expert_ff_dim
    sparse_active_params = top_k * 2 * d_model * expert_ff_dim
    sparse_flops = top_k * 4 * d_model * expert_ff_dim

    return {
        "dense_params": dense_params,
        "dense_flops": dense_flops,
        "sparse_total_params": sparse_total_params,
        "sparse_active_params": sparse_active_params,
        "sparse_flops": sparse_flops,
        "param_ratio": sparse_total_params / dense_params,
        "flops_ratio": sparse_flops / dense_flops,
        "efficiency_gain": (sparse_total_params / dense_params)
        / (sparse_flops / dense_flops),
    }
Out[10]:
Console
Dense Model: 134,217,728 params, 268,435,456 FLOPs/token
Sparse Model (8 experts, top-2): 1,073,741,824 total params, 268,435,456 active/token, 536,870,912 FLOPs/token

Efficiency: 8.0x parameters, 2.0x FLOPs, 4.0x capacity per FLOP
Out[11]:
Visualization
Grouped bar chart comparing total parameters, active parameters, and FLOPs per token between a dense model in blue and a sparse model with 8 experts and top-2 routing in orange, showing the sparse model's 8x parameter advantage with only modestly higher active compute.
Comparison of parameter counts and computational costs between a dense model and a sparse model. While the sparse model (orange) has 8x the total parameters of the dense model (blue), its active parameters and FLOPs per token are much lower relative to total capacity, achieving a 4x efficiency gain in capacity per FLOP.

The sparse model reaches materially higher parameter count while maintaining reasonable computational cost. With 8 experts and top-2 routing, the total parameter count is 8x larger than the dense baseline, but FLOPs per token only double because each token activates just 2 of the 8 experts. This creates a 4x improvement in capacity per unit of compute. The dense model's single FFN processes every token, while the sparse model distributes capacity across multiple experts and activates only a subset per token. This basic trade-off allows sparse models to scale capacity beyond what would be computationally feasible with dense architectures.

In practice, this lets a new training and deployment paradigm:

Comparison of parameter counts and effective capacity between Dense 7B and Sparse 47B models.
MetricDense 7BSparse 47B (8 experts, top-2)
Total Parameters7B47B
Active Parameters7B~12B
FLOPs per Token~14 GFLOP~24 GFLOP
Effective Capacity1x~4-6x

The sparse model processes each token with similar cost to a ~12B dense model but has the knowledge capacity of a much larger model. This has measurable implications for the scaling laws we discussed in Part XXIII: we can push the Pareto frontier of performance vs. compute. The sparse architecture in effect lets us move along a different efficiency curve, one where we can trade increased memory (for storing more expert parameters) for improved quality at fixed compute.