@@ -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+
6089class 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
270349class 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