Skip to content

fix(awq): correct scaling groups across dense and MoE families (#4039) - #4065

Merged
Qubitium merged 4 commits into
mainfrom
fix/issue-4039
Nov 14, 2026
Merged

Qubitium merged 4 commits into
mainfrom
fix/issue-4039

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4039

TL;DR

The AWQ scaling-group builder folded a scale into sources whose output is not the consumer's input. This PR closes the remaining families from the #4030/#4035 follow-up, and makes the invariant test cover every dense and MoE family that builds in transformers 5.22 (91 passed). Net: 23 files, +717/−200, concentrated in gptqmodel/models/base.py (group selection/merge) and one-line tree declarations.

1. Root causes (file:line)

Five distinct mechanisms produced the measured damage:

# Mechanism Where
1 A norm consumed by the residual is used as a fold source exaone4.py:82, gemma4.py:45, afmoe.py:38,53 — missing :postnorm
2 One norm folded as two groups (routed + shared experts) gptqmodel/models/base.py:5327 (_merge_shared_source_nodes, new)
3 Parallel blocks folded the second branch into the first branch's output projection base.py:_select_prev_op shared-source path (:4900) + :in=<tag> in rw.py:44, falcon_h1.py:26
4 fc1 → fc2 across a non-homogeneous activation gpt_neox.py:25, gpt_neo.py:27, gpt_bigcode.py:21, bloom.py:21, rw.py:22, gpt_oss.py:30, flint.py — missing :nosrc
5 Gated attention: gate_proj becomes the block's last module and is folded into o_proj afmoe.py:35, muse_glimmer.py:49, hrm_text.py:30, minimax_m2.py:98 — missing :nosrc
6 Crashes: weightless / gated norms, Conv1D, HyperConnection, Cohere2LayerNorm quantization/awq/quantize/scale.py:79 (is_foldable_source, new) + allowed_norms (:57)

Additional tree corrections: olmoe.py q/k norms moved into the q/k/v block with a group index, laguna's g_proj collected as a scale consumer (base.py:_norm_scale_consumers now reads the block), cohere2_moe's undeclared shared_experts declared, falcon split into FalconQModel (Falcon names its norms input_layernorm, RefinedWeb uses ln_1/ln_2), deepseek_v4 o_b_proj/q_b_proj marked :nosrc.

2. New flow

flowchart TD
    A["awq_get_modules_for_scaling"] --> B{"non-quantized block?"}
    B -- "yes" --> C["track last_gain_norm<br/>and last_norm_candidate"]
    B -- "no" --> D["_select_prev_op<br/>pending norm / block norm / shared in= source"]
    D --> E["generate_node_for_awq_scaling"]
    C --> E
    E --> F["_merge_shared_source_nodes<br/>one group per gain norm"]
    F --> G["_filter_awq_scaling_sources<br/>nosrc / width"]
    G --> H["AWQProcessor._quantize_layer"]
    H --> I{"apply_scale: is_foldable_source?"}
    I -- "yes" --> J["divide norm once,<br/>scale every consumer"]
    I -- "no" --> K["skip group<br/>layer quantized plainly"]
    style G fill:#fff3cd,stroke:#997404,stroke-width:2px
    style K fill:#fff3cd,stroke:#997404,stroke-width:2px
Loading

_merge_shared_source_nodes is the core fix: a gain norm may be divided exactly once, so it may have exactly one scaling group. Routed experts, shared_experts, and parallel branches are merged into that group and share one scale vector.

3. Before / after (measured relative logit change)

Tiny 2-3 layer models, random scale in [0.5, 1.5] applied with the library's own apply_scale, full-model logits compared.

family before after
exaone4_5 2.96e-01 1.65e-07
gemma4_text 3.72e-01 1.84e-06
afmoe 3.51e-01 5.32e-07
cohere 1.39e-01 1.52e-07
falcon 1.96e-01 2.08e-07
falcon_h1 1.12e-01 1.72e-07
minimax 2.89e-01 no foldable group (plain quantization)
minimax_m2 4.18e-07 3.22e-07
olmoe 1.60e-01 1.86e-07
laguna 3.02e-02 3.21e-07
solar_open 3.21e-02 2.96e-07
hy_v3 1.87e-02 2.15e-07
glm_moe_dsa 1.11e-02 4.79e-07
gpt_neox 1.85e-02 1.80e-07
gpt_neo 2.10e-02 1.65e-07
gpt_bigcode 1.24e-02 1.60e-07
bloom 8.63e-04 6.29e-08
gpt_oss 2.47e-07 2.47e-07
hrm_text crash (AttributeError) 3.07e-08
olmo crash (OlmoLayerNorm no weight) 1.78e-07
cohere2 crash (Cohere2LayerNorm not in allowed_norms) 1.59e-07
cohere2_moe crash + 1.03e-02 3.71e-07
gpt2 crash (Conv1D) no foldable group
axk2 crash (AXK2GatedRMSNorm) one merged routed+shared group, not foldable
deepseek_v4 crash (HyperConnection) one merged routed+shared group
flint MoE up_proj → down_proj fold 3.51e-07

4. Failure / ambiguity contract

Situation Before After
prev_op is a weightless or gated norm / Conv1D / HyperConnection NotImplementedError / AttributeError group skipped by is_foldable_source; layer quantized plainly
MoE gate/up block whose block norm cannot be folded fell back to the last expert linear last_norm_candidate returns the block norm, group is dropped
Two groups share one norm norm divided twice, one branch unscaled merged into one group

5. Tests

tests/test_awq_scaling_groups.py now resolves every family from MODEL_MAP, iterates all decoder layers, passes each group's _scale_consumers as consumer_map exactly as awq_processor.py:1658-1688 does, and requires < 1e-5 relative change. It covers 45 dense + MoE families, a routed/shared-merge assertion for 10 MoE families, a non-source assertion for 20 families, and static :nosrc/:postnorm assertions for definitions that have no AutoModelForCausalLM entry point (exaone4_5, muse_glimmer).

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/test_awq_scaling_groups.py -q
-> 91 passed, 2 skipped in 69.60s

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_awq_empty_groups.py tests/test_awq_scale_folding.py \
  tests/test_awq_mla_scaling.py tests/test_awq_moe_router_scaling.py \
  tests/test_exaone4_5_support.py tests/module_tree/test_registered_module_plans.py -q
-> 448 passed in 21.75s

ruff check --no-cache gptqmodel docs/eora tests setup.py
-> All checks passed!

6. Docs

docs/awq-scaling-groups.md is updated with the one-group-per-norm contract, the parallel-branch :in= rule, the foldability contract, the per-definition non-source table and a Mermaid flow (already linked from README.md:105).

7. Hardware

CPU-only tiny models (AutoModelForCausalLM.from_config), no GPU kernels exercised. Assigned pair 4090 GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42 / R9700 HIP_VISIBLE_DEVICES=4; the RTX 5090 was not touched.

8. Notes

  • deepseek_v3/deepseek_v32 cannot build a tiny config in transformers 5.22 (RuntimeError: shape '[-1, 8, 0]'), so they are covered structurally by the merge assertion through deepseek_v2/deepseek_v4.
  • minimax (original) intentionally has no foldable group: its layer feeds the normalized value into the residual, so every norm is a non-source and the layer is quantized plainly.
  • tests/test_awq_weight_mean.py fails to collect on this venv (missing parameterized), pre-existing and unrelated.

@Qubitium
Qubitium merged commit e193e55 into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4039 branch November 14, 2026 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant