You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4912ca3
Browse filesBrowse the repository at this point in the historyBrowse files
Fuse DeepSeek-V4 routed experts into a single QMoE op per layer
DeepSeekV4MoE previously exported routed experts as a hand-rolled Python
loop over num_local_experts (up to 256), emitting one MatMulNBits triple
plus mask/select ops per expert. Replace it with the shared MoELayer's
existing QMoE fusion (already used by DeepSeek-V3), so each layer's
routed experts collapse into a single com.microsoft::QMoE node.
DeepSeekV4Gate's hash-routing/top-k selection math is preserved
byte-for-byte (forward() is reused verbatim). Since hash-table lookups
and DeepSeek-V4's noaux_tc-style bias-corrected top-k select a different
tensor than the one used for weighting, they cannot express selection as
"top-k of a score" (QMoE's only selection ABI). A new
DeepSeekV4Gate.qmoe_routing() adapts the already-selected
(routing_weights, selected_experts) pair into QMoE's full-width
(router_probs, router_weights) tensors via a new
_scatter_selected_to_full helper (ScatterElements-based), reusing
QMoE's optional router_weights input (gathered at the selected experts)
rather than re-deriving selection/weights from a full score tensor.
Clipped-SwiGLU attributes (activation_alpha=1.0, activation_beta=0.0,
swiglu_limit) are threaded through MoELayer/_qmoe_forward with
backward-compatible None defaults, so existing MoELayer callers are
unaffected.
Weight preprocessing renames the gate to mlp.moe.gate.* (was mlp.gate.*)
unconditionally, and routed experts to mlp.moe.experts.*i* only for the
dense (non-QMoE) fallback; the QMoE-eligible path instead wires
stack_per_expert_moe_weights + pack_qmoe_expert_weights over the
unrenamed per-index experts. GGUF tensor mapping is updated to match
the new gate path (3 entries in _DEEPSEEK4_MAPPING); the GGUF
experts mapping is left untouched (pre-existing, separate architectural
gap: GGUF's fused expert tensors are incompatible with both the dense
per-index format and pack_qmoe_expert_weights' expected input, and this
predates this change -- affects V3/Qwen2MoE/Qwen3MoE the same way).
preprocess_weights now validates that every hash-routing table names
top_k distinct experts per token before reaching the graph: ScatterElements
overwrites rather than accumulates duplicate indices, so a repeated
expert would otherwise silently drop a contribution instead of erroring.
Structural evidence (test_qmoe_eligible_quantization_fuses_routed_experts_into_one_qmoe_per_layer):
at the real DeepSeek-V4 scale of 256 routed experts, this reduces a
2-layer graph from 1554 MatMulNBits / 7508 total nodes down to a
constant 18 MatMulNBits / 633 total nodes (independent of expert count),
with exactly one QMoE node per layer.
Numeric correctness (deepseek_v4_test.py) is proven against a pure-numpy
dense reference for hash-routed, clipped-SwiGLU, int4 block-quantized
experts, executed via real ORT CPUExecutionProvider, for both
single-block and multi-block quantization layouts; both new tests were
verified to fail against the pre-change code.
Known runtime gap (pre-existing, not introduced by this change, and
already present in DeepSeek-V3's merged QMoE gate): ORT's CUDA QMoE
kernel ignores the router_weights input entirely, always deriving
per-token weights via its own softmax-top-k over router_probs. This
makes any gate whose selection tensor differs from its weighting tensor
(hash routing, or noaux_tc-style bias-corrected top-k) CPU-EP-correct
only until CUDA support lands -- tracked upstream at
microsoft/onnxruntime#31570 (open draft PR). Documented in
_scatter_selected_to_full, DeepSeekV4Gate.qmoe_routing, and (as a
pre-existing gap made explicit) DeepSeekMoEGate.qmoe_routing.
Also fixes two DeepSeekV4 tests whose assertions referenced the old
mlp.gate.*/mlp.experts.* attribute paths, now mlp.moe.gate.*/
mlp.moe.experts.*.
Co-authored-by: Copilot <[email protected]>
0 commit comments