Efficient Inference Methods for Mixture of Experts Models on Heterogeneous Hardware

    This article explores the key challenges in Mixture of Experts (MoE) inference, such as memory capacity constraints, dynamic expert access, and decoding latency sensitivity. It introduces three core optimization mechanisms—bandwidth-adaptive execution, semantic-aware caching, and elastic resource management—to build a more stable, flexible, and efficient MoE inference system. Additionally, the article outlines the complete execution workflow, critical engineering considerations, and future development directions for optimizing sparse models on limited hardware.

    1. Why MoE Inference is Often Constrained

    The core characteristic of the Mixture of Experts (MoE) architecture is that the model contains a large number of expert networks, but each input token typically activates only a few of them. Compared to dense models, MoE can control the computation required per token while maintaining a large parameter scale, making it highly suitable for building high-capacity generative models.

    However, the reduced computation in MoE does not mean the inference process is inherently efficient. In actual execution, the system must address three key challenges:

    First, the parameter scale of the experts usually far exceeds the VRAM capacity of a single accelerator. To run the model on memory-constrained devices, some expert parameters must be placed in host memory, other devices’ VRAM, or even slower storage media. When the routing module selects an expert not residing in local VRAM, the system must first transfer the parameters before executing the computation.

    Second, expert access is dynamic. Depending on the input, context, and generation stage, the routing module may select entirely different experts. If the caching strategy relies solely on static allocation rules, it can lead to issues such as hot experts not being retained in time and cold experts occupying space long-term.

    Third, incremental decoding is highly sensitive to single-token latency. The prefill phase can typically leverage large batches for parallel computation, whereas the decoding phase requires re-executing routing, preparing expert inputs, and completing expert computation for each generated token. In this stage, data transfer, synchronization waits, and cache misses can become the primary sources of latency, sometimes even exceeding the time spent on matrix computation itself.

    Therefore, MoE inference optimization cannot focus solely on compute utilization. It must simultaneously consider VRAM capacity, memory bandwidth, device interconnect bandwidth, cache hit rate, and scheduling overhead.

    2. Three Core Optimization Mechanisms

    To address the challenges above, an efficient MoE inference system can be designed with three collaborative modules: bandwidth-adaptive execution, semantic-aware caching, and elastic resource management.

    2.1 Bandwidth-Adaptive Execution

    Traditional inference engines often predefine the storage location and loading paths for expert parameters. For example, at system startup, a portion of the experts is fixed in device VRAM while the rest are placed in CPU memory. While simple to implement, this approach cannot adapt to runtime bandwidth variations and changing access patterns.

    The basic idea of bandwidth-adaptive execution is to dynamically select a more appropriate expert execution plan based on the current phase, data transfer paths, and device load.

    During the prefill phase, the input typically contains a long context. The system can analyze the experts that might be accessed subsequently and use a double buffering mechanism for data preparation. One buffer is used for current computation, while the other loads the next batch of potentially required experts in the background. When the current computation is complete, the next batch of parameters is already prepared, reducing the idle time of compute units waiting for data.

    Double buffering is not simply about adding a memory region; it requires coordinating the following workflow:

    1. Determine candidate experts based on routing results.
    2. Check if the candidate experts are already in device VRAM.
    3. Prioritize the missing experts.
    4. Asynchronously transfer the next set of parameters while computing the current experts.
    5. Complete synchronization before the computation task actually depends on the parameters.
    6. Adjust the next loading plan based on actual hit rates.

    During the decoding phase, the computation scale per step is small, making the fixed overhead of data transfer more pronounced. Therefore, the system needs to dynamically decide on different paths based on the actual bandwidth of PCIe, host memory, and device VRAM. For instance, if device VRAM still has space and an expert is accessed frequently, that expert can be retained long-term in VRAM. If VRAM is tight but host memory bandwidth is good, on-demand loading can be used. If a specific transfer path is congested, the scheduling of experts dependent on that path should be reduced to prevent multiple transfer tasks from blocking each other.

    The focus of this mechanism is not to pursue a single fixed storage layout, but to keep the execution plan aligned with hardware status. The system can continuously monitor transfer latency, cache hit rates, queue wait times, and device utilization, updating the expert loading strategy accordingly.

    2.2 Semantic-Aware Caching

    MoE routing is not entirely random. Tokens in adjacent contexts often have semantic continuity, meaning that in certain scenarios, related tokens may repeatedly access similar experts. If the system can identify semantic boundaries in the context, it can reduce unnecessary redundant computation and state reconstruction.

    Semantic-aware caching can establish several state checkpoints during the input processing phase. Checkpoints do not necessarily correspond to fixed lengths; they can be divided based on context structure, topic changes, paragraph boundaries, or routing pattern shifts. When subsequent requests contain similar context fragments, the system can reuse existing states, avoiding the need to process the entire input from scratch.

    Compared to simple prefix caching, semantic-aware caching improves upon two aspects:

    First, the caching granularity is more flexible. Fixed-length segmentation can lead to two issues: split points falling in the middle of semantic units, resulting in poor reuse; or cache blocks being too small, leading to high management metadata and matching costs. Establishing checkpoints based on semantic boundaries achieves a better balance between reuse rate and management overhead.

    Second, the cached objects are not limited to intermediate activation values; they can also be associated with expert routing information. For continuous generation tasks, the routing sequence during the decoding phase usually exhibits a certain degree of locality. The system can use an approximate LRU strategy to maintain recently accessed experts, giving high-frequency experts a better chance to stay in fast storage.

    However, LRU is not optimal in all scenarios. In practical implementation, the following metrics can be combined for weighting:

    • Time of last access for the expert;
    • Number of accesses within a recent window;
    • Loading and unloading costs;
    • Size of expert parameters;
    • Similarity between the current request and historical requests;
    • The expert’s potential impact on subsequent tokens.

    For example, an expert that was just accessed but has a very high loading cost might be more worth retaining than an expert with a slightly higher access frequency but smaller parameters. Thus, semantic-aware caching and access frequency caching can be used in combination rather than being limited to a single replacement algorithm.

    2.3 Elastic Resource Management

    Many inference systems fix cache capacity, expert partitioning, and memory quotas at startup. While fixed configurations are stable in simple environments, they easily lead to resource waste when facing multi-tenant requests, dynamic batching, and varying context lengths.

    Elastic resource management allows the system to adjust cache size and expert allocation relationships during runtime. For example:

    • When VRAM pressure increases, proactively compress the resident space of low-frequency experts;
    • When an expert’s access frequency continuously rises, increase its cache priority;
    • When request length increases, reserve more space for intermediate state caching;
    • When multiple requests compete for resources, reallocate caches based on latency targets;
    • When device load drops, reclaim idle caches and reduce memory usage.

    It is important to note that dynamic adjustments must not disrupt ongoing computation tasks. Implementations usually need to introduce version markers, reference counting, or safe switching points. Expert parameters being used by a computation task cannot be released immediately; the system can only reclaim the corresponding space once the relevant tasks are completed and cache references drop to zero.

    Furthermore, elastic management should set upper and lower limits and fallback strategies. If the cache expands and shrinks too frequently, it will introduce additional overhead. Therefore, a sliding window can be used to track access frequency, alongside setting minimum residence times, migration thresholds, and cooldown periods to prevent the caching strategy from oscillating between two states.

    3. Complete Execution Workflow Design

    A relatively complete MoE inference workflow can be divided into the following stages:

    Stage 1: Route Analysis

    Input tokens first pass through the routing module to obtain candidate experts and their weights. Instead of immediately issuing individual data requests for each token, the system should aggregate the routing results, counting the required expert set, access counts, and priorities for the current round.

    Stage 2: Cache Hit Check

    For each candidate expert, the system checks whether it is already in device VRAM, host memory, or another available storage tier. If it hits the high-speed cache, it goes directly to the computation queue; if it only hits a low-speed tier, a transfer task is created; if it completely misses, loading is initiated based on the expert’s parameter location.

    Stage 3: Task Scheduling and Prefetching

    The scheduler prioritizes transfer tasks based on bandwidth status, expert access frequency, and computation dependencies. Experts that are about to be executed and have high access volumes are loaded first, while idle transfer channels are used to prefetch parameters that might be needed later.

    Stage 4: Expert Computation and Result Merging

    After expert computation is complete, the system merges the results according to routing weights and writes the current round’s access records into cache statistics. For experts that might still be used later, their parameters are retained; for experts that have not been accessed for a long time, their cache priority is lowered.

    Stage 5: Runtime Feedback

    The system continuously collects the following metrics:

    • Expert cache hit rate;
    • Average latency and tail latency per token;
    • Parameter transfer time;
    • PCIe or device interconnect link utilization;
    • CPU wait time;
    • VRAM peaks and fragmentation;
    • Compute unit utilization;
    • Prefetch success rate.

    These metrics serve as the basis for the next round of resource adjustments, forming a closed loop of “observe-decide-execute-feedback” within the inference system.

    4. Key Engineering Considerations

    First, it is essential to distinguish between the prefill and decoding phases. Prefill is more suitable for batch prefetching and double buffering, while decoding focuses more on single-token latency, synchronization overhead, and hot expert retention. Using the same set of strategies for both usually fails to achieve optimal results.

    Second, the number of asynchronous tasks must be controlled. Too many concurrent transfers will saturate the transfer bandwidth, causing computation tasks to fail to receive data in time. The scheduler should limit the number of in-flight data requests and dynamically adjust concurrency based on real-time bandwidth.

    Third, cache management should consider memory fragmentation. Expert parameters may vary in size. If contiguous memory is frequently allocated and freed, it can generate a large amount of unusable fragmentation. Tiered memory pools, fixed page sizes, or background defragmentation mechanisms can be used to reduce dynamic allocation costs.

    Additionally, tail latency must be taken seriously. High average throughput does not necessarily mean a good user experience. If a few requests experience long waits due to missing experts, cache thrashing, or transfer congestion, interactive applications will still show noticeable lag. Therefore, evaluation should simultaneously observe average, P95, and P99 latencies.

    Finally, performance testing needs to cover different context lengths, batch sizes, expert sparsity, and VRAM capacities, rather than drawing conclusions based on a single input. For edge devices, stability under VRAM shortages should be the focus; for workstations or servers, the emphasis should be on bandwidth utilization, multi-request competition, and throughput scalability.

    5. Applicable Value and Future Directions

    The value of these optimization approaches lies in the fact that they do not rely on infinitely increasing VRAM or simply stacking compute resources. Instead, through finer-grained caching and scheduling, they allocate limited hardware to the most valuable expert computations. For devices with small VRAM, it improves the runnability of large-scale sparse models; for devices with large VRAM, it reduces invalid transfers and improves overall throughput.

    There are several directions for future improvement. First, more accurate routing prediction models can be introduced to identify potential hot experts in advance without adding significant computation overhead. Second, caching strategies can be combined with request scheduling to allocate resources differently based on the latency targets of different requests. Third, compression, quantization, and tiered storage can be utilized to further reduce the transfer cost of expert parameters. Fourth, more comprehensive performance models can be established to estimate computation time, transfer time, and cache benefits before execution, thereby selecting a more reasonable execution path.

    Overall, the key to efficient MoE inference is not just “getting more experts to participate in computation,” but ensuring that expert parameters arrive at the compute device at the right time and through the appropriate path. Bandwidth-adaptive execution reduces waiting, semantic-aware caching improves reuse, and elastic resource management handles dynamic changes. By combining the three, a more stable, flexible, and efficient inference system can be built under limited VRAM and complex hardware environments.