Skip to content

test(awq): cover fold-graph config features and fix Gemma KV/MoE folds - #6329

Merged
Qubitium merged 1 commit into
mainfrom
audit/awq-neutrality
Nov 29, 2026
Merged

Qubitium merged 1 commit into
mainfrom
audit/awq-neutrality

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The per-family AWQ output-preservation invariant in tests/test_awq_scaling_groups.py
only built default configs. A fold can be non-neutral only when a config feature
changes the fold graph (which modules a layer owns, which tensor a consumer reads, or
which producer a fold source has). This adds
tests/test_awq_scaling_group_features.py: 70 feature-enabled tiny-fixture cases
(79 tests total) that run the same fold-neutrality check with those features enabled,
and adds gemma3n/gemma3n_text, which were absent from the invariant.

The existing parametrizations and tolerances in tests/test_awq_scaling_groups.py are
unchanged.

Failures found (pre-fix, CPU, tiny fixtures)

family feature non-neutral fold measured relative change
gemma4_text KV sharing (num_kv_shared_layers=2) input_layernorm -> [self_attn.q_proj, self_attn.o_proj] (layers 2/3) 1.341e-01 / 1.065e-01
gemma4 (composite) KV sharing same fold (layers 2/3) 1.306e-01 / 1.040e-01
gemma4_unified KV sharing same fold (layers 2/3) 1.0455e-01 / 1.0359e-01
gemma4_unified_text KV sharing same fold (layers 2/3) 1.0503e-01 / 1.0243e-01
gemma4_text attention_k_eq_v=True input_layernorm -> [self_attn.q_proj, self_attn.k_proj, self_attn.o_proj] 2.067e-01
gemma4_text enable_moe_block=True pre_feedforward_layernorm -> [mlp.gate_proj, mlp.up_proj, experts.0..3.gate_proj, experts.0..3.up_proj] 1.115e-01
gemma4 (composite) enable_moe_block=True same dense-MLP + routed-expert group 1.134e-01

The KV-sharing failure is the one reported from PR #6317 (1.839e-01 on its fixture).
gemma4_text + KV sharing + MoE reproduced both defects together (1.363e-01 on the
attention fold and 1.074e-01 on the expert fold).

During the MoE diagnosis, moving pre_feedforward_layernorm_2 before the expert block
alone reduced the error to 3.533e-02: the remaining error was the MoE router being
treated as a scale consumer of that norm. The router reads the pre-norm residual, not the
norm's output.

Fixes

  1. Gemma attention folds without a v_proj (gptqmodel/models/definitions/gemma4.py,
    wired into gemma4, gemma4_text, gemma4_unified, gemma4_unified_text, gemma3n,
    gemma3n_text): _retarget_attention_output_folds removes o_proj from a norm-fed
    group when its attention parent owns no v_proj (KV-shared consumer or
    attention_k_eq_v). It keeps q_proj (plus k_proj when the layer owns one), and
    scores a single surviving projection on itself (Hymba [BUG] #5019 (fix of #4891) incomplete: Hymba AWQ still always crashes on the real checkpoint at layer 2 - HymbaBlock replay lacks kv_last_layer for KV-reuse layers (AssertionError, no fallback) #5098 precedent). o_proj stays
    on plain quantization because it has no foldable source of its own.
  2. Gemma 4 MoE block (gemma4.py, base.py):
    • pre_feedforward_layernorm_2 is declared before router/experts, so the routed
      gate/up group anchors on the norm that actually feeds it.
    • New tree flag :noconsumer + awq_no_scale_consumer_modules() excludes
      router.proj from _norm_scale_consumers; it reads the pre-norm residual.
    • The duplicated text/composite decoder trees now share _gemma4_decoder_block() so
      the two definitions cannot drift.
  3. No assertion or tolerance was weakened.

Feature/coverage table (families in the base invariant + gemma3n)

family config field value that changes the fold graph covered
afmoe layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
afmoe num_dense_layers 0 (all MoE) or all layers (all dense) yes (base fixture)
afmoe num_experts 0/dense vs >0 routed experts yes (base fixture)
afmoe num_shared_experts 0 (no shared branch) vs >0 yes (new)
cohere use_qk_norm True: adds q/k norms yes (new)
cohere2 layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
cohere2_moe layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
cohere2_moe mlp_layer_types alternate dense/sparse list yes (new)
cohere2_moe num_experts 0/dense vs >0 routed experts yes (base fixture)
cohere2_moe num_shared_experts 0 (no shared branch) vs >0 yes (new)
deepseek_v2 first_k_dense_replace 0 (all MoE) or all layers (all dense) yes (new)
deepseek_v2 n_routed_experts 0/dense vs >0 routed experts yes (base fixture)
deepseek_v2 n_shared_experts 0 (no shared branch) vs >0 yes (new)
exaone4 layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
falcon new_decoder_architecture True: parallel attention/MLP yes (new)
falcon num_ln_in_parallel_attn 0 vs 2 norms yes (new)
falcon parallel_attn flip: parallel vs sequential attention/MLP yes (new)
falcon_h1 mamba_norm_before_gate flip: norm before/after gate yes (new)
flint decoder_sparse_step 1 (every layer MoE) vs >1 yes (base fixture)
flint layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
flint mlp_only_layers all layers dense vs [] yes (new)
flint num_experts 0/dense vs >0 routed experts yes (base fixture)
flint shared_expert_intermediate_size 0 (no shared branch) vs >0 yes (new)
gemma3n text_config.layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
gemma3n text_config.num_kv_shared_layers >0: consumer owns no k_proj/v_proj yes (new)
gemma4 text_config.attention_k_eq_v True: full-attention v_proj is None yes (new)
gemma4 text_config.enable_moe_block True: adds router + routed experts beside dense MLP yes (new)
gemma4 text_config.layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (base fixture)
gemma4 text_config.num_experts 0/dense vs >0 routed experts yes (base fixture)
gemma4 text_config.num_kv_shared_layers >0: consumer owns no k_proj/v_proj yes (new)
gemma4 text_config.use_double_wide_mlp True: wider intermediate, same module graph n/a (width only)
gemma4_text attention_k_eq_v True: full-attention v_proj is None yes (new)
gemma4_text enable_moe_block True: adds router + routed experts beside dense MLP yes (new)
gemma4_text layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (base fixture)
gemma4_text num_experts 0/dense vs >0 routed experts yes (base fixture)
gemma4_text num_kv_shared_layers >0: consumer owns no k_proj/v_proj yes (new)
gemma4_text use_double_wide_mlp True: wider intermediate, same module graph n/a (width only)
gemma4_unified text_config.attention_k_eq_v True: full-attention v_proj is None yes (new)
gemma4_unified text_config.layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (base fixture)
gemma4_unified text_config.num_kv_shared_layers >0: consumer owns no k_proj/v_proj yes (new)
gemma4_unified text_config.use_double_wide_mlp True: wider intermediate, same module graph n/a (width only)
gpt2 add_cross_attention True: adds cross-attention block yes (new)
gpt_bigcode add_cross_attention True: adds cross-attention block no (unbuildable)
gpt_neo attention_types global/local mix yes (new)
gpt_neox use_parallel_residual flip: attention and MLP share or serialize one norm yes (new)
gpt_oss layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
gpt_oss num_local_experts 0/dense vs >0 routed experts yes (base fixture)
k2_horizon decoder_sparse_step 1 (every layer MoE) vs >1 yes (base fixture)
k2_horizon mlp_only_layers all layers dense vs [] yes (base fixture)
k2_horizon num_experts 0/dense vs >0 routed experts yes (new)
k2_horizon num_shared_experts 0 (no shared branch) vs >0 yes (base fixture)
laguna layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
laguna mlp_layer_types alternate dense/sparse list yes (new)
laguna num_experts 0/dense vs >0 routed experts yes (base fixture)
laguna shared_expert_intermediate_size 0 (no shared branch) vs >0 yes (new)
lfm2_moe layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
lfm2_moe num_dense_layers 0 (all MoE) or all layers (all dense) yes (base fixture)
lfm2_moe num_experts 0/dense vs >0 routed experts yes (base fixture)
lfm3 layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
lfm3 use_sandwich_norm False: removes post-operator norm yes (new)
minimax layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
minimax num_local_experts 0/dense vs >0 routed experts yes (base fixture)
minimax_m2 num_local_experts 0/dense vs >0 routed experts yes (new)
mixtral num_local_experts 0/dense vs >0 routed experts yes (new)
nemotron_h layers_block_type alternate M/E/*/- block pattern yes (base fixture)
nemotron_h n_routed_experts 0/dense vs >0 routed experts yes (base fixture)
nemotron_h n_shared_experts 0 (no shared branch) vs >0 yes (new)
olmo3 layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
olmo_hybrid layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
olmoe num_experts 0/dense vs >0 routed experts yes (new)
opt do_layer_norm_before False: post-norm decoder yes (new)
phi qk_layernorm True: adds q/k norms yes (new)
qwen2_moe decoder_sparse_step 1 (every layer MoE) vs >1 yes (new)
qwen2_moe layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp yes (new)
qwen2_moe mlp_only_layers all layers dense vs [] yes (new)
qwen2_moe num_experts 0/dense vs >0 routed experts yes (base fixture)
qwen2_moe shared_expert_intermediate_size 0 (no shared branch) vs >0 yes (base fixture)
qwen3_moe decoder_sparse_step 1 (every layer MoE) vs >1 yes (new)
qwen3_moe mlp_only_layers all layers dense vs [] yes (new)
qwen3_moe num_experts 0/dense vs >0 routed experts yes (base fixture)
smollm3 layer_types alternate mix: sliding/full, linear/full, conv/full, moe/mlp no (unbuildable)
solar_open n_routed_experts 0/dense vs >0 routed experts yes (base fixture)
solar_open n_shared_experts 0 (no shared branch) vs >0 yes (new)
stablelm qk_layernorm True: adds q/k norms yes (new)
stablelm use_parallel_residual flip: attention and MLP share or serialize one norm yes (new)

The enumeration script also inspected every other MODEL_MAP definition: 110 model
types declare at least one curated graph-changing field. 74 of those are outside the
base invariant's 60-family buildable set (mostly VL/audio wrappers and ports without a
tiny AutoModelForCausalLM fixture); the full machine table marks them no/unknown
rather than silently omitting them.

Unbuildable feature variants (recorded, not dropped)

case error
smollm3 layer_types=[sliding, full] ValueError: missing per-layer head_dim (heterogeneous per-layer config rejects a global head_dim)
nemotron_h hybrid_override_pattern AttributeError: property 'hybrid_override_pattern' has no setter; the default ME*- fixture already covers linear-attention, MoE, full-attention and dense-MLP blocks
internvl_chat layer_types=[sliding, full] TypeError: 'NoneType' object cannot be interpreted as an integer in the vision-wrapper forward
gpt_bigcode add_cross_attention=True NotImplementedError: Cross-attention not implemented for MQA
minimax_m2/gpt_oss num_local_experts=1 RuntimeError: selected index k out of range (top_k=2 with one expert)
falcon num_ln_in_parallel_attn=2 AttributeError: 'FalconDecoderLayer' object has no attribute 'input_layernorm'; the definition tree declares the single-norm parallel layout. num_ln_in_parallel_attn=0 is covered and neutral.

gemma3n's default composite fixture is ~976M parameters (300M MobileNet vision tower);
the test shrinks the text stack, vision tower (mobilenetv3_small_100) and audio tower
to ~1.6M parameters. It skips if timm is unavailable.

Verification (CPU only)

cd tests
PYTHONPATH=/root/openai-audit/issue-neutral:/root/openai-audit/issue-neutral/tests \
  CUDA_VISIBLE_DEVICES="" /root/venv/bin/python -m pytest \
  test_awq_scaling_group_features.py -q -rs -p no:cacheprovider
# 79 passed

PYTHONPATH=... /root/venv/bin/python -m pytest \
  module_tree/test_awq_* test_awq_scale_folding.py test_awq_scaling_groups.py \
  test_awq_scaling_group_features.py test_awq_empty_groups.py \
  test_awq_scale_search_safety.py test_awq_moe_router_scaling.py \
  test_awq_falcon_h1_parallel_branch_4741.py \
  test_awq_cohere2_moe_parallel_norm_4898.py test_awq_hybrid_moe_root_4903.py \
  test_awq_moe_shared_expert_group_4760.py test_awq_mla_scaling.py \
  test_awq_ling2_fold_4677.py test_gemma4_support.py \
  test_gemma4_unified_support.py test_native_definition_loading_1.py \
  test_layer_type_replay.py test_kv_cache_estimate_layer_kinds_4542.py \
  -q -rs -p no:cacheprovider
# 515 passed, 2 skipped (opt350, muse_glimmer: no buildable CausalLM fixture)

/root/venv/bin/ruff check gptqmodel/models/base.py \
  gptqmodel/models/definitions/gemma3n.py \
  gptqmodel/models/definitions/gemma4.py \
  gptqmodel/models/definitions/gemma4_unified.py \
  tests/test_awq_scaling_group_features.py
# All checks passed

A full tests/test_awq_*.py sweep was also attempted: 328 tests reported PASSED with no
FAILED before the run was stopped at the slow CPU end-to-end quantization test
test_awq_gemv.py::test_awq_gemv_quantized_model_loads_and_generates (runtime, not a
failure).

Follow-ups

None requiring a redesign. The unbuildable variants above are fixture/definition-layout
limitations, not observed non-neutral folds. falcon's 2-norm parallel layout is not
declared by the definition tree, so the invariant cannot exercise it; its 0-norm and
1-norm layouts are covered.

Rebased audit/awq-neutrality onto origin/main abde192. gemma3n.py and
gemma4_unified.py conflicted with main's newer _kv_shared_replay
helpers. Kept main's awq_module_replay_inputs/retarget behavior and
applied this PR's _retarget_attention_output_folds on top, so KV-shared
consumers (main) and attention_k_eq_v layers (this PR) both keep o_proj
out of the norm-fed fold. Dropped the PR's duplicate
awq_get_modules_for_scaling definitions in gemma4.py in favour of one
method per class; kept the PR's _gemma4_decoder_block refactor and tree
fixes (postnorm, pre_feedforward_layernorm_2, router noconsumer).

Tests: new test_awq_scaling_group_features.py 79 passed; base
test_awq_scaling_groups.py 172 passed, 2 skipped on both main and the
rebase. ruff clean.
@Qubitium
Qubitium force-pushed the audit/awq-neutrality branch from fcb5338 to 68ad780 Compare November 29, 2026 08:46
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Rebased onto abde192e. gemma3n.py and gemma4_unified.py conflicted with main's newer _kv_shared_replay helpers. Kept main's awq_module_replay_inputs/retarget_kv_shared_attention_groups behavior and layered this PR's _retarget_attention_output_folds on top, so KV-shared consumers (main) and attention_k_eq_v layers (this PR) both keep o_proj out of the norm-fed fold. In gemma4.py, dropped the PR's duplicate awq_get_modules_for_scaling definitions in favour of one method per class that runs both retargets; kept the _gemma4_decoder_block() refactor and tree fixes (postnorm, pre_feedforward_layernorm_2, router noconsumer). Tests: new test_awq_scaling_group_features.py 79 passed; base test_awq_scaling_groups.py 172 passed / 2 skipped on both main and the rebase. ruff check passes on all changed Python files.

@Qubitium
Qubitium force-pushed the audit/awq-neutrality branch 2 times, most recently from fb73a14 to 68ad780 Compare November 29, 2026 08:49
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 12:36
@Qubitium
Qubitium merged commit 840a916 into main Nov 29, 2026
8 checks passed
@Qubitium
Qubitium deleted the audit/awq-neutrality branch November 29, 2026 12:36
Qubitium added a commit that referenced this pull request Nov 29, 2026
- test_checkpoint_method_compatibility: the fake BaseQModel.quantize()
  receiver now needs `quantized` (#6347 reads it before checkpoint
  validation).
- test_qwen2_family_compat: clear the explicit HF_CONVERSION_MAP_REVERSED
  (#6360) so the test still covers the transformers fallback path.
- registered_module_plans.json: re-record the Gemma4/Gemma4Text plan after
  #6329 moved pre_feedforward_layernorm_2 ahead of the router block.

Co-authored-by: agent <agent@local>
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