Skip to content

fix(awq): clear the prev-op tracker when a present block's last declaration resolves to no module - #6449

Merged
Qubitium merged 2 commits into
mainfrom
fix/mimo-v2-awq-norm-fold
Nov 29, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/mimo-v2-awq-norm-fold

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

On MiMo-V2, AWQ folds self_attn.o_proj's input scale into input_layernorm, in the same group as q/k/v. The two cannot share one scale: o_proj reads the attention output, not the normed hidden state. With identity quantization (fold only), the full-model logits of a tiny random-init mimo_v2 change by about 1e-1 relative. With this fix the change is 1.5e-7.

Cause. The generic planner (BaseQModel._awq_generic_get_modules_for_scaling) tracks a "previous op" in tree order:

  • After each quantized block, it points last_module at the block's last declaration (base.py:8255 on ef7af83db).
  • The next block falls back to that tracker when no norm in the pending run feeds it.
  • _try_update_last_module keeps the old tracker when that declaration resolves to no module.

MiMo-V2 declares self_attn: (qkv_proj:0, q_proj:0, k_proj:0, v_proj:0, o_proj:1, attention_sink_bias:!). As a result:

  • The q/k/v block ends with attention_sink_bias, which is a parameter (or None), never a module.
  • The tracker therefore kept input_layernorm from before the block.
  • The norm's width check is the only guard, and it passes whenever num_attention_heads * v_head_dim == hidden_size. That holds for the config defaults, for split and fused layouts, and for MHA and GQA.

The released MiMo-V2.5/V2.6 checkpoints (fused_qkv, v_head_dim=128, so o_proj is 8192 or 16384 wide against a 4096 or 6144 hidden size) are rejected by that width check today, so they are not affected.

Fix (shared planner, no per-model code). When the layer owns a quantized block but the block's last declaration resolves to no module, the producer of that block's output is unknown. The tracker is now cleared instead of left pointing at a source from before the block. The next block then gets no fallback source, so o_proj is quantized without a fold.

What Changed

  • gptqmodel/models/base.py: at the tracker update at the end of each quantized block, if _try_update_last_module fails and the block is not absent, set last_module and last_module_name to None (+12/-1).
  • docs/awq-scaling-groups.md: the flow node and one paragraph now describe the rule.
  • tests/module_tree/test_mimo_v2_local_port.py: new real-model regression test (below).
  • tests/module_tree/test_awq_raon1_mini_neutrality.py: Raon-1 mini's q/k/v block ends with dt_bias:!, so the generic planner also handed its o_proj the input norm. The definition already strips that group, so Raon's effective plan does not change. The negative control that pinned the stale generic group is replaced by test_raon_generic_planner_gives_o_proj_no_source.
  • No API change. The only plan change for supported definitions is that the wrong groups listed below are no longer emitted.
  • Out of scope: emitting a v_proj -> o_proj fold for split-MHA MiMo-V2. The planner never emitted a correct one, and inferring it would need the role-based derivation from the AWQ fold sequence (test(awq): pin every definition's effective fold plan and add the role-derived folds alongside #6446), not a guess in the positional tracker.

Effect on every MODEL_MAP definition (fold-plan diff, main vs branch)

I ran the meta-device fold-plan inventory from #6446 (tests/module_tree/awq_fold_inventory.py) on origin/main (ef7af83db) and on this branch.

  • It covers all 210 MODEL_MAP types and 254 buildable config variants. I added 4 MiMo-V2 variants: split_sinks, fused_mha, split_gqa and the released MiMo-V2.5-Base.
  • It calls the real awq_get_modules_for_scaling on every decoder layer and records source, targets, consumers, boundary and the _quantize_layer outcome for each group.
  • The 19 config-default variants that do not build are identical on both sides; each of those types also has a released/fixture variant that does build. dbrx is the registry sentinel.

Only three types change:

Type / variant main branch Why the change is correct
mimo_v2 default, split_sinks, split_gqa, fused_mha (all layers) input_layernorm -> [q,k,v,o_proj] or [qkv_proj, o_proj] (status ok, non-neutral) input_layernorm -> [q,k,v] / [qkv_proj]; o_proj not grouped The intended fix: o_proj reads the attention output.
mimo_v2 v25_base (released) unchanged unchanged The width check already refused the stale source.
diffusion_gemma parity, layer 1 (attention_k_eq_v, no v_proj) input_layernorm -> [q_proj, k_proj, o_proj] input_layernorm -> [q_proj, k_proj] Same mechanism: the owned q/k/v block ends with the absent v_proj. This is the separately tracked diffusion_gemma bug (attention part only, not the routed up->down note).
dots3_note default, every sliding_attention layer (2-4, 6-8, ...) the layer raises _AWQScaleGroupError: source self_attn.kv_a_proj_with_mqa has multiple scaling groups no raise; input_layernorm -> [q_a, kv_a] ok, kv_a -> [q_b, kv_b] drop:width, MLP groups ok Same mechanism: the stage-1 block ends with indexer.weights_proj, which sliding layers lack. The stale kv_a_proj_with_mqa became o_proj's source and conflicted. On a tiny CPU dots3_note checkpoint, GPTQModel.load(...).quantize(...) with AWQ now plans 3 groups on the sliding layer instead of aborting. It later stops at packing a 2-wide indexer projection of the tiny fixture (out_features 2 must be divisible by 8), which is unrelated.

Every other definition's plan is byte-identical.

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.

Interpreter: /root/venv/bin/python (Python 3.14.4, torch 2.15.0+cu132, transformers 5.23.0), CPU only, HF_HUB_OFFLINE=1.

New regression test: tests/module_tree/test_mimo_v2_local_port.py::test_awq_o_proj_is_not_folded_into_input_layernorm_when_widths_match[{4,2}-{split,fused_qkv}].

  • It builds the real port with the test file's tiny config, set to v_head_dim = 8 so that heads * v_head_dim == hidden_size.
  • Layer 0 is full attention without a sink plus a dense MLP; layer 1 is SWA with a sink plus routed MoE.
  • It applies every emitted group with non-uniform scales and no quantization through assert_awq_folds_neutral, on full-model logits.
  • It pins the attention groups to exactly input_layernorm -> q/k/v (or qkv_proj).
# origin/main (ef7af83db) with only the new test copied in: 4 failed, 25 passed
python -P -m pytest -q tests/module_tree/test_mimo_v2_local_port.py
#   [4-split]     input_layernorm -> [q_proj, k_proj, v_proj, o_proj] changed the layer output by 1.235e-01
#   [4-fused_qkv] input_layernorm -> [qkv_proj, o_proj] changed the layer output by 1.235e-01
#   [2-split]     ... 1.052e-01      [2-fused_qkv] ... 1.052e-01
# branch: 29 passed

# AWQ onboarding CPU baseline + touched model tests (branch): 861 passed, 2 skipped
python -P -m pytest -q -n 16 \
  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 \
  tests/test_awq_scaling_groups.py tests/test_awq_mimo_v2_rotary_selection.py tests/test_mimo_v2_support.py \
  tests/test_mimo_v2_local_port.py tests/module_tree/test_mimo_v2_local_port.py
# skips: test_declared_non_sources_never_become_prev_ops[opt350|muse_glimmer] (pre-existing: not buildable CausalLM families)

# Broad sweep (branch): every tests/module_tree test plus every tests/test_awq*.py
python -P -m pytest -q -n 32 tests/module_tree tests/test_awq*.py
#   4905 passed, 1017 skipped (CUDA/kernel/download gated; CUDA hidden), 2 xfailed, 3 failed:
#   - test_awq_raon1_mini_neutrality.py::test_raon_dropped_generic_folds_are_not_neutral[0-o_proj-...]
#       This negative control pinned the generic planner's stale `input_layernorm -> o_proj` fold, which this
#       PR removes; it is replaced by test_raon_generic_planner_gives_o_proj_no_source
#       (fails on main, passes here; the file now passes, 6 passed).
#   - test_awq_hybrid_recurrent_mask_4671.py::test_awq_batch_size_one_quantizes_a_mamba_hybrid  (also fails on main)
#   - test_awq_weight_mean.py::test_awq_pseudo_quantize_nan_guard_uses_one_host_read        (also fails on main)
#   Not finished: three CPU real-model quantize+generate tests (test_awq_gemv/test_awq_marlin
#   *_quantized_model_loads_and_generates, test_awq_moe TestGroupSize::test_quant_and_inference_0) were still
#   running after 10+ minutes and were stopped. They are unverified here.

bash format/check.sh   # ruff 0.14.2: All checks passed!

Measured Impact

Fold-only (identity quantization) relative change of full-model logits for each emitted group. The model is a tiny random-init MiMoV2ForCausalLM built from the vendored port config: hidden 32, 4 heads, head_dim = v_head_dim = 8, 2 layers (full + SWA), norm gains U(0.5, 1.5), scales linspace(0.5, 1.5).

Variant main worst group branch worst group
split, MHA 1.024e-01 (input_layernorm -> q,k,v,o_proj) 1.489e-07
split, MHA, sink bias on both layer kinds 7.727e-02 1.219e-07
split, GQA (kv=2) 1.046e-01 1.439e-07
fused_qkv, MHA, SWA sink 9.984e-02 (input_layernorm -> qkv_proj,o_proj) 1.427e-07

No GPU A/B was run. The shared-path change is covered by the full fold-plan diff above, which leaves every other definition identical. That diff is structural, so the Qwen2.5 family has an identical plan by construction.

Model Support / Module Plan

Not applicable: no MODEL_MAP entry, alias, tree or definition changes. tests/module_tree/test_registered_module_plans.py passed (included above).

Model test node IDs and results: tests/module_tree/test_mimo_v2_local_port.py 29 passed; tests/test_mimo_v2_local_port.py, tests/test_mimo_v2_support.py and tests/test_awq_mimo_v2_rotary_selection.py passed in the baseline run above.

AWQ onboarding safety

  • Native forward evidence identifies each source, all quantized and non-quantized consumers, intervening operations, and complete replay scope.
  • Real-model neutral-fold and candidate-search tests cover applicable layouts/configs, shared branches/routers, and nontrivial scales; passing module-plan or width checks alone is not AWQ qualification.
  • Native context/chunk parity, failure rollback, and configured fallback/error behavior are tested where applicable; no-group modes still quantize all linears. Not changed: replay, search and fallback are untouched, and o_proj keeps its existing ungrouped plain-quantization path.
  • Each variant is AWQ-scaling qualified, tested plain-quantization-only, or explicitly unsupported/unverified; exact test nodes, skips and limits are recorded.

AWQ evidence and per-variant policy:

  • In MiMoV2Attention.forward (gptqmodel/models/ports/mimo_v2/modeling_mimo_v2.py), o_proj reads attn_output (softmax(QK^T + sinks) V, reshaped). It does not read input_layernorm's output.
  • attention_sink_bias is a per-head logit parameter, not a module.
  • Per variant:
    • split/fused, MHA/GQA, with or without sinks: input_layernorm -> q/k/v (or qkv_proj) is qualified neutral by the new test and the old one.
    • o_proj is plain-quantized with no fold.
    • Dense and MoE MLP groups are unchanged and stay qualified by the existing test_awq_scale_folds_preserve_real_dense_moe_and_swa_layers.

Logic Flow Documentation

  • This change does not alter a major logic flow, or the required diagrams and docs page are included below.

It is a local change to one tracker-update rule. The existing docs/awq-scaling-groups.md flow diagram (linked from README.md) has its tracker node updated, and the page now has a paragraph describing the rule.

Situation Before (base.py:8255 at ef7af83db) After
Owned block, last declaration resolves tracker = that module same
Owned block, last declaration resolves to no module (attention_sink_bias:! parameter, absent v_proj, absent indexer.weights_proj) tracker silently keeps the source from before the block tracker cleared; the next block has no fallback source
Block the layer does not own at all (#4759 sibling branches) tracker kept tracker kept (unchanged)

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.

Notes

  • The same mechanism caused the diffusion_gemma attention_k_eq_v o_proj fold and the dots3_note sliding-layer abort (table above). Both disappear with this change, so this PR is the single planner fix for all three.
  • Added regression tests for the missing-v_proj case (second commit), both failing on origin/main and passing here:
    • tests/module_tree/test_awq_fold_safety_4175.py::test_diffusion_gemma_full_attention_o_proj_is_not_folded_into_the_input_norm: the vendored port built from the pinned parity config (o_proj width == hidden). Every emitted fold is neutral on the layer output and o_proj is in no group. On main the output changes by 1.970e-01.
    • tests/test_awq_ling2_fold_4677.py::test_absent_last_producer_does_not_hand_o_proj_the_input_norm[k-eq-v|with-v-proj]: a generic tree with an optional last v_proj; without it o_proj gets no source, and with it the v_proj -> o_proj fold remains.
    • Fold-only logits on a tiny diffusion_gemma: main 1.44e-1, branch 2.4e-7 for the attention group; worst over all groups 6.5e-7.
    • Earlier suspicion of a routed up->down fold error (3.4e-2) on diffusion_gemma was a probe artifact: production syncs the decoder's expert modules to the encoder's after quantize, and measured in that order every routed fold is <= 3.4e-7.
    • pytest tests/module_tree/test_awq_fold_safety_4175.py tests/test_awq_ling2_fold_4677.py tests/module_tree/test_mimo_v2_local_port.py -> 49 passed.
  • The positional tracker stays a heuristic. The role-derived folds in the AWQ lifecycle sequence (test(awq): pin every definition's effective fold plan and add the role-derived folds alongside #6446) are meant to replace it.

🤖 Generated with Claude Code

Qubitium and others added 2 commits November 29, 2026 21:00
…ration resolves to no module

The generic AWQ planner tracks the previous op in tree order: after each
quantized block it points `last_module` at the block's last declared entry,
and the next block falls back to it when no norm in the pending run feeds
it. `_try_update_last_module` silently kept the old tracker when that entry
resolved to no module.

MiMo-V2 declares `self_attn: (qkv_proj, q_proj, k_proj, v_proj, o_proj:1,
attention_sink_bias:!)`, so the q/k/v block ends with `attention_sink_bias`,
a parameter (or `None`), never a module. The tracker kept `input_layernorm`
from before the block, and `self_attn.o_proj` was folded into the input norm
together with q/k/v whenever `num_attention_heads * v_head_dim ==
hidden_size` (the config defaults; split and fused layouts, MHA and GQA).
Fold-only (identity quant) full-model logits changed by ~1e-1.

When the layer owns the block but its last declaration resolves to no
module, the block's output producer is unknown, so the tracker is now
cleared and the next block gets no fallback source (o_proj is quantized
without a fold). Walking back to an earlier entry is not safe: Hymba's
mixer block ends with unrelated `:!` norms. A block the layer does not own
at all still keeps the tracker, so absent sibling branches (#4759) are
unchanged.

The same rule removes Raon-1 mini's generic `input_layernorm -> o_proj`
fallback (its q/k/v block ends with `dt_bias:!`); the definition already
strips it, so its negative-control test now pins that the generic planner
gives `o_proj` no source.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… absent-producer case

DiffusionGemma's full-attention layers own q_proj/k_proj but no v_proj. On
origin/main the tracker keeps input_layernorm as last_module after the Q/K/V
block, o_proj falls back to it, and the merge emits
input_layernorm -> [q_proj, k_proj, o_proj] (layer output change ~2e-1).

- test_awq_fold_safety_4175: build the vendored port from the pinned parity
  config (o_proj width == hidden, so only data flow separates them), check
  every emitted fold is neutral and o_proj is in no group.
- test_awq_ling2_fold_4677: a generic tree with an optional last v_proj;
  without v_proj o_proj gets no source, with v_proj the v -> o fold remains.

Both fail on origin/main and pass with the planner fix in #6449.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review November 29, 2026 21:29
@Qubitium
Qubitium merged commit ea8c25e into main Nov 29, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/mimo-v2-awq-norm-fold branch November 29, 2026 21:29
Qubitium added a commit that referenced this pull request Nov 29, 2026
…ma/dots3_note/glm5_next fixes (#6452)

#6446 pinned four wrong planner outcomes as `bug:` entries, and its inventory
test requires a listed difference to still differ. #6449 and #6451 fixed them,
so test_awq_fold_inventory.py fails on main (4 planner cases, then 2
canonical cases).

- Regenerate the four fixture entries with
  `awq_fold_inventory.py --write --types dots3_note,mimo_v2,diffusion_gemma,glm5_next`:
  mimo_v2 and diffusion_gemma (k_eq_v layer 1) drop self_attn.o_proj from the
  input_layernorm group, dots3_note sliding layers plan instead of raising,
  glm5_next adds mlp.shared_experts.gate_proj/up_proj as consumers.
- diffusion_gemma/parity and dots3_note/default now match the role-derived
  folds, so their entries are removed.
- mimo_v2 and glm5_next still differ from the derivation for reasons that are
  not bugs (v_proj -> o_proj is not emitted after a `:!` leaf; glm5_next
  linear-attention layers need `v_proj:nosrc`), so they are reclassified.

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