Inference Cost of Extremely Sparse MoE and Hybrid Attention: How to Calculate It
This article explains how to estimate inference costs for extremely sparse Mixture-of-Experts (MoE) models and hybrid attention designs. It covers per-token compute, weight bandwidth, and KV Cache memory, the difference between total and active parameters, how linear and sparse attention layers affect KV Cache size, and how to test these effects with controlled experiments.
1. Where Inference Costs Come From
Inference cost comes mainly from three parts: the compute per token in the forward pass, the bandwidth needed to read model weights, and the KV Cache memory that grows with context length. In short-context scenarios, weights and the feed-forward network (FFN) usually account for most of the cost. When the context grows to tens of thousands or even a million tokens, the compute of the attention component and the KV Cache footprint rise quickly and limit how many requests the service can handle concurrently.
A single number is rarely enough to judge cost. Total parameter count indicates model capacity, active parameter count affects compute per step, and the attention structure and KV Cache determine memory pressure in long-context scenarios. When selecting a model, compare these three items in the same table.
2. Extremely Sparse MoE: Active Parameters Determine Compute, Total Parameters Determine Capacity
2.1 How to Calculate Sparsity
Sparse MoE splits the feed-forward layer into multiple experts. Each token passes through only the few experts selected by the gating network. The following figures come from the configuration example of a public model. Verify them against the original technical report before citing them. They should not be treated as a general rule for all extremely sparse models:
- Total parameters: about 320 billion;
- Active parameters per token: about 18 billion, an activation ratio of about 5.6%;
- Routed experts: about 288, with 8 selected per token.
Check the counting method before calculating active parameters. Usually this includes the attention layers, dense layers, shared experts, and the routed experts that are selected. Unselected experts are not counted. Whether embedding and output head are counted depends on the report. Reports count differently, so confirm each model’s method before comparing active parameters.
Sparse MoE handles “how much knowledge is stored” and “how much computation per step” separately. Total parameters determine the patterns and knowledge the model can store. Active parameters determine per-step compute and weight reads. When the activation ratio drops, compute per token approaches that of a mid-sized dense model, but the model still has large capacity. The cost is more complex routing, communication, and memory layout. In multi-GPU deployments, measure the communication overhead of expert parallelism separately.
2.2 Shallow Structure and Residual Connections
These models may have fewer layers, for example 45, and keep the bottom few layers dense to stabilize the basic representations. To compensate for limited depth, some designs add learnable residual mixing paths between layers. A residual connection normally adds a layer’s input directly to its output so information can pass across layers. Here, multiple cross-layer paths are mixed with learned weights.
Whether this design helps needs to be verified through ablation experiments. Remove the structure, then compare results with the same parameter count and training data. If a team wants to try a similar design on its own model, it should first run controlled comparisons on a small-scale model, then decide whether to scale up.
3. Hybrid Attention: Costs Mainly Fall on Compute and KV Cache
3.1 Sources of Cost
The compute of standard full attention grows as O(n²) with sequence length n. The prefill stage processes the entire input in one pass and has the highest compute pressure. In the decode stage, each generated token must read the KV Cache of all previous tokens.
The KV Cache size for a single request can be roughly estimated with the following formula:
KV Cache bytes ≈ 2 × L_full × H_kv × d_head × n × bytes per element
Here, L_full is the number of attention layers that keep the full KV, H_kv is the number of KV heads, d_head is the dimension of each head, and n is the context length. With bf16, bytes per element is 2. For concurrent batches, multiply the result by the number of requests in the batch. This formula assumes that every layer stores KV that grows with length. If linear attention layers use a fixed-size state, they are not counted in L_full. The exact treatment depends on the implementation.
3.2 3:1 Interleaving of Linear and Sparse Attention
A common industry practice is to place about 3 linear attention layers for every 1 sparse attention layer. Using 45 layers as an example, about 34 layers are linear attention and 11 are sparse attention. The table below compares the two layer types:
| Layer Type | Compute Complexity | KV Cache Growth | Suited Tasks | Main Risk |
|---|---|---|---|---|
| Linear attention | Roughly linear with length | Usually a fixed-size state; implementations vary widely | Local and medium-range dependencies | Weaker precise retrieval than full attention |
| Sparse attention | Each query accesses only selected tokens | Grows with length; can be reduced by index compression | Retrieving key tokens at long distances | Retrieval may miss key tokens |
Assuming only 11 layers store length-dependent KV and linear layers store none, the KV size is about 11/45 of full attention, or about one quarter. This is an estimate under that assumption. Actual values depend on the number of KV heads per layer, whether a sliding window is used, and the framework’s cache strategy.
3.3 Index Compression: Effects and How to Measure Them
In sparse attention layers, the Keys used for retrieval can be pooled, meaning several adjacent Keys are merged into one index unit. The system first uses the coarse-grained index to filter candidate regions, then performs exact computation on the candidate tokens. This step mainly reduces the compute and index reads during retrieval.
It does not mean the overall KV Cache shrinks by the same factor. Whether Values are compressed, whether the stored KV itself is compressed, and whether the linear layer state changes all affect the final memory footprint. Some reports give figures such as “attention compute reduced about 3x and KV Cache reduced about 4x.” These apply to specific sequence lengths, batch sizes, and baselines, so they should not be applied directly to your own workload.
To verify these gains, run controlled experiments on the same model weights:
- Fix sequence length, for example test 32K, 128K, and 1M separately;
- Fix batch size, and record results at levels such as 1, 4, and 16;
- Enable and disable indexing
