Skip to content

fix(awq): build scaling groups for norm-less trees and skip non-sources (#4005) - #4030

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-4005
Nov 14, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-4005

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4005

TL;DR

AWQ now (a) never finalizes a layer without quantizing it when no scaling group
can be built, and (b) refuses to fold a scale into a module whose output is not
the consumer's input. Phi3, Apertus, CodeGen and MPT build a non-empty scaling
map (or fall back to plain quantization) instead of crashing at pack time with
KeyError: 'q_zeros', and OLMo2/OLMo3/Exaone4, OPT, Phi, GPT-J and Starcoder2
stop building groups that change the decoder layer output by 2e-2 to 4e-1.
+735/−83 across 19 files.

1. The problem

Two independent failures in awq_get_modules_for_scaling and the AWQ processor:

  1. No scaling groups at all. The tracker update was skipped by
    continue when a block had no prev_op, so a tree whose first block is
    quantized left last_module unset for the whole layer. The processor then
    took the empty-group path, called _finalize_quantized_layer without
    quantizing, and pack_module later popped a missing q_zeros.
    Phi3/Apertus/CodeGen/MPT all hit this (AWQMAP --- → QUANT_EXC KeyError 'q_zeros').
  2. Groups that are not output-preserving. prev_op came from tree order
    (last width-matching gain norm, else the previous linear). Nothing checked
    that the candidate's output really is the consumer's input, so groups that
    cross a residual add, a parallel branch, or a GELU/SiLU/xIELU activation were
    folded anyway.
flowchart TD
    A["awq_get_modules_for_scaling"] --> B{"block quantized?"}
    B -- "no" --> C["pending_nonquant + last_module"]
    B -- "yes" --> D{"prev_op is None?"}
    D -- "yes" --> E["continue<br/>tracker NOT updated"]
    E --> F["next block also has no prev_op"]
    F --> G["empty scaling map"]
    D -- "no" --> H["fold into tree-order prev_op<br/>no data-flow check"]
    G --> I["_finalize_quantized_layer<br/>without quantizing"]
    I --> J["pack_module: KeyError 'q_zeros'"]
    H --> K["silent layer-output change"]
    style E fill:#f8d7da,stroke:#b02a37,stroke-width:2px
    style J fill:#f8d7da,stroke:#b02a37,stroke-width:2px
    style K fill:#f8d7da,stroke:#b02a37,stroke-width:2px
Loading

Measured on tiny models with the library's own apply_scale and a random scale
in [0.5, 2] (relative decoder-layer logit change; Llama is 1.5e-7):

Family Invalid group before Before After
OLMo2 post_attention_layernorm → gate/up 4.28e-1 group gone, 2 valid groups, worst 2.7e-7
OLMo3 post_attention_layernorm → gate/up 4.27e-1 group gone, 2 valid groups, worst 2.6e-7
Exaone4 post_attention_layernorm → gate/up 4.28e-1 group gone, 2 valid groups, worst 2.5e-7
OPT (pre-norm) final_layer_norm → q/k/v, out_proj → fc1 2.81e-1, 2.18e-1 tree fixed, 4 valid groups, worst 3.4e-7
OPT-350m (post-norm) self_attn_layer_norm → fc1 2.73e-1 group gone, 2 valid groups, worst 1.2e-7
Phi input_layernorm → q, q → k, k → v, dense → fc1, fc1 → fc2 1.65e-1, 1.2e-3, 6.6e-2, 1.66e-1, 1.6e-2 1 valid group (v → dense), worst 1.6e-7
GPT-J ln_1 → qkv, out_proj → fc_in, fc_in → fc_out 1.84e-1, 1.66e-1, 2.1e-2 1 valid group (v → out_proj), worst 1.8e-7
Starcoder2 c_fc → c_proj 2.1e-2 group gone, 3 valid groups, worst 1.8e-7
Apertus — (no groups built) KeyError: 'q_zeros' 3 valid groups, worst 2.1e-7
Phi3 — (no groups built) KeyError: 'q_zeros' 1 valid group, worst 1.3e-7
CodeGen — (no groups built) KeyError: 'q_zeros' no output-preserving group exists; layer falls back to plain quantization
MPT — (no groups built) KeyError: 'q_zeros' 2 valid groups, worst 2.2e-7

2. The new flow

flowchart TD
    A["awq_get_modules_for_scaling"] --> B{"block quantized?"}
    B -- "no" --> C["pending_nonquant + last_module"]
    B -- "yes" --> D["resolve inp and _select_prev_op"]
    D --> E{"prev_op found?"}
    E -- "no" --> F["skip_block = True"]
    E -- "yes" --> G["generate_node_for_awq_scaling"]
    F --> H["tracker update to block[-1]<br/>always runs"]
    G --> H
    H --> I["_filter_awq_scaling_sources"]
    I --> J{"prev_op flagged nosrc<br/>or gain width mismatch?"}
    J -- "yes" --> K["drop group"]
    J -- "no" --> L["keep group"]
    K --> M["AWQProcessor._quantize_layer"]
    L --> M
    M --> N{"any group left?"}
    N -- "no" --> O["_quantize_layer_fallback<br/>apply_quant(scales_list=[])"]
    N -- "yes" --> P["scale search + apply_scale"]
    style K fill:#fff3cd,stroke:#997404,stroke-width:2px
    style O fill:#d1e7dd,stroke:#146c43,stroke-width:2px
Loading

3. New core: declared data-flow non-sources

prev_op selection still happens in _select_prev_op (the :postnorm work
merged for #4002 is reused), but the result is validated against the definition's
declared forward data flow:

  • :postnorm (already merged) marks OLMo2/OLMo3/Exaone4 post_attention_layernorm.
  • :nosrc (AWQ_NO_SCALE_SOURCE_FLAG, gptqmodel/models/base.py:413) marks a
    leaf whose output is not the direct input of the following consumer:
    gptqmodel/models/base.py:4624 drops its group after selection.
Definition Declared non-sources
olmo2, olmo3, exaone4 post_attention_layernorm:!:postnorm
opt post-norm tree self_attn_layer_norm:!:nosrc
phi input_layernorm:!:nosrc, self_attn.dense:1:nosrc, mlp.fc1:0:nosrc
gptj ln_1:!:nosrc, attn.out_proj:1:nosrc, mlp.fc_in:0:nosrc
starcoder2 mlp.c_fc:0:nosrc
apertus mlp.up_proj:0:nosrc
codegen ln_1:!:nosrc, attn.out_proj:1:nosrc, mlp.fc_in:0:nosrc
mpt ffn.up_proj:0:nosrc

Tree fixes:

  • Phi3: add input_layernorm / post_attention_layernorm.
  • Apertus: ("attention_layernorm:!") was a string, not a 1-tuple, so both
    norms were dropped by the tree compiler.
  • CodeGen: add ln_1 (there is no post-attention norm; attention and MLP are
    parallel).
  • MPT: add norm_1 / norm_2.
  • OPT: pre-norm tree now has self_attn_layer_norm before attention and
    final_layer_norm before fc1; a _resolve_effective_module_tree override
    selects a post-norm tree for do_layer_norm_before=False (OPT-350m).
  • Exaone4: q_norm/k_norm were the last entries of the QKV block, so the
    tracker made k_norm the next prev_op; they now follow their projections.
  • Phi: drop the non-existent post_attention_layernorm and put q/k/v in one
    subset.

4. Failure and ambiguity contract

Situation Before After
first block quantized, no norm in the tree every block skipped → empty map → pack crash first block skipped, later blocks use the previous linear; invalid ones are filtered
prev_op is a declared non-source folded, silent accuracy loss group dropped, consumer quantized plainly
prev_op gain width ≠ consumer input apply_scale shape error group dropped
no group survives layer finalized unquantized → KeyError: 'q_zeros' _quantize_layer_fallback runs plain quantization
q_zeros still missing at pack debug print("modddd", ...) + KeyError explicit RuntimeError naming the module

5. Change inventory

Area File ± Purpose
tracker + validation gptqmodel/models/base.py +152/−? tracker update on every block, _filter_awq_scaling_sources, nosrc flag
processor gptqmodel/looper/awq_processor.py +21/−? fallback on both empty-group paths, remove debug print
definitions definitions/{apertus,codegen,exaone4,gptj,mpt,olmo2,olmo3,opt,phi,phi3,starcoder2}.py +? norms, tuples, subset order, nosrc/postnorm
inventory tests/module_tree/fixtures/registered_module_plans.json +13/−6 8 plan cases updated
tests tests/test_awq_scaling_groups.py, tests/test_awq_empty_groups.py +355 fail-before/pass-after
docs docs/awq-scaling-groups.md, docs/module-plan-guide.md, README.md +139 flow, contracts, flag table, link

6. Tests

Test Invariant
test_scaling_groups_preserve_decoder_layer_output[13 families] every foldable group leaves the decoder-layer logits unchanged (< 1e-5; measured 1.2e-7 – 3.4e-7)
test_families_without_declared_norms_still_produce_a_scaling_map[phi3,apertus,codegen,mpt] the issue's empty-AWQMAP repro now builds a map
test_declared_non_sources_never_become_prev_ops[9 cases] no fold into a declared non-source
test_no_module_configuration_falls_back_to_plain_quantization empty map → apply_quant(..., scales_list=[]), layer marked quantized
test_no_surviving_group_falls_back_to_plain_quantization all groups filtered → same fallback
tests/module_tree/test_registered_module_plans.py the compiled plan matches the updated inventory

7. Validation

Fail-before (source reverted, tests kept):

pytest tests/test_awq_scaling_groups.py -q      -> 21 failed, 5 passed
pytest tests/test_awq_empty_groups.py -q        -> 1 failed, 1 passed

Pass-after, CUDA venv (/opt/venvs/cu130t/bin/python, Py3.14t, torch 2.16.0.dev+cu130):

pytest tests/test_awq_scaling_groups.py tests/test_awq_empty_groups.py \
       tests/test_awq_scale_folding.py tests/module_tree/test_registered_module_plans.py -q
-> 462 passed
ruff check gptqmodel docs/eora tests setup.py -> All checks passed!

Pass-after, ROCm venv (/opt/venvs/rocm101t/bin/python, torch 2.16.0.dev+rocm10.1):

pytest tests/test_awq_scaling_groups.py tests/test_awq_empty_groups.py \
       tests/module_tree/test_registered_module_plans.py -q
-> 445 passed

End-to-end tiny-model AWQ runs (CPU, device="cpu", apply_clip=False because
the tiny shapes are below the clip tile):

Model Before After
tiny Phi3 QUANT_EXC KeyError 'q_zeros' QUANT_OK FORMAT.GEMM
tiny Apertus QUANT_EXC KeyError 'q_zeros' QUANT_OK FORMAT.GEMM
tiny CodeGen QUANT_EXC KeyError 'q_zeros' QUANT_OK FORMAT.GEMM (plain fallback)

Hardware: the numerical tests and the end-to-end runs are CPU-only. GPU
nvidia-smi --query-compute-apps was checked before the GPU-adjacent runs; no
timing was measured, so the shared 4090 does not affect any number here.

8. Reviewer notes

  • nosrc is a static declaration of the definition's forward graph; the
    apply_scale invariant test pins it for every family listed above. A new
    family with a residual, parallel branch, or non-homogeneous activation
    between a producer and its consumer must declare it — see
    docs/awq-scaling-groups.md.
  • The post-norm OPT tree is selected from config.do_layer_norm_before through
    _resolve_effective_module_tree; pre-norm checkpoints keep the original
    order with the two norms moved to their forward positions.
  • CodeGen legitimately has no output-preserving group (parallel block, fused
    QKV, GELU MLP), so its layers use the plain fallback rather than a wrong fold.
  • Not in scope: LFM3 sandwich norms (already handled by the merged :postnorm
    work) and MoE expert up→down groups (unchanged).

…es (#4005)

- update the scaling tracker even when a block has no prev_op, so a layer
  whose first block is quantized (Phi3/Apertus/CodeGen/MPT) is not skipped
  entirely
- fall back to plain quantization on both empty-group paths instead of
  finalizing the layer unquantized (which crashed in pack with KeyError
  'q_zeros'), and replace the leftover debug print with an explicit error
- add the missing norms to Phi3, CodeGen and MPT, and use 1-tuples for the
  Apertus norms
- declare forward-data-flow non-sources with the new 'nosrc' tree flag and
  drop their groups (OLMo2/OLMo3/Exaone4 post-norms, OPT post-norm, Phi,
  GPT-J, Starcoder2, Apertus, CodeGen, MPT)
- fix the OPT pre/post-norm trees and Exaone4's q_norm/k_norm order
Reuse the data-flow metadata merged for #4002 (POST_NORM_FLAG) for
OLMo2/OLMo3/Exaone4 instead of a second declaration, and keep :nosrc for
parallel branches, residual crossings, non-homogeneous activations, and
post-norm OPT's residual norm.
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