Skip to content

fix(awq): resolve multimodal rotary and stop folding across residual/nonlinearity (#4121) - #4135

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4121-awq-vl-and-trees
Nov 15, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4121-awq-vl-and-trees

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Part of #4121 (items 6-10)

TL;DR

AWQ could not find the decoder rotary inside a multimodal wrapper, so #4109's
stale-position_embeddings drop left the attention unpacking None
(Qwen2-VL crash). Four more definition trees folded a scale across a residual
or a non-homogeneous activation. The durable part: gpt_pangu, olmo_hybrid,
gemma4, gemma4_unified and nemotron_h are now in FAMILY_MODEL_TYPES, so
the 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:2px
Loading
# Old mechanism file:line (parent 13ee428f2)
6 root rotary resolved only from the declared path or model.rotary_emb gptqmodel/looper/awq_processor.py:324-330
7 mlp: ("c_fc:0", "c_proj:1") folds across GELU; the last block's ln_1 group contains attn.q_proj, which reads custom_query gptqmodel/models/definitions/pangu_alpha.py:20-30
8 one static tree; post_attention_layernorm treated as a gain source on the full-attention layer, where its output is added to the residual gptqmodel/models/definitions/olmo_hybrid.py:56
9 composite/unified trees kept v_proj:0 and post_feedforward_layernorm:! while the text tree already had :nosrc / :postnorm gptqmodel/models/definitions/gemma4.py:263,268-271, gemma4_unified.py:71
10 mixer.up_proj feeds down_proj across relu(x)^2 gptqmodel/models/definitions/nemotron_h.py:28

2. 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:2px
Loading

3. Per-subsystem detail

3.1 Item 6 — multimodal rotary resolution

_get_root_rotary now resolves, in order: the declared rotary_embedding, the
text-model fast path (model.rotary_emb / model.rotary), then a scan of
named_modules(). The scan skips

  • any module under a vision/audio tower segment (visual, vision_tower,
    audio_tower, image_encoder, ...). On Qwen2-VL model.visual.rotary_pos_emb
    is the shallowest name match and has the vision head width.
  • any module under a numbered container (layers.0.self_attn.rotary_emb on
    Nanbeige/DeciLM). Those are per-layer, old-style (x, seq_len) modules that
    _prepare_module_forward's module-local fallback already handles; returning
    them 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:

parent 13ee428f2 this branch
_get_root_rotary() None Qwen2VLRotaryEmbedding
position_embeddings after _prepare_module_forward dropped rebuilt, (1, 20, 128)
attention forward TypeError: cannot unpack non-iterable NoneType object (modeling_qwen2_vl.py:555) OK, (1, 20, 1536)

Tiny Qwen2-VL (build_tiny_qwen2_vl), full GPTQModel.load + quantize() with
variable-length 48/20 calibration:

parent this branch
AWQ 4-bit g32 TypeError: cannot unpack non-iterable NoneType object AWQ QUANTIZE OK, q_proj -> AwqTorchLinear
GPTQ 4-bit g32 GPTQ QUANTIZE OK GPTQ QUANTIZE OK

The tiny repro shows the trigger: the attention replay received
x=(1, 48, 128) with position_embeddings captured from the 20-token sample.

3.2 Item 7 — PanGu-alpha

mlp becomes ("c_fc:0:nosrc", "c_proj:1") because GPTPanguMLP.forward is
c_proj(gelu(c_fc(x))). PanguAlphaQModel.awq_get_modules_for_scaling also
drops attn.q_proj from the top-query block's ln_1 group: that block's q_proj
reads custom_query = wqe(position_ids), so scaling it with the ln_1 group
multiplies q by the scale while its input is not divided.

3.3 Item 8 — OLMo Hybrid

get_post_norm_modules is a classmethod, so it cannot describe a model whose
layers alternate shapes. New BaseQModel.awq_post_norm_modules(layer) is the
per-layer extension point, and awq_get_modules_for_scaling uses it.
OlmoHybridQModel adds post_attention_layernorm only when the layer has a
self_attn:

residual = h
h = post_attention_layernorm(self_attn(h))   # attention layer: added to the residual
h = residual + h

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:nosrc and
post_feedforward_layernorm:!:postnorm; the composite (gemma4) and unified
(gemma4_unified) trees had not copied them. The composite tree additionally
marks post_per_layer_input_norm as :postnorm: Gemma4's decoder applies it
to per_layer_projection's output and adds that to a residual, so it cannot
absorb the projection's input scale either.

3.5 Item 10 — NemotronH

NemotronHMLP.forward is down_proj(act_fn(up_proj(x))) with relu2, i.e. the
nonlinearity sits directly between up_proj and down_proj, so up_proj:3:nosrc.

4. Failure and ambiguity contract

Situation Before After
Decoder rotary nested in a multimodal wrapper None -> captured cos/sin dropped -> attention unpacks None found via scan; cos/sin rebuilt for the replayed length
Vision tower rotary is the shallowest name match would be picked by a naive scan excluded by tower segment
Per-layer (x, seq_len) rotary handled by the module-local fallback unchanged: root resolution still returns None
Hybrid layer whose post-norm sits on the residual folded (3.6e-1) group dropped for that layer shape
Tree folds across GELU / relu2 folded (4.5e-2 / 2.2e-2) :nosrc drops the group
A definition still declares the old flags — test_declared_non_sources_never_become_prev_ops fails

5. Change inventory

Area File Purpose
Rotary resolution gptqmodel/looper/awq_processor.py declared -> fast path -> guarded scan, cached
Per-layer post-norms gptqmodel/models/base.py awq_post_norm_modules(layer) hook
Trees definitions/pangu_alpha.py, olmo_hybrid.py, gemma4.py, gemma4_unified.py, nemotron_h.py :nosrc / :postnorm declarations, top-query q_proj exclusion
Durable harness tests/test_awq_scaling_groups.py five families added to FAMILY_MODEL_TYPES, tiny configs for composite/remote configs, structural non-source pins
Tests tests/test_awq_vl_rotary_resolution.py (new), tests/tiny_remote_models.py rotary resolution + tiny Qwen2-VL AWQ/GPTQ
Docs docs/awq-scaling-groups.md, README.md mechanisms, diagrams, validation table

6. Tests / guarantees

Test Invariant
test_awq_scaling_groups.py::test_scaling_groups_preserve_decoder_layer_output (gpt_pangu, olmo_hybrid, gemma4, gemma4_unified, nemotron_h) folding a random scale leaves the decoder layer's logits unchanged to 1e-5 relative
test_awq_scaling_groups.py::test_declared_non_sources_never_become_prev_ops c_fc, v_proj, post_feedforward_layernorm, post_per_layer_input_norm, mixer.up_proj never become prev_op
test_awq_vl_rotary_resolution.py nested rotary found, vision tower never used, per-layer rotaries ignored, stale cos/sin rebuilt; tiny Qwen2-VL quantizes with AWQ and GPTQ

7. Validation

Environment: /opt/venvs/cu130t/bin/python (Python 3.14t, torch
2.16.0.dev+cu130, transformers 5.22.2), PYTHONPATH=$PWD.
Hardware: RTX 4090 GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e for the real-model
runs; 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_scale with a random scale in [0.5, 1.5], decoder-layer output)

Definition Group Before After
gpt_pangu mlp.c_fc -> mlp.c_proj (GELU) 4.49e-02 dropped
gpt_pangu ln_1 -> [q,k,v] on the top-query layer q_proj output changes 2.93e-01 (layer 7.4e-07, silent) ln_1 -> [k,v] at 1.19e-08; q_proj quantized plainly
olmo_hybrid post_attention_layernorm -> gate/up on the attention layer 3.58e-01 dropped
olmo_hybrid same group on the linear-attention layer 1.64e-08 (valid) kept, 1.64e-08
gemma4 post_feedforward_layernorm -> per_layer_input_gate 2.42e-01 dropped
gemma4 v_proj -> o_proj 1.04e-01 dropped
gemma4 post_per_layer_input_norm -> per_layer_projection 2.22e-01 dropped
gemma4_unified v_proj -> o_proj 1.87e-01 dropped
nemotron_h mixer.up_proj -> down_proj (relu2) 2.17e-02 dropped

Worst surviving group per family after the fix: gpt_pangu 1.7e-07,
olmo_hybrid 2.0e-07, gemma4 2.5e-07, gemma4_unified 2.9e-07,
nemotron_h 1.4e-07.

Durable harness

Parent commit 13ee428f2 with only the new test file:

pytest "tests/test_awq_scaling_groups.py::test_scaling_groups_preserve_decoder_layer_output" \
       -k "gpt_pangu or olmo_hybrid or gemma4 or nemotron_h"
-> 5 failed, 1 passed
   gemma4: 3.293e-01, gemma4_unified: 2.882e-01, olmo_hybrid: 3.017e-01,
   gpt_pangu: 4.694e-02, nemotron_h: 2.125e-02

This branch:

pytest tests/test_awq_scaling_groups.py
-> 102 passed, 2 skipped

pytest tests/test_awq_vl_rotary_resolution.py tests/test_awq_position_kwargs.py \
       tests/test_awq_rotary_device.py tests/test_gemma4_support.py \
       tests/test_gemma4_unified_support.py tests/test_nemotron_h_support.py \
       tests/test_nemotron_h_puzzle_support.py tests/test_pangu_alpha_replay.py \
       tests/module_tree/test_registered_module_plans.py \
       tests/module_tree/test_gemma_olmo_family_plan_coverage.py \
       tests/test_layer_module_coverage.py
-> 501 passed

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

tests/test_awq_vl_rotary_resolution.py on the parent commit: 3 of the 6 unit
tests fail (_get_root_rotary returns None, and the replay drops cos/sin).

8. Reviewer notes

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant