Repository navigation
fix(awq): resolve multimodal rotary and stop folding across residual/nonlinearity (#4121) - #4135
Merged
Merged
Conversation
…nonlinearity (#4121) Items 6-10 of #4121. - awq_processor: `_get_root_rotary` now resolves the declared path, the text fast path (`model.rotary_emb` / `model.rotary`), then scans `named_modules` for a decoder rotary embedding, skipping vision/audio towers and numbered per-layer rotaries. Qwen2-VL / Qwen2.5-VL / Gemma 3 / Mistral 3 keep it on `model.language_model.rotary_emb`; without it the replay dropped the captured `position_embeddings` and the attention unpacked None (TypeError: cannot unpack non-iterable NoneType object). - pangu_alpha: `c_fc:0:nosrc` (GELU between c_fc and c_proj) and drop the top-query block's `attn.q_proj` from the `ln_1` group, because that block's q_proj reads `custom_query = wqe(position_ids)`. - base: new `awq_post_norm_modules(layer)` per-layer hook; `olmo_hybrid` marks `post_attention_layernorm` as a post-norm only on full-attention layers, where its output is added to the residual. - gemma4 / gemma4_unified: copy the text tree's `v_proj:0:...:nosrc` and `post_feedforward_layernorm:!:postnorm`; the composite tree also marks `post_per_layer_input_norm` as a post-norm. - nemotron_h: `up_proj:3:nosrc` (relu2 sits between up_proj and down_proj). Adds gpt_pangu, olmo_hybrid, gemma4, gemma4_unified and nemotron_h to FAMILY_MODEL_TYPES so the fold-error harness covers the whole class, plus a tiny Qwen2-VL builder and tests/test_awq_vl_rotary_resolution.py.
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.
Part of #4121 (items 6-10)
TL;DR
AWQ could not find the decoder rotary inside a multimodal wrapper, so #4109's
stale-
position_embeddingsdrop left the attention unpackingNone(
Qwen2-VLcrash). Four more definition trees folded a scale across a residualor a non-homogeneous activation. The durable part:
gpt_pangu,olmo_hybrid,gemma4,gemma4_unifiedandnemotron_hare now inFAMILY_MODEL_TYPES, sothe fold-error harness catches this whole class instead of one issue at a time.
Net: 12 files, +691/-20.
1. The problem
flowchart TD A["AWQProcessor._get_root_rotary"] --> B{"model.rotary_emb?"} B -- "no, VL wrapper" --> C["returns None"] C --> D["_prepare_module_forward drops<br/>stale position_embeddings"] D --> E["Qwen2VLAttention: cos, sin = position_embeddings"] E --> F["TypeError: cannot unpack non-iterable NoneType"] G["definition.module_tree"] --> H["awq_get_modules_for_scaling"] H --> I{"prev_op really feeds<br/>the consumer?"} I -- "no: GELU, relu2,<br/>post-norm, parallel branch" --> J["fold changes the layer output"] I -- "yes" --> K["apply_scale"] style C fill:#f8d7da,stroke:#842029,stroke-width:2px style F fill:#f8d7da,stroke:#842029,stroke-width:2px style J fill:#f8d7da,stroke:#842029,stroke-width:2pxfile:line(parent13ee428f2)model.rotary_embgptqmodel/looper/awq_processor.py:324-330mlp: ("c_fc:0", "c_proj:1")folds across GELU; the last block'sln_1group containsattn.q_proj, which readscustom_querygptqmodel/models/definitions/pangu_alpha.py:20-30post_attention_layernormtreated as a gain source on the full-attention layer, where its output is added to the residualgptqmodel/models/definitions/olmo_hybrid.py:56v_proj:0andpost_feedforward_layernorm:!while the text tree already had:nosrc/:postnormgptqmodel/models/definitions/gemma4.py:263,268-271,gemma4_unified.py:71mixer.up_projfeedsdown_projacrossrelu(x)^2gptqmodel/models/definitions/nemotron_h.py:282. The new flow
flowchart TD A["AWQProcessor._get_root_rotary"] --> B{"declared rotary_embedding?"} B -- "yes" --> C["get_module_by_name_prefix"] B -- "no" --> D{"model.rotary_emb / model.rotary?"} D -- "no" --> E["_scan_root_rotary over named_modules"] E --> F["skip vision/audio towers<br/>and numbered per-layer rotaries"] F --> G["shallowest rotary embedding wins"] G --> H["cached on the processor"] C --> H D --> H H --> I["_prepare_module_forward rebuilds<br/>position_ids and cos/sin for seq_len"] J["awq_get_modules_for_scaling"] --> K["awq_post_norm_modules(layer)"] K --> L["static :postnorm set<br/>plus per-layer additions"] L --> M{"prev_op is a post-norm<br/>or a :nosrc leaf?"} M -- "yes" --> N["group dropped:<br/>consumer quantized plainly"] M -- "no" --> O["apply_scale"] style N fill:#d1e7dd,stroke:#0f5132,stroke-width:2px style I fill:#d1e7dd,stroke:#0f5132,stroke-width:2px3. Per-subsystem detail
3.1 Item 6 — multimodal rotary resolution
_get_root_rotarynow resolves, in order: the declaredrotary_embedding, thetext-model fast path (
model.rotary_emb/model.rotary), then a scan ofnamed_modules(). The scan skipsvisual,vision_tower,audio_tower,image_encoder, ...). On Qwen2-VLmodel.visual.rotary_pos_embis the shallowest name match and has the vision head width.
layers.0.self_attn.rotary_embonNanbeige/DeciLM). Those are per-layer, old-style
(x, seq_len)modules that_prepare_module_forward's module-local fallback already handles; returningthem as "root" would break the mask-refresh path that calls
rotary(x, position_ids).The result is cached, so the scan runs once per processor.
Real Qwen2-VL-2B-Instruct, replaying one attention block with a 48-token capture
against a 20-token activation:
13ee428f2_get_root_rotary()NoneQwen2VLRotaryEmbeddingposition_embeddingsafter_prepare_module_forward(1, 20, 128)TypeError: cannot unpack non-iterable NoneType object(modeling_qwen2_vl.py:555)(1, 20, 1536)Tiny Qwen2-VL (
build_tiny_qwen2_vl), fullGPTQModel.load+quantize()withvariable-length 48/20 calibration:
TypeError: cannot unpack non-iterable NoneType objectAWQ QUANTIZE OK, q_proj -> AwqTorchLinearGPTQ QUANTIZE OKGPTQ QUANTIZE OKThe tiny repro shows the trigger: the attention replay received
x=(1, 48, 128)withposition_embeddingscaptured from the 20-token sample.3.2 Item 7 — PanGu-alpha
mlpbecomes("c_fc:0:nosrc", "c_proj:1")becauseGPTPanguMLP.forwardisc_proj(gelu(c_fc(x))).PanguAlphaQModel.awq_get_modules_for_scalingalsodrops
attn.q_projfrom the top-query block'sln_1group: that block'sq_projreads
custom_query = wqe(position_ids), so scaling it with theln_1groupmultiplies q by the scale while its input is not divided.
3.3 Item 8 — OLMo Hybrid
get_post_norm_modulesis a classmethod, so it cannot describe a model whoselayers alternate shapes. New
BaseQModel.awq_post_norm_modules(layer)is theper-layer extension point, and
awq_get_modules_for_scalinguses it.OlmoHybridQModeladdspost_attention_layernormonly when the layer has aself_attn:The linear-attention layer feeds the same-named norm straight into the MLP, so
its fold stays valid.
3.4 Item 9 — composite and unified Gemma 4
The text tree already declared
v_proj:0:in=x:nosrcandpost_feedforward_layernorm:!:postnorm; the composite (gemma4) and unified(
gemma4_unified) trees had not copied them. The composite tree additionallymarks
post_per_layer_input_normas:postnorm:Gemma4's decoder applies itto
per_layer_projection's output and adds that to a residual, so it cannotabsorb the projection's input scale either.
3.5 Item 10 — NemotronH
NemotronHMLP.forwardisdown_proj(act_fn(up_proj(x)))withrelu2, i.e. thenonlinearity sits directly between
up_projanddown_proj, soup_proj:3:nosrc.4. Failure and ambiguity contract
None-> captured cos/sin dropped -> attention unpacksNone(x, seq_len)rotaryNone:nosrcdrops the grouptest_declared_non_sources_never_become_prev_opsfails5. Change inventory
gptqmodel/looper/awq_processor.pygptqmodel/models/base.pyawq_post_norm_modules(layer)hookdefinitions/pangu_alpha.py,olmo_hybrid.py,gemma4.py,gemma4_unified.py,nemotron_h.py:nosrc/:postnormdeclarations, top-queryq_projexclusiontests/test_awq_scaling_groups.pyFAMILY_MODEL_TYPES, tiny configs for composite/remote configs, structural non-source pinstests/test_awq_vl_rotary_resolution.py(new),tests/tiny_remote_models.pydocs/awq-scaling-groups.md,README.md6. Tests / guarantees
test_awq_scaling_groups.py::test_scaling_groups_preserve_decoder_layer_output(gpt_pangu,olmo_hybrid,gemma4,gemma4_unified,nemotron_h)1e-5relativetest_awq_scaling_groups.py::test_declared_non_sources_never_become_prev_opsc_fc,v_proj,post_feedforward_layernorm,post_per_layer_input_norm,mixer.up_projnever becomeprev_optest_awq_vl_rotary_resolution.py7. Validation
Environment:
/opt/venvs/cu130t/bin/python(Python 3.14t, torch2.16.0.dev+cu130, transformers 5.22.2),
PYTHONPATH=$PWD.Hardware: RTX 4090
GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7efor the real-modelruns; CPU (
CUDA_VISIBLE_DEVICES="") for the tiny-model tests; ROCm(
/opt/venvs/rocm101t/bin/python,HIP_VISIBLE_DEVICES=1) for the unit tests.Per-group fold error (tiny configs;
apply_scalewith a random scale in [0.5, 1.5], decoder-layer output)gpt_pangumlp.c_fc -> mlp.c_proj(GELU)gpt_panguln_1 -> [q,k,v]on the top-query layerln_1 -> [k,v]at 1.19e-08;q_projquantized plainlyolmo_hybridpost_attention_layernorm -> gate/upon the attention layerolmo_hybridgemma4post_feedforward_layernorm -> per_layer_input_gategemma4v_proj -> o_projgemma4post_per_layer_input_norm -> per_layer_projectiongemma4_unifiedv_proj -> o_projnemotron_hmixer.up_proj -> down_proj(relu2)Worst surviving group per family after the fix:
gpt_pangu1.7e-07,olmo_hybrid2.0e-07,gemma42.5e-07,gemma4_unified2.9e-07,nemotron_h1.4e-07.Durable harness
Parent commit
13ee428f2with only the new test file:This branch:
tests/test_awq_vl_rotary_resolution.pyon the parent commit: 3 of the 6 unittests fail (
_get_root_rotaryreturnsNone, and the replay drops cos/sin).8. Reviewer notes
(module-local fallback), while a wrong pick would rebuild cos/sin with another
tower's width. Vision/audio segments and numbered per-layer modules are
therefore excluded rather than ranked.
olmo_hybridis intentionally absent fromtest_declared_non_sources_never_become_prev_ops: itspost_attention_layernormis a valid source on linear-attention layers, soonly the per-layer fold invariant can pin it.
(
per_layer_input_gate,post_per_layer_input_norm,per_layer_projection)even though the forward runs projection then norm; both norms are marked
:postnorm, which drops the groups either way. Reordering would also require:nosrcon the gate and would change the registry block snapshot.