Repository navigation
fix(awq): build scaling groups for norm-less trees and skip non-sources (#4005) - #4030
Merged
Merged
Conversation
…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
# Conflicts: # README.md
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.
This was referenced Nov 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 Starcoder2stop 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_scalingand the AWQ processor:continuewhen a block had noprev_op, so a tree whose first block isquantized left
last_moduleunset for the whole layer. The processor thentook the empty-group path, called
_finalize_quantized_layerwithoutquantizing, and
pack_modulelater popped a missingq_zeros.Phi3/Apertus/CodeGen/MPT all hit this (
AWQMAP ---→QUANT_EXC KeyError 'q_zeros').prev_opcame 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:2pxMeasured on tiny models with the library's own
apply_scaleand a random scalein
[0.5, 2](relative decoder-layer logit change; Llama is 1.5e-7):post_attention_layernorm → gate/uppost_attention_layernorm → gate/uppost_attention_layernorm → gate/upfinal_layer_norm → q/k/v,out_proj → fc1self_attn_layer_norm → fc1input_layernorm → q,q → k,k → v,dense → fc1,fc1 → fc2v → dense), worst 1.6e-7ln_1 → qkv,out_proj → fc_in,fc_in → fc_outv → out_proj), worst 1.8e-7c_fc → c_projKeyError: 'q_zeros'KeyError: 'q_zeros'KeyError: 'q_zeros'KeyError: 'q_zeros'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:2px3. New core: declared data-flow non-sources
prev_opselection still happens in_select_prev_op(the:postnormworkmerged for #4002 is reused), but the result is validated against the definition's
declared forward data flow:
:postnorm(already merged) marks OLMo2/OLMo3/Exaone4post_attention_layernorm.:nosrc(AWQ_NO_SCALE_SOURCE_FLAG,gptqmodel/models/base.py:413) marks aleaf whose output is not the direct input of the following consumer:
gptqmodel/models/base.py:4624drops its group after selection.olmo2,olmo3,exaone4post_attention_layernorm:!:postnormoptpost-norm treeself_attn_layer_norm:!:nosrcphiinput_layernorm:!:nosrc,self_attn.dense:1:nosrc,mlp.fc1:0:nosrcgptjln_1:!:nosrc,attn.out_proj:1:nosrc,mlp.fc_in:0:nosrcstarcoder2mlp.c_fc:0:nosrcapertusmlp.up_proj:0:nosrccodegenln_1:!:nosrc,attn.out_proj:1:nosrc,mlp.fc_in:0:nosrcmptffn.up_proj:0:nosrcTree fixes:
input_layernorm/post_attention_layernorm.("attention_layernorm:!")was a string, not a 1-tuple, so bothnorms were dropped by the tree compiler.
ln_1(there is no post-attention norm; attention and MLP areparallel).
norm_1/norm_2.self_attn_layer_normbefore attention andfinal_layer_normbeforefc1; a_resolve_effective_module_treeoverrideselects a post-norm tree for
do_layer_norm_before=False(OPT-350m).q_norm/k_normwere the last entries of the QKV block, so thetracker made
k_normthe nextprev_op; they now follow their projections.post_attention_layernormand put q/k/v in onesubset.
4. Failure and ambiguity contract
prev_opis a declared non-sourceprev_opgain width ≠ consumer inputapply_scaleshape errorKeyError: 'q_zeros'_quantize_layer_fallbackruns plain quantizationq_zerosstill missing at packprint("modddd", ...)+KeyErrorRuntimeErrornaming the module5. Change inventory
gptqmodel/models/base.py_filter_awq_scaling_sources,nosrcflaggptqmodel/looper/awq_processor.pydefinitions/{apertus,codegen,exaone4,gptj,mpt,olmo2,olmo3,opt,phi,phi3,starcoder2}.pynosrc/postnormtests/module_tree/fixtures/registered_module_plans.jsontests/test_awq_scaling_groups.py,tests/test_awq_empty_groups.pydocs/awq-scaling-groups.md,docs/module-plan-guide.md,README.md6. Tests
test_scaling_groups_preserve_decoder_layer_output[13 families]test_families_without_declared_norms_still_produce_a_scaling_map[phi3,apertus,codegen,mpt]test_declared_non_sources_never_become_prev_ops[9 cases]test_no_module_configuration_falls_back_to_plain_quantizationapply_quant(..., scales_list=[]), layer marked quantizedtest_no_surviving_group_falls_back_to_plain_quantizationtests/module_tree/test_registered_module_plans.py7. Validation
Fail-before (source reverted, tests kept):
Pass-after, CUDA venv (
/opt/venvs/cu130t/bin/python, Py3.14t, torch 2.16.0.dev+cu130):Pass-after, ROCm venv (
/opt/venvs/rocm101t/bin/python, torch 2.16.0.dev+rocm10.1):End-to-end tiny-model AWQ runs (CPU,
device="cpu",apply_clip=Falsebecausethe tiny shapes are below the clip tile):
QUANT_EXC KeyError 'q_zeros'QUANT_OK FORMAT.GEMMQUANT_EXC KeyError 'q_zeros'QUANT_OK FORMAT.GEMMQUANT_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-appswas checked before the GPU-adjacent runs; notiming was measured, so the shared 4090 does not affect any number here.
8. Reviewer notes
nosrcis a static declaration of the definition's forward graph; theapply_scaleinvariant test pins it for every family listed above. A newfamily with a residual, parallel branch, or non-homogeneous activation
between a producer and its consumer must declare it — see
docs/awq-scaling-groups.md.
config.do_layer_norm_beforethrough_resolve_effective_module_tree; pre-norm checkpoints keep the originalorder with the two norms moved to their forward positions.
QKV, GELU MLP), so its layers use the plain fallback rather than a wrong fold.
:postnormwork) and MoE expert up→down groups (unchanged).