Repository navigation
refactor(models): replace config-conditional AWQ group overrides with fold-declaration variants (batch B) - #6636
Merged
Conversation
Qubitium
marked this pull request as ready for review
December 1, 2026 09:50
… 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
force-pushed
the
refactor/awq-config-conditional-folds-b
branch
from
December 1, 2026 10:04
2da970a to
44ee683
Compare
7 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AWQ PR 6 of the lifecycle cleanup (batch B). Since #6480, a definition without
awq_groups_from_tree_orderplans its AWQ groups fromawq_fold_declarations(layer). This PR turns the config-conditionalawq_get_modules_for_scalingnode filters on that path into small variants ofthe derived folds. It also replaces the class-identity check that blocked those
variants with one declared flag.
Diff:
15 files changed, 135 insertions(+), 148 deletions(-)(net −13) (production code undergptqmodel/:11 files changed, 75 insertions(+), 118 deletions(-), net −43).No numerics, checkpoint-format or public-API change: the fold inventory, a
stress and data-dependent A/B over every
MODEL_MAPtype, and GPU AWQ A/B runsare identical (see Tests).
What Changed
One mechanism fix that enables the variants.
_awq_nodes_from_declarationspicked the empty-capture contract with
type(self).awq_fold_declarations is BaseQModel.awq_fold_declarations(
base.py:7687on main). With that check, any variant would have changed arouted expert's empty
downcapture from the processor's starved-memberfallback (#4675) to a skipped group (#5995). This is why #6493 kept the node
filters on k2_horizon and gemma4. The contract is now the declared class flag
awq_hand_declared_folds(defaultFalse). Only the two definitions that writetheir own folds set it: PanGu and Inkling. A filter of
super()'s folds keepsthe derived contract.
Overrides replaced by fold-declaration variants (each filters
super().awq_fold_declarations(layer)):llama.py(allLlamaQModelsubclasses)awq_get_modules_for_scalingreturns[]for longllama / stablelm_epoch / yi / q/k-norm cohere and stablelm (llama.py:65)awq_fold_declarationsreturns[]under the same conditiontelechat2.pykey_valuesource and, in post-LN residual mode, both norm sources (telechat2.py:40)key_valuebecomeskey_value:0:nosrcin the tree; it has novrole, so the derived folds never use ithymba.pyup_projsource and rewritesmodule2inspectfrommambatomamba.in_proj(hymba.py:60)boundary="mamba.in_proj"k2_horizon.pyinput_layernormon MoVA layers (k2_horizon.py:138)gemma4.pyx2,gemma4_unified.pyx2retarget_kv_shared_attention_groups(nodes)re-anchors the KV-sharedself_attngroup onq_proj(_kv_shared_replay.py:262,gemma4.py:310/399,gemma4_unified.py:125/174)kv_shared_attention_folds(layer, folds): the same rule applied to the folds (about 15 lines instead of 45)ovis2_6_moe.pyawq_get_modules_for_scalingcallsBaseQModel.awq_get_modules_for_scalingpastOvis2_5QModel(ovis2_6_moe.py:96)Ovis2_5QModelno longer overrides itAWQ_FOLD_DIFFERENCESloses the 13 entries that these variants close: llama x7,telechat post_ln, hymba x2, gemma4_text kv_shared/parity_moe, and k2 MoVA.
Kept, with reasons:
awq_groups_from_tree_order = True, and every type hasdrop:widthtracker groups. Moving it to declarations drops them, which is the "66 junk-group-only" set that needs a maintainer decision. The tracker path never callsawq_fold_declarations.TRACKER_PLANNEDqk_layernorm. Declarations would add thev_proj -> o_projfold that the tracker misses, which changes numerics.raise NotImplementedErroris a fail-closed guard for an unsupported method, not a group filter. Moving it would make everyawq_fold_declarationscaller raise and would save no lines.Out of scope / follow-ups:
drop:widthdecision.
awq_hand_declared_foldsfor its MoE definitionsin the same way.
Tests
Interpreter: /root/venv/bin/python (torch 2.15.0+cu132, transformers 5.23.0).
Each side ran with its own checkout first on
PYTHONPATH(python -P, printedgptqmodel.__file__).main= origin/maincc0c705b7.E1, fold inventory (all 210 types).
Result:
changed: none(210 types).Stress and data-dependent A/B, main vs branch (extends #6493's
ab_stress.pyand
perturb.py). The script covers everyMODEL_MAPtype and every inventoryconfig variant. It adds a
+mhavariant of each (every attention confignum_key_value_heads == num_attention_heads) and extra stress variants:kv_half, wherekey_valueout ==densein, with and without post-LN;plus the composite parity MoE config;
k_eq_v.For each distinct decoder layer it plans five input-feature cases: full, half
of the routed experts unrouted (empty captures), 20% absent features (
dynamicexclusions), 20% empty features, and an empty MoE root. Each case records every
node field that the processor reads.
Result:
compared 7248 layer plans over 530 config variants (210 types); build errors identical on both sides: 36; types with differences: []. The new Gemma 4 stress variants exercise the KV-shared rule: there are q_proj-boundary groups on gemma4 kv_shared/parity_moe, gemma4_unified kv_shared and gemma4_unified_text kv_shared. The 36 build errors are config variants that fail to build identically on both sides; they match the build errors already recorded in the fixture.Negative control: on the branch, with the old identity check put back (not
awq_hand_declared_folds), the same A/B reports differences onhymbaandgemma4_text/parity_moein theunroutedandempty20cases. The flag is whatkeeps those identical.
E2, CPU suites, main vs branch. 189 test files: the onboarding-skill
baseline, every file in the map's E2 list, every test calling
awq_get_modules_for_scaling/awq_fold_declarations, and every test naminga touched definition (llama family, telechat, hymba, ovis2_6, pangu, inkling2,
lfm3, raon1, gemma4, k2_horizon, kv-shared).
tests/models/(GPU checkpoints)is excluded.
Result on the final branch (rebased) vs main
cc0c705b7:2 failed, 6732 passed, 786 skipped, 2 xfailed1 failed, 6734 passed, 786 skipped, 2 xfailed, plus the 1 new testAll passing node ids are the same. The only differences are three load-sensitive tests whose outcome flips between runs on both sides, so they are flaky rather than caused by this change:
tests/test_awq_hybrid_recurrent_mask_4671.py::test_awq_batch_size_one_quantizes_a_mamba_hybrid: the tiny tokenizer fixture is "unusable" under-n 40. It failed on main in run 1 and on the branch in run 2, and passes on both sides in isolation.tests/test_awq_weight_mean.py::test_awq_pseudo_quantize_nan_guard_uses_one_host_read: fails on both sides in run 1 and on main only in run 2.tests/test_awq_weight_mean.py::test_awq_weight_mean_matches_legacy_impl (cpu, group size 64): a wall-clock speed assertion that failed on main only in run 2.Skips, the same on both sides, and what they leave unverified:
-m meta_coveragesweeps intest_moe_expert_coverage.py: module-tree expert coverage. The only tree change is a TeleChatnosrcflag, which is not a coverage change./monster/datacheckpoint tests: real LFM3 checkpoints. E3 covers LFM3-1.7B.After the rebase onto
e24a4dd2c, these re-ran and passed (1133 tests):test_awq_fold_inventory.py,test_hymba_awq_4891.py,test_ovis2_6_moe_local_port.py,test_awq_shared_kv_states_replay.py,test_k2_horizon_support.py,test_telechat2_local_port.py,test_llama_local_port.py,test_awq_fold_declarations.pyandtest_awq_moe_starved_expert_group_4675.py. The commits pulled in by the rebase do not touch any changed file or the AWQ group planner.New and changed tests:
tests/test_hymba_awq_4891.py::test_hymba_fold_variant_keeps_the_derived_unrouted_expert_contract(new). A Hymba layer with one unrouted expert keeps that expert's
up_proj -> down_projgroup with its empty capture, exactly as the deriveddefault does. With
awq_hand_declared_foldsthe group is skipped. It fails onthe branch with the old identity check.
tests/test_hymba_awq_4891.py::test_owned_hymba_drops_only_the_gateless_expert_up_to_down_fold(changed). Its "generic" oracle is now the derived declarations
(
BaseQModel.awq_fold_declarations), becauseBaseQModel.awq_get_modules_for_scalingnow dispatches to Hymba's variant.
tests/module_tree/test_ovis2_6_moe_local_port.py(changed). It asserted thatthe removed pass-through override exists. It now asserts that Ovis2.6 uses the
base planner and declarations.
E3, GPU AWQ A/B (GPU 2):
AWQConfig(bits=4, group_size=128), bf16, 64wikitext-2 rows, batch 4, seed 0, the same on both sides. Every saved
safetensors tensor is compared bytewise, and every json and
quant_log.csv(time column excluded) is compared.
LlamaQModel.awq_fold_declarationsQwen2QModel)kv_shared_attention_folds, Gemma 4 compositeThe Llama branch run used the branch revision from before the Gemma 4 / K2 conversion. Those later edits do not touch the
LlamaQModelpath. Peak CUDA memory: 0.4 to 1.2 GiB.Lint:
bash format/check.shpasses:All checks passed!(ruff 0.14.2).Re-verified after rebase onto
9d7c6f403(head44ee68326)Rebased over #6624 (AWQ batch C), #6630 (tree-order dynamic-excluded reader), #6633/#6634 (scale limit, stateless rotary) and #6637. The only conflicts were in
docs/awq-scaling-groups.mdandAWQ_FOLD_DIFFERENCES, against #6624's entries; both sides were kept.python -P tests/module_tree/awq_fold_inventory.py --check:changed: none(210 types).-n 40): branch 6743 passed / 786 skipped / 2 xfailed; main 6742 passed / 786 skipped / 2 xfailed. No failures on either side.bash format/check.sh: all checks passed.Measured Impact
Not applicable: behaviour-preserving refactor. The E3 checkpoints are
bit-identical.
Model Support / Module Plan
No new
MODEL_MAPentry. The only module-tree change istelechat2.py: key_value:0 -> key_value:0:nosrc. That flag affects AWQsources only, and
key_valuewas never one. Registry and module-plan tests(
test_registered_module_plans.py,test_definition_subset_ordering.py,test_telechat2_local_port.py) pass. Model test node IDs are in the E2 runabove.
AWQ onboarding safety
No variant changes which groups are searched or folded. Every variant
reproduces the node filter it replaces on every config, layer variant and
capture perturbation above. The existing per-model neutrality tests still pass:
tests/module_tree/test_telechat2_local_port.py::test_awq_scale_folds_preserve_every_decoder_layer_output,tests/test_hymba_awq_4891.py::test_owned_hymba_awq_folds_preserve_layer_output,tests/test_awq_shared_kv_states_replay.py,tests/test_k2_horizon_support.pyand
tests/test_llama_local_port.py. Per-variant policy is unchanged: theLlama alias layouts are plain-quantization-only, TeleChat post-LN is up -> down
only, Hymba relu2 experts have no up -> down, K2 MoVA layers have no
input-norm fold, and a Gemma 4 KV-shared tail scores
q_projalone.Logic Flow Documentation
This replaces a decision mechanism (per-model node filters, plus a
class-identity contract) with declared folds and a declared flag.
docs/awq-scaling-groups.md(already linked from README) has the updated flowdiagram and text.
flowchart TD A["awq_get_modules_for_scaling"] --> B{"awq_groups_from_tree_order?"} B -- "yes" --> T["_awq_generic_get_modules_for_scaling<br/>tracker, plus kept node overrides"] B -- "no" --> C["awq_fold_declarations(layer)"] C --> C1["BaseQModel: canonical_awq_folds + awq_folds"] C1 --> C2["definition variant filters super():<br/>llama, telechat2, hymba, k2_horizon,<br/>gemma4 kv_shared_attention_folds"] C2 --> D["_resolve_awq_fold_declarations"] D -- "malformed" --> E["raise AWQFoldDeclarationError"] D --> F["merge_by_source"] F --> G{"search input captured?"} G -- "no" --> H["skip group<br/>targets quantize plainly"] G -- "empty" --> X{"routed expert down and<br/>not awq_hand_declared_folds?"} X -- "no" --> H X -- "yes" --> I["node for the processor's<br/>starved-member fallback"] G -- "yes" --> I2["generate_node_for_awq_scaling"] style E fill:#f8d7da,stroke:#842029,stroke-width:2px style H fill:#fff3cd,stroke:#997404,stroke-width:2pxtype(self).awq_fold_declarations is BaseQModel.awq_fold_declarations(base.py:7687)not self.awq_hand_declared_folds(base.py:7717);Trueonly onpangu_alpha.py:45,inkling2_loop.py:146llama.py:65returns[]awq_fold_declarationsreturns[](llama.py:65)key_valuetelechat2.py:40telechat2.py:42,key_value:0:nosrcmodule2inspect(hymba.py:60)boundary="mamba.in_proj"(hymba.py:60)k2_horizon.py:138k2_horizon.py:138retarget_kv_shared_attention_groups(nodes)(_kv_shared_replay.py:262)kv_shared_attention_folds(layer, folds)ovis2_6_moe.py:96Review Requirements
Kernel Accuracy Requirements
Not applicable: no kernel change.
Notes
dynamicexcluding ablock that reads a shared norm still folds the norm into the remaining blocks
on the tracker path.
awq_hand_declared_foldsis new class data. Any out-of-tree definition thatwrites its own expert
downfolds inawq_fold_declarationsmust set it tokeep the skip contract.
🤖 Generated with Claude Code