[AWQ] Balance the MoE router in `build_hybrid_attention_mappings` (Qwen3-Next / Qwen3.5-MoE / Qwen3.6) #3338
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
twu3202
SUMMARY:
Fixes #3337.
build_hybrid_attention_mappingssmoothspost_attention_layernorminto the routed experts, the shared expert andshared_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 computesW_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_mappingsalready uses forQwen3MoeForCausalLM, 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 Parameterhere. The Qwen3.5 router is not annn.Linear: #2482 even swaps it for aLinearin 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.weightis all that is needed. That is exactly what"re:.*mlp.gate$"already does forQwen3MoeTopKRouter, which is not aLineareither. cc @brian-dellabetta, since this closes the TODO from #2451.It is enough on its own:
Linear._compute_layer_meansskips the router with the same warning it already gives for an ignoredshared_expert_gate, once per MoE layer. I'm happy to silence it in this PR or a follow-up._smoothmultipliesrouter.weightbyscales.view(1, -1), soF.linear(x / s, W_r · s) = F.linear(x, W_r). The parent module staysmlp.Side effects worth knowing:
balance_layers[0], which is the module AWQ hooks to collect activation statistics. Under the defaultmoe_calibrate_all_experts=Truethose statistics are identical. With it off, they now cover all tokens instead of only the tokens routed to expert 0.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 aweightand noLinear, every MoEpost_attention_layernormresolves with its ownmlp.gateexactly once, under parentmodel.layers.{i}.mlp.test_smoothing_preserves_qwen3_5_moe_function: a tiny randomQwen3_5MoeForCausalLM(CPU, fp32, experts linearized, offset norms undernorm_calibration_context, routers and norms re-initialised). The test asserts that aQwen3_5MoeTopKRouteris resolved as a balance layer. It then applies random per-channel scales through every resolved mapping as_smoothdoes (re-implemented in the test, since_smoothis a closure) and requires a logit change below 1e-4.TEST PLAN:
pytest tests/llmcompressor/modifiers/transform/awq/on CPU, onmain427b72bcb with compressed-tensorsmain2e13ef2 from source:- with this change: 47 passed, 3 skipped (GPU-only);
- without it: the two new tests fail and everything else passes.
ruff checkis clean, andruff formatleaves the new code unchanged.AWQModifier→GPTQModifieroneshoton 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.