Cost Reduction for MoE Large Models: Attention Optimization and Cache Management
This article explores strategies to reduce the inference costs of Mixture-of-Experts (MoE) large models. It analyzes how MoE lowers computation per request while introducing memory and communication overheads. The piece also delves into combining linear and sparse attention mechanisms to optimize sequence processing, and clarifies the distinction between indexer caches and KV Caches within sparse attention frameworks, offering practical insights for inference deployment.
In large model inference services, computational costs are primarily determined by three factors: the computation required per request, the memory footprint, and the data volume transferred across multiple GPUs. This article focuses on the recently popular Mixture-of-Experts (MoE) models, detailing optimization approaches for these three aspects and providing methods that can be validated in your own environment. The specific figures mentioned are based on public introductions; actual parameters should be subject to official releases.
1. How MoE Reduces Computation per Inference
MoE models split the feedforward network into multiple expert networks. A router selects a few experts to participate in the computation for each token. For example, based on publicly available figures, a model with approximately 320 billion total parameters may only activate around 18 billion parameters per inference. This allows the model to store a vast amount of knowledge while keeping the single-pass computation tied only to the activated parameters.
However, MoE is not without its trade-offs. The parameters of all experts must reside in memory or be distributed across multiple GPUs, meaning the memory footprint remains close to the total parameter count. Routing can also lead to unbalanced expert loads, where some experts are frequently invoked while others remain idle. While load balancing losses are typically applied during training, deployment requires careful attention to expert distribution across cards. When experts are scattered across multiple GPUs, the forwarding of tokens between experts incurs communication overhead. To determine whether an MoE model is cost-effective, one must evaluate the activated parameters, memory footprint, and communication volume simultaneously; focusing solely on activated parameters leads to a one-sided conclusion.
2. Combining Linear and Sparse Attention
The computational cost of standard self-attention scales quadratically with the sequence length N. Each token must calculate its relevance with all other tokens, forming an N×N matrix. As the context length increases, this overhead becomes increasingly pronounced.
Linear attention uses kernel functions to approximate softmax, allowing the calculation order to be adjusted so that the cumulative result of K and V is computed first, and then multiplied by Q. This reduces the computational complexity to approximately linear with N. The trade-off is that this approximation introduces errors, potentially weakening the model’s performance on tasks requiring precise retrieval. Furthermore, for shorter sequences, the extra computation of the kernel function itself may offset the gains, meaning linear attention is not always faster.
Sparse attention addresses this by limiting the scope a token can attend to, such as only looking at a local sliding window plus a few global representative tokens. The computation then scales with the window size rather than the entire sequence length. The risk is that long-distance information might be lost, requiring compensation through global tokens or inter-layer designs.
In practice, models typically mix these two mechanisms across different layers: some layers handle long-range information, while others manage fine-grained local dependencies. This hybrid approach lowers overall computation, but the actual savings ratio depends on the baseline architecture, sequence length, and whether KV reads are factored into the cost. Some sources claim figures like “attention computation reduced to one-third,” but without specifying the baseline and testing conditions, these cannot be directly applied. For validation, one should fix the same set of inputs and separately measure the latency and throughput during the prefill and decode phases.
3. Distinguishing Indexer Cache from KV Cache
The KV Cache stores the Key and Value for every processed token at each layer to avoid redundant computation. The longer the context, the more memory it occupies, increasing the pressure on memory bandwidth during the decoding phase.
In sparse attention, there is also an indexer, which determines which historical positions the current token should attend to and typically maintains its own cache. It is crucial to treat the indexer cache and the KV Cache as two different types of data. The compression ratio of one cannot be directly applied to the results of the other.
Indexer cache reuse mechanisms, like IndexPool, aim to compress the indexer cache from multiple replicas down to a single copy. If an inference framework originally retains four indexer cache replicas per request state, merging them into one will significantly reduce the memory footprint for the indexer component. However, the total footprint of the KV Cache still depends on the storage precision of K and V, the paging method, and whether prefixes are shared.
