Optimizing Large-Scale MoE Inference Systems: Practical Engineering Notes

    This article walks through the engineering work required to serve trillion-parameter MoE models with hundreds of experts. It explains why attention runtime state, not memory or compute, drives inference framework redesign, and covers multi-level KV cache pools that separate logical coordinates from physical pages, state consistency between compressed attention and sparse retrieval, coordination among prefix caching, graph capture, and disaggregated prefill/decode deployment, parallelism and expert load balancing, and a deployment checklist. Readers will find concrete design principles, a pool manager interface sketch, and observability guidance for building stable inference services.

    1. Why “a bigger model” is not the hardest part

    When parameter counts reach the trillions and a single model contains hundreds of experts, intuition says the bottleneck is memory and compute. Field experience points the other way: what actually forces an inference framework to be reworked is the change in the state space of attention at runtime.

    Traditional inference engines treat the KV cache as a set of raw K/V tensors, one per layer. Addressing is simple and maps one-to-one with layers. That assumption breaks down completely once every layer runs sliding-window attention and also switches dynamically between two compressed attention variants. The engine now has to maintain, all at once: logical coordinates for all tokens, a physical page pool for the sliding-window portion, two compression pools with different compression ratios, an index pool for sparse retrieval, and an intermediate state pool for the compression process itself. It gets harder: these pools must also keep consistent mappings across prefix cache reuse, graph capture, disaggregated prefill and decode deployment, and tiered offloading.

    So deploying this class of models is essentially about translating a new attention structure into a deployable, tunable, reusable serving path, not simply adding a new model class.

    2. Multi-level cache pools: decoupling the logical view from physical storage

    The core design principle is separating logical coordinates from physical pages. The logical layer only knows token indices; the physical layer allocates by page, and a page table does the translation. This yields three direct benefits:

    • Different attention branches can share one logical sequence and each map to a different physical pool, avoiding duplicated metadata per structure.
    • On a prefix cache hit, only page table entries need to be reused; no data movement is required.
    • Pool size becomes an independently tunable parameter, configurable per workload based on the input/output length distribution.

    A sketch of the pool management interface:

    1. class KVPoolManager:
    2. def alloc(self, logical_range, pool_kind): ...
    3. def free(self, logical_range, pool_kind): ...
    4. def remap(self, logical_range, src_kind, dst_kind): ...
    5. def stats(self) -> dict: ... # hit rate, fragmentation, swap in/out volume

    The highest-value engineering investment is stats: without observability broken down per pool, tuning is guesswork.

    3. State consistency for compressed attention and sparse retrieval

    Compressed attention squeezes long history into a shorter representation, while the index structure picks out the small set of tokens that actually need to participate in computation. Layering the two shifts the problem from “compute fast” to “keep state consistent”:

    1. Compression is incremental; when new tokens arrive, the compression state is updated rather than recomputing the full history.
    2. The index pool and the compression pool must stay version-synchronized, otherwise retrieval can land on already-invalidated positions.
    3. The sparse-selected token set is dynamic and shapes differ across requests in a batch, so padding or bucketing is needed to keep the computation graph stable.

    The practical recommendation is to instrument three quantities separately: logical sequence length, compressed length, and the number of tokens actually participating in a given round. The ratios among the three directly show whether the compression and sparse strategies are actually taking effect.

    4. Making prefix caching, graph capture, and disaggregated deployment work together

    Prefix caching requires page tables to be shareable across requests; graph capture requires stable tensor shapes and memory addresses; disaggregated prefill and decode requires state that can be serialized and transferred. The requirements look conflicting, and a workable compromise is:

    • Use dynamic shapes during prefill, and fixed shapes during decode, brought into graph capture.
    • Mark shared pages read-only, with copy-on-write.
    • For disaggregated deployment, transfer only the logical length and page table deltas, rebuilding compression state on demand.

    5. Parallelism and expert load balancing

    Parallelism for very large MoE models is typically a combination of tensor parallelism, expert parallelism, context parallelism, and data parallelism. Context parallelism splits long sequences and significantly reduces per-GPU activation pressure, but introduces cross-device communication. Expert parallelism raises throughput but can cause load skew from hot experts. Mitigations include capacity factor control, expert replication, and routing correction based on real-time statistics. When running on a container platform, the parallel topology should also be a scheduling constraint so that cross-node communication does not land on slow links.

    6. Deployment checklist

    • Are per-pool capacity and hit rates observable separately?
    • Are compression state and index versions strictly consistent?
    • Does the decode phase run entirely on a fixed-shape path?
    • For long-context scenarios, has the communication cost of context parallelism been evaluated?
    • Is tiered offloading available, moving cold data to object storage or local disk?
    • Do logs and monitoring alerts cover all three curves: queueing latency, time to first token, and throughput?

    7. Closing

    The new generation of sparse architectures offers not a single optimization point but a systemic rework of the inference stack. Get cache tiering, state consistency, shape stability, and parallel topology right, and architectural innovation on the model side can actually turn into stable throughput on the serving side. The same approach is a useful architectural reference when building in-house inference services in cloud environments such as Baidu AI Cloud.