Repository navigation
fix(awq,adapter): honor norm data flow and PEFT pattern rules (#4002) - #4023
Merged
Merged
Conversation
AWQ: - Tag LFM3 sandwich post-norms with a definition-level :postnorm flag and exclude them from prev_op selection, so scales fold into operator_norm / ffn_norm instead of post_operator_norm / post_ffn_norm. - Drop the forward-source gain fallback. When the live probe cannot resolve weight vs 1+weight, scale_ln_fcs raises UnresolvableGainOffset and apply_scale skips the group without mutating weights or features. Adapter: - Match rank_pattern / alpha_pattern with PEFT's exact re.match(r'(.*\.)?(pattern)$', name) rule instead of endswith plus an unanchored search. - Accept positive float alpha_pattern values from JSON exports. Tests: LFM3 folding, probe-failure skip, PEFT dot-boundary parity, and float alpha loading. Docs: docs/awq-norm-selection.md and docs/adapter-pattern-matching.md, linked from README.
Qubitium
added a commit
that referenced
this pull request
Nov 14, 2026
…ule bits (#4002) (#4031) * fix(awq): dispatch dequantization on the saved AWQ layout and per-module bits (#4002) `dequantize_model` decoded every AWQ checkpoint with the GEMM reader. That is exact for GEMM 4-bit and AwqTorch 8-bit, but GPTQModel can also write GEMV (`[out, in/8]` natural order), GEMV_FAST/LLM_AWQ (`pack_intweight` INT16 interleave with fp16 scaled zeros) and per-module `dynamic` widths, so those checkpoints either raised `IndexError`, `NotImplementedError: "rshift_cpu" not implemented for 'Half'` or a size-mismatch `RuntimeError`. Read `format`/`version` from the saved config, resolve `bits`/`group_size` through `dynamic`, and decode each layout with its own inverse transform. The per-module width is no longer taken from the checkpoint-wide `bits`. Fixes item 5 of #4002; items 1-4 landed in #4023. * docs(awq): point the dequantization scatter reference at the real line (#4002) --------- Co-authored-by: GPTQModel Bot <[email protected]>
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 #4002
This PR covers items 1-4 of #4002 only. Item 5 (
utils/model_dequant.pyAWQ GEMV/GEMV_FAST layouts) is intentionally out of scope and is owned by a separateFixes #4002PR.TL;DR
post_operator_norm/post_ffn_normare tagged:postnormand never used asprev_op.scale_ln_fcsraisesUnresolvableGainOffset, andapply_scaleskips the group without mutating weights or captured features.rank_pattern/alpha_patternuse PEFT 0.21.2's exactre.match(r"(.*\.)?(pattern)$", name)rule.alpha_patternaccepts positive real values, including JSON4.0.1. AWQ folded LFM3 scales into sandwich post-norms (item 1)
Root cause
gptqmodel/models/base.py:4407onorigin/main(_select_prev_op) chose the last width-matching gain norm in a non-quantized run. LFM3's tree listsoperator_norm, post_operator_normbeforeconvandffn_norm, post_ffn_normbeforefeed_forward, butpost_*_normruns on the block output after the residual add (use_sandwich_norm: true). Tree position is not forward data flow.Before (measured with the real
LFM3QModel.awq_get_modules_for_scalingon a layer mirroringLfm3DecoderLayer, seed 0):Old flow
flowchart TD A["_select_prev_op(input_width)"] --> B["reverse pending_nonquant"] B --> C{"norm type, 1-D weight,<br/>weight.numel == input width?"} C -- "yes" --> D["prev_op = last matching norm"] C -- "no" --> E["prev_op = last_module"] D --> F["scale_ln_fcs folds into that norm"] style D fill:#f8d7da,stroke:#b02a37,stroke-width:2pxNew flow
flowchart TD A["awq_get_modules_for_scaling"] --> B["post_norm_modules = set(get_post_norm_modules())"] B --> C["iterate full_layer_modules"] C --> D{"block quantized?"} D -- "no" --> E["append block[-1] to pending_nonquant"] E --> C D -- "yes" --> F["_select_prev_op(input_width)"] F --> G["reverse scan pending_nonquant"] G --> H{"_is_gain_norm:<br/>norm type, 1-D weight, width match,<br/>name not in post_norm_modules"} H -- "yes" --> I["prev_op = candidate norm"] H -- "no candidate" --> J{"last_module_name in post_norm_modules?"} J -- "yes" --> K["prev_op = None; skip block"] J -- "no" --> L["prev_op = last_module fallback"] I --> M["generate_node_for_awq_scaling"] L --> M K --> N["continue without folding"] style K fill:#f8d7da,stroke:#b02a37,stroke-width:2pxFix
POST_NORM_FLAG = ":postnorm"atgptqmodel/models/base.py:420, collected byget_post_norm_modulesat:1202(also registered in_MODULE_TREE_RUNTIME_METHODS)._is_gain_normrejects declared post-norms (gptqmodel/models/base.py:4432);_select_prev_opskips them and refuses thelast_modulefallback when that module is a post-norm (:4443).post_operator_normandpost_ffn_normwith:postnorm.After:
The issue reports 0.688 on the real LFM3 weights; our fixture mirrors the same layer and reproduces the wrong
prev_opand the 1e-7-class after state. No LFM3 checkpoint was cached locally.2. Source-text fallback guessed a gain (item 2)
Root cause
gptqmodel/quantization/awq/quantize/scale.py:62-84,97onorigin/mainfell back to grepping the forward source for1.0+self.weightwhen the live probe was ambiguous, then folded as an offset gain. HFMuseSparkTextInputScaledRMSNormscales before normalizing, so its gain is not foldable.Before (real class from transformers 5.22.2):
New flow
flowchart TD A["apply_scale"] --> B["scale_ln_fcs"] B --> C["_norm_gain_offset"] C --> D{"resolve_gain_offset live probe"} D -- "0.0 or 1.0" --> E["fold gain in float32"] D -- "ambiguous or raises" --> F["raise UnresolvableGainOffset"] F --> G["apply_scale logs and skips the group"] G --> H["weights, features, output unchanged"] style F fill:#f8d7da,stroke:#b02a37,stroke-width:2px style G fill:#f8d7da,stroke:#b02a37,stroke-width:2pxFix
_source_gain_offsetand theinspectimport._norm_gain_offset(scale.py:65) raisesUnresolvableGainOffset(scale.py:36) instead of guessing.apply_scale(scale.py:150) logs and skips that group before touching weights orinput_feat_dict.After:
scale_ln_fcsraises,apply_scaleskips, and the norm weight, consumer weight, captured input feature, and block output are bit-identical.3. LoRA patterns matched more broadly than PEFT (item 3)
Root cause
gptqmodel/adapter/adapter.py:26-49onorigin/mainusedendswithwithout a dot boundary and then an unanchoredre.search. PEFT 0.21.2get_pattern_keyusesre.match(rf"(.*\.)?({key})$", name).Measured before (base rank 8, base alpha 8, pattern alpha 64), against real PEFT 0.21.2:
1.self_attn.q_projmodel.layers.1.self_attn.q_proj1.self_attn.q_projmodel.layers.10.self_attn.q_proj1.self_attn.q_projmodel.layers.11.self_attn.q_projmodel.layers.1model.layers.1.self_attn.q_projmodel.layers.1model.layers.10.self_attn.q_projmodel.layers.1model.layers.11.self_attn.q_projup_projmodel.layers.0.mlp.gate_up_projup_projmodel.layers.0.mlp.up_projup_projmodel.layers.0.mlp.down_projAfter the fix every per-module scale matches PEFT exactly (delta 0.0) on CUDA and ROCm.
New flow
flowchart TD A["dynamic_rank_override / dynamic_alpha_override"] --> B["iterate pattern keys in insertion order"] B --> C["_rank_pattern_matches(pattern, weight_key)"] C --> D["pcre.compile with optional dot-prefix group and end anchor"] D -- "match" --> E["first match wins"] D -- "no match" --> F{"legacy export:<br/>pattern or key has uppercase?"} F -- "yes" --> G["retry with pcre.IGNORECASE"] F -- "no" --> H["next pattern"] G -- "match" --> E G -- "no match" --> H H --> B E --> I["lora_scale = alpha / rank<br/>or alpha / sqrt(rank) for rsLoRA"]Fix
_rank_pattern_matches(gptqmodel/adapter/adapter.py:27) now uses PEFT's exact rule. The case-insensitive retry keeps the pattern unchanged and only addspcre.IGNORECASE, preserving\S,\D,\Wsemantics for legacy exports.4. Float alpha crashed the loader (item 4)
Root cause
gptqmodel/adapter/adapter.py:392onorigin/mainassertedisinstance(override, int). JSON has no integer type, so"alpha_pattern": {"...": 4.0}raisedAssertionErrorinpost_init(and was silently skipped underpython -O).Fix
dynamic_alpha_override(adapter.py:380) validatesnumbers.Real > 0, converts withfloat(), and raisesValueErrorfor invalid values instead of relying on an assert. A4.0alpha now loads with scale4.0 / 8 = 0.5, matching PEFT exactly.Validation
Hardware:
GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e(nvidia-smi index 1).nvidia-smi --query-compute-appsshowed the 4090 free before runs; the RTX 5090 was never used.HIP_VISIBLE_DEVICES=1only.Commands and results (run after merging current
origin/main):Fail-before evidence (same tests run against a clean
origin/mainworktree):test_lfm3_sandwich_post_norms_are_not_fold_sourcesseespost_operator_norm/post_ffn_normand fails.test_probe_failure_skips_scale_instead_of_guessing_from_sourcefolds with error 0.113 and mutates weights on main.test_alpha_pattern_matches_peft_dot_boundaryfails withassert 7.0 <= 1e-07for all three patterns.test_float_alpha_pattern_loads_with_peft_scalefails atassert isinstance(override, int).test_rank_pattern_requires_a_dot_boundaryfails on the over-match.Docs
docs/awq-norm-selection.md(linked fromREADME.md).docs/adapter-pattern-matching.md(linked fromREADME.md).docs/awq-scale-folding.mdto match the new selection and fail-closed probe flow.Reviewer notes / limitations
:postnorm; the LFM3 family is tagged here.Lfm3DecoderLayer(use_sandwich_norm=True) because no LFM3 checkpoint is cached. The wrongprev_opand the 1e-7-class after state are reproduced; the 0.688 real-model number is quoted from the issue.up_projrow in the issue mentionsdown_proj; in this fixture the over-match isgate_up_proj(down_projdoes not end inup_proj). The committed tests pin the PEFT-parity result forgate_up_proj,up_proj, anddown_proj.GPTQMODEL_*switches, no dense inference weight caches, no device-family changes. CUDA and ROCm were both exercised.