Skip to content

refactor(models): delete AWQ group overrides that the role rules make redundant (batch A) - #6493

Merged
Qubitium merged 1 commit into
mainfrom
refactor/awq-delete-redundant-overrides-a
Nov 30, 2026
Merged

Qubitium merged 1 commit into
mainfrom
refactor/awq-delete-redundant-overrides-a

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

AWQ lifecycle step 5, batch A (map awq_lifecycle.md, PR 5). Since #6480, most of these definitions build their AWQ groups from fold declarations (canonical_awq_folds plus awq_folds). Their awq_get_modules_for_scaling post-filters were written against the old tree tracker's stale fallbacks. This PR deletes the filters the role rules make redundant. Where a filter still mattered for some config, it is replaced by tree data (:nosrc, :postnorm) or a small awq_fold_declarations variant. Five overrides stay, because removing them changes the pinned plan.

Behavior-preserving:

  • E1: awq_fold_inventory.py --check reports changed: none for all 210 types.
  • Structural stress A/B: 55 cases (inventory variants plus MHA and coincident-width variants of every touched type), equal to main.
  • Data-dependent A/B: 514 perturbed plans, equal to main.
  • E3 (GPU): bit-identical on Gemma3-4B, Gemma-3n-E2B and Gemma-4-E2B.

The meta inventory alone was not sufficient. Two filters that the map listed as redundant only matter on configs the inventory does not build:

  • DeepSeek-V2 MLA: when kv_lora_rank + qk_rope_head_dim == heads * v_head_dim, deleting the filter would fold kv_a_proj_with_mqa into o_proj. tests/test_awq_mla_scaling.py's tiny config has exactly this layout.
  • K2-Horizon query_key_norm: when heads * head_dim == hidden_size, the norm walk picks q_norm instead of input_layernorm. The existing test_k2_horizon_local_port.py::...[qknorm] neutrality test caught this.

Both cases are now tree data and have regression tests.

What Changed

Definition origin/main (126e83fb3) This PR
baichuan awq_get_modules_for_scaling strips o_proj (baichuan.py:31) deleted: W_pack -> o_proj is never width-equal (3h vs h), and the present W_pack block stops the norm walk
llada2 strips attention.dense (llada2.py:45) deleted: neither query_key_value nor dense has a role, and query_key_value stops the norm walk
spark2_5 strips out_proj (spark2_5.py:48) deleted: same reasoning (q_k_v_proj/out_proj have no role)
gemma3n, gemma3n_text retarget_kv_shared_attention_groups (gemma3n.py:279, 350) deleted: input_layernorm and v_proj are nosrc, so no attention group exists
gemma4*, gemma4_unified* (4 classes) _retarget_attention_output_folds + KV-shared retarget (gemma4.py:86, 357, 450; gemma4_unified.py:128, 181) _retarget_attention_output_folds deleted: v_proj is nosrc, so no group targets o_proj. The KV-shared q_proj boundary retarget stays
deepseek_v2 drops latent-norm sources and every group with o_proj (deepseek_v2.py:13) override deleted. kv_a_proj_with_mqa and kv_b_proj are :nosrc (latent-norm folds are never derived)
k2_horizon strips o_proj, re-pins q/k/v/gate to input_layernorm, drops that group on MoVA layers (k2_horizon.py:138) attention v_proj:nosrc, q_norm/k_norm :postnorm. The override keeps only the MoVA rule (3 lines)
lfm3, lfm3_omni keep nodes from ffn_norm (+ operator_norm on conv layers) (lfm3.py:85) the same filter as an awq_fold_declarations variant. lfm3_vl sets awq_fold_declarations = BaseQModel.awq_fold_declarations, the default it reached before
raon1_mini keep the two decoder norms, strip o_proj (raon1_mini.py:69) an awq_fold_declarations variant that keeps the two norm sources

Also:

  • tests/module_tree/test_awq_fold_inventory.py: the lfm3, lfm3_omni and raon1_mini entries are removed from AWQ_FOLD_DIFFERENCES, because their declarations now equal the effective plan.
  • docs/awq-scaling-groups.md: two nosrc table rows and one sentence on the new declaration variants.
  • _kv_shared_replay.py: the docstring no longer describes the tracker.

Kept, with the reason (each removal was tried; --check names the type as changed):

Override Why it stays
decilm.py:34 (deci, nemotron-nas) from declarations, v_proj -> o_proj moves before the MLP folds, and the linear groups carry per-feature kwargs instead of the layer kwargs (already TRACKER_PLANNED)
qwen.py:24 the w1 -> c_proj group would carry c_proj's per-feature kwargs (already TRACKER_PLANNED)
solar_open2.py:92 tiny_mha would gain v_proj -> o_proj (canonical-unsafe; already TRACKER_PLANNED)
deepseek_vl_v2.py:120, xing4_0.py:86 both are among the 66 drop:width-only opt-outs. Leaving the tracker removes only their kv_b_proj -> o_proj drop:width group, which needs the maintainer decision on those 66

k2_horizon and the four gemma4 classes keep a node-level awq_get_modules_for_scaling rather than a declaration variant on purpose. Overriding awq_fold_declarations switches a definition to the declared-path empty-capture contract (base.py:7333): a routed expert's empty down group would be skipped instead of reaching the starved-member fallback. Both families have routed experts.

Out of scope: batch B/C (PR 6/7). That includes lfm3_vl's explicit v_proj -> o_proj override and raon1_mini in PR 6's list (its whole override is gone here).

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/changed tests:

  • tests/test_awq_mla_scaling.py::test_mla_scale_groups_only_contain_consumers_of_the_fold_source: also asserts that no group targets o_proj on its coincident-width config.
  • tests/test_k2_horizon_support.py::test_k2_horizon_awq_attention_groups_with_mha_widths[dense|qknorm|mova]: with MHA widths and hidden-width q/k norms, the attention group's source is input_layernorm with q/k/v/gate (none on MoVA), and no group targets o_proj or uses q_norm/k_norm.

Both pass on origin/main and on this branch. They fail on this branch without the new nosrc/postnorm flags (2 failed / 1 passed each time).

  • tests/module_tree/test_awq_raon1_mini_neutrality.py::test_raon_dropped_generic_folds_are_not_neutral: the negative control now builds the role-derived default folds (BaseQModel.awq_fold_declarations) directly, since BaseQModel.awq_get_modules_for_scaling applies Raon's variant.
# CPU, PYTHONPATH=<clone>, HF_HUB_OFFLINE=1, CUDA_VISIBLE_DEVICES=, torch 2.15, transformers 5.23
# E2: #6480's 214-file set (onboarding baseline, every planner/fold/declaration test and every
# awq_get_modules_for_scaling caller) + every test file for a touched type, gemma/KV-shared tests and the
# inventory test (240 files), minus 3 model-scale GPU e2e files (test_awq_gemv.py, test_awq_moe.py, test_awq_marlin.py)
python -P -m pytest -q -p no:cacheprovider -n 64 -rfEs $(cat e2_files.txt)
# rebased head 2b2034d3a (on 580b1a11f): 0 failed, 6699 passed, 813 skipped (pre-rebase head e3610f939: the same)
#   An earlier run on a draft of this branch failed test_k2_horizon_local_port.py::...[qknorm] and Raon's
#   negative control; that is how the q_norm re-pin was found (now `postnorm`, see Summary)
# main 126e83fb3 (same files, main's test versions): 1 failed, 6695 passed, 813 skipped
#   FAILED test_deepseek_vl_v2_local_port.py::test_processor_image_forward_cache_generation_and_calibration[tail-2D-True]
#   (known xdist flake; passes on the branch runs)
# Skips (both sides, 813): CUDA/ROCm/accelerator-kernel tests with CUDA hidden, BitBLAS absent, optional extras,
#   network assets, 2 ricursive sliding_window==1. They leave the GPU kernels unverified here; no kernel is touched.

# Onboarding baseline (included above)
python -P -m pytest -q 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

# E1
HF_HUB_OFFLINE=1 python -P tests/module_tree/awq_fold_inventory.py --check   # changed: none (210 types), on 2b2034d3a and e3610f939
bash format/check.sh                                                          # All checks passed!

Measured Impact

Not applicable: no claimed issue. E3 is bit-identical.

Model Support / Module Plan

Not applicable: no MODEL_MAP entry, path, tag or fusion change. The new :nosrc/:postnorm flags are AWQ source metadata only: the block compiler drops them from block names, and only awq_folds.py, fold_rules.py and the tracker read them. tests/module_tree/test_registered_module_plans.py and test_definition_subset_ordering.py pass (in E2).

AWQ onboarding safety

No grouping changes. Every touched type's effective plan equals main's on:

  • every inventory variant (E1);
  • MHA variants of every inventory variant;
  • the coincident-width MLA configs;
  • Gemma4 KV-shared + k_eq_v;
  • 514 perturbed plans: half the routed experts unrouted, 20% of features absent (dynamic), 20% empty, MoE root empty; 3 seeds.

The per-variant AWQ policy is unchanged from main. The existing native neutrality tests pass unchanged: test_awq_raon1_mini_neutrality.py, test_k2_horizon_local_port.py::test_awq_scale_folds_preserve_dense_moe_and_mova_layer_outputs[*], test_awq_scaling_groups.py (lfm3, gemma4, gemma4_unified, k2_horizon, deepseek_v2 families), test_awq_shared_kv_states_replay.py, and the lfm3/llada2/spark2_5/baichuan/deepseek_v2 local ports.

Structural stress A/B (ab_stress.py, scratch):

  • Main and the branch each plan, in their own interpreter, every decoder layer of the 15 touched types. The variants are the inventory's config variants, a +mha copy of each (num_key_value_heads = num_attention_heads in every sub-config), the coincident-width DeepSeek-V2 MLA config (q_lora and lite), and Gemma4 KV-shared + k_eq_v.
  • Result: cases=55 equal=55 differ=0 (llada2_moe's bare default needs llada_version and fails to build on both sides; its mini/flash variants build).
  • Sensitivity: the same run without the new deepseek_v2/k2 flags differs on deepseek_v2/mla_coincident* and on every k2_horizon/*+mha case.

Logic Flow Documentation

  • The required diagrams and docs page are included below.
  • New-flow Mermaid diagram(s) describe the shipped code using real symbol names.
  • The removed/replaced mechanism is tabulated with file:line evidence (What Changed).
  • Failure and ambiguity paths are marked and visually distinct from success.
  • A reference page under docs/ repeats the diagrams, titled for the mechanism, and is linked from README.md (docs/awq-scaling-groups.md, flow unchanged from refactor(awq): build scaling groups from fold declarations; the tree tracker stays only as the test oracle #6480; this PR updates its declaration and nosrc sections).

The decision mechanism changes from "post-filter the planner's nodes per model" to "declare the source eligibility in the tree, or filter the declarations":

flowchart TD
    A["awq_get_modules_for_scaling"] --> B["awq_fold_declarations(layer)"]
    B -- "default" --> C["canonical_awq_folds<br/>_find_norm skips :postnorm (k2 q_norm/k_norm)<br/>_linear_edges skips :nosrc (k2 v_proj, dsv2 kv_a/kv_b)"]
    B -- "lfm3 / raon1_mini variant" --> D["super().awq_fold_declarations<br/>keep input-norm sources"]
    C --> E["_awq_nodes_from_declarations<br/>declared_fold, merge_by_source"]
    D --> E
    E -- "malformed" --> X["raise AWQFoldDeclarationError"]
    E -- "no capture" --> Y["skip group<br/>targets quantize plainly"]
    E --> F["_filter_awq_scaling_sources"]
    F --> G{"definition post-filter left?"}
    G -- "k2_horizon MoVA" --> H["drop input_layernorm group"]
    G -- "gemma4 KV-shared" --> I["retarget_kv_shared_attention_groups<br/>q_proj boundary"]
    G -- "none" --> J["AWQProcessor._quantize_layer"]
    H --> J
    I --> J
    style X fill:#f8d7da,stroke:#842029,stroke-width:2px
    style Y fill:#fff3cd,stroke:#997404,stroke-width:2px
Loading

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, pack or dispatch change. E3 shows identical packed tensors.

Notes

  • E3 (GPU A/B). AWQConfig(bits=4, group_size=128), 64 wikitext rows, seed 0, bf16, origin/main 126e83fb3 vs this branch on the same GPU. Every safetensors tensor is compared bytewise, plus the json files and quant_log.csv without the time columns:

    Model (type) Tensors Result Peak CUDA MiB main / branch
    unsloth/gemma-3-4b-it (gemma3, untouched; spot check) 1359 bit-identical 2085 / 2085
    unsloth/gemma-3n-E2B-it (gemma3n, override deleted) 2026 bit-identical 880 / 880
    google/gemma-4-E2B-it (gemma4, KV-shared layers; _retarget_attention_output_folds deleted) 2501 bit-identical 791 / 791

    The Gemma-3n and Gemma-4 branch runs used e3610f939, the pre-rebase head. The Gemma3 branch run used the same tree minus the later K2 postnorm/test edits, which touch no Gemma code.

    Gemma-4-E2B's KV-shared layers plan input_layernorm -> self_attn.q_proj alone, so the retained retarget is exercised. Not run: the other touched families (no small cached checkpoints: baichuan, llada2, spark2_5, deepseek_v2, k2_horizon, lfm3, raon1_mini). For those, the structural and perturbation A/B plus their native CPU tests are the evidence.

  • Net lines. 17 files changed, 106 insertions(+), 258 deletions(-) (−152). Production code plus docs (gptqmodel/, docs/): 13 files changed, 50 insertions(+), 246 deletions(-). Most of the remaining additions are the two regression tests.

  • The known dynamic-exclusion shared-norm fold on the tracker path (refactor(awq): build scaling groups from fold declarations; the tree tracker stays only as the test oracle #6480 Notes) is untouched: no tracker type changes here.

🤖 Generated with Claude Code

… redundant (batch A)

Since #6480 these definitions plan AWQ groups from their fold declarations
(canonical_awq_folds plus awq_folds), and their awq_get_modules_for_scaling
post-filters mostly guarded against the old tree tracker's stale fallbacks.

Deleted (no replacement; the role-derived folds never emit what they strip):
* baichuan, llada2, spark2_5: o_proj/dense/out_proj never joins a norm group,
  and the packed QKV projection never matches o_proj's width.
* gemma3n (text and composite): input_layernorm and v_proj are `nosrc`, so no
  attention group exists for the KV-shared retarget to act on.
* gemma4/gemma4_unified: _retarget_attention_output_folds. v_proj is `nosrc`,
  so no group targets o_proj. The four overrides keep only
  retarget_kv_shared_attention_groups (KV-shared q_proj boundary).

Replaced by data or a fold-declaration variant:
* deepseek_v2: `nosrc` on kv_a_proj_with_mqa and kv_b_proj instead of dropping
  o_proj groups. Without it a config with kv_lora_rank + qk_rope_head_dim ==
  heads * v_head_dim (tests/test_awq_mla_scaling.py's tiny config) would fold
  kv_a_proj_with_mqa into o_proj.
* k2_horizon: `nosrc` on the attention v_proj instead of stripping o_proj (MHA
  widths would add v_proj -> o_proj) and `postnorm` on q_norm/k_norm instead of
  re-pinning the attention group to input_layernorm (with
  heads * head_dim == hidden_size the norm walk would pick q_norm). The
  override keeps only the MoVA rule (no input_layernorm group when v_router is
  present).
* lfm3, raon1_mini: awq_fold_declarations keeps the input-norm folds only
  (lfm3 also drops operator_norm on attention layers). lfm3_vl keeps the base
  declarations it reached before.

Kept, because removing them changes the pinned plan: decilm (fold order and
per-feature kwargs), qwen (per-feature kwargs), solar_open2 (tiny_mha would
gain v_proj -> o_proj), deepseek_vl_v2 and xing4_0 (leaving the tracker drops
their drop:width groups; that opt-out needs a maintainer decision).
k2_horizon and gemma4 keep node-level overrides: a declaration override would
switch their routed experts to the declared-path empty-capture contract.

tests/module_tree/awq_fold_inventory.py --check reports no change for all 210
types. New regressions pin the coincident-width MLA config and K2 MHA, q/k-norm
and MoVA layouts; Raon's negative control now builds the default folds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review November 30, 2026 10:29
@Qubitium
Qubitium merged commit 186737f into main Nov 30, 2026
6 checks passed
@Qubitium
Qubitium deleted the refactor/awq-delete-redundant-overrides-a branch November 30, 2026 10:29
Qubitium added a commit that referenced this pull request Dec 1, 2026
… fold-declaration variants (batch B)

Since #6480 a definition without `awq_groups_from_tree_order` plans its AWQ
groups from `awq_fold_declarations(layer)`. Its config-conditional
`awq_get_modules_for_scaling` post-filters become small variants of the
derived folds:

* llama: `awq_fold_declarations` returns `[]` for LongLLaMA, StableLM-Epoch,
  Yi and q/k-norm Cohere/StableLM, else `super()`.
* telechat2: drops the two norm folds in post-layernorm residual mode. The
  `key_value` filter is moved into the tree as `key_value:0:nosrc` (the
  derived folds never use it: it has no `v` role).
* hymba: drops a gateless (relu2) expert's `up_proj -> down_proj` and declares
  the input-norm fold's replay boundary as `mamba.in_proj` instead of
  rewriting `module2inspect`.
* k2_horizon: drops the input-norm fold on MoVA layers.
* gemma4, gemma4_unified (text and composite):
  `retarget_kv_shared_attention_groups(nodes)` becomes
  `kv_shared_attention_folds(layer, folds)`, which replays a KV-shared
  layer's input-norm fold on `self_attn.q_proj` alone.
* ovis2_6_moe: deleted. It only called `BaseQModel.awq_get_modules_for_scaling`
  past Ovis2_5QModel, which no longer overrides it.

`_awq_nodes_from_declarations` chose the empty-capture contract by class
identity (`type(self).awq_fold_declarations is BaseQModel...`), so any
variant would have moved a routed expert's empty `down` capture from the
processor's starved-member fallback (#4675) to a skip (#5995). That would
have changed Hymba's and Gemma 4's MoE layers, and it is why #6493 kept the
K2-Horizon and Gemma 4 node filters. The contract is now the
declared `awq_hand_declared_folds` flag, set by the two definitions that write
their own folds (PanGu, Inkling); a filter of the derived folds keeps the
derived contract.

Kept, because removing them changes behaviour or needs a maintainer decision:
bloom, chatglm, dbrx_converted, rw, ricursive and nemotron_h still plan from
the tree tracker and have drop:width groups (leaving the tracker drops them);
nanbeige stays on the tracker for its qk_layernorm tracker gap; kimi_k3's
NotImplementedError is a fail-closed guard, not a group filter; cohere_compass
re-prepares kwargs until PR 10. raon1_mini was converted in #6493 and the
deepseek_v4 MTP override was removed with #6435.

`tests/module_tree/awq_fold_inventory.py --check` reports no change for all 210
types; a stress and data-dependent A/B (MHA and coincident-width variants,
unrouted experts, absent and empty captures) is equal on all 210 types, and a
GPU AWQ A/B is bit-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Dec 1, 2026
… fold-declaration variants (batch B) (#6636)

Since #6480 a definition without `awq_groups_from_tree_order` plans its AWQ
groups from `awq_fold_declarations(layer)`. Its config-conditional
`awq_get_modules_for_scaling` post-filters become small variants of the
derived folds:

* llama: `awq_fold_declarations` returns `[]` for LongLLaMA, StableLM-Epoch,
  Yi and q/k-norm Cohere/StableLM, else `super()`.
* telechat2: drops the two norm folds in post-layernorm residual mode. The
  `key_value` filter is moved into the tree as `key_value:0:nosrc` (the
  derived folds never use it: it has no `v` role).
* hymba: drops a gateless (relu2) expert's `up_proj -> down_proj` and declares
  the input-norm fold's replay boundary as `mamba.in_proj` instead of
  rewriting `module2inspect`.
* k2_horizon: drops the input-norm fold on MoVA layers.
* gemma4, gemma4_unified (text and composite):
  `retarget_kv_shared_attention_groups(nodes)` becomes
  `kv_shared_attention_folds(layer, folds)`, which replays a KV-shared
  layer's input-norm fold on `self_attn.q_proj` alone.
* ovis2_6_moe: deleted. It only called `BaseQModel.awq_get_modules_for_scaling`
  past Ovis2_5QModel, which no longer overrides it.

`_awq_nodes_from_declarations` chose the empty-capture contract by class
identity (`type(self).awq_fold_declarations is BaseQModel...`), so any
variant would have moved a routed expert's empty `down` capture from the
processor's starved-member fallback (#4675) to a skip (#5995). That would
have changed Hymba's and Gemma 4's MoE layers, and it is why #6493 kept the
K2-Horizon and Gemma 4 node filters. The contract is now the
declared `awq_hand_declared_folds` flag, set by the two definitions that write
their own folds (PanGu, Inkling); a filter of the derived folds keeps the
derived contract.

Kept, because removing them changes behaviour or needs a maintainer decision:
bloom, chatglm, dbrx_converted, rw, ricursive and nemotron_h still plan from
the tree tracker and have drop:width groups (leaving the tracker drops them);
nanbeige stays on the tracker for its qk_layernorm tracker gap; kimi_k3's
NotImplementedError is a fail-closed guard, not a group filter; cohere_compass
re-prepares kwargs until PR 10. raon1_mini was converted in #6493 and the
deepseek_v4 MTP override was removed with #6435.

`tests/module_tree/awq_fold_inventory.py --check` reports no change for all 210
types; a stress and data-dependent A/B (MHA and coincident-width variants,
unrouted experts, absent and empty captures) is equal on all 210 types, and a
GPU AWQ A/B is bit-identical.

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