Skip to content

fix(awq): compensate dense expert gate/up under the MoE root - #6451

Merged
Qubitium merged 1 commit into
mainfrom
fix/glm5-next-awq-shared-experts
Nov 29, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/glm5-next-awq-shared-experts

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

The generic AWQ planner folds the norm in front of a :moe root (post_attention_layernorm) into every routed expert's gate_proj/up_proj. _moe_block_consumers compensated only the direct children of that root that own a 2-D weight, i.e. routers and shared_expert_gate.

GLM-5.3 (glm5_next) keeps its shared experts dense:

"shared_experts:shared": ("gate_proj:!", "up_proj:!", "down_proj:!")

They read the same normed MoE input. Their leaves are grandchildren of the root, though, so they were neither fold targets nor _scale_consumers. The scale divided out of the norm was never multiplied back into their input columns, and the dense shared expert's output changed.

Measured on a tiny random-init GLM-5.3 built from the vendored port config, with the fold only (no quantization): logits relative change 2.3e-2 on main, 5.9e-8 on this branch.

The fix is general, with no glm5_next special case. Any declared non-quantized (:!/:?) leaf with a gate/up role anywhere under the MoE root is now a scale consumer of the MoE-input group. The existing width check and the noconsumer opt-out still apply.

No linked issue. This was found while building the AWQ fold-plan inventory (AWQ lifecycle debloat).

What Changed

  • gptqmodel/models/base.py, _moe_block_consumers:
    • It keeps the direct-children rule.
    • It also adds declared non-quantized descendants of the :moe root whose plan roles include gate or up.
    • Consumers are deduplicated and kept in order: direct children first.
    • The processor needs no change. apply_scale already multiplies _scale_consumers, and the scale search already leaves consumers unchanged.
  • tests/module_tree/test_awq_glm5_next_neutrality.py (new):
    • It builds a tiny 45+MTP-layer GLM-5.3 through the definition loader and Defuser on CPU.
    • It sets non-unit norm gains.
    • It applies every emitted fold of a sparse layer with a non-uniform scale (shared awq_neutrality helper) and asserts the full-model logits are unchanged.
    • It pins the consumer list ["mlp.gate", "mlp.shared_experts.gate_proj", "mlp.shared_experts.up_proj"].
  • docs/awq-scaling-groups.md: one sentence under "One group per norm", plus a before/after row.

Behavior change: limited to definitions that declare a dense gate/up expert family under a :moe root and run the generic AWQ planner. On every current MODEL_MAP definition that is only glm5_next (see the fold-plan diff below).

Out of scope:

  • mimo_v2 and diffusion_gemma stale-fallback folds (separate PRs).
  • Dynamically excluded (dynamic negative-match) modules. These are config-level, not declared :!; this PR does not change them.

Tests

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

Regression: red on origin/main (ef7af83), green on this branch. The test file was copied into a detached origin/main worktree for the red run.

HF_HUB_OFFLINE=1 CUDA_VISIBLE_DEVICES="" PYTHONPATH=$PWD python -P -m pytest -q tests/module_tree/test_awq_glm5_next_neutrality.py
# origin/main:
E  AssertionError: type: post_attention_layernorm -> ['mlp.experts.0.gate_proj', 'mlp.experts.0.up_proj',
   'mlp.experts.1.gate_proj', 'mlp.experts.1.up_proj'] changed the layer output by 2.535e-02
1 failed in 2.95s
# this branch:
1 passed in 2.99s

Affected families. These are every test file that calls awq_get_modules_for_scaling, reads _scale_consumers or uses assert_awq_folds_neutral, plus all tests/test_awq*.py, all non-real-model glm5_next tests and tests/module_tree/test_registered_module_plans.py: 219 files.

CUDA_VISIBLE_DEVICES=<1 GPU> python -P -m pytest -n 12 -q -rfEs <219 files>
# 5091 passed, 76 skipped, 1 failed, 1 not run
  • Failed: tests/test_awq_variable_length_ppl.py::test_awq_beats_rtn_with_variable_length_calibration (dense Qwen2.5-0.5B, RuntimeError: Inference tensors do not track version counter). It fails identically on origin/main. It is pre-existing, unrelated, and its model has no MoE root.
  • Not run: tests/test_awq_moe.py::TestGroupSize::test_quant_and_inference_0 (a real Qwen3-30B-A3B quantize). I stopped it after more than 30 min on one GPU. Its fold plan is unchanged by this PR (qwen3_moe has no dense experts; see the diff below).
  • Skips (76):
    • 54: RUN_AWQ_FP16_HEURISTIC_BENCH benchmark gate.
    • 15: two-CUDA-device tests.
    • 6: BitBLAS not installed.
    • 1 each: free-threaded Python, gptqmodel[minicpm-o] extra, network-only release asset, Mistral-7B-AWQ not in the local cache, cuda:1 unavailable.
    • 2: muse_glimmer/opt350 non-CausalLM families.
    • 2: ricursive known Transformers sliding-window limitation.

End-to-end processor path: tests/test_awq_shared_indexer_replay.py::test_glm5_next_shared_indexer_replay_is_per_batch passed on the branch. It is the existing AWQ quantize of a tiny GLM-5.3 checkpoint.

bash format/check.sh: All checks passed (ruff 0.14.2).

Measured Impact

The fold alone with identity quantization: apply_scale with linspace(0.5, 1.5) scales and the group's _scale_consumers. It runs on the tiny random-init GLM-5.3 from tests/fixtures/ports/native_parity/glm5_next.json: 2 layers (KDA+dense, DSA+MoE, 8 routed + 1 shared expert), with norm gains drawn from U(0.5, 1.5).

Fold (sparse layer 1) main branch
post_attention_layernorm -> routed gate/up, logits rel. change 2.338e-02 5.915e-08
consumers mlp.gate mlp.gate, mlp.shared_experts.gate_proj, mlp.shared_experts.up_proj
every other fold of layers 0/1 (input_layernorm, up->down) <= 1.5e-07 unchanged

Fold-plan diff across every MODEL_MAP definition. This uses the fold-plan inventory harness from the AWQ canonical-folds work. It builds each of the 210 types and its config variants on meta and calls awq_get_modules_for_scaling on every decoder layer. It records source, targets, consumers, boundary, feature and the structural outcome. I ran it on main and on this branch.

  • 210/210 types match, except glm5_next.
  • The build errors are the same 18 default-config variants on both sides; those types have other buildable variants.
  • The glm5_next difference is one field. It appears in both sparse-layer variants (3,7,...,43, MTP layer 45 and the KDA+MoE layers):
post_attention_layernorm  consumers: ['mlp.gate'] -> ['mlp.gate', 'mlp.shared_experts.gate_proj', 'mlp.shared_experts.up_proj']
  • Status stays ok; there is no conflict or width drop.
  • The only other class trees with dense gate/up under a :moe root are kimi_k3 and kimi_linear (block_sparse_moe.shared_experts). Both reject AWQ (awq_get_modules_for_scaling raises NotImplementedError), so nothing changes for them.
  • deepseek_v4's dense MTP shared experts live in its MTP-only tree, which the AWQ planner does not use.

Model Support / Module Plan

Not applicable. There is no new model, alias or tree change. The registered module-plan tests pass (tests/module_tree/test_registered_module_plans.py).

Model test node IDs and results: tests/module_tree/test_awq_glm5_next_neutrality.py::test_glm5_next_awq_folds_compensate_dense_shared_experts PASSED.

AWQ onboarding safety

  • Native forward evidence identifies each source and all quantized and non-quantized consumers. Glm5NextTextMoE.forward feeds the same normed hidden_states to gate, experts and shared_experts.
  • Real-model neutral-fold test with non-trivial scales and gains, covering the router plus the dense shared branch.
  • Native context/chunk parity and fallback behavior: not changed by this PR.
  • Per-variant policy: on glm5_next sparse layers, the MoE-input group is AWQ-scaled with router plus dense shared-expert compensation. Dense layers are unchanged.

Logic Flow Documentation

Not applicable. The selection order and mechanism are unchanged; the existing consumer collector gains one structural source of consumers. Documented in docs/awq-scaling-groups.md.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions (fold-plan diff above).

Kernel Accuracy Requirements

Not applicable. No kernel change.

Notes

  • Pre-existing, unrelated: tests/test_awq_variable_length_ppl.py::test_awq_beats_rtn_with_variable_length_calibration fails on origin/main with Inference tensors do not track version counter.

🤖 Generated with Claude Code

The generic AWQ planner folds the norm before a `:moe` root into every
routed expert's gate/up projection. `_moe_block_consumers` compensated
only direct children of the root that own a 2-D weight (routers,
`shared_expert_gate`). An expert family the tree keeps dense, such as
GLM-5.3's `shared_experts: ("gate_proj:!", "up_proj:!", "down_proj:!")`,
also reads the MoE input. Its leaves are grandchildren of the root, so
they were neither fold targets nor scale consumers. Their input columns
were never multiplied by the scale that was divided out of
`post_attention_layernorm`, so the dense shared expert's output changed.
With identity quantization, the fold alone changes the tiny GLM-5.3
logits by 2.3e-2.

`_moe_block_consumers` now also collects the declared non-quantized
leaves with a gate/up role anywhere under the MoE root, subject to the
existing width check and `noconsumer` opt-out. `apply_scale` already
multiplies `_scale_consumers` by the scale, and the search already leaves
consumers unchanged, so no processor change is needed. The fold change
drops to 5.9e-8.

Across every MODEL_MAP definition, the resolved fold plans differ from
main only in the glm5_next MoE layers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 21:30
@Qubitium
Qubitium merged commit 8e8864d into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/glm5-next-awq-shared-experts branch November 29, 2026 21:30
Qubitium added a commit that referenced this pull request Nov 29, 2026
…ma/dots3_note/glm5_next fixes (#6452)

#6446 pinned four wrong planner outcomes as `bug:` entries, and its inventory
test requires a listed difference to still differ. #6449 and #6451 fixed them,
so test_awq_fold_inventory.py fails on main (4 planner cases, then 2
canonical cases).

- Regenerate the four fixture entries with
  `awq_fold_inventory.py --write --types dots3_note,mimo_v2,diffusion_gemma,glm5_next`:
  mimo_v2 and diffusion_gemma (k_eq_v layer 1) drop self_attn.o_proj from the
  input_layernorm group, dots3_note sliding layers plan instead of raising,
  glm5_next adds mlp.shared_experts.gate_proj/up_proj as consumers.
- diffusion_gemma/parity and dots3_note/default now match the role-derived
  folds, so their entries are removed.
- mimo_v2 and glm5_next still differ from the derivation for reasons that are
  not bugs (v_proj -> o_proj is not emitted after a `:!` leaf; glm5_next
  linear-attention layers need `v_proj:nosrc`), so they are reclassified.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant