Optimizing Inference Systems for Trillion-Parameter MoE Models
This article walks through the engineering practices behind large-scale MoE inference optimization, focusing on the state space created by hybrid attention rather than model size alone. It covers layered attention configuration, unified token coordinates with physically separate cache pools, incremental maintenance of compressed states, multi-tier caching and prefix reuse, sparse retrieval and compression execution paths, expert parallelism load and memory balancing, multi-step prediction, and disaggregated prefill and decode deployment. A final acceptance checklist helps teams verify correctness, stability, and reusability in production.
1. The Real Problem: State Space, Not Model Size
When models reach the trillion-parameter scale and adopt Mixture-of-Experts (MoE) architectures, the main pressure on the inference system does not come only from weight size or the number of experts. It comes from the state space that attention must maintain at runtime. New-generation models often mix multiple attention patterns within the same network: some layers keep sliding window attention to control complexity, others switch between two granularities of compressed attention, and a few retain global dense attention to preserve long-range modeling capability.
This means the cache can no longer be treated as a simple structure with one raw key-value tensor per layer. In engineering terms, the system must simultaneously maintain at least: a full token coordinate index, a sliding window physical pool, a coarse-grained compression pool, a fine-grained compression pool, an indexer auxiliary pool, a compression state pool, and the mappings among these pools across prefix reuse, graph capture, disaggregated prefill and decode deployment, and cold data offloading. Any inconsistency in these mappings shows up as lower hit rates, higher memory fragmentation, or accuracy anomalies in long contexts.
2. From Hybrid Attention to a Deployable Path
2.1 Layer Strategy and Configuration
The first step toward a runnable system is to turn “layer type” into explicit configuration instead of conditional branches scattered across model code. Declare the attention category, window length, compression granularity, and whether the index is shared for each layer, then let the runtime generate a unified execution plan.
layer_plan = [{"type": "swa", "window": 4096},{"type": "c4", "compress": 4, "indexer": True},{"type": "c128", "compress": 128},{"type": "full", "window": None},]
2.2 Unified Coordinates, Separate Physical Storage
Logically, all layers share the same token coordinates (position, sequence ownership, and whether the token is covered by the prefix cache). Physically, each layer lands in a different pool. During decoding, the system first looks up the logical position by coordinate and then translates it into a physical slot in the corresponding pool. Only this way can prefix reuse and paging management stay consistent across layers.
2.3 Incremental Maintenance of Compressed States
Compressed attention requires block-level summaries. These summaries must be updated incrementally as tokens are appended, not recomputed at every step. A common approach is to write summaries into a dedicated state pool and fix their addresses during graph capture, avoiding jitter caused by reallocation at every step.
3. Multi-Tier Cache Pools and Prefix Reuse
The goal of a multi-tier pool design is to place data with different lifecycles where it belongs: hot data stays in GPU memory, warm data stays in host memory, and cold data sinks to external storage. Three engineering points matter:
- Unified addressing: all layers use the same logical keys, avoiding per-pool custom indexes;
- Aligned paging granularity: page size should be a suitable common multiple of compression granularity and window length to reduce internal fragmentation;
- Reference counting and reclamation: prefix sharing means the same physical page can be referenced by multiple requests, so reclamation must be based on reference counts rather than simple LRU.
Prefix reuse must also handle partial hits. The matched length is usually not an integer multiple of the page size, so the trailing portion smaller than one page needs to fall back to recomputation and be explicitly recorded in the scheduler; otherwise positional encoding misalignment will occur.
4. Sparse Retrieval and the Compression Execution Path
In long-context scenarios, memory access in the attention operator is the main bottleneck. The common industry approach is “coarse filtering first, exact computation second”: a lightweight index structure scores historical tokens, and only the top few candidates participate in exact attention. At the same time, compression, scoring, and sampling are fused into a small number of kernels to reduce round-trip copies of intermediate tensors.
query ──> index scoring ──> candidate filtering (top-k)│ │└──> compressed summary read ──┴──> exact attention ──> output
Note that the number of candidates, recall, and latency form a clear three-way tradeoff. Defaults should be determined through offline evaluation and allowed to be overridden per business scenario, rather than hard-coded inside the operator.
5. Expert Parallelism: Load and Memory
The difficulty with MoE is load skew: a few hot experts slow down the whole batch. Practical measures include capacity-factor throttling, redundant expert replicas, and placement policies that adjust dynamically by popularity. On the memory side, expert weights, cache pools, and communication buffers need a unified budget with headroom reserved for fragmentation, avoiding frequent swapping after long stable runs.
For parallelism, data parallelism, tensor parallelism, and context parallelism must work together. Splitting attention state along the context dimension can significantly reduce per-GPU cache pressure, but it introduces extra communication and synchronization points, so the split ratio should be chosen based on network topology.
6. Multi-Step Prediction and Deployment Shape
Multi-step prediction (producing multiple candidate tokens in one forward pass) improves throughput, but it turns cache writes into “speculative pre-writes with uncertainty,” which must support rollback and placeholder management. On deployment, disaggregating prefill and decode has become mainstream: the former aims to saturate compute, while the latter aims for memory and memory-access efficiency. The two exchange intermediate state through message queues and object storage, then a container platform handles unified orchestration and unified observability for monitoring and alerting.
7. Acceptance Checklist
- Is there a single source of truth for the mapping between logical coordinates and physical pools?
- Is there an explicit fallback path for partial prefix hits?
- Are compressed summaries updated incrementally with stable addresses?
- Are candidate filtering parameters configurable and evaluable?
- Do expert hotspots have redundancy and throttling?
- Have pre-writes and rollbacks been validated under load testing?
- Is tiered caching with cold data offloading in place?
Chaining these pieces into a closed loop is what allows trillion-scale MoE models to be stable, tunable, and reusable in real business settings. This methodology also applies to model serving practices on Baidu AI Cloud.
