Repository navigation
feat(awq): declare qualified folds per layer variant (#5995) - #6263
Conversation
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.
Review: declarations fix the fold edges; three non-blocking gapsNo 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 Non-blocking
|
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.
Review follow-up: declared-fold activation and silent degradationPushed 1. The declared fold now scores the declared source's activationPanGu top-query ( Before ( After ( 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 Ordinary variants are unchanged: PanGu 2. Missing captured target is now a warning, not a silent skipBefore: After: Severity choice: warning, not New / updated test nodes
Red on the unmodified PR head The ordinary and Green after the fix (CPU,
The only failures are the two No GPU tests were run; no labels/merge. |
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]: onesource, its scaledtargets, and any additionalconsumersthat 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_scalingbefore scale search or weight mutation:nosrc, and must be foldable;nn.Linearthe plan already quantizes, with input width equal to the source's output width;AWQFoldDeclarationErrornaming the layer and offending path;Resolved paths are translated into the existing scaling nodes. When a generic node exists for the same source, its captured
inp, per-featurekwargsandmodule2inspectare retained, so search/replay/packing interfaces are unchanged.Migrations
awq_get_modules_for_scalingfilter is removed. The ordinary block declaresln_1 -> q/k/v; the top-query block declaresln_1 -> k/v; both keepattn.v_proj -> attn.c_projandln_2 -> mlp.c_fc.up_proj -> down_proj; the two attention folds stay excluded.New test nodes
tests/module_tree/test_awq_fold_declarations.py:test_declaration_api_exports_and_normalizes_pathstest_declared_fold_ignores_same_width_sibling_branchtest_sibling_reorder_keeps_resolved_fold_edgestest_plain_only_declaration_returns_no_fold_and_does_not_mutatetest_malformed_declaration_fails_before_mutation[target|source|consumer|conflicting-sources]test_declared_consumers_are_translated_and_compensatedtest_plain_only_variant_still_quantizes_and_packstests/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_casetest_inkling2_loop_awq_declaration_matches_filter_oracle[sliding|full|sliding-loop|full-loop]Red evidence at parent (
HEAD~1, tests applied)Result: 10 failed, 5 passed. The failures are the intended contract gaps:
norm -> mixer.left, thenmixer.left -> mixer.right,mixer.right -> out);DID NOT RAISE ValueError(unresolved target/source/consumer and conflicting sources);DID NOT RAISE AssertionError);AWQFoldDeclarationdoes 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
Result: 376 passed, 9 skipped (skips: 7 PanGu tokenizer cases because
jiebais absent; 2 unbuildable families intest_awq_scaling_groups.py).Additional fold/plan families:
Result: 716 passed, 7 skipped (missing
/monster/data/model/...assets and a free-threaded-only case).Ruff:
Result:
All checks passed!Filters migrated vs retained
awq_get_modules_for_scalingfilters, proven equivalent by the oracle tests above._merge_shared_source_nodes/_filter_awq_scaling_sourcessafety filters remain in place for declared and generic nodes.Skips / pre-existing / unverified
test_public_plain_awq_pack_infer_and_two_reloadsis skipped in the default environment becausejiebais absent. Withjiebatemporarily installed it runs and fails identically at parentHEAD(RuntimeError: shape '[1, 15, 4, 8]' is invalid for input of size 288in PanGu replay); it is pre-existing and not caused by this change, so the test is left unchanged.