Skip to content

fix(awq): dispatch dequantization on the saved AWQ layout and per-module bits (#4002) - #4031

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-4002-dequant
Nov 14, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-4002-dequant

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4002

Scope: item 5 only. Items 1-4 of #4002 landed separately in #4023 (:postnorm flag, get_post_norm_modules(), _select_prev_op, docs/awq-norm-selection.md). This PR does not touch models/base.py::_select_prev_op, awq/quantize/scale.py, or adapter/adapter.py.

TL;DR

dequantize_model now dispatches on the AWQ layout declared in the saved checkpoint (format, else version, else GEMM) and resolves bits/group_size per module through dynamic, instead of decoding every AWQ checkpoint with the GEMM reader. +171/-22 in gptqmodel/utils/model_dequant.py, new 11-case exact-oracle test file, new docs/awq-dequant-layouts.md.

1. The problem

convert_awq_file had one decode path. It assumed the GEMM packing for every checkpoint: unpack_awq + reverse_awq_order, group_size = unpacked_weight.shape[0] // scales.shape[0], and a single checkpoint-wide bits (gptqmodel/utils/model_dequant.py:1589-1597 and :1583 on main @ 8f2efe552).

GPTQModel writes four incompatible layouts under the same key names, so three of the four #4002 cases could not decode:

Case Writer Before
GEMM 4-bit AwqGEMMLinear.pack exact
AwqTorch 8-bit AwqTorchLinear.pack (pack_awq contiguous stream) exact
GEMV 4-bit AwqGEMVLinear.pack ([out, in/8] natural order) IndexError: index 87 is out of bounds for dimension 0 with size 8
GEMV_FAST / LLM-AWQ 4-bit AwqGEMVFastLinear.pack (pack_intweight INT16 interleave, fp16 scaled zeros) NotImplementedError: "rshift_cpu" not implemented for 'Half'
4-bit run with 8-bit down_proj via dynamic any RuntimeError: The size of tensor a (256) must match the size of tensor b (128) at non-singleton dimension 1

2. The new flow

flowchart TD
    A["convert_awq_file<br/>read shard keys"] --> B["collect qweight / qzeros<br/>or scaled_zeros / scales"]
    B --> C["_awq_layout(config)<br/>format, else version, else gemm"]
    C --> D{"layout"}
    D -- "gemm" --> E["_dequantize_awq_gemm<br/>unpack + reverse_awq_order<br/>repeat_interleave groups"]
    D -- "gemv" --> F["_dequantize_awq_gemv<br/>natural-order unpack<br/>index scales by group"]
    D -- "gemv_fast / llm-awq" --> G["_dequantize_awq_gemv_fast<br/>unpack nibbles + inverse interleave<br/>code * scale + offset"]
    E --> H["_awq_module_int(dynamic, bits)<br/>_awq_module_int(dynamic, group_size)"]
    F --> H
    G --> H
    H --> I["write dense weight [out, in]"]
    D -. "unsupported width" .-> J["NotImplementedError naming the layout"]
    style J fill:#f8d7da,stroke:#b02a37,stroke-width:2px
Loading

The layout comes from the checkpoint, not from the tensor shapes: format is authoritative (AWQConfig also mirrors it into version for older readers), and a checkpoint with neither field keeps the historical GEMM assumption.

gemv_fast needed an explicit inverse of pack_intweight: it interleaves four output rows per INT16 word, then reshapes the (64 columns x 4 lanes) tile as if transposed, which mixes the lane index with the low six column bits. _awq_gemv_fast_k_order + a scatter restore the natural [out, in] codes; the stored scales/scaled_zeros are already code * scale + offset, so they are applied per group without a second integer subtract.

3. Before / after

Mechanism Before After
Layout selection GEMM only, gptqmodel/utils/model_dequant.py:1589 _awq_layout, gptqmodel/utils/model_dequant.py:1545
Per-module width config["bits"], gptqmodel/utils/model_dequant.py:1583 dynamic_get(dynamic, prefix, "bits", default), :1561
Per-module group size inferred from GEMM shapes dynamic_get(dynamic, prefix, "group_size", default)
GEMV decode IndexError natural-order unpack, :1613
GEMV_FAST / LLM-AWQ decode NotImplementedError on Half rshift nibble unpack + inverse interleave, :1632
scaled_zeros key treated as an opaque passthrough tensor collected as the zero-point buffer for gemv_fast

4. Failure and ambiguity contract

Situation Behaviour
format and version absent assume gemm (historical default)
gemv / gemv_fast / llm-awq with bits != 4 NotImplementedError naming the layout and suggesting a GEMM export
gemv_fast with in_features % 64 != 0 ValueError naming the width
missing qweight / scales / zero-point buffer KeyError naming the module and the missing keys
dynamic rule sets bits: false (excluded module) falls back to the checkpoint default instead of width 0

5. Change inventory

File +/- Purpose
gptqmodel/utils/model_dequant.py +171/-22 layout dispatch, per-module widths, three decoders
tests/test_awq_dequant_layouts.py new, 11 tests exact fp64 oracle per layout, scaled_zeros, dynamic, end-to-end
docs/awq-dequant-layouts.md new layout table, flow diagram, failure contract
README.md +1 link to the docs page

6. Tests

Independent fp64 oracle only: each case builds (code - zero) * scale from the codes/zeros/scales it chose, packs that exact weight with the backend's own writer, then decodes through convert_awq_file/dequantize_model and compares with rtol=0, atol=0. The decoder is never its own reference.

Fail-before (source change stashed, tests kept), CUDA venv:

$ CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae PYTHONPATH=$PWD \
    /opt/venvs/cu130t/bin/python -m pytest tests/test_awq_dequant_layouts.py -q
7 failed, 4 passed
  IndexError: index 45/87 is out of bounds for dimension 0 with size 8      (gemv, x2)
  NotImplementedError: "rshift_cpu" not implemented for 'Half'             (gemv_fast, llm-awq, x4)
  RuntimeError: size of tensor a (256) must match ... b (128)              (dynamic)

Pass-after:

Command CUDA venv ROCm venv
pytest tests/test_awq_dequant_layouts.py -q 11 passed 11 passed
pytest tests/test_awq_gemv_fast_pack_vectorized.py tests/test_awq_gemv_pack_vectorized.py tests/test_awq_pack_vectorized.py -q 5 passed 5 passed
ruff check gptqmodel docs/eora tests setup.py All checks passed -

tests/test_awq_dynamic_group_size.py::test_dynamic_group_size_selects_compatible_kernel_and_packs fails on this branch and on origin/main unchanged (its stub predates _module_format from #3796); it is not caused by this PR and is not touched here.

Hardware: numerics run on CPU (both venvs are CPU-only for this test file); the CUDA venv supplies the native GEMM writer, the ROCm venv the portable AwqTorchLinear GEMM writer — both emit the same on-disk GEMM layout, so the 4-bit GEMM case asserts the identical checkpoint bytes on each host.

7. Reviewer notes

  • gemv/gemv_fast/llm-awq decode only 4-bit: those writers are 4-bit-only (SUPPORTS_BITS = [4]), and the layouts have no defined wider packing. The error names the layout rather than guessing.
  • dequantize_model still writes dense weights only; no quantized-weight cache is introduced or retained.
  • Not in scope: vLLM awq_marlin reads a [0,2,1,3] byte interleave, so a natural-order 8-bit AWQ checkpoint is not guaranteed to load there. This PR only makes dequantize_model read what GPTQModel writes.

GPTQModel Bot and others added 3 commits November 14, 2026 16:25
…ule 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant