Skip to content

Thread linear_class through Qwen3.5 self_attn/linear_attn/shared_expert - #493

Closed
titaiwangms wants to merge 1 commit into
fix/olive-suffix-mismatchfrom
fix/qwen35-linear-class-threading
Closed

titaiwangms wants to merge 1 commit into
fix/olive-suffix-mismatchfrom
fix/qwen35-linear-class-threading

Conversation

@titaiwangms

Copy link
Copy Markdown
Contributor

Stacked on #491

This PR is stacked on #491 (the Olive suffix fix) — its base branch is fix/olive-suffix-mismatch, not main. Please merge #491 first; this diff will shrink to just this PR's own commit once that lands and the base is retargeted to main.

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 Linear regardless of config.quantization, unlike the generic Attention/MLP classes used elsewhere in Mobius:

  • Qwen35Attention (full-attention layers): q_proj/k_proj/v_proj/o_proj
  • GatedDeltaNet (linear-attention layers): in_proj_qkv/in_proj_z/in_proj_b/in_proj_a/out_proj
  • Qwen2MoELayer's shared_expert/shared_expert_gate (reused by Qwen35MoEBlock) — the always-active shared expert isn't part of the fused QMoE op, so it needs its own quantization wiring separate from the routed experts

Since 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

  • Added an optional linear_class constructor parameter to Qwen35Attention and GatedDeltaNet, 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 (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).
  • Added an optional linear_class parameter to Qwen2MoELayer for shared_expert/shared_expert_gate. Defaults to None (plain Linear), so existing Qwen2MoEDecoderLayer (Qwen2-MoE) callers are unaffected.
  • Added a _linear_factory(config) helper in qwen35.py (mirrors the existing pattern already used in deepseek.py) and threaded it through Qwen35DecoderLayer/Qwen35MoEDecoderLayer's self_attn/linear_attn/mlp construction.

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 check on all changed files — clean.
  • 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, without excluding linear_attn/shared_expert via modules_to_not_convert, now succeeds end-to-end: self_attn/linear_attn/shared_expert weights save as quantized u8 tensors, and a full ONNX model + genai_config.json are produced.

Closes #492.

…_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.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions

Copy link
Copy Markdown

Performance Comparison

Comparing 1a88a3c → 70de6ad

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0% ⚪
bert (feature-extraction) num_nodes 60 60 +0.0% ⚪
falcon model_size_bytes 364 KB 364 KB +0.0% ⚪
falcon num_nodes 66 66 +0.0% ⚪
gemma2 model_size_bytes 428 KB 428 KB +0.0% ⚪
gemma2 num_nodes 105 105 +0.0% ⚪
gpt2 model_size_bytes 388 KB 388 KB +0.0% ⚪
gpt2 num_nodes 54 54 +0.0% ⚪
llama model_size_bytes 425 KB 425 KB +0.0% ⚪
llama num_nodes 60 60 +0.0% ⚪
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
llama (static-cache) num_nodes 56 56 +0.0% ⚪
mamba (ssm-text-generation) model_size_bytes 296 KB 296 KB +0.0% ⚪
mamba (ssm-text-generation) num_nodes 94 94 +0.0% ⚪
phi3 model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 num_nodes 58 58 +0.0% ⚪
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 (static-cache) num_nodes 54 54 +0.0% ⚪
qwen2 model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 num_nodes 60 60 +0.0% ⚪
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 (static-cache) num_nodes 56 56 +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) num_nodes 264 264 +0.0% ⚪
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0% ⚪
qwen3_5_text (hybrid-text-generation) num_nodes 126 126 +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) num_nodes 428 428 +0.0% ⚪
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0% ⚪
t5 (seq2seq) num_nodes 166 166 +0.0% ⚪
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0% ⚪
whisper (speech-to-text) num_nodes 128 128 +0.0% ⚪

No performance regressions.

@github-actions

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 1a88a3c → 70de6ad

Model Sub-model Changes Status
bert (feature-extraction) model 0 ⚪
falcon model 0 ⚪
gemma2 model 0 ⚪
gemma4 (gemma4) decoder 0 ⚪
gemma4 (gemma4) embedding 0 ⚪
gemma4 (gemma4) vision_encoder 0 ⚪
gemma4_text model 0 ⚪
gpt2 model 0 ⚪
llama model 0 ⚪
llama (static-cache) model 0 ⚪
mamba (ssm-text-generation) model 0 ⚪
phi3 model 0 ⚪
phi3 (static-cache) model 0 ⚪
qwen model 0 ⚪
qwen (static-cache) model 0 ⚪
qwen2 model 0 ⚪
qwen2 (static-cache) model 0 ⚪
qwen2_moe model 0 ⚪
qwen2_moe (static-cache) model 0 ⚪
qwen3 model 0 ⚪
qwen3 (static-cache) model 0 ⚪
qwen3_5_moe (hybrid-text-generation) model 0 ⚪
qwen3_5_text (hybrid-text-generation) model 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) decoder 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) embedding 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) vision_encoder 0 ⚪
qwen3_moe model 0 ⚪
qwen3_moe (static-cache) model 0 ⚪
qwen3_next (hybrid-text-generation) model 0 ⚪
t5 (seq2seq) decoder 0 ⚪
t5 (seq2seq) encoder 0 ⚪
whisper (speech-to-text) decoder 0 ⚪
whisper (speech-to-text) encoder 0 ⚪

No architecture changes detected. ✅


Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
@titaiwangms

Copy link
Copy Markdown
Contributor Author

Consolidated into #495 along with #491 and #494 to simplify review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants