Skip to content

fix(awq): decode 8-bit packed readers in natural order (#3974) - #3993

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-3974
Nov 14, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-3974

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3974

TL;DR

AWQ only interleaves the 4-bit packed layout. The 8-bit Marlin repack (Python and CUDA), the Machete un-interleave, and convert_awq_file all assumed the INT4 interleave existed for 8-bit too, and convert_awq_file additionally ignored the checkpoint width. The readers now undo the interleave only for 4-bit, decode 8-bit in natural column order, and take bits from the quantization config. 4-bit behaviour is unchanged.

Root cause

1. 8-bit Marlin/Machete un-interleaved data that was never interleaved

pack_awq (gptqmodel/quantization/awq/utils/packing_utils.py) uses the classic [0, 2, 4, 6, 1, 3, 5, 7] interleave only for bits == 4; every other width goes through _pack_contiguous, a plain little-endian stream. reverse_awq_order is likewise a no-op for bits != 4, and the AllSpark reader documents that "AWQ_ORDER only applies to the 4-bit layout".

The 8-bit readers disagreed:

Site (main) Old behaviour
gptqmodel/utils/marlin.py:1124-1133 undo_interleave = argsort([0, 2, 1, 3]) for 8-bit
gptqmodel_ext/marlin/awq_marlin_repack.cu:99-105 undo_pack[4] = {0, 2, 1, 3} for every non-4-bit width
gptqmodel/nn_modules/qlinear/machete_awq.py:35-45 _undo_awq_interleave(..., 8) permuted with [0, 2, 1, 3]

That swaps columns 1 and 2 inside every 32-bit word for both weights and zero points, so an 8-bit layer produces silently wrong output.

2. convert_awq_file skipped the INT4 nibble order and hardcoded 4-bit

gptqmodel/utils/model_dequant.py:1569-1572 unpacked qweight/qzeros with the generic unpack_cols (lane i → column i) and never applied AWQ_REVERSE_ORDER, so even a 4-bit checkpoint came out with output channels permuted inside every group of 8. bits = 4 was also hardcoded, so an 8-bit shard was decoded as 4-bit and crashed on the shape mismatch.

Fix

  • awq_to_marlin_zero_points: undo the interleave only for num_bits == 4; 8-bit goes to marlin_zero_points in natural order. Unsupported widths still raise.
  • awq_marlin_repack.cu: cur_n_pos_unpacked = cur_n_pos by default; only the num_bits == 4 branch applies undo_pack.
  • AwqMacheteLinear._undo_awq_interleave: returns 8-bit input unchanged.
  • convert_awq_file: new keyword-only config parameter; bits = int(config.get("bits", 4)); decodes with unpack_awq + reverse_awq_order (a no-op for 8-bit). dequantize_model passes its quantization_config.
  • Docs: docs/awq-packed-layout.md, linked from README.md.

Logic flow

flowchart TD
    A["AWQ checkpoint<br/>qweight, qzeros, scales"] --> B{"bits from config"}
    B -->|"4"| C["unpack contiguous words<br/>lane i to column i"]
    B -->|"8"| C
    B -->|"other"| X["raise: unsupported AWQ width"]:::fail
    C --> D{"bits == 4 ?"}
    D -->|"yes"| E["reverse AWQ interleave<br/>AWQ_REVERSE_ORDER"]
    D -->|"no, 8-bit"| F["identity<br/>natural order"]
    E --> G["dense weight<br/>out_features x in_features"]
    F --> G

    classDef fail fill:#ffdddd,stroke:#cc0000,color:#660000
Loading
flowchart LR
    A["packed AWQ word<br/>K x N/pack"] --> B{"num_bits == 4 ?"}
    B -->|"yes"| C["undo AWQ interleave<br/>0,4,1,5,2,6,3,7"]
    B -->|"no, 8-bit"| D["identity lane index"]
    C --> E["Marlin / Machete layout<br/>permutation"]
    D --> E
    E --> F["quantized MMA"]

    classDef fail fill:#ffdddd,stroke:#cc0000,color:#660000
Loading

The failure path is the bits guard: awq_to_marlin_zero_points and _undo_awq_interleave raise for widths other than 4/8 instead of guessing a layout.

Fail-before / pass-after

The new tests use an independent Torch packer and a dense (codes - zeros) * scales oracle; no reference value comes from the code under test. Fail-before was captured in a clean worktree at origin/main (1a30538bc) with only the new test files applied.

Before (base):

$ PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest -q \
    tests/kernels/test_awq_machete_marlin.py::test_awq_marlin_zero_points_8bit_keeps_natural_column_order \
    tests/kernels/test_awq_machete_marlin.py::test_machete_undo_awq_interleave_8bit_is_identity \
    tests/test_model_dequant_fp8.py::test_convert_awq_file_4bit_restores_natural_column_order \
    tests/test_model_dequant_fp8.py::test_convert_awq_file_reads_8bit_from_config \
    tests/test_model_dequant_fp8.py::test_dequantize_model_awq_8bit_uses_config_bits
FAILED test_awq_marlin_zero_points_8bit_keeps_natural_column_order
  Mismatched elements: 128 / 256 (50.0%); greatest abs diff: 3874109587
FAILED test_machete_undo_awq_interleave_8bit_is_identity
FAILED test_convert_awq_file_4bit_restores_natural_column_order
  Mismatched elements: 735 / 1024 (71.8%); greatest abs diff: 0.2719879
FAILED test_convert_awq_file_reads_8bit_from_config
  TypeError: convert_awq_file() got an unexpected keyword argument 'config'
FAILED test_dequantize_model_awq_8bit_uses_config_bits
  RuntimeError: The size of tensor a (32) must match the size of tensor b (16)
5 failed
$ CUDA_VISIBLE_DEVICES=GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e ... \
    pytest -q tests/kernels/test_awq_machete_marlin.py::test_awq_marlin_cuda_8bit_matches_dense_reference
FAILED ...[128]  Mismatched: 958/2048 (46.8%); greatest abs diff: 4.390625
FAILED ...[-1]   Mismatched: 931/2048 (45.5%); greatest abs diff: 4.7265625
2 failed

After (this branch):

$ PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest -q <the same five CPU tests>
5 passed

$ CUDA_VISIBLE_DEVICES=GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e \
    PYTHONPATH=$PWD PATH=/usr/local/cuda/bin:$PATH \
    /opt/venvs/cu130t/bin/python -m pytest -q -rs tests/kernels/test_awq_machete_marlin.py
13 passed, 2 skipped

The two skips are Machete (SM90-only) and Swordfish (Blackwell-only); neither can run on the RTX 4090. Machete's 8-bit decision is covered by test_machete_undo_awq_interleave_8bit_is_identity on CPU.

Full dequant test file on CUDA: 33 passed, 1 skipped, 1 failed. The single failure is test_ignored_layers_are_honored_by_non_fp8_converters, which fails identically on the base worktree because bitsandbytes is not installed in this environment (RuntimeError: ... requires the 'bitsandbytes' package). It is unrelated to this change.

CUDA and ROCm

  • CUDA RTX 4090 (SM 8.9, GPU-1ece7ec2-...): test_awq_marlin_cuda_8bit_matches_dense_reference passes for group_size=128 and group_size=-1; the same layer's max error against the dense oracle moved from 3.306 to 4.88e-4. All 4-bit Marlin tests still pass.
  • ROCm R9700 (HIP_VISIBLE_DEVICES=1): the five CPU regression tests pass, tests/kernels/test_awq_torch.py passes (14 tests), and an 8-bit/4-bit AwqTorchLinear layer on the GPU matches the dense oracle with max_err=0.000000 for both widths. Marlin and Machete are CUDA-only (SUPPORTS_DEVICES = [DEVICE.CUDA]), so their kernels were not executed on ROCm.
  • CUDA ordinal 4 (RTX 5090) was never touched.

Lint

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

(The branch is rebased onto main after #3987 fixed the known audio.py F401.)

Reviewer notes

  • convert_awq_file still assumes the GEMM packed axis (qweight is [in_features, out_features/pack]). GEMV/GEMV_FAST checkpoints pack the other axis; the issue marked that path unverified and it is not changed here.
  • Machete's kernel cannot execute on the available 4090 (SM90 required); only its pure-Torch un-interleave is exercised here.

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.

[BUG] AWQ packed-format readers: 8-bit Marlin/Machete wrong un-interleave; dequantize_model scrambles AWQ channels

1 participant