Skip to content

fix(awq): aggregate variable-length calibration for every module (#4063) - #4071

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4063-awqcal
Nov 14, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4063-awqcal

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #4063

TL;DR

AWQ declared token-row calibration packing only for MoE modules. Every dense module therefore kept only the last variable-length capture, which is the shortest sample because calibration is sorted by length descending. AWQ scale search saw 32 of 55,091 tokens on Qwen2.5-0.5B and measured no better than RTN. This makes token rows the default for every module and checks fallback coverage per layer. Llama-3.2-1B AWQ drops from 24.15 to 21.23 PPL (RTN 24.03).

1. Root cause

BaseQModel.awq_input_feature_aggregation (gptqmodel/models/base.py:4785-4802 at ad0525cca) returned {"mode": "token_rows"} only for MoE roots and their children, and None for every dense module. AWQProcessor._layer_input_features (gptqmodel/looper/awq_processor.py:1355-1370) then fell through to _can_concat_batch_tensors; variable-length captures such as [1, 423, H] and [1, 36, H] cannot be concatenated, so the else branch kept tensors[-1] (awq_processor.py:1357). With batch_size=1 and calibration_sort="desc" that is the shortest document.

Second effect: _should_fallback_group (awq_processor.py:1535-1561) summed retained_tokens over the group's siblings. q/k/v (3x) and gate/up (2x) passed the percentage threshold while a lone down_proj fell below it and silently became RTN.

flowchart TD
    A["BaseQModel.awq_input_feature_aggregation(name)"] --> B{"name under a MoE root?"}
    B -- yes --> C["mode token_rows<br/>capture_root for the root"]
    B -- no --> D["None"]
    D --> E["AWQProcessor._layer_input_features"]
    C --> E
    E --> F{"_can_concat_batch_tensors?"}
    F -- yes --> G["batch concat<br/>all rows"]
    F -- no --> H["features[name] = tensors[-1]<br/>mode latest_batch"]
    H --> I["_should_fallback_group<br/>sums retained_tokens over siblings"]
    I --> J["q/k/v and gate/up pass by sum<br/>lone down_proj falls to RTN"]
    style H fill:#ffd6d6,stroke:#cc0000
    style J fill:#ffd6d6,stroke:#cc0000
Loading
Old mechanism file:line at ad0525c Replacement
MoE-only token-row policy gptqmodel/models/base.py:4785-4802 {"mode": "token_rows"} for every module; MoE roots keep capture_root
Latest-capture fallback gptqmodel/looper/awq_processor.py:1357 _pack_token_rows over every capture
Summed group coverage gptqmodel/looper/awq_processor.py:1546-1554 per-layer should_use_fallback

2. New flow

flowchart TD
    A["BaseQModel.awq_input_feature_aggregation(name)"] --> B{"name is a MoE root?"}
    B -- yes --> C["mode token_rows<br/>capture_root = True"]
    B -- no --> D["mode token_rows"]
    C --> E["AWQProcessor._layer_input_features"]
    D --> E
    E --> F["_token_row_budget<br/>max of largest batch rows and batch count"]
    F --> G["_pack_token_rows<br/>deterministic bounded sample<br/>every non-empty batch contributes"]
    G --> H["feature_stats<br/>mode token_rows, raw_tokens, retained_tokens, batches"]
    H --> I["_should_fallback_group"]
    I --> J{"every layer individually covered?"}
    J -- yes --> K["AWQ scale search on packed rows"]
    J -- no --> L["RTN fallback for the whole group"]
    style L fill:#ffe9b3,stroke:#b38600
Loading

AWQProcessor._feature_aggregation_policy still asks the model class, so a definition can narrow the policy; returning None restores the legacy latest-capture path and the per-layer fallback check then applies. The MoE shared pre-router capture (capture_root) and the router/padding fixes from #3036/#4035/#4060 are unchanged.

3. Memory bound

AWQProcessor._token_row_budget (awq_processor.py:807) is:

retained = min(total_rows, max(largest_batch_rows, non_empty_batch_count))

For batch_size=1 that is one longest-sample equivalent: 5,405 rows on Qwen2.5-0.5B (of 55,091) and 5,384 on Llama-3.2-1B (of 54,302). It is independent of corpus size, so the replay tensor does not grow with the number of calibration documents; the raw per-batch captures are released by _finalize_quantized_layer. Peak allocated VRAM for the Qwen AWQ run rose from 0.423 GB to 0.498 GB.

4. Measured PPL

Wikitext-2 PPL, 4-bit, group_size=128, batch_size=1, first 128 C4 documents, 80 x 512-token chunks, save then reload, RTX 4090. fp16 baselines use the same harness.

Model fp16 RTN AWQ before AWQ after
Llama-3.2-1B-Instruct, asym 19.13 24.03 24.15 21.23
Qwen2.5-0.5B-Instruct, asym 18.68 22.77 22.79 22.23
Qwen2.5-0.5B-Instruct, sym - 24.84 26.21 24.92

Before the fix AWQ was tied with or worse than RTN. After it AWQ is 2.80 PPL better than RTN on Llama-3.2-1B and 0.54 better on Qwen2.5-0.5B (asymmetric). The symmetric Qwen case improves by 1.29 PPL but stays 0.08 PPL above RTN at the default bound. Raising the budget to 20,000 rows on the same corpus measures 24.68 PPL, so the residual is the bounded replay sample, not a lost capture; the default bound is kept for 70B memory safety.

5. Fail-before / pass-after

Regression test: tests/test_awq_variable_length_ppl.py (gpu + slow; 128 C4 docs, 80 x 512 wikitext chunks, save+reload, asserts AWQ PPL is at least 0.5% below RTN).

Before, on ad0525cca (test file copied into a clean worktree):

$ CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae PYTHONPATH=$PWD \
  GPTQMODEL_AWQ_PPL_WIKITEXT=/root/stableabi/n4063/wikitext2_test.txt \
  /opt/venvs/cu130t/bin/python -m pytest tests/test_awq_variable_length_ppl.py -q
AssertionError: AWQ perplexity 22.7882 must be at least 0.5% below RTN 22.7654
FAILED

After, on this branch:

$ CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae PYTHONPATH=$PWD \
  GPTQMODEL_AWQ_PPL_WIKITEXT=/root/stableabi/n4063/wikitext2_test.txt \
  /opt/venvs/cu130t/bin/python -m pytest tests/test_awq_variable_length_ppl.py -q
{'rtn_ppl': 22.7654, 'awq_ppl': 22.2303, ...}
1 passed

ROCm:

$ HIP_VISIBLE_DEVICES=0 PYTHONPATH=$PWD GPTQMODEL_AWQ_PPL_WIKITEXT=/root/stableabi/n4063/wikitext2_test.txt \
  /opt/venvs/rocm101t/bin/python -m pytest tests/test_awq_variable_length_ppl.py -q
{'rtn_ppl': 22.7646, 'awq_ppl': 22.1543, ...}
1 passed

Unit-level fail-before (same file copied to the main worktree): test_public_moe_model_policies_enable_pointwise_roots_and_children, test_awq_dense_variable_length_packs_every_batch_by_default, and test_awq_fallback_group_checks_each_layer_independently all fail on ad0525cca; all pass here.

6. Other tests

# CUDA and ROCm
tests/test_awq_weight_mean.py -k "public_moe_model_policies or fallback or dense_variable_length"  -> 5 passed
tests/module_tree/test_moe_tags.py tests/test_awq_scale_search_memory.py                          -> 82 passed
tests/models/awq/test_qwen3_moe_feature_aggregation.py                                            -> 1 passed

The real MoE test now records attention as token_rows (raw 101 / retained 53 / 3 batches) while expert telemetry and the mlp scale feature are unchanged. Full tests/test_awq_weight_mean.py on ROCm reports 70 passed, 1 skipped, 6 failed. Five of the six also fail on ad0525cca; the sixth (test_awq_weight_mean_matches_legacy_impl_0_cpu_gs32) is the same CPU-timing assertion family (streaming mean within 5% of the legacy loop) and flips under the current machine load (load average > 100). None of them touch aggregation.

Ruff (pinned 0.14.2): ruff check gptqmodel docs/eora tests setup.py -> All checks passed!

7. Docs

Committed docs/awq-calibration-aggregation.md (old/new flow, per-layer fallback contract, memory bound, measured table) and linked it from README.md next to the other AWQ calibration pages.

8. Reviewer notes

AWQ declared token-row packing only for MoE modules, so every dense module
fell back to the latest capture when calibration batches had different
sequence lengths. Calibration is sorted by length descending, so that was
the shortest sample (32 of 55,091 tokens on Qwen2.5-0.5B) and AWQ measured
no better than RTN.

Make token_rows the default policy for every module (MoE roots keep the
shared pre-router capture), and evaluate fallback coverage per layer
instead of summing retained tokens over the group's siblings.

Wikitext-2 PPL, 4-bit g128, 128 C4 calibration docs, save+reload:
  Llama-3.2-1B asym: 24.15 -> 21.23 (RTN 24.03)
  Qwen2.5-0.5B asym: 22.79 -> 22.23 (RTN 22.77)
  Qwen2.5-0.5B sym:  26.21 -> 24.92 (RTN 24.84)

Add a variable-length AWQ-vs-RTN perplexity regression test, unit coverage
for the default policy and per-layer fallback, and the calibration
aggregation reference page.
@Qubitium
Qubitium merged commit 39b6276 into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4063-awqcal branch November 14, 2026 23:44
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