Repository navigation
Fix QMoE scale precision: scales silently forced entire MoE FFN onto CPU EP - #505
Merged
Merged
Conversation
Copilot stopped reviewing on behalf of
titaiwangms due to an error
August 17, 2026 15:04
Performance Comparison
|
🏗️ Architecture Diff
No architecture changes detected. ✅ Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed) |
- Fix pre-existing test test_deepseek_v2_lite_int4_uses_one_qmoe_per_moe_layer in _group_query_attention_test.py: it hardcoded the old (buggy) FLOAT expectation for QMoE scale dtypes, missed by the original fix's test sweep. Confirmed via test run that this was a real CI-breaking regression; updated to FLOAT16 and corrected the stale comment. - Make the BF16/FLOAT/FLOAT16 parametrized tests in _qmoe_fusion_test.py non-vacuous: thread activation_dtype into the synthetic MatMulNBits scale fixtures (previously hardcoded FLOAT16 regardless of the parametrization), so the tests actually exercise mixed source/target dtype packing. - Harden fuse_dense_moe_to_qmoe's activation-dtype guard: fall back to an expert's existing MatMulNBits scale dtype when layer.hidden.dtype is None (common for untyped, graph-internal values in real models), instead of silently skipping the layer. Added a regression test (test_fuses_when_hidden_dtype_is_untyped). - Add fc2 scale content assertion to test_scales_match_activation_dtype (previously only fc1 was checked). Full suite: 1963 passed, 2 skipped. ruff check + format clean.
Contributor
Author
Full review pass — findings & fixesRan a 4-reviewer pass (code / critical / deep / integration) over this PR. Summary: Confirmed & fixed (this commit):
Verified correct, no fix needed:
Discussed, deferred as follow-up (not blocking this PR):
Full suite after fixes: 1963 passed, 2 skipped. |
justinchuby
approved these changes
Aug 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
When exporting a hybrid MoE model (e.g.
Qwen/Qwen3.6-35B-A3B) via the native QMoE-emission path inMoELayer(_moe.py) at a non-float32 precision (e.g.fp16for CUDA), thefc1_scales/fc2_scalesparameters were pinned with_keep_float32 = True, which_cast_module_dtype()(_builder.py) honors by skipping them during the FLOAT->target-dtype cast pass. Every other parameter (weights, activations) was correctly cast to FLOAT16, but the scales stayed FLOAT32.ONNX Runtime's
com.microsoft::QMoEkernel (quant_type="int") requires itsT2type constraint (used byfc1_scales/fc2_scales) to be exactly equal toT(the activation dtype) — confirmed by readingonnxruntime/contrib_ops/{cuda,cpu}/moe/moe_quantization*.{cc,h}. There is no registered kernel variant forT2=FLOAT32whileT=FLOAT16.Because of this mismatch, ONNX Runtime silently fails to find any matching QMoE kernel — on either CPU or CUDA EP — and falls back to placing every QMoE node on
CPUExecutionProvider, insertingInsertedPrecisionFreeCast_*bridging nodes. Since QMoE is the entire MoE FFN compute (the dominant cost for a large MoE model), this silently forced 100% of the MoE compute onto CPU even when the workflow explicitly targeted CUDA — and also brokeenable_cuda_graphin ONNX Runtime GenAI, since not all decoder nodes were assigned to the same EP.Fix
_moe.py: stop pinningfc1_scales/fc2_scalesto FLOAT32; let them be downcast to the model's target export precision (FP16/BF16) like every other float parameter._qmoe_fusion.py(the separate "dense-fallback -> QMoE" rewrite rule): had the same class of bug — scales and router logits were hardcoded to FLOAT/FLOAT32 regardless of the model's activation dtype. Fixed to use the activation dtype instead._moe_test.pyand_qmoe_fusion_test.pyto reflect the corrected (dtype-matching) behavior, and added explicit FP32/FP16/BF16 coverage in_qmoe_fusion_test.py.Validation
python -m pytest src/mobius/components/_moe_test.py src/mobius/rewrite_rules/_qmoe_fusion_test.py -v— 39 passed.ruff— passed.Rtn(moe=true)+MobiusBuilder(precision=fp16, CUDA EP)export againstQwen/Qwen3.6-35B-A3B. All 430 QMoE scale initializers in the exported graph are now FLOAT16 (previously FLOAT32).onnxruntime(verbose session logging): zero "CUDA kernel not found in registries" messages for QMoE, and all 40 QMoE nodes are now correctly assigned toCUDAExecutionProvider(previously all 40 fell back to CPU).onnxruntime_genai.Model(...)now loads successfully withenable_cuda_graph=1ingenai_config.json, with no manual workaround needed (previously required manually strippingenable_cuda_graphfrom the generated config).Related: builds on top of the export fixes merged in #495.