Optimizing Inference Systems for Ultra-Scale MoE Models

    This article explores why next-generation ultra-scale MoE models make inference systems heavier, focusing on runtime state management rather than model loading. It details core challenges such as cache state space explosion, coupling between prefix caching and parallelism, conflicts between graph execution and dynamic shapes, and resource contention between prefill and decode. It then covers engineering practices including multi-level cache pools with a unified address space, layered hashing for prefix reuse, pipelining of compressors and sparse indexes, speculative decoding with shadow caches, and expert parallelism load balancing, followed by deployment and tuning recommendations.

    1. Why Next-Generation Large Models Make Inference “Heavier”

    Over the past two years, ultra-scale Mixture-of-Experts (MoE) architectures have become one of the mainstream directions in large model evolution. Once parameter counts scale from hundreds of billions to trillions, simply “loading the model into GPU memory” is no longer the hard part. The real challenge shows up in runtime state management during inference.

    Traditional inference frameworks usually treat the key-value cache (KV Cache) as a set of raw tensors organized by layer: one copy per layer, growing linearly with sequence length. But once a model introduces a hybrid structure with multiple attention forms, that assumption no longer holds. A typical next-generation design may include:

    • Sliding Window Attention (SWA): attends only to the most recent fixed number of tokens, so the cache can be reused in a circular fashion;
    • Compressed Attention (CSA / HCA): compresses long history into lower-resolution representations, forming multiple caches with different compression ratios;
    • Sparse retrieval-based attention: uses a lightweight index structure to locate candidate tokens first, then performs exact computation.

    This means the inference engine must maintain multiple physical cache pools at the same time, along with the mapping between them and the “full token coordinates.” Engineering complexity rises geometrically.

    2. Core Challenges for Inference Engines

    2.1 Cache State Space Explosion

    When every layer switches among sliding window and two compressed attention forms, the cache is no longer a single structure. It includes at least: a sliding window physical pool, a low compression ratio pool, a high compression ratio pool, an index pool, and a compression state pool. Over the lifecycle of any single request, all of these pools must advance in sync.

    2.2 Coupling Between Prefix Caching and Parallelism Strategies

    Prefix reuse (Prefix Cache) relies on deterministic hashing of the prefix. Multiple cache pools mean the hash key must uniquely identify “which cache kind at which layer, compressed to which level.” At the same time, tensor parallelism, expert parallelism, and context parallelism change how the cache is physically partitioned, so prefix reuse tends to break across parallel topologies.

    2.3 Conflict Between Graph Execution and Dynamic Shapes

    To reduce kernel launch overhead, inference engines commonly use graph capture (CUDA Graph). But the state length of compressed attention and the number of candidates in sparse retrieval are dynamic. A strategy of “segmented capture + shape bucketing” is needed to balance performance and flexibility.

    2.4 Resource Contention Between Prefill and Decode

    The prefill stage is compute-intensive, while the decode stage is memory-access intensive. If both are co-located on the same machine, they interfere with each other. The industry commonly adopts PD disaggregated deployment: prefill and decode use separate resource pools and transfer intermediate states over a high-speed link.

    3. Engineering Implementation of Key Optimization Techniques

    3.1 Multi-Level Cache Pools and a Unified Address Space

    The recommended approach is to establish a unified “logical token coordinate” and then build a physical mapping table for each cache kind. Example:

    1. class KVCacheManager:
    2. def __init__(self):
    3. self.pools = {
    4. "swa": SWAPool(capacity=...),
    5. "c4": CompressPool(ratio=4),
    6. "c128": CompressPool(ratio=128),
    7. "index": IndexPool(),
    8. "state": CompressStatePool(),
    9. }
    10. def append(self, layer_id, kind, token_ids, kv):
    11. logical = self.allocate_logical(token_ids)
    12. return self.pools[kind].write(layer_id, logical, kv)

    The key point: logical coordinates stay stable, while physical pools are replaceable. This way, prefix caching, offloading, and cross-machine transfer all operate only on logical coordinates.

    3.2 Layered Hashing for Prefix Reuse

    Do not use a single global hash. Instead, compute a composite key for each layer and each cache kind:

    1. key = hash(model_id, layer_id, cache_kind, compress_level, prefix_tokens)

    Reuse happens only when everything matches. A partial match falls back to the nearest common prefix, avoiding incorrect reuse that would cause accuracy problems.

    3.3 Pipelining Compressors and Sparse Indexes

    Both compression (aggregating long history into short representations) and sparse index construction can overlap with the main computation. Engineering recommendations:

    • Use an asynchronous queue for compression, triggered by “blocks” rather than per token;
    • Keep the index structure resident in GPU memory, generating only the candidate list dynamically;
    • Set upper and lower bounds on the number of candidates, truncating by importance when exceeded.

    3.4 Speculative Decoding and Multi-Token Prediction

    A draft model or lightweight prediction head generates multiple candidates at once, which the main model then verifies in parallel. When combined with compressed attention, note that cache writes during the draft stage must be rollback-able; otherwise, a verification failure causes state pollution. A common approach is to introduce a Shadow Cache and merge it only after verification passes.

    3.5 Load Balancing for Expert Parallelism

    The difficulty with MoE is skewed expert routing. Options include:

    • Soft constraints with a capacity factor plus token dropping;
    • Cross-device expert rearrangement, migrating based on real-time popularity;
    • Overlapping communication and computation to hide all-to-all latency.

    4. Deployment Forms and Tuning Recommendations

    1. Prioritize PD disaggregation: scale prefill and decode independently, with intermediate states traveling over a high-speed network.
    2. Context parallelism: split ultra-long sequences along the sequence dimension, combined with ring communication.
    3. GPU memory watermark management: set independent watermarks for each cache pool, prioritizing offload of cold data with high compression ratios.
    4. Observability: instrument hit rate, offload volume, and compression latency separately for each cache kind, rather than observing only end-to-end latency.
    5. Load testing method: use real long-text plus high-concurrency mixed workloads. Testing with a single short prompt cannot expose cache management defects.

    5. Conclusion

    Optimizing inference for next-generation ultra-scale MoE models is essentially about translating “model structure innovation” into a “deployable, tunable, and reusable service path.” Whoever organizes multiple cache pools, prefix reuse, parallelism strategies, and disaggregated deployment into a self-consistent runtime can achieve stable throughput and latency under long-context and high-concurrency scenarios. This methodology also applies to general infrastructure combinations such as container platforms, object storage, and monitoring and alerting on Baidu AI Cloud, helping enterprises build self-controlled large model inference service capabilities.