Skip to content

Commit 087e15f

Browse files
justinchubyCopilot
andcommitted
Fix DeepSeek MoE routed experts losing linear_class quantization
_DeepSeekMoEFFN.__init__ threaded linear_class into _SharedExpertMLP but not into the MoELayer that builds the routed-expert dense-loop fallback. A quantized config therefore quantized attention, dense FFN, and the shared expert, but silently left every routed MoE expert's gate/up/down projections as plain float MatMul -- losing quantization and breaking the fuse_dense_moe_to_qmoe post-hoc rewrite, which only pattern-matches a quantized MatMulNBits dense-fallback shape. Fix: pass linear_class through to MoELayer's routed-expert construction (one line). DeepSeek-V4's MoE FFN is unaffected -- it recomputes its own quantized class per-expert independently of MoELayer's linear_class and was already correct. Added test_deepseek_moe_ffn_linear_class_reaches_routed_experts, which isolates and asserts on module.moe.experts vs module.shared_experts separately (not just an aggregate creation count) so the regression is caught precisely. Verified: fails on pre-fix code, passes post-fix. Full suite (3249 passed, 9 skipped) and ruff check/format clean. Co-authored-by: Copilot <[email protected]>
1 parent bed2983 commit 087e15f

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎src/mobius/models/deepseek.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,14 @@ def __init__(
315315
super().__init__()
316316
assert config.num_local_experts is not None
317317
assert config.moe_intermediate_size is not None
318-
self.moe = MoELayer(config, gate=gate)
318+
# ``linear_class`` must reach the routed-expert dense-loop fallback
319+
# too (not just the shared expert below): otherwise a quantized
320+
# config quantizes every other linear in the model (attention,
321+
# dense FFN, shared expert) but silently leaves the routed MoE
322+
# experts as plain float `MatMul`, which both loses quantization
323+
# and breaks the `fuse_dense_moe_to_qmoe` post-hoc rewrite (it
324+
# only matches a quantized `MatMulNBits` dense-fallback pattern).
325+
self.moe = MoELayer(config, gate=gate, linear_class=linear_class)
319326
# Shared expert uses moe_intermediate_size * n_shared_experts
320327
n_shared = config.n_shared_experts or 1
321328
shared_intermediate = config.moe_intermediate_size * n_shared

‎src/mobius/models/deepseek_test.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,57 @@ def test_deepseek_dense_moe_fallback_matches_numpy_reference():
9797
assert all(node.domain != "com.microsoft" for node in graph)
9898

9999

100+
def test_deepseek_moe_ffn_linear_class_reaches_routed_experts():
101+
"""``linear_class`` must reach the routed dense-loop experts, not just the shared expert.
102+
103+
Regression test for a bug where ``_DeepSeekMoEFFN`` constructed its
104+
``MoELayer`` without forwarding ``linear_class``, so a quantized config
105+
quantized attention/dense-FFN/shared-expert linears but silently left the
106+
routed MoE experts as plain float ``MatMul`` -- losing quantization and
107+
breaking the ``fuse_dense_moe_to_qmoe`` post-hoc rewrite, which only
108+
matches a quantized ``MatMulNBits`` dense-fallback pattern.
109+
"""
110+
from mobius.components._common import Linear
111+
112+
created: list[Linear] = []
113+
114+
class TrackingLinear(Linear):
115+
def __init__(self, *args, **kwargs):
116+
super().__init__(*args, **kwargs)
117+
created.append(self)
118+
119+
config = make_config(
120+
hidden_size=4,
121+
intermediate_size=8,
122+
moe_intermediate_size=3,
123+
num_local_experts=4,
124+
num_experts_per_tok=2,
125+
n_group=2,
126+
topk_group=1,
127+
n_shared_experts=1,
128+
scoring_func="sigmoid",
129+
topk_method="noaux_tc",
130+
)
131+
module = _DeepSeekMoEFFN(config, DeepSeekMoEGate(config), linear_class=TrackingLinear)
132+
133+
# The dense loop-over-experts fallback must have been built with
134+
# TrackingLinear for every routed expert's projections. Check
135+
# `module.moe.experts` directly (not the global `created` list) so this
136+
# assertion can't pass merely because the shared expert below picked up
137+
# the class -- that path already worked before this fix.
138+
assert module.moe.experts is not None
139+
routed_linears = [m for m in module.moe.experts.modules() if isinstance(m, Linear)]
140+
assert len(routed_linears) > 0
141+
assert all(isinstance(m, TrackingLinear) for m in routed_linears)
142+
# The shared expert must still be quantized too (it already worked
143+
# before this fix; guard against a future regression there as well).
144+
shared_linears = [m for m in module.shared_experts.modules() if isinstance(m, Linear)]
145+
assert len(shared_linears) > 0
146+
assert all(isinstance(m, TrackingLinear) for m in shared_linears)
147+
# Sanity: the tracking list saw both groups (routed + shared).
148+
assert len(created) == len(routed_linears) + len(shared_linears)
149+
150+
100151
def test_noaux_tc_supports_single_expert_groups():
101152
config = make_config(
102153
hidden_size=4,

0 commit comments

Comments
 (0)