Mixture-of-Experts and Long-Context Inference in LLM Platforms: Engineering Practice

    This article explains how large language model platforms reduce cost through three engineering directions: sparse activation with Mixture-of-Experts (MoE) architecture, compressing the KV cache with latent attention, and improving training and inference resource utilization. It covers expert routing and load balancing, KV cache size estimation, multi-token prediction, low-precision training, sparse attention, chunked prefill, key performance metrics, and deployment considerations such as private deployment and licensing.

    1. Background: Engineering Constraints in LLM Platforms

    The cost of serving large language models comes mainly from three sources: compute for training, GPU memory used during inference, and throughput and latency under high concurrency. A traditional dense model activates all of its parameters in every forward pass, so a larger model needs more computation per inference step. When the context length grows to tens or even hundreds of thousands of tokens, the key-value (KV) cache in the attention mechanism quickly fills GPU memory and limits how many requests can run concurrently.

    As a result, LLM platforms have generally pursued engineering optimizations in three directions:

    1. Reduce the activation size of each computation so that only a subset of parameters takes part in each calculation. A typical approach is the Mixture-of-Experts architecture.
    2. Compress the state stored during inference, for example by reducing the size of the KV cache or making the cached data more compact.
    3. Improve resource utilization in training and inference, including load balancing, low-precision computation, and more efficient decoding strategies.

    The following sections cover these three directions and provide calculation methods that can be used directly for capacity planning.

    2. Mixture-of-Experts Architecture: Trading Sparse Activation for Capacity

    2.1 Basic Principles

    The Mixture-of-Experts (MoE) architecture replaces the feed-forward network layers in a Transformer with multiple parallel “expert” sub-networks. A router then selects a small number of these experts for each input token to process.

    Two key concepts need to be kept distinct:

    • Total parameters: the sum of all expert parameters and shared parameters. This determines the model’s knowledge capacity.
    • Active parameters: the parameters that a single token actually passes through. This determines the computation cost of each step.

    For example, a model may have a very large total parameter count, but each token activates only a small portion of it. In that case, the per-token compute cost is much lower than that of a dense model of the same total size.

    2.2 Routing and Top-K Selection

    The router is usually a linear layer. It outputs a score for each expert, selects the K highest-scoring experts, and normalizes their weights. The following pseudocode, which does not depend on any specific framework, illustrates the process:

    1. def moe_layer(tokens, router_weights, experts, top_k=2):
    2. # tokens: [batch, seq, hidden]
    3. scores = router_weights.linear(tokens) # [batch, seq, num_experts]
    4. probs = softmax(scores, axis=-1)
    5. topk_probs, topk_idx = select_top_k(probs, k=top_k)
    6. topk_probs = topk_probs / sum(topk_probs, axis=-1) # Normalize the weights
    7. output = zeros_like(tokens)
    8. for expert_id, expert in enumerate(experts):
    9. mask = (topk_idx == expert_id) # Which tokens selected this expert
    10. if mask.any():
    11. output += mask_weight(topk_probs, mask) * expert(tokens[mask])
    12. return output

    Two points need attention during implementation. First, tokens are not distributed evenly across experts, so some experts may become overloaded, and a scheduling strategy is needed. Second, expert parallelism across devices adds communication overhead, so expert placement should be designed together with the network topology.

    3. Attention Optimization: Compressing the KV Cache

    3.1 KV Cache Size in GPU Memory

    During autoregressive decoding, the model must store the keys and values of all previous tokens. The cache size can be estimated with the following formula:

    1. Cache size in bytes = 2 × layers × attention heads × head dimension × sequence length × bytes per element

    Consider a hypothetical configuration with 32 layers, 32 attention heads, a head dimension of 128, a sequence length of 65,536, and 2 bytes per element (FP16). Substituting these values gives a cache of about 32 GiB for a single sequence. Even when the model weights fit in GPU memory, the number of concurrent requests in long-context scenarios is severely limited.

    3.2 The Basic Idea of Latent Attention

    Multi-head Latent Attention (MLA) does not cache the full keys and values of each head. Instead, it projects them into a low-dimensional latent vector and caches only that vector. When computing attention, the model restores the required keys and values through learned up-projection matrices.

    This is essentially low-rank compression. Suppose each token at each layer caches a latent vector of dimension 512, while standard multi-head attention stores 2 × 32 × 128 = 8192 values per token. The cache would then shrink to roughly 1/16 of its original size. This figure only illustrates the compression idea. The actual compression ratio depends on the choice of latent dimension and the trade-off with model quality.

    3.3 Design Trade-offs

    Low-dimensional comp