Skip to content

feat(awq): declare qualified folds per layer variant (#5995) - #6263

Merged
Qubitium merged 3 commits into
mainfrom
fix/5995-issue
Nov 29, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/5995-issue

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #5995.

Summary

Definitions can now declare the qualified AWQ folds of a concrete layer variant instead of post-filtering groups selected by module-tree order. The module tree still owns quantization order and coverage; the declaration only says which existing source/targets are qualified.

Declaration API

BaseQModel.awq_fold_declarations(layer) returns:

  • None (default): the generic planner owns selection;
  • []: no fold is qualified, so the variant is explicitly quantized plainly;
  • Sequence[AWQFoldDeclaration]: one source, its scaled targets, and any additional consumers that need compensation.

AWQFoldDeclaration(source, targets, consumers=()) normalizes a single path or a sequence. Mappings and objects with the same keys are also accepted.

Resolution runs in awq_get_modules_for_scaling before scale search or weight mutation:

  • every path is layer-relative and must resolve on the constructed layer;
  • the source must be declared by the module tree, must not carry nosrc, and must be foldable;
  • every target must be an nn.Linear the plan already quantizes, with input width equal to the source's output width;
  • every consumer must have a 2-D weight of that width and must not be a quantized target;
  • duplicate sources, unresolved paths, or a target/consumer claimed by two sources raise AWQFoldDeclarationError naming the layer and offending path;
  • multi-target folds need a common parent module so the existing replay can score all targets.

Resolved paths are translated into the existing scaling nodes. When a generic node exists for the same source, its captured inp, per-feature kwargs and module2inspect are retained, so search/replay/packing interfaces are unchanged.

Migrations

  • PanGu: the awq_get_modules_for_scaling filter is removed. The ordinary block declares ln_1 -> q/k/v; the top-query block declares ln_1 -> k/v; both keep attn.v_proj -> attn.c_proj and ln_2 -> mlp.c_fc.
  • Inkling: the filter is removed. Each owned expert declares up_proj -> down_proj; the two attention folds stay excluded.
  • Tests keep the retired filter logic as comparison oracles and keep the existing negative controls that deliberately apply the excluded attention folds and measure output drift.

New test nodes

tests/module_tree/test_awq_fold_declarations.py:

  • test_declaration_api_exports_and_normalizes_paths
  • test_declared_fold_ignores_same_width_sibling_branch
  • test_sibling_reorder_keeps_resolved_fold_edges
  • test_plain_only_declaration_returns_no_fold_and_does_not_mutate
  • test_malformed_declaration_fails_before_mutation[target|source|consumer|conflicting-sources]
  • test_declared_consumers_are_translated_and_compensated
  • test_plain_only_variant_still_quantizes_and_packs

tests/test_pangu_alpha_local_port.py:

  • test_awq_declares_pangu_layer_variant_folds[ordinary|top-query]
  • test_awq_pangu_declaration_matches_filter_oracle[ordinary|top-query]
  • test_awq_pangu_declaration_retains_capture_and_replay_inputs[ordinary|top-query]

tests/module_tree/test_inkling2_loop_plan.py:

  • test_inkling2_loop_awq_declares_expected_expert_groups[sliding|full|sliding-loop|full-loop]
  • test_inkling2_loop_awq_empty_declaration_fails_positive_case
  • test_inkling2_loop_awq_declaration_matches_filter_oracle[sliding|full|sliding-loop|full-loop]

Red evidence at parent (HEAD~1, tests applied)

cd /root/openai-audit/issue-5995/tests && PYTHONPATH=/root/openai-audit/issue-5995:/root/openai-audit/issue-5995/tests CUDA_VISIBLE_DEVICES="" /root/venv/bin/python -m pytest module_tree/test_awq_fold_declarations.py test_pangu_alpha_local_port.py::test_awq_declares_pangu_layer_variant_folds test_pangu_alpha_local_port.py::test_awq_pangu_declaration_retains_capture_and_replay_inputs module_tree/test_inkling2_loop_plan.py::test_inkling2_loop_awq_empty_declaration_fails_positive_case -q -rf -p no:cacheprovider

Result: 10 failed, 5 passed. The failures are the intended contract gaps:

  • the generic planner accepts a same-width wrong-branch source (norm -> mixer.left, then mixer.left -> mixer.right, mixer.right -> out);
  • reordering independent siblings changes the resolved edges;
  • an explicit plain-only declaration is ignored;
  • malformed declarations DID NOT RAISE ValueError (unresolved target/source/consumer and conflicting sources);
  • an Inkling empty plan still emits folds (DID NOT RAISE AssertionError);
  • AWQFoldDeclaration does not exist yet.

The PanGu migration tests pass at parent because the old filter was already neutral there; they pin that the declaration preserves the old edges and capture/replay inputs.

Green evidence

cd /root/openai-audit/issue-5995/tests && PYTHONPATH=/root/openai-audit/issue-5995:/root/openai-audit/issue-5995/tests CUDA_VISIBLE_DEVICES="" /root/venv/bin/python -m pytest module_tree/test_awq_fold_declarations.py module_tree/test_inkling2_loop_plan.py test_pangu_alpha_local_port.py module_tree/test_pangu_alpha_local_port.py test_awq_scaling_groups.py test_awq_scale_folding.py test_awq_scale_search_safety.py module_tree/test_awq_fold_safety_4175.py module_tree/test_awq_relationships.py -q -rs -p no:cacheprovider

Result: 376 passed, 9 skipped (skips: 7 PanGu tokenizer cases because jieba is absent; 2 unbuildable families in test_awq_scaling_groups.py).

Additional fold/plan families:

cd /root/openai-audit/issue-5995/tests && PYTHONPATH=/root/openai-audit/issue-5995:/root/openai-audit/issue-5995/tests CUDA_VISIBLE_DEVICES="" /root/venv/bin/python -m pytest module_tree/test_lfm3_local_port.py module_tree/test_lfm3_vl_local_port.py module_tree/test_lfm3_omni_local_port.py test_lfm3_local_port.py test_lfm3_support.py test_lfm3_native_coverage.py test_lfm3_vl_local_port.py test_lfm3_omni_local_port.py module_tree/test_awq_raon1_mini_neutrality.py module_tree/test_awq_nanbeige_neutrality.py module_tree/test_bloom_awq_relationships.py module_tree/test_dots3_note_fork_plan.py module_tree/test_kimi_linear_fork_plan.py module_tree/test_kimi_k3_fork_plan.py module_tree/test_registered_module_plans.py module_tree/test_explicit_module_plan.py module_tree/test_gemma_olmo_family_plan_coverage.py test_subset_plan.py module_tree/test_subset.py module_tree/test_module_tree_variants.py test_fp8_plan_cache_5592.py test_kv_plan_unknowns_5698.py -q -rs -p no:cacheprovider

Result: 716 passed, 7 skipped (missing /monster/data/model/... assets and a free-threaded-only case).

Ruff:

/root/venv/bin/ruff check gptqmodel/models/base.py gptqmodel/models/__init__.py gptqmodel/models/definitions/pangu_alpha.py gptqmodel/models/definitions/inkling2_loop.py tests/module_tree/test_awq_fold_declarations.py tests/module_tree/test_inkling2_loop_plan.py tests/test_pangu_alpha_local_port.py

Result: All checks passed!

Filters migrated vs retained

  • Removed: PanGu and Inkling per-definition awq_get_modules_for_scaling filters, proven equivalent by the oracle tests above.
  • Retained: all other definitions still use the generic planner; the global _merge_shared_source_nodes / _filter_awq_scaling_sources safety filters remain in place for declared and generic nodes.

Skips / pre-existing / unverified

  • test_public_plain_awq_pack_infer_and_two_reloads is skipped in the default environment because jieba is absent. With jieba temporarily installed it runs and fails identically at parent HEAD (RuntimeError: shape '[1, 15, 4, 8]' is invalid for input of size 288 in PanGu replay); it is pre-existing and not caused by this change, so the test is left unchanged.
  • No GPU tests were run; all evidence above is CPU-only.

Comment thread gptqmodel/models/base.py Fixed
Addresses the code-quality bot note on #6263: _awq_fold_paths intentionally
accepts either one path or a sequence, so annotate the tuple return and
document why the arity is caller-defined.
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Review: declarations fix the fold edges; three non-blocking gaps

No blocking issues found: a definition with no declaration still takes the unchanged generic path, malformed declarations raise before any weight mutation, and the PanGu/Inkling migrations produce the old filter's edges (374 of the PR's listed tests pass here; the two test_public_plain_awq_pack_infer_and_two_reloads failures also fail at the parent commit).

Non-blocking

  1. gptqmodel/models/base.py:7606 — when a generic node exists for the declared source, the declared fold reuses that node's _input_feature_name even when the feature belongs to a target the declaration removed. Concrete PanGu top-query case (verified with the new _pangu_declared_nodes fixture): the declaration is ln_1 -> k/v, but node["_input_feature_name"] == "attn.q_proj" and node["inp"] is features["attn.q_proj"] (q_proj's custom_query), not ln_1's output. The fold stays output-neutral, but the AWQ scale search and replay score k/v on the wrong activation. This is pre-existing (the old filter kept the same node), so not a regression; consider preferring a declared target's feature when _input_feature_name is not in target_names.

  2. gptqmodel/models/base.py:7612-7621 — a declared fold is silently skipped (log.debug + continue) when none of its targets captured a feature. An explicit declaration can therefore degrade to plain quantization with no error; only a debug log. If that is intended for missing captures, please document it; otherwise warn or raise.

  3. tests/test_pangu_alpha_local_port.py:585 and tests/module_tree/test_inkling2_loop_plan.py:1292 — the "oracle" tests reimplement the retired filters inline instead of comparing against the pre-change implementation, and they compare only the resolved edges, not the captured inp/kwargs. A copy error in the oracle would pass. Pin the old output with a pre-change digest, or import the base implementation.

A declared fold reused the generic node's captured input even when that
feature belonged to a target the declaration removed. PanGu's top-query
block declares `ln_1 -> k/v`, but its generic `ln_1` node captured
`q_proj`'s `custom_query`, so the AWQ scale search scored k/v on the query
embedding instead of the norm output. Re-derive the feature from a declared
target's capture whenever the generic feature is not one of the targets;
ordinary variants keep the exact generic feature, kwargs and replay module.

Also warn, with the layer and target paths, when none of a declaration's
targets captured a feature. That condition used to be a debug log plus a
silent fallback to plain quantization. It stays a warning rather than an
error because the declaration is structurally valid and a target can
legitimately not run during calibration; the rest of AWQ already treats a
missing capture as a skipped group and falls back.

Tests: PanGu top-query activation selection, ordinary-variant identity,
Inkling identity, `None` generic parity, and the missing-capture warning.
@Qubitium

Qubitium commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

Review follow-up: declared-fold activation and silent degradation

Pushed 2d0e3af2 on fix/5995-issue (on top of c7289895). Both findings from the review are fixed; the declaration contract for None (generic planner) and [] (explicitly plain) is unchanged.

1. The declared fold now scores the declared source's activation

PanGu top-query (h[-1]) declares ln_1 -> attn.k_proj, attn.v_proj. The generic ln_1 node had been selected from q_proj, which reads custom_query in that variant.

Before (c7289895):

ln_1 node _input_feature_name = attn.q_proj
ln_1 node inp is features['attn.q_proj'] = True
ln_1 node inp is features['attn.k_proj'] = False
q_proj capture == custom_query values: True

After (2d0e3af2):

ln_1 node _input_feature_name = attn.k_proj
ln_1 node inp is features['attn.q_proj'] = False
ln_1 node inp is features['attn.k_proj'] = True

The scale-search input is now a declared target's capture (the norm output). The wrong tensor was the query embedding, and the per-channel mean |x| used by the search differs by 97.5% on average (99.1% max), so the old scales were learned from a materially different objective. module2inspect (attn) and the global replay kwargs (custom_query, per-feature masks/positions) are retained; per-feature kwargs now come from the selected target.

Ordinary variants are unchanged: PanGu h[0] still selects attn.q_proj / attn.c_proj / mlp.c_fc and the declared node's inp is the exact same object as the generic node's. Inkling's up_proj -> down_proj nodes likewise reuse the generic _input_feature_name and inp object.

2. Missing captured target is now a warning, not a silent skip

Before: log.debug(...) + continue, so an explicit declaration degraded to plain quantization with no warning at the default INFO level.

After:

awq_get_modules_for_scaling: skipping declared fold `norm` on layer layer (_BranchLayer);
none of its targets ['mixer.left', 'mixer.right'] captured an input feature;
they fall back to plain quantization

Severity choice: warning, not AWQFoldDeclarationError. The declaration is structurally valid (paths resolve and the plan quantizes the targets); a missing capture is a calibration/runtime condition, and a target can legitimately not run. The rest of AWQ already treats a missing capture as a skipped group and falls back to plain quantization, so raising here would add a new abort path for a condition the pipeline already handles. The warning names the layer, the source, and every declared target. awq_fold_declarations() -> None still goes straight to the generic planner; [] still means explicitly plain and is not an error.

New / updated test nodes

  • tests/test_pangu_alpha_local_port.py::test_awq_pangu_top_query_scores_declared_source_activation (new)
  • tests/test_pangu_alpha_local_port.py::test_awq_pangu_ordinary_declaration_keeps_generic_search_input (new)
  • tests/test_pangu_alpha_local_port.py::test_awq_pangu_declaration_retains_capture_and_replay_inputs[ordinary|top-query] (updated for the corrected top-query input)
  • tests/module_tree/test_awq_fold_declarations.py::test_declared_fold_without_captured_target_warns_and_skips (new)
  • tests/module_tree/test_awq_fold_declarations.py::test_no_declaration_keeps_the_generic_planner (new)
  • tests/module_tree/test_inkling2_loop_plan.py::test_inkling2_loop_awq_declaration_matches_filter_oracle[sliding|full|sliding-loop|full-loop] (strengthened to assert feature/inp identity)

Red on the unmodified PR head c7289895 with only the test changes applied:

FAILED test_pangu_alpha_local_port.py::test_awq_pangu_top_query_scores_declared_source_activation
  assert 'attn.q_proj' in ('attn.k_proj', 'attn.v_proj')
FAILED test_pangu_alpha_local_port.py:test_awq_pangu_declaration_retains_capture_and_replay_inputs[top-query]
  assert 'attn.q_proj' in ('attn.k_proj', 'attn.v_proj')
FAILED module_tree/test_awq_fold_declarations.py::test_declared_fold_without_captured_target_warns_and_skips
  AssertionError: []

The ordinary and None tests passed at the same head, as intended.

Green after the fix (CPU, CUDA_VISIBLE_DEVICES=""):

  • three named suites (test_awq_fold_declarations.py, test_inkling2_loop_plan.py, test_pangu_alpha_local_port.py): 82 passed, 2 failed
  • broader declaration/AWQ set from the PR evidence: 385 passed, 2 failed, 2 skipped
  • LFM3 suites: 176 passed, 6 skipped (missing /monster/data/model/... assets)
  • plan/subset families: 540 passed, 1 skipped

The only failures are the two test_public_plain_awq_pack_infer_and_two_reloads parameterizations already documented as pre-existing; I re-ran them at c7289895 with the production code unchanged and they fail identically (RuntimeError: shape '[1, 15, 4, 8]' is invalid for input of size 288 in PanGu replay). Ruff on the four changed files: All checks passed!.

No GPU tests were run; no labels/merge.

@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 08:19
@Qubitium
Qubitium merged commit d71e124 into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5995-issue branch November 29, 2026 08:19
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.

Declare AWQ fold sources and consumers per layer variant

1 participant