Skip to content

fix(awq): capture declared MoE roots without lifecycle hooks (#4697) - #4723

Merged
Qubitium merged 1 commit into
mainfrom
fix/4697-awq-moe-root-capture
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/4697-awq-moe-root-capture

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR

AWQ registered its MoE-root capture hook only from the optional
moe_lifecycle_hooks object. Definitions that declare a :moe root without
lifecycle hooks left the root feature empty, so awq_get_modules_for_scaling
read a zero-width "source", treated every routed expert as width-foreign, and
silently dropped the entire gate/up scaling group. Resolve the declared roots
from ModulePlan.moe_roots() in the subset executor and register the AWQ
capture hook there; warn instead of dropping the group silently when a root
still has no activations.

Net: 4 files, +83 implementation/docs, +190 regression test.

1. The problem

BaseQModel.awq_input_feature_aggregation returns capture_root: True for
every declared MoE root, so AWQProcessor.preprocess seeds an empty root task.
The only hook that records that task was registered from the optional routing
lifecycle:

flowchart TD
    A["run_subset_stage"] --> B{"gptq_model.moe_lifecycle_hooks<br/>is not None?"}
    B -- "yes" --> C["hooks.get_moe_block_for_subset"]
    C --> D["register_moe_root_capture_hook<br/>record pre-router input"]
    B -- "no" --> E["no root hook"]
    E --> F["tasks root stays empty"]
    F --> G["_layer_input_features<br/>root feature = torch.empty(0)"]
    G --> H["awq_get_modules_for_scaling<br/>input_width = 0"]
    H --> I["every expert looks width-foreign"]
    I --> J["gate/up group dropped<br/>only log.debug, no warning"]
    style E fill:#ffcdd2,stroke:#b71c1c
    style J fill:#ffcdd2,stroke:#b71c1c
Loading

awq_get_modules_for_scaling selected the root feature for a MoE gate/up block
when last_module_root in input_feat, but an empty tensor is still "in" the
mapping. The width filter then produced input_width = 0 and rejected all
sixteen expert projections, so no post_attention_layernorm -> experts.*.gate/up
group reached AWQProcessor._quantize_layer.

Mechanism Before After
Root hook registration lifecycle object only, origin/main:gptqmodel/looper/stage_subset.py:998-1017 module plan for AWQ, gptqmodel/looper/stage_subset.py:1020-1044
Root feature for definitions without hooks (0,), origin/main:gptqmodel/models/base.py:5728-5736 captured pre-router input
Empty root feature width 0, all consumers foreign, log.debug only explicit warning and skip, gptqmodel/models/base.py:5745-5759

2. The new flow

flowchart TD
    A["awq_input_feature_aggregation<br/>plan.moe_roots()"] --> B["capture_root policy"]
    B --> C["AWQProcessor.preprocess<br/>seed root task"]
    D["run_subset_stage<br/>_model_module_plan(looper.gptq_model)"] --> E{"AWQ and layer has<br/>plan.is_expert?"}
    E -- "yes" --> F["module.get_submodule(template)<br/>layer-level root"]
    F --> G{"same module as<br/>lifecycle block?"}
    G -- "no" --> H["register_moe_root_capture_hook"]
    G -- "yes" --> I["already registered, skip"]
    H --> J["record_moe_root_input_feature<br/>bounded token rows"]
    J --> K["_layer_input_features<br/>aggregate root feature"]
    K --> L["awq_get_modules_for_scaling<br/>norm -> experts gate/up"]
    L --> M["AWQProcessor._quantize_layer<br/>apply_scale"]
    E -- "no" --> N["no root hook"]
    style N fill:#fff3cd,stroke:#997404
Loading

The plan-based registration runs only for AWQ (is_awq_processor) and only
when the layer actually contains a declared expert (plan.is_expert). GPTQ's
padding-zeroing hook keeps its existing lifecycle path. A lifecycle block that
is the same object as the declared root is registered once.

3. Failure and ambiguity contract

Situation Before After
No moe_lifecycle_hooks, top-level :moe root gate/up group dropped silently group emitted and applied
Lifecycle block == declared root one hook one hook (deduplicated)
Declared root still captures nothing width 0, silent drop log.warning names the root; group is skipped and the experts stay on plain quantization
Nested root (mixer.experts) group builder reads a layer-level root feature unchanged; the existing shared-expert feature supplies the input
Non-MoE layer of a hybrid family no root work no hook (no declared expert in the layer)

4. Change inventory

Area File Change
Subset executor gptqmodel/looper/stage_subset.py resolve declared layer-level MoE roots from the module plan and register the AWQ capture hook
Scaling-group builder gptqmodel/models/base.py warn and skip when the selected root feature is empty instead of treating width 0 as a foreign source
Reference page docs/awq-scaling-groups.md new "MoE-root capture" section with the shipped flow, before/after table and test reference
Regression tests/test_awq_moe_root_capture_4697.py real tiny Granite-MoE and gpt-oss quantize; negative empty-root warning

5. Tests / guarantees

Test Invariant
test_declared_moe_root_capture_drives_gate_up_group[granitemoe-block_sparse_moe-8] every routed expert gate/up row reports scale_feature == "block_sparse_moe", token-row aggregation and the full calibration token count
test_declared_moe_root_capture_drives_gate_up_group[gpt_oss-mlp-8] same for mlp; covers a :nosrc expert-up layout where only the gate/up group can be folded
test_empty_declared_root_warns_instead_of_silent_drop an empty declared root emits the warning and no expert gate/up module enters a scaling group

Red evidence: on unpatched origin/main (19c40b201) both real-quantize cases
fail at assert row["scale_feature"] == moe_root with
'block_sparse_moe.experts.0.gate_proj' / 'mlp.experts.0.gate_proj', and the
warning test fails because caplog.text contains no empty-root message.

6. Validation

python -m pytest -q -ra \
  tests/test_awq_moe_root_capture_4697.py \
  tests/test_awq_scaling_groups.py \
  tests/test_awq_gptq_lifecycle.py \
  tests/test_moe_padding_rows.py \
  tests/test_awq_scale_search_safety.py \
  tests/test_awq_moe_router_scaling.py \
  tests/test_awq_empty_groups.py \
  tests/test_awq_position_kwargs.py \
  tests/test_awq_scale_search_memory.py \
  tests/test_bloom_awq_replay.py \
  tests/module_tree/test_awq_relationships.py \
  tests/module_tree/test_bloom_awq_relationships.py \
  tests/module_tree/test_registered_module_plans.py \
  tests/module_tree/test_definition_subset_ordering.py
# 747 passed, 6 skipped, 1 failed

The branch is rebased on current main (2ed6a6e4f), so the run includes
#4720's tests/test_awq_moe_starved_expert_group_4675.py. The one failure is
test_definition_subset_ordering.py::test_issue_4372_tiny_model_data_flow[nemotron_h],
which fails identically on unpatched origin/main with
ValueError: Pointer argument cannot be accessed from Triton (cpu tensor?).
It is an existing environment limitation, not a regression from this change.
The 6 skips are the existing mamba_ssm CPU-fallback and unavailable-family
skips.

The new file alone:

python -m pytest -q -ra tests/test_awq_moe_root_capture_4697.py
# 3 passed

Additional real-model checks: a tiny gpt-oss quantize() now produces two
scaling groups per layer (attention qkv and post_attention_layernorm -> 16 expert gate/up) instead of one, with the router collected as a scale
consumer; a tiny Granite-MoE produces ten groups, including the expert gate/up
group.

7. Reviewer notes

  • The issue lists Nemotron-H and Nemotron-Omni among the affected families.
    Their declared root is nested (mixer.experts), so the group builder already
    falls back to the shared-expert feature and emits the group on unpatched
    main; a tiny native Nemotron-H quantize confirms the group is applied. The
    plan-based hook deliberately skips nested templates because the group builder
    reads a layer-level root feature; changing that boundary would need a
    separate consumer/replay contract for latent MoE layouts.
  • The empty-root warning is the "warn instead of silently drop" half of the
    issue's suggested fix. It does not substitute a per-expert feature for the
    pre-router input, because experts see disjoint token sets and a single
    expert's rows are not a valid scoring input for the whole group.
  • No environment-variable switch is introduced; the behavior follows the
    existing module plan and the AWQ processor identity.

Definitions such as Granite-MoE, gpt-oss, ERNIE-4.5-MoE, Gemma-4,
DBRX-converted, Diffusion-Gemma, Kimi and Nemotron-H puzzle declare a :moe
root but do not set moe_lifecycle_hooks. AWQ registered the root capture hook
only from that optional routing object, so the declared root feature stayed
(0,) and awq_get_modules_for_scaling dropped the routed-expert gate/up group
without a warning.

Resolve the declared roots from ModulePlan.moe_roots() in the subset executor
and register the AWQ capture hook on each layer-level root, deduplicated
against the lifecycle block. Keep the GPTQ padding-hook path unchanged.

If a declared root still has no activations, warn and skip the group instead
of treating width 0 as a foreign consumer and dropping it silently.

Add a real-quantize regression for Granite-MoE and gpt-oss plus a negative
test for the empty-root warning.
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.

2 participants