Bandwidth and Cache Optimization for On-Device Mixture-of-Experts Inference
Mixture-of-Experts (MoE) models activate only a subset of experts per token, but on-device deployment still requires storing and fetching the full set of expert weights. This article explains how to budget GPU memory, overlap weight transfers with computation during prefill and decoding, reuse context checkpoints and expert weights through caching, and adjust resource quotas elastically—along with practical guidance for measuring whether these optimizations actually reduce latency.
Deployment Is About More Than Fitting the Model
Mixture-of-Experts (MoE) models use a router to select a subset of experts for each input token, rather than passing every token through all experts. Sparse activation reduces per-forward computation cost, but it does not mean a device only needs to store a fixed set of expert weights: different inputs, different layers, and even adjacent generation steps may select different experts. Deployment still requires allocating storage for the full model and ensuring that the weights needed for the current step can reach the execution device in time.
When GPU memory cannot hold all weights, a common approach is to keep non-resident experts in host memory, moving them to the accelerator on demand, or executing them directly on the CPU when the implementation allows. This involves two distinct costs: one is the space occupied by weights, caches, and temporary tensors; the other is the wait time that scheduling, transfers, and computation expose on the inference critical path. The former determines whether the model runs stably; the latter determines how long users wait and whether output is smooth.
Before deployment, establish a memory budget that accounts separately for resident weights, the expert weight cache, the attention KV cache, operator workspaces, and transfer buffers. Running on short inputs does not guarantee stability on long contexts: as the sequence grows, the attention KV cache typically expands, and if the expert cache fills the remaining memory, the two will compete for space.
Bandwidth-Aware Execution: Overlap Transfers in Prefill, Reduce Waits in Decoding
Prefill: Use Double Buffering to Hide Load Time
The prefill phase processes input tokens in batches and builds the attention states needed for subsequent generation. After routing completes for a layer, the same expert may serve multiple tokens, so the system can group those tokens and process each group in turn. Compared with token-by-token decoding, this batched computation typically provides a longer window for overlapping transfers.
The basic double-buffering approach works as follows: while the device computes with weights in one buffer, the system asynchronously loads the weights of the next known group of experts into a second buffer. Once loading completes and current computation finishes, the buffers swap roles. This optimizes the time the user actually waits, not the number of bytes transferred. If current computation takes long enough, loading the next group of weights can be largely hidden; if interconnect bandwidth is insufficient, some non-overlapped waiting remains.
Scheduling must not treat later layers whose routing has not yet completed as definitive prefetch targets. Loading weights too early that may never be used wastes bandwidth and may evict more valuable resident experts. Double buffering itself also consumes GPU memory; its capacity should be evaluated together with expert weight block size, workspace requirements, and achievable overlap—not simply made as large as possible.
Decoding: Compare Two Execution Paths for Cache-Missed Experts
Decoding generates tokens based on the previous step’s results. At low concurrency, the tokens routed to the same expert per step are often few, so transferring an entire expert weight block for a small activation may take longer than the matrix computation itself. For experts missed in the cache, compare two paths by end-to-end completion time:
- Transfer to the accelerator for execution: account for weight queuing and transfer, device-side computation, and necessary synchronization; if the weights will be reused, they can also be added to the cache.
- Execute on the host: this requires compatible operators and weight formats on the host side. Estimate the time to send input activations to the host, read weights and compute there, and transfer results back with synchronization.
The second path avoids transferring the entire expert weight block to the device—it does not eliminate all data movement. When host memory bandwidth is constrained, compute threads are saturated, or host-side operators are inefficient, staying on the host is not necessarily faster. The actual choice should be based on recently observed transfer times, host computation times, and queue waits, not on nominal hardware bandwidth. To avoid the two paths flip-flopping at similar costs, set a switching margin: change the choice only when the expected gain covers measurement error and scheduling overhead.
Two Kinds of Caches: Reusing Context States and Expert Weights Separately
Context Checkpoints: Avoid Full Prefill After Local Edits
Multi-turn conversations and document Q&A often reuse the same instructions, history, or source material. The system can save context checkpoints at relatively stable positions—such as the ends of messages or section boundaries—so that subsequent requests can restore existing attention states when the prefix genuinely matches, recomputing only the new or modified parts.
“Semantic awareness” here refers to choosing checkpoint positions; it does not mean that texts with similar meaning can share states. For example, if a user edits only the last paragraph of a document, the system can restore the most recent valid checkpoint before the edit and compute the remaining text; but if the change occurs at the beginning, later paragraphs may have invalid attention states even if unchanged. Reusability should be judged by the actual token prefix and the inference configuration that affects states, not by string similarity or section headings.
Checkpoints also have storage costs. Saving them too sparsely means long suffixes must be recomputed after local edits; saving them too densely increases memory pressure and management overhead. Prioritize saving prefixes with both high reuse probability and high recomputation cost. Shared services should also isolate checkpoints by tenant or data permissions—supporting reuse at the cache level does not justify allowing cross-user state restoration.
Expert Weight Cache: Exploit Temporal Locality in Routing
If a request repeatedly selects the same experts in a layer across adjacent generation steps, keeping recently used weights in memory reduces repeated transfers. Least Recently Used (LRU) is an easily verifiable baseline: update access order on hits, and evict the least recently used entries when space runs out. What the cache stores is expert weights, not past routing results; every new token still needs normal routing and cannot simply reuse the previous step’s expert selection.
Routing locality is not a fixed property. Task switches and interleaved multi-request execution can both shift the distribution of hot experts. Moreover, hit rate alone can be misleading: avoiding one large weight load may save more waiting than avoiding many small loads. When evaluating, track per-layer hit rate, transfer bytes triggered by misses, and actual wait time together. If the problem is concentrated in a few layers, tuning the cache allocation for those layers usually yields clearer gains than blindly enlarging the whole weight cache.
Elastic Resource Management: Adjusting Quotas Without Interrupting Requests
Fixed cache quotas are simple but poorly suited to simultaneously handling short conversations, long documents, and bursty concurrency. One runtime approach: first reserve enough space for in-flight requests, attention states, and essential workspaces, then hand the remaining budget to the expert weight cache. When memory pressure rises, the system can shrink the cache and progressively evict evictable entries; when pressure falls and miss costs remain persistently high, it can grow the cache again. Adjusting quotas does not require restarting the inference process, provided the implementation correctly tracks resource usage.
Specifically, weights that are being read by operators, are waiting for asynchronous transfers to complete, or are still held by scheduled tasks must not be reclaimed early. Shrinking should first stop new entries from occupying space beyond the target, then wait for relevant tasks to release references—rather than forcibly flushing to the target capacity immediately. Pressure thresholds should use high and low watermarks: trigger shrinking only above the high watermark, and allow expansion only after dropping below the low watermark and staying there for a period, so the cache does not oscillate with short-lived load spikes. If reclaimable space is still insufficient, consider throttling admission of new requests or reducing concurrency instead of sacrificing essential state for in-flight requests.
How to Verify That Optimizations Work
Performance testing should distinguish time-to-first-token from steady-state decoding speed. The former mainly reflects queuing, prefill, and the computation cost of the first output token; the latter reflects token throughput once generation begins. Reporting only tokens per second can mask a long initial wait; reporting only average first-token latency can hide intermittent stalls during generation caused by expert cache misses.
Use on-demand loading with a fixed cache quota as the baseline. Under identical model weights, quantization precision, input length, output length, and concurrency, enable double buffering, dynamic execution paths, context checkpoints, and elastic caching one at a time. Report cold-start and warm-cache results separately, and record the median and high percentiles of first-token latency, inter-token intervals, peak memory usage, expert weight transfer volume, and host-side load. Workloads should cover at least three scenarios: long inputs, continuous multi-turn edits, and interleaved multi-request execution, to test transfer overlap, prefix reuse, and cache scheduling respectively. When citing external experiments, state the model scale, quantization method, host memory, and test workload; being able to load a model of a given parameter count does not mean it can generate smoothly at arbitrary context lengths or concurrency.
When putting this into practice, prioritize by critical-path waits: if long inputs stall on expert loading, first measure how much transfer double buffering can hide; if multi-turn edits repeatedly recompute the same prefix, introduce checkpoints first; if output speed fluctuates, trace expert misses and execution paths; if long contexts cause frequent memory pressure, then adjust cache quotas and admission control. This way you can determine whether each mechanism addresses a real bottleneck—and identify whether latency has merely shifted from device transfer to host computation or request queuing.
