Skip to content

fix(awq): keep a dynamic-excluded reader in its source's scaling group on the tree-order planner - #6630

Merged
Qubitium merged 1 commit into
mainfrom
fix/awq-dynamic-excluded-norm-reader
Dec 1, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/awq-dynamic-excluded-norm-reader

Conversation

@Qubitium

@Qubitium Qubitium commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

AWQ with a dynamic -: rule that excludes a whole block of modules could fold a scale into a source that the excluded modules still read, and those modules got no compensation. This happened on the tree-order planner, which still plans the 92 types with awq_groups_from_tree_order = True. The decoder output then changed even though nothing in it was quantized yet.

An excluded module captures no input, so it is missing from input_feat. _awq_generic_get_modules_for_scaling skipped any block without a captured feature, and with it the fact that the block reads a source:

  • Parallel decoders (falcon, refinedWebModel, moss, falcon_h1). With the MLP (or Mamba) branch excluded, the shared input norm was folded into the attention branch only. The excluded branch then read the divided norm output.
  • Attention branch excluded (falcon, refinedWebModel, falcon_h1). The MLP's :in= source was never recorded, so the MLP input was folded into self_attention.dense / self_attn.o_proj. That output is on the other branch.
  • Other readers of the same norm. qwen3_next's router (mlp.gate is quantized in its AWQ tree), the nemotron_h shared and routed experts, and ernie4_5_moe's dense gate_proj/up_proj blocks lost their compensation the same way.

The fold-declaration path (#6480; cohere, the qwen3.5 family and the other 118 types) already keeps an excluded target in its fold. The processor then finds a member without features and does not fold that source. This PR makes the tree-order planner do the same, so both planners agree.

What Changed

  • BaseQModel._awq_generic_get_modules_for_scaling: a block the layer owns but whose modules have no captured input is no longer skipped. It is planned from its modules' input width and merged into its source's group by _merge_shared_source_nodes. A merged group whose first block captured nothing has no search input. It is dropped after merging, with a warning when captured members lose the fold. This matches a declared fold whose first branch captured nothing. No processor change.
  • _module_input_width: input channels of a 2-D weight module (Conv1D stores [in, out]).
  • docs/awq-scaling-groups.md: contract rule 7, a dynamic-excluded reader stays in its source's group.
  • Behaviour change only under dynamic exclusion (or a looper-pruned module) on tree-order types. The affected source is no longer folded. Its captured readers are quantized without that scale, or with the configured fallback when they share a group with the excluded member, exactly as on the declaration path.
  • Out of scope (follow-up): on both planners, an excluded member counts as a "starved" member. With the default Fallback, its captured siblings therefore drop to RTN instead of AWQ clip-quantization (e.g. query_key_value when Falcon's MLP is excluded, or Cohere's q/k/v when its MLP is excluded). That is safe but not ideal. Compensating excluded readers would keep AWQ for the siblings: fold the scale into them and search on the captured members only. It would change the declaration path too.

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.

New: tests/module_tree/test_awq_dynamic_excluded_reader.py. It builds tiny random-init models from the vendored ports (float64, non-unit norm gains) and removes the excluded modules' captured inputs, as the processor's input_feat does under a -: rule. It then folds every emitted group with a non-uniform scale into all of its members and consumers and compares the full-model logits.

# origin/main 44aa0f98e (worktree), same test file
PYTHONPATH=<main> python -P -m pytest -q tests/module_tree/test_awq_dynamic_excluded_reader.py
FAILED ...[falcon-mlp]         falcon excluding ['mlp.dense_h_to_4h', 'mlp.dense_4h_to_h']: a planned fold changed the logits by 1.548e-01: [('input_layernorm', ['self_attention.query_key_value'], [])]
FAILED ...[falcon-attention]   ... changed the logits by 1.580e-01: [('self_attention.dense', ['mlp.dense_h_to_4h'], [])]
FAILED ...[falcon-qkv]         ... changed the logits by 1.580e-01: [('self_attention.dense', ['mlp.dense_h_to_4h'], [])]
FAILED ...[qwen3_next-router]  ... changed the logits by 5.585e-04 (post_attention_layernorm folded into the experts, router excluded)
FAILED ...test_falcon_excluded_mlp_keeps_the_norm_unfolded
FAILED ...test_falcon_excluded_attention_does_not_reroute_the_mlp
6 failed, 2 passed  (cohere-mlp and qwen3_5_text-in_proj_z: declaration path, already safe)

# this branch
python -P -m pytest -q tests/module_tree/test_awq_dynamic_excluded_reader.py
8 passed in 3.48s

Existing coverage (CPU, -n 16):

python -P -m pytest -q -n 16 \
  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 \
  tests/module_tree/test_awq_dynamic_excluded_reader.py tests/module_tree/test_awq_fold_inventory.py \
  tests/module_tree/test_awq_fold_declarations.py tests/module_tree/test_awq_fold_safety_4175.py \
  tests/test_awq_scaling_groups.py tests/test_awq_falcon_two_norm_parallel_5414.py \
  tests/test_awq_falcon_h1_parallel_branch_4741.py tests/test_awq_moe_starved_expert_group_4675.py \
  tests/test_awq_moe_shared_expert_group_4760.py tests/test_awq_moe_root_capture_4697.py \
  tests/test_awq_hybrid_moe_root_4903.py tests/test_awq_ling2_fold_4677.py tests/test_awq_moe_prune_uncovered_4456.py \
  tests/test_awq_scaling_group_features.py tests/test_awq_dynamic_fallback_threshold_5820.py \
  tests/test_awq_cohere2_moe_parallel_norm_4898.py tests/test_awq_mla_scaling.py
1680 passed, 3 skipped in 83.11s
# skips: test_awq_scaling_groups.py::test_declared_non_sources_never_become_prev_ops[opt350-forbidden8] and
#        [muse_glimmer-forbidden15] ("not exercised as a buildable CausalLM family");
#        test_awq_moe_prune_uncovered_4456.py::test_tiny_qwen3_moe_awq_fallback_none_keeps_uncovered_experts_dense
#        (needs CUDA) -> run on GPU below

CUDA_VISIBLE_DEVICES=<1 GPU> python -P -m pytest -q tests/test_awq_moe_prune_uncovered_4456.py
3 passed in 70.39s

HF_HUB_OFFLINE=1 python -P tests/module_tree/awq_fold_inventory.py --check
changed: none

bash format/check.sh   # ruff 0.14.2
All checks passed!

Measured Impact

Fold-only (identity quantization) worst relative change of the full-model logits. Every planned group is folded with scales linspace(0.55, 1.45) into all of its members and consumers, on tiny vendored-port models with the listed modules excluded:

Case (excluded) origin/main this PR
falcon parallel (mlp.*) 1.548e-01 3.4e-16
falcon parallel (self_attention.*) 1.580e-01 0 (no fold)
falcon parallel (self_attention.query_key_value) 1.580e-01 0 (no fold)
qwen3_next (mlp.gate, the router) 5.585e-04 2.6e-08
cohere (mlp.*, declaration path) 8.0e-08 8.0e-08
qwen3_5_text (linear_attn.in_proj_z, declaration path) 1.3e-08 1.3e-08

Real CPU AWQ quantize: a tiny Falcon with dynamic={"-:.*\.mlp\..*": {}}, GPTQModel.load + quantize, bits=4, group_size=32. On main, input_layernorm.weight changes by 32% / 12% relative (layers 0 / 1), while the excluded dense_h_to_4h changes only by the bf16 load cast (1.7e-3), so the MLP input is uncompensated. On this branch, input_layernorm changes only by the same load cast (1.7e-3). No fold happens, and query_key_value takes the configured fallback, like Cohere on the declaration path.

Blast radius under dynamic exclusion. All 210 MODEL_MAP types were planned on meta, for every config variant and distinct decoder layer. Each layer was planned with nothing excluded, then with each non-expert quantized module excluded alone, then with each branch prefix excluded (mlp.*, self_attn.*, mlp.experts.*, ...). That is 5838 plans, main vs branch:

  • Plans with nothing excluded: unchanged for every type.
  • Plans that change: only 10 tracker types: ernie4_5_moe, ernie4_5_moe_vl, ernie4_5_vl_moe, falcon, falcon_h1, moss, nemotron_h_puzzle, nemotronh_nano_omni_reasoning_v3, qwen3_next, refinedWebModel. Each change replaces an uncompensated or rerouted fold with an unfolded source, or with a group that carries the excluded reader.
  • Oracle flags: a source still folded while one of its readers in the unexcluded plan is excluded, or a fold not present in the unexcluded plan. On main there were 50 flags across 12 types; on this branch only 5 remain, on deepseek_vl_v2 and xing4_0. Those 5 are kv_a_layernorm -> kv_b_proj when q_b_proj is excluded, a data-flow-correct MLA fold (kv_b_proj is that norm's only reader) that the oracle flags only because the unexcluded plan does not emit it.
  • Declaration-path types: no flags on main or branch.

GPU AWQ A/B without dynamic (bits=4, group_size=128, 64 wikitext rows, bf16, one GPU, main then branch). The tensors and json/csv files of the saved checkpoints were compared:

Model Planner Result
Qwen/Qwen2.5-0.5B-Instruct declarations bit-identical (626 tensors + config/quant_log/quantize_config files)
FlameF0X/TinyMoE-100m-2x8-retrained (mixtral) tree-order bit-identical (873 tensors + config/quant_log/quantize_config files)
# docs contract
python -P -m pytest -q tests/test_docs_contract_refs.py
266 passed

Model Support / Module Plan

Not applicable: no MODEL_MAP entry, module tree or definition changes. The fold inventory for all 210 types is unchanged (--check: changed: none).

Model test node IDs and results (or "not applicable"): not applicable (see Tests for the registry/plan suites run).

AWQ onboarding safety

  • Native forward evidence identifies each source, all quantized and non-quantized consumers, intervening operations, and complete replay scope.
  • Real-model neutral-fold and candidate-search tests cover applicable layouts/configs, shared branches/routers, and nontrivial scales; passing module-plan or width checks alone is not AWQ qualification.
  • Native context/chunk parity, failure rollback, and configured fallback/error behavior are tested where applicable; no-group modes still quantize all linears.
  • Each variant is AWQ-scaling qualified, tested plain-quantization-only, or explicitly unsupported/unverified; exact test nodes, skips and limits are recorded.

AWQ evidence and per-variant policy: this change only removes folds. A tree-order source that an excluded module reads is no longer folded, so no new fold is qualified. Neutrality is covered on real vendored-port models: Falcon parallel with MLP, attention and qkv excluded, and Qwen3-Next with the router excluded. Cohere and Qwen3.5-text on the declaration path are covered as guards. The full-model logits are measured with non-uniform scales (table above), and every test asserts which groups survive. A real CPU AWQ run on Falcon confirms the processor no longer divides the norm and that query_key_value takes the configured fallback. Chunk parity and rollback are untouched (no search or replay change). Only the planner's response to a missing feature changed.

Logic Flow Documentation

  • This change does not alter a major logic flow, or the required diagrams and docs page are included below.

Diagram summary: not applicable. The planner's selection order is unchanged. One branch, "block without a captured feature", now keeps the block in its source's group instead of skipping it, and the contract is recorded as rule 7 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.

Kernel Accuracy Requirements

  • Not applicable: no kernel change.

Notes

  • AWQ PRs that move types off the tree-order planner shrink the affected set. This fix stays in the shared planner and is independent of them.
  • Rebased onto 4f3ba22 (feat(sinq): add A-SINQ with the AWQ activation-aware scale search #6615, which touches unrelated regions of base.py/awq_processor.py); the CPU suite (1680 passed, 3 skipped), the new test and awq_fold_inventory.py --check (changed: none) were rerun on the rebased head. The sweep, numeric table and GPU A/B ran against base 44aa0f9.
  • Sweep coverage: 18 default config variants do not build on meta in the inventory harness (they are error entries in the committed fold inventory too); each of those 18 types is still covered through another variant (parity, released, ...). Data-dependent outcomes (starved experts, empty captures) are outside the meta sweep.
  • Planning sweep scripts and logs are kept out of the repo. The test above pins the behaviour.

🤖 Generated with Claude Code

…p on the tree-order planner

A module excluded by a `dynamic` `-:` rule captures no input, so it is
absent from `input_feat`. The tree-order planner
(`_awq_generic_get_modules_for_scaling`, still used by the 92 types with
`awq_groups_from_tree_order = True`) skipped any block with no captured
feature, which also dropped the fact that the block reads a source:

* parallel decoders (falcon, refinedWebModel, moss, falcon_h1): with the
  MLP (or Mamba) branch excluded, the shared input norm was folded into
  the attention branch only, and the excluded branch read the divided
  norm output uncompensated (relative logits change 0.15 on a tiny
  Falcon; a real CPU AWQ run divides input_layernorm by the scale and
  leaves dense_h_to_4h untouched);
* with the attention branch excluded, the MLP's `:in=` source was never
  recorded, so the MLP input was folded into `self_attention.dense` /
  `self_attn.o_proj`, an output on the other branch;
* qwen3_next's AWQ-quantized router, nemotron_h shared/routed experts and
  ernie4_5_moe's split dense gate/up blocks lost their compensation the
  same way.

The fold-declaration path already keeps an excluded target in its fold,
so the processor finds a member without features and does not fold the
source. The tracker now does the same: an uncaptured block is planned
from its modules' input width and merged into its source's group; a
group whose first block captured nothing is dropped after merging,
matching a declared fold whose first branch captured nothing.

Plans without exclusions are unchanged (fold inventory --check: changed:
none). Across all 210 MODEL_MAP types, planning every layer variant with
every single quantized module and every branch prefix excluded changes
plans only for the 10 tracker types above.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review December 1, 2026 09:48
@Qubitium
Qubitium merged commit 00b829d into main Dec 1, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/awq-dynamic-excluded-norm-reader branch December 1, 2026 09:48
Qubitium added a commit that referenced this pull request Dec 1, 2026
…6657)

The fold-declaration planner skipped a declared fold when its first
feature was absent or empty. For a MoE gate/up group whose root capture
was seeded but empty (a missing capture hook, a dynamic exclusion), this
dropped a fold the tree planner keeps and could not tell the processor
that the group's readers share the source (#6630).

Use the tracker's feature precedence: the MoE root only when it holds
rows, else the first declared target with a capture; when none captured
anything, keep the node with `inp=None` and the full target set. The
processor's fallback check then sees the starved members and does not
fold the source without them. Full-capture plans are unchanged for all
210 MODEL_MAP types.

Keep the derived routed-expert `down` empty capture (#4675) and the
hand-written-declaration skip (#5995). Build the node from the merged
declaration so a merged source keeps its full target set.

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