Skip to content

Commit 1125b8a

Browse files
authored
Fix Qwen3.6 Olive mixed-precision export (#515)
## Summary - keep Qwen3.5/3.6-MoE GatedDeltaNet projections in floating point for Olive-format checkpoints - keep the shared-expert gate floating point while preserving quantized attention, shared-expert MLP, and fused QMoE experts - preserve existing dense, GPTQ/AWQ, and unquantized graph construction This aligns Mobius with the module selection introduced by microsoft/Olive#2630 and unblocks the multimodal Qwen3.6-35B-A3B recipe in microsoft/olive-recipes#588. ## Validation - `python -m pytest -q src/mobius/models/qwen35_test.py src/mobius/components/_gated_deltanet_test.py src/mobius/components/_moe_test.py` (57 passed) - targeted text/VL tests cover mixed initializer binding, dense Olive behavior, non-Olive graph preservation, and fused QMoE weights --------- Copilot-Session: e10674c1-6909-4b09-9f5a-d41b28c89d2d
1 parent 2481e3c commit 1125b8a

4 files changed

Lines changed: 379 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,40 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Qwen3.5/3.6-MoE mixed float/quantized decoder (Olive checkpoints)
11+
12+
#### Fixed
13+
14+
- **Olive-quantized** Qwen3.5/3.6-**MoE** exports (text and VL) no longer
15+
quantize the modules that Olive's quantization walk skips:
16+
- `linear_attn` (GatedDeltaNet) projections now use plain `Linear`.
17+
- `shared_expert_gate` (the `[1, hidden]` sigmoid gate of `Qwen35MoEBlock`)
18+
now uses plain `Linear`.
19+
20+
Olive's `ModelWrapper` excludes `linear_attn` for the `qwen3_5_moe` /
21+
`qwen3_5_moe_text` model types (`MAMBA` table) and every
22+
`shared_expert_gate` (`SHARED_EXPERT_GATE` table) from quantization since
23+
microsoft/Olive#2630, so those checkpoints never contain the packed
24+
`MatMulNBits` initializers the previous graph expected (e.g.
25+
`linear_attn.in_proj_qkv.weight` as `[48, 2, 8]` uint8 + scales). Ordinary
26+
self-attention, the shared-expert MLP, the dense MLP and the fused
27+
`com.microsoft::QMoE` experts remain quantized; router `gate` behavior is
28+
unchanged.
29+
30+
Scope is deliberately narrow:
31+
- **Dense** Qwen3.5/3.6 (`Qwen35CausalLMModel` and its VL split) is *not* in
32+
Olive's `MAMBA` table, so its `linear_attn` stays quantized.
33+
- Other checkpoint formats retain their previous graph construction; this
34+
change only aligns Mobius with Olive's module selection.
35+
36+
#### Added
37+
38+
- `Qwen2MoELayer(..., shared_expert_gate_class=...)` to override the linear
39+
factory for `shared_expert_gate` only (`None` falls back to `linear_class`,
40+
so existing callers are unaffected).
41+
42+
---
43+
1044
### NVIDIA Cosmos 3 Edge vision-language model (`cosmos3_edge`)
1145

1246
#### Added

‎src/mobius/models/moe.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,20 @@ class Qwen2MoELayer(MoELayer):
276276
Forward: ``routing_output + sigmoid(shared_gate(h)) * shared_expert(h)``
277277
278278
``linear_class``, when provided, is used for the shared expert's MLP and
279-
gate, and is threaded through to :class:`MoELayer` so it also drives the
280-
dense loop-over-experts fallback (used when the config doesn't match the
281-
native QMoE ABI). It has no effect on the fused QMoE path, whose
279+
for ``shared_expert_gate`` unless ``shared_expert_gate_class`` overrides
280+
the gate factory. It is also threaded through to :class:`MoELayer` so it
281+
drives the dense loop-over-experts fallback (used when the config doesn't
282+
match the native QMoE ABI). It has no effect on the fused QMoE path, whose
282283
quantized parameters are constructed directly by
283284
:meth:`MoELayer._init_qmoe_parameters`.
284285
286+
``shared_expert_gate_class`` overrides the factory used for the
287+
``shared_expert_gate`` projection only; when it is ``None`` the gate falls
288+
back to ``linear_class`` (backward compatible). Callers pass an explicit
289+
factory when their checkpoint's quantizer leaves this tiny ``[1, hidden]``
290+
gate in floating point — see
291+
:class:`~mobius.models.qwen35.Qwen35MoEBlock`.
292+
285293
Replicates HuggingFace ``Qwen2MoeSparseMoeBlock``.
286294
"""
287295

@@ -290,18 +298,21 @@ def __init__(
290298
config: ArchitectureConfig,
291299
gate: nn.Module | None = None,
292300
linear_class: type | None = None,
301+
shared_expert_gate_class: type | None = None,
293302
):
294303
super().__init__(config, gate=gate, linear_class=linear_class)
295304
assert config.shared_expert_intermediate_size is not None, (
296305
"Qwen2MoELayer requires config.shared_expert_intermediate_size"
297306
)
298307
if linear_class is None:
299308
linear_class = Linear
309+
if shared_expert_gate_class is None:
310+
shared_expert_gate_class = linear_class
300311
shared_config = dataclasses.replace(
301312
config, intermediate_size=config.shared_expert_intermediate_size
302313
)
303314
self.shared_expert = MLP(shared_config, linear_class=linear_class)
304-
self.shared_expert_gate = linear_class(config.hidden_size, 1, bias=False)
315+
self.shared_expert_gate = shared_expert_gate_class(config.hidden_size, 1, bias=False)
305316

306317
def forward(self, op: OpBuilder, hidden_states: ir.Value):
307318
# Routing expert output: top-k weighted sum [B, S, H]

‎src/mobius/models/qwen35.py‎

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ def _linear_factory(config: ArchitectureConfig) -> type | None:
4242
4343
Returns ``None`` (meaning "use plain ``Linear``") when the model is
4444
unquantized. Shared by the dense (:class:`Qwen35DecoderLayer`) and MoE
45-
(:class:`Qwen35MoEDecoderLayer`) variants so ``self_attn``/``linear_attn``/
46-
``mlp``/``shared_expert`` are all quantized consistently.
45+
(:class:`Qwen35MoEDecoderLayer`) variants so ``self_attn``/``mlp``/
46+
``shared_expert`` are all quantized consistently.
47+
48+
A few modules opt out of this factory for specific quantizers — see
49+
:data:`_FLOAT_MODULE_QUANT_METHODS`.
4750
"""
4851
quantization = config.quantization
4952
if quantization is None or quantization.quant_method == "none":
@@ -57,6 +60,32 @@ def _linear_factory(config: ArchitectureConfig) -> type | None:
5760
)
5861

5962

63+
#: Quantizers that produce Qwen3.5/3.6-**MoE** checkpoints in which the
64+
#: GatedDeltaNet (``linear_attn``) projections and the ``shared_expert_gate``
65+
#: are left in floating point while the rest of the decoder is quantized.
66+
#:
67+
#: Olive's PyTorch-side quantization walk skips both module groups for the
68+
#: MoE model types: ``ModelWrapper.MAMBA`` maps ``qwen3_5_moe`` and
69+
#: ``qwen3_5_moe_text`` to ``linear_attn``, and ``SHARED_EXPERT_GATE``
70+
#: excludes ``shared_expert_gate`` for every model type
71+
#: (microsoft/Olive#2630). The **dense** Qwen3.5 model types are *not* in
72+
#: Olive's ``MAMBA`` table, so dense Olive checkpoints still carry a
73+
#: quantized ``linear_attn`` — the exclusion below is deliberately scoped to
74+
#: the MoE stack (:class:`Qwen35MoEDecoderLayer` / :class:`Qwen35MoEBlock`).
75+
#:
76+
#: Other checkpoint formats retain the graph construction used before this
77+
#: Olive-specific compatibility fix.
78+
_FLOAT_MODULE_QUANT_METHODS = frozenset({"olive"})
79+
80+
81+
def _keeps_modules_float(config: ArchitectureConfig) -> bool:
82+
"""True when the checkpoint's quantizer leaves the opt-out modules float."""
83+
quantization = config.quantization
84+
return (
85+
quantization is not None and quantization.quant_method in _FLOAT_MODULE_QUANT_METHODS
86+
)
87+
88+
6089
class Qwen35DecoderLayer(nn.Module):
6190
"""Qwen3.5 decoder layer with hybrid attention.
6291
@@ -69,11 +98,20 @@ class Qwen35DecoderLayer(nn.Module):
6998
7099
When ``config.quantization`` is set, ``self_attn``/``linear_attn``/``mlp``
71100
projections are built with a quantized-linear factory (see
72-
:func:`_linear_factory`) so quantized checkpoints (e.g. Olive
73-
RTN/GPTQ) load correctly instead of hitting a dense-vs-packed shape
74-
mismatch.
101+
:func:`_linear_factory`) so quantized checkpoints (e.g. Olive RTN/GPTQ)
102+
load correctly instead of hitting a dense-vs-packed shape mismatch.
103+
104+
``linear_attn`` (GatedDeltaNet) stays quantized here: Olive's ``MAMBA``
105+
exclusion table only covers the MoE model types, so dense Qwen3.5
106+
checkpoints do carry packed linear-attention projections. The MoE stack
107+
overrides this via :attr:`float_linear_attn_quant_methods` (see
108+
:class:`Qwen35MoEDecoderLayer`).
75109
"""
76110

111+
#: Quantization methods whose checkpoints leave ``linear_attn`` float.
112+
#: Empty for the dense stack; see :data:`_FLOAT_MODULE_QUANT_METHODS`.
113+
float_linear_attn_quant_methods: frozenset[str] = frozenset()
114+
77115
def __init__(self, config: ArchitectureConfig, layer_idx: int):
78116
super().__init__()
79117
layer_types = config.layer_types or []
@@ -83,7 +121,9 @@ def __init__(self, config: ArchitectureConfig, layer_idx: int):
83121
linear_class = _linear_factory(config)
84122

85123
if self.layer_type == "linear_attention":
86-
self.linear_attn = GatedDeltaNet(config, linear_class=linear_class)
124+
self.linear_attn = GatedDeltaNet(
125+
config, linear_class=self._linear_attn_class(config, linear_class)
126+
)
87127
else:
88128
self.self_attn = Qwen35Attention(config, linear_class=linear_class)
89129

@@ -93,6 +133,23 @@ def __init__(self, config: ArchitectureConfig, layer_idx: int):
93133
config.hidden_size, eps=config.rms_norm_eps
94134
)
95135

136+
@classmethod
137+
def _linear_attn_class(
138+
cls, config: ArchitectureConfig, linear_class: type | None
139+
) -> type | None:
140+
"""Linear factory for ``linear_attn`` — ``None`` means plain float.
141+
142+
Returns ``None`` when the checkpoint's quantizer left GatedDeltaNet in
143+
floating point (a quantized graph would then expect packed
144+
``MatMulNBits`` initializers the checkpoint never contains); otherwise
145+
the same factory used for the rest of the layer.
146+
"""
147+
quantization = config.quantization
148+
method = quantization.quant_method if quantization is not None else None
149+
if method in cls.float_linear_attn_quant_methods:
150+
return None
151+
return linear_class
152+
96153
def forward(
97154
self,
98155
op: OpBuilder,
@@ -263,9 +320,31 @@ class Qwen35MoEBlock(Qwen2MoELayer):
263320
shared_expert.{gate,up,down}_proj.weight
264321
shared_expert_gate.weight → sigmoid gate for shared expert
265322
266-
Retained as a named subclass for readability within the Qwen3.5 hybrid stack.
323+
Retained as a named subclass for readability within the Qwen3.5 hybrid stack,
324+
and to keep ``shared_expert_gate`` in plain floating :class:`Linear` for the
325+
quantizers listed in :data:`_FLOAT_MODULE_QUANT_METHODS`: Olive excludes
326+
this ``[1, hidden]`` gate from its quantization walk (microsoft/Olive#2630),
327+
so a quantized gate module would expect packed ``MatMulNBits`` initializers
328+
that such a checkpoint never contains. Other checkpoint formats retain
329+
the previous graph construction. The router ``gate`` (top-k logits) is
330+
unaffected — it is created by
331+
:class:`~mobius.components._moe.MoELayer` and was never quantized.
267332
"""
268333

334+
def __init__(
335+
self,
336+
config: ArchitectureConfig,
337+
gate: nn.Module | None = None,
338+
linear_class: type | None = None,
339+
):
340+
super().__init__(
341+
config,
342+
gate=gate,
343+
linear_class=linear_class,
344+
# ``None`` falls back to ``linear_class``.
345+
shared_expert_gate_class=Linear if _keeps_modules_float(config) else None,
346+
)
347+
269348

270349
class Qwen35MoEDecoderLayer(Qwen35DecoderLayer):
271350
"""Qwen3.5-MoE decoder layer with hybrid attention and MoE FFN.
@@ -276,12 +355,26 @@ class Qwen35MoEDecoderLayer(Qwen35DecoderLayer):
276355
277356
The routed experts are quantized via the fused ``com.microsoft::QMoE``
278357
path (see :class:`~mobius.components._moe.MoELayer`), driven directly
279-
by ``config.quantization``. The always-active ``shared_expert``/
280-
``shared_expert_gate`` are not part of that fused op, so they are
281-
quantized separately via the same ``linear_class`` factory used for
282-
``self_attn``/``linear_attn``/dense ``mlp`` (see :func:`_linear_factory`).
358+
by ``config.quantization``. The always-active ``shared_expert`` is not
359+
part of that fused op, so it is quantized separately via the same
360+
``linear_class`` factory used for ``self_attn``/dense ``mlp`` (see
361+
:func:`_linear_factory`).
362+
363+
For Olive-quantized MoE checkpoints only, ``linear_attn`` (via
364+
:attr:`float_linear_attn_quant_methods`) and ``shared_expert_gate`` (see
365+
:class:`Qwen35MoEBlock`) stay in floating point, matching the modules
366+
Olive excludes from its quantization walk for ``qwen3_5_moe`` /
367+
``qwen3_5_moe_text``. Other checkpoint formats retain the previous graph
368+
construction.
369+
370+
Note: ``super().__init__`` builds a dense :class:`MLP` that is immediately
371+
replaced by the MoE block here, so the dense module never reaches the
372+
exported graph.
283373
"""
284374

375+
#: Olive leaves the MoE stack's GatedDeltaNet projections float.
376+
float_linear_attn_quant_methods: frozenset[str] = _FLOAT_MODULE_QUANT_METHODS
377+
285378
def __init__(self, config: ArchitectureConfig, layer_idx: int):
286379
super().__init__(config, layer_idx)
287380
self.mlp = Qwen35MoEBlock(config, linear_class=_linear_factory(config))

0 commit comments

Comments
 (0)