Repository navigation
Thread linear_class through Qwen3.5 self_attn/linear_attn/shared_expert - #493
titaiwangms wants to merge 1 commit into
Conversation
…_expert Qwen35Attention, GatedDeltaNet, and Qwen2MoELayer's shared_expert (reused by Qwen35MoEBlock) all hardcoded plain Linear regardless of config.quantization, unlike the generic Attention/MLP classes used elsewhere in Mobius. Since Mobius rebuilds the ONNX graph from scratch rather than tracing the PyTorch model, this meant the corresponding initializer slots were always built expecting dense float weights -- so a quantized checkpoint's packed uint8 tensors for these modules could never load, failing with a shape mismatch (or, before #491's suffix fix, "initializer without weights"). Fix: - Add an optional `linear_class` constructor parameter to Qwen35Attention (q/k/v/o_proj) and GatedDeltaNet (in_proj_qkv/z/b/a, out_proj), matching the pattern already used by the generic Attention/MLP classes. GatedDeltaNet's dt_bias/A_log/conv1d.weight are small recurrent-state parameters and are intentionally left unquantized regardless of this argument. - Add an optional `linear_class` parameter to Qwen2MoELayer for its shared_expert/shared_expert_gate (the routed experts are already quantized separately via the fused QMoE path, driven directly by config.quantization -- this only covers the always-active shared expert, which isn't part of that fused op). Defaults to None (plain Linear), so existing Qwen2MoEDecoderLayer callers are unaffected. - Add a `_linear_factory(config)` helper in qwen35.py (mirrors the existing pattern in deepseek.py) and thread it through Qwen35DecoderLayer/Qwen35MoEDecoderLayer's self_attn/linear_attn/mlp construction. Verified end-to-end: Olive Rtn(bits=4, group_size=16, sym=true, moe=true) -> MobiusBuilder against optimum-intel-internal-testing/tiny-random-qwen3.5-moe now succeeds without excluding linear_attn/shared_expert via modules_to_not_convert -- self_attn/linear_attn/shared_expert weights save as quantized u8 tensors and a full ONNX model + genai_config are produced. Fixes #492.
|
|
Performance Comparison
|
🏗️ Architecture Diff
No architecture changes detected. ✅ Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed) |
There was a problem hiding this comment.
Pull request overview
Adds quantized linear wiring across Qwen3.5 attention, DeltaNet, dense MLP, and shared-expert paths, building on #491’s weight-suffix fix.
Changes:
- Introduces a Qwen3.5 quantized-linear factory.
- Makes attention and DeltaNet projections injectable.
- Quantizes Qwen MoE shared experts and gates.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/mobius/models/qwen35.py |
Threads quantized linear construction through Qwen3.5 layers. |
src/mobius/models/moe.py |
Adds configurable shared-expert linear layers. |
src/mobius/components/_gated_deltanet.py |
Supports injected projection factories. |
src/mobius/components/_attention.py |
Supports injected Q/K/V/O projection factories. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| from mobius.components._gated_deltanet import GatedDeltaNet | ||
| from mobius.components._mlp import MLP | ||
| from mobius.components._moe import _supported_qmoe_quantization | ||
| from mobius.components._quantized_linear import make_quantized_linear_factory |
| self.layer_type: str = ( | ||
| layer_types[layer_idx] if layer_idx < len(layer_types) else "full_attention" | ||
| ) | ||
| linear_class = _linear_factory(config) |
Stacked on #491
This PR is stacked on #491 (the Olive suffix fix) — its base branch is
fix/olive-suffix-mismatch, notmain. Please merge #491 first; this diff will shrink to just this PR's own commit once that lands and the base is retargeted tomain.Problem (#492)
For Qwen3.5/3.6's hybrid DeltaNet + full-attention MoE architecture, only the fused MoE expert path had real quantization wiring. Three call sites hardcoded plain
Linearregardless ofconfig.quantization, unlike the genericAttention/MLPclasses used elsewhere in Mobius:Qwen35Attention(full-attention layers):q_proj/k_proj/v_proj/o_projGatedDeltaNet(linear-attention layers):in_proj_qkv/in_proj_z/in_proj_b/in_proj_a/out_projQwen2MoELayer'sshared_expert/shared_expert_gate(reused byQwen35MoEBlock) — the always-active shared expert isn't part of the fused QMoE op, so it needs its own quantization wiring separate from the routed expertsSince Mobius rebuilds the ONNX graph from scratch (rather than tracing the PyTorch model), the initializer slots for these modules were always built expecting dense float weights, so a quantized checkpoint's packed weights could never load — surfacing as a shape mismatch (e.g.
model.layers.3.self_attn.k_proj.weight: model expects [16, 32], got [16, 2, 8]).Fix
linear_classconstructor parameter toQwen35AttentionandGatedDeltaNet, matching the pattern already used by the genericAttention/MLPclasses.GatedDeltaNet'sdt_bias/A_log/conv1d.weightare small recurrent-state parameters and are intentionally left unquantized regardless of this argument (open design question from Qwen3.5/3.6: quantization not wired for self_attn/linear_attn/dense MLP (only MoE experts are) #492, resolved here: always float).linear_classparameter toQwen2MoELayerforshared_expert/shared_expert_gate. Defaults toNone(plainLinear), so existingQwen2MoEDecoderLayer(Qwen2-MoE) callers are unaffected._linear_factory(config)helper inqwen35.py(mirrors the existing pattern already used indeepseek.py) and threaded it throughQwen35DecoderLayer/Qwen35MoEDecoderLayer'sself_attn/linear_attn/mlpconstruction.Testing
pytest src/mobius/models/qwen35_test.py src/mobius/models/_qwen35_mtp_test.py src/mobius/components/_attention_test.py src/mobius/components/_gated_deltanet_test.py src/mobius/components/_moe_test.py src/mobius/components/_mlp_test.py src/mobius/_weight_utils_test.py src/mobius/models/deepseek_test.py— 198 passed.ruff checkon all changed files — clean.Rtn(bits=4, group_size=16, sym=true, moe=true)->MobiusBuilderagainstoptimum-intel-internal-testing/tiny-random-qwen3.5-moe, without excludinglinear_attn/shared_expertviamodules_to_not_convert, now succeeds end-to-end:self_attn/linear_attn/shared_expertweights save as quantizedu8tensors, and a full ONNX model +genai_config.jsonare produced.Closes #492.