Skip to content

fix(awq): validate group size against the packing kernel (#4095) - #4104

Merged
Qubitium merged 2 commits into
mainfrom
fix/awq-dynamic-group-size-out-features
Nov 15, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/awq-dynamic-group-size-out-features

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Part of #4095 (item 3)

TL;DR

Phi-tiny-MoE AWQ with the default group_size=128 failed while packing the
expert gate_proj/up_proj (out_features=448). The pre-calibration group
retune only checked the input axis. It now asks the resolved kernel's own
validate(in_features, out_features, group_size), so those 1024 expert modules
retune to 64 before calibration and the quantize completes.

Root cause

AWQProcessor._ensure_group_size_divisibility and
AWQProcessor._largest_supported_group_divisor
(gptqmodel/looper/awq_processor.py:513-600 before this change) required only
in_features % group_size == 0. AwqGEMMLinear._validate
(gptqmodel/nn_modules/qlinear/gemm_awq.py:209-229) additionally requires
out_features % group_size == 0.

#4084 already retuned the 512 expert down_proj modules
(in_features=448) from 128 to 64. The expert gate_proj/up_proj
(in_features=4096, out_features=448) divided 128 on the input axis, so they
kept 128 and failed in create_quant_module after the entire calibration pass:

AWQConfig(bits=4, group_size=128), Phi-tiny-MoE-instruct bf16:
NotImplementedError: AWQ GEMM requires a group size divisible by 32 and out_features divisible by that group size:
  got out_features=448, group_size=128
  (awq_processor.py:3469 pack_module -> utils/model.py:806 create_quant_module)

A duplicated partial predicate is exactly how this drifted from the kernel.

The fix

  • New AWQProcessor._kernel_group_size_supported calls the resolved kernel's
    validate(bits=..., group_size=..., in_features=..., out_features=..., pack_dtype=..., dtype=...). No predicate is re-implemented in the looper.
  • _ensure_group_size_divisibility retunes a module when its configured size
    fails that validation, and _largest_supported_group_divisor picks the
    largest SUPPORTS_GROUP_SIZE value that validates.
  • -1 (per-channel) is used only when it validates. When even per-channel does
    not pack the shape, the configured size is left untouched and no override is
    recorded, so a non-quantized module such as the router:! PhiMoE router is
    not rewritten with an invalid group size.

dtype is passed from the module weight; device is left to
create_quant_module because this runs in the processor constructor before
per-module placement is resolved.

Before / after (same 4096 -> 448 expert projection)

origin/main:

retuned group_size after _ensure_group_size_divisibility: 128
kernel validate(128): (False, NotImplementedError('AWQ GEMM requires a group size divisible by 32 and out_features divisible by that group size: got out_features=448, group_size=128'))

This branch:

retuned group_size after _ensure_group_size_divisibility: 64
kernel validate(128): (False, NotImplementedError(... got out_features=448, group_size=128))

Full model, Phi-tiny-MoE-instruct, bf16, AWQConfig(bits=4, group_size=128, sym=True, format=GEMM), 32 C4 documents, batch_size=1:

QUANTIZE_OK seconds=162.4
Quantize summary: 1 layer(s), 1664 module(s), 2m42s, peak VRAM 0.92 GiB
SHAPES in_features,out_features,group_size -> module count / example
  448x4096  g64 : 512 modules, e.g. model.layers.0.mlp.experts.0.down_proj
  4096x448  g64 : 1024 modules, e.g. model.layers.0.mlp.experts.0.gate_proj
  2048x4096 g128: 32 modules, e.g. model.layers.0.self_attn.o_proj
  4096x512  g128: 64 modules, e.g. model.layers.0.self_attn.k_proj
  4096x2048 g128: 32 modules, e.g. model.layers.0.self_attn.q_proj

The attention projections keep 128; only the expert matrices retune. The
non-quantized router no longer receives a -1 override.

Flow

flowchart TD
    A["AWQProcessor._ensure_group_size_divisibility"] --> B["AWQProcessor._kernel_group_size_supported"]
    B --> C["resolved kernel validate<br/>in_features, out_features, group_size"]
    C -- valid --> D["keep configured group size"]
    C -- invalid --> E["AWQProcessor._largest_supported_group_divisor"]
    E --> F{"any supported size validates?"}
    F -- Yes --> G["record +:^module$ dynamic group_size override"]
    F -- No --> H{"per-channel -1 validates?"}
    H -- Yes --> I["record per-channel -1"]
    H -- No --> J["leave configured size, no override"]
    G --> K["_search_best_scale / pack_module"]
    I --> K
    J --> K
    D --> K
    classDef failure fill:#fdd,stroke:#c33
    class J failure
Loading

Before/after of the decision mechanism:

Mechanism Before After
Retune predicate in_features % group_size == 0 kernel validate(in_features, out_features, group_size)
Expert gate_proj/up_proj 4096 -> 448 kept 128, raised in create_quant_module retuned to 64 before calibration
No supported size unconditional -1 -1 only when it validates, otherwise no override

The same flow is committed to docs/awq-dynamic-group-size.md (already linked
from README.md).

Tests

  • tests/test_awq_dynamic_group_size.py::test_awq_retunes_group_size_for_non_divisible_out_features
    (new) fails on origin/main (AttributeError: AWQProcessor has no attribute _resolve_module_kernel) and passes here.
  • tests/test_awq_dynamic_group_size.py::test_awq_leaves_unpackable_module_untouched
    (new) pins the no-override path for the 4096 -> 16 router shape.
  • The existing 448-wide input-axis test now uses a real
    448 -> 4096 down_proj shape (fp16) instead of an un-packable 448 -> 8.
  • test_dynamic_group_size_selects_compatible_kernel_and_packs was missing its
    _module_format stub binding on main and failed there; it is bound now.

Commands and results:

Command Result
CUDA_VISIBLE_DEVICES=GPU-1ece7ec2-... pytest -q tests/test_awq_dynamic_group_size.py 5 passed
HIP_VISIBLE_DEVICES=1 pytest -q tests/test_awq_dynamic_group_size.py 5 passed
ruff check gptqmodel docs/eora tests setup.py All checks passed

Hardware: RTX 4090 (GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e), CUDA 13.0 /
torch 2.16.0.dev; AMD R9700 (gfx1201) via HIP_VISIBLE_DEVICES=1, ROCm 10.1 /
torch 2.16.0.dev for the unit tests.

Relation to #4100

#4100 (Prajwal7-2002) targets the same defect with a different
awq_processor.py patch. This PR supersedes it: it covers every case #4100
covers (input-axis retune, output-axis retune, the kernel-owned predicate, and
the same missing _module_format stub fix in the test file) and adds the
per-channel/no-override contract, the docs/awq-dynamic-group-size.md flow
update, and a CUDA + ROCm test run.

- ask the resolved kernel's validate(in_features, out_features, group_size)
  instead of duplicating a partial in_features check
- retune Phi-tiny-MoE expert gate/up (4096 -> 448) from 128 to 64 before
  calibration instead of failing in create_quant_module
- use per-channel -1 only when it validates; leave unpackable modules such as
  the non-quantized PhiMoE router untouched
- docs/awq-dynamic-group-size.md: new decision flow and measured Phi run

Part of #4095 (item 3)
@Qubitium
Qubitium merged commit 1a3943e into main Nov 15, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/awq-dynamic-group-size-out-features branch November 15, 2026 08:59
Qubitium added a commit that referenced this pull request Nov 15, 2026
…tions (#4091) (#4110)

* fix(awq): replay packed calibration rows with their own mask and positions (#4091)

- carry per-row provenance out of token-row packing
- build a per-document block-diagonal attention mask and original position ids
- fixes the batch_size>1 TypeError and cross-document attention in scale search
- docs/awq-calibration-aggregation.md: replay flow and measured A/B table

Part of #4091 (item 1)

* test(awq): keep the shape-only kernel stub out of the kernel registry

The merged #4104 test helper subclassed AwqGEMMLinear, so it registered a
second AWQ_GEMM/GEMM kernel. Collecting the PPL test file and the group-size
test file in one process made get_kernel_for_backend reject the real kernel as
ambiguous. Use a non-subclass stub that delegates to AwqGEMMLinear._validate.
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