Repository navigation
fix(awq): dispatch dequantization on the saved AWQ layout and per-module bits (#4002) - #4031
Merged
Merged
Conversation
…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.
This was referenced Nov 14, 2026
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.
Fixes #4002
Scope: item 5 only. Items 1-4 of #4002 landed separately in #4023 (
:postnormflag,get_post_norm_modules(),_select_prev_op,docs/awq-norm-selection.md). This PR does not touchmodels/base.py::_select_prev_op,awq/quantize/scale.py, oradapter/adapter.py.TL;DR
dequantize_modelnow dispatches on the AWQ layout declared in the saved checkpoint (format, elseversion, else GEMM) and resolvesbits/group_sizeper module throughdynamic, instead of decoding every AWQ checkpoint with the GEMM reader. +171/-22 ingptqmodel/utils/model_dequant.py, new 11-case exact-oracle test file, newdocs/awq-dequant-layouts.md.1. The problem
convert_awq_filehad 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-widebits(gptqmodel/utils/model_dequant.py:1589-1597and:1583onmain@8f2efe552).GPTQModel writes four incompatible layouts under the same key names, so three of the four #4002 cases could not decode:
AwqGEMMLinear.packAwqTorchLinear.pack(pack_awqcontiguous stream)AwqGEMVLinear.pack([out, in/8]natural order)IndexError: index 87 is out of bounds for dimension 0 with size 8AwqGEMVFastLinear.pack(pack_intweightINT16 interleave, fp16 scaled zeros)NotImplementedError: "rshift_cpu" not implemented for 'Half'down_projviadynamicRuntimeError: The size of tensor a (256) must match the size of tensor b (128) at non-singleton dimension 12. 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:2pxThe layout comes from the checkpoint, not from the tensor shapes:
formatis authoritative (AWQConfigalso mirrors it intoversionfor older readers), and a checkpoint with neither field keeps the historical GEMM assumption.gemv_fastneeded an explicit inverse ofpack_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 storedscales/scaled_zerosare alreadycode * scale + offset, so they are applied per group without a second integer subtract.3. Before / after
gptqmodel/utils/model_dequant.py:1589_awq_layout,gptqmodel/utils/model_dequant.py:1545config["bits"],gptqmodel/utils/model_dequant.py:1583dynamic_get(dynamic, prefix, "bits", default),:1561dynamic_get(dynamic, prefix, "group_size", default)IndexError:1613NotImplementedErroronHalfrshift:1632scaled_zeroskeygemv_fast4. Failure and ambiguity contract
formatandversionabsentgemm(historical default)gemv/gemv_fast/llm-awqwithbits != 4NotImplementedErrornaming the layout and suggesting a GEMM exportgemv_fastwithin_features % 64 != 0ValueErrornaming the widthqweight/scales/ zero-point bufferKeyErrornaming the module and the missing keysdynamicrule setsbits: false(excluded module)5. Change inventory
gptqmodel/utils/model_dequant.pytests/test_awq_dequant_layouts.pyscaled_zeros,dynamic, end-to-enddocs/awq-dequant-layouts.mdREADME.md6. Tests
Independent fp64 oracle only: each case builds
(code - zero) * scalefrom the codes/zeros/scales it chose, packs that exact weight with the backend's own writer, then decodes throughconvert_awq_file/dequantize_modeland compares withrtol=0, atol=0. The decoder is never its own reference.Fail-before (source change stashed, tests kept), CUDA venv:
Pass-after:
pytest tests/test_awq_dequant_layouts.py -qpytest tests/test_awq_gemv_fast_pack_vectorized.py tests/test_awq_gemv_pack_vectorized.py tests/test_awq_pack_vectorized.py -qruff check gptqmodel docs/eora tests setup.pytests/test_awq_dynamic_group_size.py::test_dynamic_group_size_selects_compatible_kernel_and_packsfails on this branch and onorigin/mainunchanged (its stub predates_module_formatfrom #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
AwqTorchLinearGEMM 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-awqdecode 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_modelstill writes dense weights only; no quantized-weight cache is introduced or retained.awq_marlinreads a[0,2,1,3]byte interleave, so a natural-order 8-bit AWQ checkpoint is not guaranteed to load there. This PR only makesdequantize_modelread what GPTQModel writes.