← Back to Pull requests

[AWQ] Balance the MoE router in `build_hybrid_attention_mappings` (Qwen3-Next / Qwen3.5-MoE / Qwen3.6) #3338

Open
unknown opened this pull request with 567 lines diff across 7 files
View on GitHub
unknown unknown commented on Oct 20, 2026 06:41 UTC
Author

SUMMARY:
Fixes #3337.

build_hybrid_attention_mappings smooths post_attention_layernorm into the routed experts, the shared expert and shared_expert_gate. It skips the router (mlp.gate), which reads the same activations. This is the mapping that Qwen3-Next, Qwen3.5-MoE and Qwen3.6 MoE models get by default. After AWQ the router computes W_r · (x / s), so its logits shift and part of the top-k expert choices change. On a Qwen3.6-35B-A3B W4A16 build, 14–17 % of the top-8 slots changed with identical inputs. Compensating only the routers cut that build's KL to BF16 on zh/en wiki text by 24–41 %. Details and a CPU reproduction are in the issue.

The fix adds "re:.mlp.gate$" to that balance list, the same entry _moe_default_mappings already uses for Qwen3MoeForCausalLM, and removes the TODO.

About that TODO: #2451 balanced the router in the static MoE mappings and left # TODO: should add "re:.mlp.gate.weight$" but is a Parameter here. The Qwen3.5 router is not an nn.Linear: #2482 even swaps it for a Linear in its calibration block, so that it lands in the saved ignore list. But it is a module (Qwen3_5MoeTopKRouter / Qwen3NextTopKRouter) that holds that Parameter as .weight. Mappings match modules by name, so pointing at the module rather than at .weight is all that is needed. That is exactly what "re:.*mlp.gate$" already does for Qwen3MoeTopKRouter, which is not a Linear either. cc @brian-dellabetta, since this closes the TODO from #2451.

It is enough on its own:

  • Resolution matches balance layers by name and does not require Linear.

  • Grid search only patches quantized balance layers. The router is ignored for quantization, so the search is unchanged; _compute_layer_means skips the router with the same warning it already gives for an ignored shared_expert_gate, once per MoE layer. I'm happy to silence it in this PR or a follow-up.

  • _smooth multiplies router.weight by scales.view(1, -1), so F.linear(x / s, W_r · s) = F.linear(x, W_r). The parent module stays mlp.
  • Side effects worth knowing:

  • Activation stats: the router becomes balance_layers[0], which is the module AWQ hooks to collect activation statistics. Under the default moe_calibrate_all_experts=True those statistics are identical. With it off, they now cover all tokens instead of only the tokens routed to expert 0.

  • Grid search consistency: before this change, the grid search scored candidates with the original routing, while the saved model routed differently. Now the two agree.

  • Dense layers: Qwen3-Next can have dense layers (mlp_only_layers / decoder_sparse_step). None of the MoE patterns match them, so nothing changes there. Released checkpoints are MoE in every layer.
  • Tests, in test_dynamic_mappings.py::TestHybridMoeRouterBalancing:

  • test_router_is_resolved_as_balance_layer: on the synthetic hybrid MoE model, with a router that is a plain module with a weight and no Linear, every MoE post_attention_layernorm resolves with its own mlp.gate exactly once, under parent model.layers.{i}.mlp.

  • test_smoothing_preserves_qwen3_5_moe_function: a tiny random Qwen3_5MoeForCausalLM (CPU, fp32, experts linearized, offset norms under norm_calibration_context, routers and norms re-initialised). The test asserts that a Qwen3_5MoeTopKRouter is resolved as a balance layer. It then applies random per-channel scales through every resolved mapping as _smooth does (re-implemented in the test, since _smooth is a closure) and requires a logit change below 1e-4.
  • TEST PLAN:

  • pytest tests/llmcompressor/modifiers/transform/awq/ on CPU, on main 427b72bcb with compressed-tensors main 2e13ef2 from source:

  • - with this change: 47 passed, 3 skipped (GPU-only);
    - without it: the two new tests fail and everything else passes.
  • ruff check is clean, and ruff format leaves the new code unchanged.

  • GPU, end to end: the same router entry in an AWQModifier → GPTQModifier oneshot on CUDA (llm-compressor 0.14.0, tiny Qwen3.6-style model, on-device and offloaded sequential pipelines).

  • - With the router: every router's columns end up scaled like its smoothed norm, within bf16 rounding (≤ 0.56 %), and the exports load in vLLM 0.31.
    - With the stock mappings: all routers stay byte-identical to the base, off by up to 21.6 % from what their smoothed norms require.
  • I have not run a before/after accuracy comparison on a public checkpoint with stock vs fixed mappings. I can add one if you'd like.
  • Diff: 567 lines (+425 / -142) across 7 files Reviewer: twu3202 Review duration: 7573m
    All Vouch review confidence checks passed
    Residual risk 0.29 = Change Risk (0.55) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.55 · 567 lines diff across 7 files
    0.55
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.379
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #3338"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 54.7% (MEDIUM) · Higher = larger blast radius
    54.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 29.2% · Safe to merge
    29.2%