A Deep Dive into Large-Scale MoE Architectures: Routing, KV Cache, and Hybrid Attention

    This article breaks down the core bottlenecks of trillion-parameter MoE models, including KV cache growth in long contexts, hybrid attention designs, and a comparison of four MoE routing patterns, along with a practical deployment checklist.

    1. Why Parameter Count Is No Longer the Only Metric

    Once total parameter counts reach the trillion scale, the industry’s focus has shifted from “how many parameters can we stack” to “how much effective capability do we get per unit of compute.” The reason is straightforward: under a dense architecture, every token passes through all parameters, so training and inference costs scale linearly with parameter count. A Mixture of Experts (MoE) architecture decouples total parameter count from per-token compute, making “large capacity” and “low activation cost” possible at the same time.

    But MoE is not a free lunch. It turns the problem of “insufficient compute” into three new bottlenecks: memory capacity, routing balance, and KV cache expansion under long contexts. Understanding these three points is key to understanding large-scale model architecture.

    2. The Real Bottleneck of Long Contexts: The KV Cache

    Standard multi-head attention (MHA) needs to cache the Key/Value vectors of historical tokens during inference, and its memory usage is proportional to sequence length. A rough estimate:

    1. # KV cache memory estimation (illustrative)
    2. bytes_per_token = 2 * n_layers * n_kv_heads * head_dim * dtype_bytes
    3. total_gb = bytes_per_token * seq_len * batch_size / (1024 ** 3)
    4. # With millions of tokens, dozens of layers, and multiple heads, this easily reaches tens of GB

    This is the root cause of “the longer the context, the less the GPU memory seems to suffice.” Industry responses fall into three categories: compressing the cache (quantization, low-rank projection); sparse/local attention (computing only some tokens); and layering and sharing (cross-layer reuse, cross-token aggregation). Hybrid attention is a combined approach from the third category.

    3. Hybrid Attention: The Design Logic of Layered Division of Labor

    A typical cost-effective approach enables two complementary mechanisms at the same time:

    • Compressed Shared Attention (CSA): In shallow layers, long sequences are globally compressed, aggregating a large number of tokens into a fixed-length summary representation that later layers can share and reuse. The idea is related to the “global token” approach, but engineering practice usually adds two points: the compression ratio can be dynamically adjusted based on input complexity; and hierarchical pre-training objectives are introduced during training so the model explicitly learns “what information can be compressed and what must be preserved.”
    • Hierarchical Context Attention (HCA): In deep layers, local fine-grained attention is performed, computing high-resolution interactions only on the retrieved relevant segments.

    The combined effect is that global information is not lost, local details are not diluted, and the KV cache size can be reduced by an order of magnitude, making million-level contexts feasible in a single-machine, multi-GPU environment. Illustration:

    1. # Layered attention pseudocode
    2. h = embed(tokens)
    3. for layer in shallow_layers: # global compression
    4. summary = csa_compress(h, ratio=adaptive_ratio(h))
    5. h = attn(h, kv=summary) # shared summary KV
    6. for layer in deep_layers: # local refinement
    7. idx = select_topk_segments(h, k=64)
    8. h = hca_attn(h, kv=h[idx])

    Engineering caveats: too low a compression ratio loses long-range dependencies, while too high a ratio degenerates into ordinary attention; the layer split point, summary length, and number of retrieved segments all need ablation studies on a validation set.

    4. A Side-by-Side Comparison of Four MoE Routing Patterns

    Even when all called MoE, implementations differ greatly. The core variables are the “total/active parameter ratio,” “whether shared experts exist,” and “routing granularity.”

    1. Shared expert + a few routed experts (e.g., 8+1): One shared expert is always active and handles general knowledge, while 8 routed experts activate Top-2 per token. The advantages are stable training and more balanced expert load, making it suitable for general scenarios with high stability requirements.
    2. Pure MoE + full-size matrices: About 800B total parameters and 28B active parameters, with multiple sizes ranging from a few billion to a few hundred billion sharing one training and inference framework. The value is that developers can smoothly choose a size based on hardware conditions, and a loose ecosystem and license make it easier to form a closed toolchain loop.
    3. Very large total parameters + small activation (about 1T/32B) + code-aware routing: Some experts specifically strengthen code semantics and generation, so it performs strongly on code understanding and repository-level repair benchmarks. This suggests that a routing prior is itself a means of capability injection.
    4. Extremely low activation ratio (about 295B total / 21B active, roughly 7% activation): Almost all “capacity” is placed in the parameters, giving the lowest per-token cost, but requiring the most from memory bandwidth and expert caching strategies. It suits throughput-first batch scenarios.

    Selection advice: choose a low activation ratio for latency-sensitive cases; choose a shared-expert structure when stability is the priority; choose a model with a domain routing prior for code and structured output; choose a series with a complete size matrix for multi-device deployment.

    5. Deployment Checklist

    • Measure KV cache and first-token latency before discussing context length;
    • Watch for “long-tail latency” caused by uneven expert load;
    • Use a unified evaluation set for ablation, avoiding reliance on a single leaderboard score;
    • On the inference side, combine container platforms, monitoring/alerting, and logging services for capacity and cost observability.

    In practice at Baidu AI Cloud, deploying such large-scale models typically relies on general capabilities such as container platforms, object storage, and monitoring/alerting to achieve elastic scaling and observability, turning architectural advantages into stable business throughput.