Skip to content

fix(qlinear): restore bare-module state_dict() and classify MPS kernels in the audit (#4130) - #4138

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4130-test-regressions
Nov 15, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4130-test-regressions

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4130

TL;DR

Two test regressions on main. state_dict() raised AttributeError on every GPTQ kernel built through __new__ without __init__ (Machete was just the first test to reach it), and the kernel-contract audit had no category for platform-restricted kernels, so two newly registered MPS-only MLX kernels made it red. 3 files, +79/−4.

1. AttributeError: 'MacheteLinear' object has no attribute '_qzeros_format'

Root cause

#4103 (0ff46b6) made _save_to_state_dict emit the qzero layout for every GPTQ kernel. The emitted code comes from GPTQQuantLinear._emitted_qzeros_format (gptqmodel/nn_modules/qlinear/__init__.py:1448), which ended in a bare self._qzeros_format read (old line 1450). __init__ assigns that attribute (__init__.py:1327), but modules built through __new__ never run it — the packing tests construct exactly that (tests/test_packing_numeric_boundaries.py:84 _bare). Nothing in the family defaulted, so serialization itself raised.

The sibling reads in the same family were already defensive: _checkpoint_qzeros_format uses getattr(self, "_checkpoint_format_on_disk", None) / getattr(self, "format", None), and _is_runtime_layout uses getattr(self, flag, False) for its three flags. _qzeros_format was the only unguarded read reachable from _emitted_qzeros_format and _write_state_dict_layout_metadata; checkpoint_layout_qzeros and the public qzero_format() getter read the same attribute.

The hole is family-wide, not Machete-specific. On parent 6f9ad69e6, a bare TorchLinear fails the same way:

$ python - <<'PY'   # parent worktree, both classes
TorchLinear  FAIL AttributeError 'TorchLinear' object has no attribute '_qzeros_format'
MacheteLinear FAIL AttributeError 'MacheteLinear' object has no attribute '_qzeros_format'

Fail-before (parent 6f9ad69e6)

$ pytest -q "tests/test_packing_numeric_boundaries.py::test_machete_rejects_offset_overflow_before_repacking[cpu-MacheteLinear]"
tests/test_packing_numeric_boundaries.py:268: in test_machete_rejects_offset_overflow_before_repacking
    before = {name: value.clone() for name, value in module.state_dict().items()}
torch/nn/modules/module.py:2275: in state_dict
    self._save_to_state_dict(destination, prefix, keep_vars)
gptqmodel/nn_modules/qlinear/__init__.py:1508: in _save_to_state_dict
    self._write_state_dict_layout_metadata(destination, prefix)
gptqmodel/nn_modules/qlinear/__init__.py:1496: in _write_state_dict_layout_metadata
    code = self._emitted_layout_code()
gptqmodel/nn_modules/qlinear/__init__.py:1479: in _emitted_layout_code
    return self._emitted_qzeros_format()
gptqmodel/nn_modules/qlinear/__init__.py:1450: in _emitted_qzeros_format
    return int(self._qzeros_format)
AttributeError: 'MacheteLinear' object has no attribute '_qzeros_format'. Did you mean: 'qzero_format'?

Fix

GPTQQuantLinear._runtime_qzeros_format() (gptqmodel/nn_modules/qlinear/__init__.py:1426) returns getattr(self, "_qzeros_format", 1) — the value __init__ installs — and is now the single reader used by _emitted_qzeros_format, checkpoint_layout_qzeros, and the qzero_format() getter, so the whole family shares one default instead of only the attribute the failing test hit.

Pass-after

$ pytest -q "tests/test_packing_numeric_boundaries.py::test_machete_rejects_offset_overflow_before_repacking"
4 passed   # cpu/cuda x MacheteLinear/AwqMacheteLinear

$ pytest -q "tests/test_packing_numeric_boundaries.py::test_bare_gptq_module_state_dict_uses_the_v1_layout_default"
2 passed   # TorchLinear, MacheteLinear  (2 failed on parent)

The new test_bare_gptq_module_state_dict_uses_the_v1_layout_default pins the contract for a portable GPTQ kernel and a CUDA-only one; both params raise the traceback above on parent and pass on the branch.

2. test_audit_covers_every_registered_kernel: two MLX kernels unaccounted for

Root cause

The audit compared the registry against three hand-written sets (tests/test_rocm_kernel_contracts.py:92 old). #4097 registered MacaqMlxQuantLinear (gptqmodel/nn_modules/qlinear/mlx.py:1061) and NF12MlxQuantLinear (mlx.py:1122); both declare SUPPORTS_DEVICES = [DEVICE.MPS] and SUPPORTS_PLATFORM = [PLATFORM.DARWIN], and neither had an inventory entry.

Fail-before (parent 6f9ad69e6)

$ pytest -q tests/test_rocm_kernel_contracts.py::test_audit_covers_every_registered_kernel
>       assert {c.__name__ for c in KERNELS} == ROCM_KERNELS | CUDA_ONLY_KERNELS | OTHER_KERNELS
E       AssertionError: assert {'AWQBitBLASM...2Linear', ...} == {'AWQBitBLASM...2Linear', ...}
E         Extra items in the left set:
E         'MacaqMlxQuantLinear'
E         'NF12MlxQuantLinear'
tests/test_rocm_kernel_contracts.py:97: AssertionError

Fix

The audit now has a platform classification, not a looser allowlist:

  • PLATFORM_RESTRICTED_KERNELS records the two reviewed kernels (tests/test_rocm_kernel_contracts.py:60).
  • _platform_restricted_kernels() (:69) derives the bucket from the shipped declaration: any registered kernel whose SUPPORTS_PLATFORM omits both LINUX and ALL. A newly registered MPS-only kernel is classified automatically instead of re-breaking the test.
  • test_audit_covers_every_registered_kernel (:121) still requires an exact match, now including the derived bucket, and asserts the explicit entries really are platform-restricted.
  • test_platform_restricted_kernels_declare_reachable_families (:131) holds the new bucket to a contract: no LINUX, no DEVICE.ROCM, and no DEVICE.CUDA on a Darwin-only kernel. Without it the derived bucket would let an MPS kernel claim an unreachable family and still pass.

A Linux-capable kernel with no inventory entry still fails the equality — the audit is not widened.

Guard probe (scratch, not committed) against the shipped test functions:

derived includes new mps kernel: True
audit with new mps kernel: PASS (auto-covered)
platform contract with new mps kernel: PASS
bad darwin+cuda: FAIL as intended -> _BadDarwinCudaKernel
unclassified linux kernel: FAIL as intended

Pass-after

$ pytest -q tests/test_rocm_kernel_contracts.py::test_audit_covers_every_registered_kernel \
            tests/test_rocm_kernel_contracts.py::test_platform_restricted_kernels_declare_reachable_families
2 passed

Validation

Both regressions are CPU-reproducible. Parent baselines were captured in a clean git worktree at origin/main (6f9ad69e6).

Command CUDA venv (CPU) ROCm venv (CPU)
pytest -q tests/test_packing_numeric_boundaries.py tests/test_rocm_kernel_contracts.py tests/test_checkpoint_state_invariant.py 577 passed 574 passed, 3 skipped
pytest -q tests/test_checkpoint_compat.py tests/test_hf_packing_api.py 73 passed, 3 skipped 71 passed, 4 skipped, 1 pre-existing failure (below)
pytest -q tests/kernels/test_qlinear_hierarchy.py tests/test_qzero_offsets.py 28 passed —
ruff check gptqmodel docs/eora tests setup.py All checks passed! All checks passed!

Hardware: RTX 4090 GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae (CUDA_VISIBLE_DEVICES pinned) and R9700 gfx1201 (HIP_VISIBLE_DEVICES=0). The suites run on CPU; pinning only keeps device_map="auto" inside a single GPU family. The ROCm skips are the CUDA-only parametrizations (e.g. MacheteLinear on cuda).

Pre-existing, not caused by this PR

  • tests/test_hf_packing_api.py::test_gpu_feasibility_and_best_format fails on ROCm on parent 6f9ad69e6 as well: it expects hf_check_packing_feasibility(..., backend=BACKEND.MARLIN, device="cuda:0") to be true, and Marlin is CUDA-only. Reproduced in the clean parent worktree with the same command.
  • An unpinned run of tests/test_checkpoint_compat.py (no CUDA_VISIBLE_DEVICES) fails test_untied_lm_head_quantizes_and_reloads because device_map="auto" spans the host's 4090s (SM 8.9) and RTX 5090 (SM 12.0) and the selector rejects mixed compute capabilities. Pinning to the 4090 makes it pass; unrelated to this change.
  • Four other kernels still cannot produce a bare state_dict(): ExllamaV2Linear/AwqExllamaV2Linear read _native_shuffled and BitsAndBytesLinear/BitsAndBytesMlxQuantLinear read _cpu_packing_active directly in their own _save_to_state_dict overrides. Those reads exist in 0ff46b6~1 (pre-fix(checkpoint): make state_dict() a real checkpoint for every kernel (#4091) #4103), no test exercises them, and they are outside the _emitted_qzeros_format / _write_state_dict_layout_metadata family this issue names, so they are left untouched.

Scope

Per the issue, the #4121 TeleChat2 regression is another worker's fix; gptqmodel/looper/awq_processor.py and tests/test_telechat2_coverage.py are untouched, as are gptqmodel/models/loader.py, gptqmodel/utils/hf.py, gptqmodel/quantization/, and gptqmodel/looper/*_processor.py.

@Qubitium
Qubitium merged commit 1997993 into main Nov 15, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4130-test-regressions branch November 15, 2026 11:37
Qubitium added a commit that referenced this pull request Nov 15, 2026
…4123) (#4141)

* fix(rocm): pin every Hessian builder and serialize mixed BLAS scopes (#4123)

#4105 scoped the deterministic rocBLAS pin to `ProcessorStep.GPTQ`, which
dropped QQQ (it builds an X^T X Hessian in `add_batch`) and the other Hessian
builders. QQQ then accumulated on hipBLASLt and four runs produced three
distinct checkpoints differing in 490/627 tensors.

- base: gate on a `_HESSIAN_PROCESSOR_STEPS` set (GPTQ, QQQ, VPTQ, MACaQ,
  EXL3, FP8_GSQ) instead of `ProcessorStep.GPTQ`, and always enter the guard:
  Hessian builders pin rocBLAS, AWQ/ParoQuant/RTN pin hipBLASLt.
- rocm: carry a `_QUANT_BLAS_GUARD_TARGET` on the refcounted guard and make a
  mixed-backend run wait for the active one instead of flipping the
  process-global backend mid-run (AWQ vs serial was 319/627 differing).
- rocm: warn for the opt-out spellings (""/0/off/default) too, not only the
  two opt-ins.
- cpp: refresh the staged in-checkout include root every call, because the
  cache fingerprint hashes the staged copy, so a stale stage hid header edits.
- docs: correct "AWQ/RTN/QQQ have no Hessian", add the per-method
  determinism/concurrency tables, and scope the split-K=1 speed claim.

* test(rocm): hash 0-dim checkpoint tensors and record merged-tree counts (#4123)

Current main adds a 0-dim `qzeros_format` scalar to the checkpoint (#4138),
which the cross-process child script could not `view(torch.uint8)`. Flatten
before the view so the scalar is hashed too, and record the merged-tree
confirmation (GPTQ 0/963, QQQ 0/627 over four runs each).

* docs(rocm): correct the guard primitive line range (#4123)

---------

Co-authored-by: GPTQModel Bot <[email protected]>
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