Status: Implementation in progress
Date: 2026-09
Implement adaptive expert placement for Qwen 3.6 and other Mixture-of-Experts (MoE) models whose expert weights do not
all fit in GPU memory.
Each expert is resident either on CPU or CUDA. The configured global offload target determines how many experts remain
on CPU, while the complementary set resides on CUDA. Each MoE and QMoE node maintains one exponentially decayed
counter per expert, uses those counters to rank experts, and updates its placement asynchronously.
The placement policy has two levels, both evaluated by the pilot after a complete model inference:
Training, router changes, expert-weight quantization, and multiple CUDA devices are outside this implementation.
An exploratory trace was collected from Qwen3.5-35B-A3B INT4 on CUDA using 10 prompts. It contains 59,880 valid routing
records from 40 QMoE layers, with 256 experts per layer and top-k 8. The frequency ranks below are zero-based and
learned from the complete trace. These preliminary results demonstrate the routing concentration but are not an
end-to-end offload performance measurement.
The first graph compares the fraction of routing decisions selecting an expert at or above a frequency-rank threshold
with the fraction of expert-weight bytes that can be offloaded at that threshold. Keeping only the 64 most frequent
experts of each layer on CUDA retains about 25.1% of expert bytes and covers about 68.9% of observed selections. Keeping
the first 128 experts retains about 50.2% of expert bytes and covers about 90.3% of selections. Keeping the first 192
retains about 75.3% of expert bytes and covers about 98.4% of selections. The curve therefore shows substantial routing
concentration, but it also shows that an aggressive memory reduction sends a meaningful fraction of expert work to CPU.
The second graph compares the same rank-threshold distribution for representative early, middle, and late QMoE layers.
The shapes differ substantially: for example, layer 20 concentrates its selections among fewer experts, while layer 0
uses a broader portion of its expert set. A single fixed per-layer CUDA capacity is therefore unlikely to use the global
budget efficiently. This motivates per-node counters and the second implementation step's redistribution across nodes.
The four numerical policy parameters are exposed as session configuration entries:
| Session option | Parameter | Meaning and valid range |
|---|---|---|
session.moe_cpu_offload_experts |
Offload target | Global integer expert count (>= 1) or proportion (0 < value < 1). |
session.moe_expert_counter_alpha |
alpha |
Counter decay coefficient, finite and >= 0; default 0.9. |
session.moe_expert_counter_beta |
beta |
Increment for a used expert, finite and >= 0; default 0.1. |
session.moe_expert_swap_epsilon |
epsilon |
Relative swap margin, finite and >= 0. |
The optional session.moe_expert_counter_state_file path is configured separately from these four numerical parameters.
The offload target has the following meaning:
1 is the total number of experts to offload to CPU.0 and 1 is the proportion of all experts to offload to CPU. The concrete expert count isceil(value * total_expert_count).1, non-finite values, and counts larger than the totalThe global CUDA budget is:
cuda_expert_count = total_expert_count - cpu_offload_expert_count
One CUDA slot contains all weights required to execute one expert. Moving an expert changes its residency: the expert
leaving CUDA is transferred to CPU before the replacement expert is transferred from CPU to CUDA.
Each MoE and QMoE node owns one counter for each of its experts. After the node executes, every counter is updated
once:
count_{t+1}(e) = alpha * count_t(e) + beta * (1 if expert e was used, otherwise 0)
The used term is binary for one invocation. Selecting the same expert for several rows still contributes 1, not the
number of routed rows.
The policy exposes alpha, beta, and epsilon as validated non-negative parameters. The counter coefficients must
satisfy alpha + beta <= 1; zero is allowed for either coefficient. Counters are updated in place and remain bounded
by the larger of their initial value and 1. Their defaults and constraints are covered by option-parsing tests.
Expert identity is (graph_scope, node_index, node_type, expert_id). Ranking is by descending counter. Ties are resolved
by expert_id, then graph scope and node index, so placement is deterministic.
An optional session setting names a UTF-8 text file containing initial counter values:
session.moe_expert_counter_state_file=<path>
The file starts with a format-version line and contains one line per(graph_scope, node_index, node_type, expert_id, counter_value) record. Loading validates:
Experts omitted from the file start at zero. When the setting is absent, all counters start at zero.
The first offload implementation does not use counters to choose its initial placement. It visits participatingMoE and QMoE kernels in graph order and fills the global CUDA expert budget from the first kernels. If the budget
ends within one kernel, the lowest expert IDs of that boundary kernel remain on CUDA. All remaining experts, including
those of the last kernels, execute on CPU. This placement remains fixed for the lifetime of the session and separates
hybrid CPU/CUDA inference correctness from the adaptive policy. Counter-based placement changes are added only in the
second implementation step.
One invocation uses an immutable expert-to-device mapping:
After execution and counter updates, a node with experts on both CPU and CUDA computes:
cpu_max = maximum counter among CPU experts
cuda_min = minimum counter among CUDA experts
The node exchanges the corresponding experts only when:
cpu_max > (1 + epsilon) * cuda_min
Counter ties use expert ID order. The pilot evaluates placement only after the complete model inference and selects at
most one exchange per node for that boundary. Exchanges are fully asynchronous with respect to the next inference.
Each exchange uses a pinned host staging buffer and an extra CUDA staging slot so the published mapping remains usable
until the complete exchange is ready:
The CPU-bound transfer must always complete before the CPU-to-CUDA transfer starts. An exchange never overwrites the
currently published CUDA slot. If the next inference reaches the same MoE or QMoE before the exchange completes,
the kernel uses the pre-exchange mapping and does not wait. A later invocation observes the new placement only after
the pilot has published the completed exchange.
inference t
-> every MoE/QMoE executes with immutable placement P
-> counters are updated
-> after Run() completes, the pilot selects and enqueues exchanges
inference t+1, node L
-> if L's exchange is incomplete, execute with placement P without waiting
-> if L's exchange is complete, publish and execute with placement P+1
CUDA devices expose their copy-engine count, but that value does not provide a portable expert-level concurrency
guarantee. The initial implementation therefore permits at most two in-flight expert exchanges per CUDA device. Two
independent pinned buffers, two extra CUDA staging slots, and separate device-to-host and host-to-device streams form a
bidirectional pipeline. After exchange A finishes its device-to-host transfer, its host-to-device transfer may overlap
the device-to-host transfer of exchange B. Events preserve the CPU-first ordering within each exchange; buffers and
CUDA slots are never shared by transfers that overlap. Hardware with one copy engine serializes the transfers without
changing correctness. Additional exchanges remain queued for a later completion or inference boundary. For the
measured Qwen model, where one QMoE expert occupies 1,775,616 bytes, this limit requires about 3.4 MiB of pinned staging
memory and 3.4 MiB of temporary CUDA storage.
After each complete model inference, the pilot first publishes completed exchanges, then recomputes how many CUDA
experts belong to each MoE and QMoE node while preserving the global CUDA budget.
The allocation objective is lexicographic:
Within each node, keep the experts with the highest counters. Redistribution may transfer slot ownership between
nodes, whereas a per-node exchange changes the expert stored in a slot without changing that node's slot count.
Redistribution never drains or waits for pending exchanges. It schedules up to the available two-exchange concurrency
limit and leaves additional non-conflicting exchanges queued. A node with an incomplete exchange continues using its
published pre-exchange placement. Slot metadata changes only after both transfer directions complete.
The implementation extends com.microsoft::MoE and com.microsoft::QMoE without changing the exported ONNX model
contract.
The graph node remains assigned to the CUDA execution provider. Its CUDA kernel:
The implementation must verify that initializer prepacking and memory planning can materialize each expert only on its
assigned device. If the existing input-memory contract cannot support this without
regressing the regular CUDA path, an internal graph transformer may insert an experimental MoEWithCPUOffload
operator. That operator must reuse MoE/QMoE schema semantics and kernels and must not become part of the exported
model contract.
When session.moe_cpu_offload_experts is absent, CPU and CUDA MoE/QMoE behavior remains unchanged.
The root SessionState owns a dedicated KernelPilotMoeExpertState shared with its subgraph session states. It contains:
CPU and CUDA kernels obtain their session-owned KernelPilot through OpKernelContext::GetKernelPilot().
The generic pilot in core/framework carries no kernel-specific logic; KernelPilotMoeExpertSelection, defined inkernel_pilot_moe_expert_selection.h, deduplicates selected expert IDs. CUDA-specific copies and events stay in a
provider-side adapter.
After successful kernel execution, the executor commits the collected usage throughKernelPilotMoeExpertState::RecordUsage(), which owns the
counter-update logic. These are internal C++ calls, not a public C API.
Expert identity includes graph scope as well as node and expert IDs, so nodes in different
subgraphs cannot collide. The state persists across Run() calls and is isolated from other sessions.
The CUDA cache manager owns device-specific resources and execution state:
Initialization builds an immutable dictionary from (OpKernel pointer, local expert ID) to a global expert index.
Counters are ordinary values; their update path does not acquire a mutex. Counting and adaptive placement require
non-overlapping Run() calls on a session, enforced at run entry. Distinct kernels may update disjoint expert ranges,
but a kernel cannot execute twice simultaneously. Global snapshots are read between runs.
Each invocation captures a stable placement mapping, and a slot cannot be overwritten until all work using its
previous contents has completed.
Errors are explicit. Invalid configuration, invalid initial state, allocation failures, copy failures, and event
failures fail session initialization or execution rather than silently disabling offload or retaining stale placement.
The session-global expert state and counters are already implemented. The remaining implementation is split into two
steps so that hybrid inference is validated before placement starts changing at runtime. Each pull request includes
the tests and documentation for its own scope.
MoE and QMoE kernels in graph order, splitting only theMoE or QMoE model contract.cpu_max > (1 + epsilon) * cuda_min rule and let the pilot schedule exchanges after inference.MoE starts; use the pre-exchange placement for that invocation.After these two implementation steps, the remaining work is end-to-end measurement. Run reproducible CPU-only,
CUDA-only, and hybrid evaluations with identical models, prompts, and generation settings; sweep offload targets and
policy parameters; and record the metrics listed below. Commit the evaluation scripts, aggregate results, and documented
commands while keeping oversized raw traces outside the repository.
Measure: