Skip to content

fix(cpu): rebuild the AVX512 checkpoint qweight instead of cloning it (#4783) - #4845

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
Qubitium:fix/issue-4783-avx512-snapshot
Nov 17, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
Qubitium:fix/issue-4783-avx512-snapshot

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4783.

TorchCpuAvx512Linear is what CPU AUTO selects for a bf16 GPTQ model on an AVX512-BF16 host. Its transform_cpu replaces qweight with the packed runtime words, but the class declared _FUSED_TRAIN_STATE_REBUILD = None, so capture_fused_train_state kept a full clone of the checkpoint words next to the runtime layout. The AVX512 transform also never called finalize_fused_train_state, so even a verified inverse could not have dropped it.

Measured in the issue: 5467 → 9060 MiB resident tensor bytes on Llama-3.1-8B-GPTQ-INT4, of which 3458 MiB was the snapshot.

Change

unpack_hf_int4 is the documented inverse of pack_hf_int4 and is already used by dequantize_weight. The new rebuild_avx512_qweight() restores the checkpoint words in pure Torch:

  1. unpack the runtime words back to codes;
  2. scatter them back through ret_idx when the transform gathered the input channels (a desc_act checkpoint), exactly as dequantize_weight does;
  3. pack the nibbles into [in_features // pack_factor, out_features].

_can_rebuild_checkpoint_qweight admits the new "avx512_gptq" kind and — unlike the ATen path, which drops the gather — allows a non-identity ret_idx because the gather is undone here. transform_cpu now ends with finalize_fused_train_state, which drops the clone only after the rebuilt words match the bytes captured before the transform, so a layout this build cannot invert still keeps its snapshot (fail-closed, unchanged).

Verification

test_cpu_avx512_snapshot_rebuild_4783.py covers the identity order and a desc_act permutation. It asserts the clone is gone, the snapshot points at a RebuiltCheckpointTensor, the rebuilt words equal the captured checkpoint words bitwise, and state_dict() still emits those words.

parent commit (9cd6f78b5) this PR
identity FAIL — TorchCpuAvx512Linear cannot rebuild its checkpoint qweight PASS
desc_act (non-identity ret_idx) FAIL — same PASS
rebuilt words vs captured bytes — bitwise equal, both cases
test_resave_dense_parameter_dtype_4466.py, test_awq_runtime_dtype_storage.py, test_cpu_int4_mmap_detach_4785.py, test_kernel_compile_levels_gptq.py (AVX512/ATen) — 27 passed, 4 skipped

test_cpu_int4_snapshot_dedup_4395.py has one failure (torch_aten); it reproduces identically on the parent commit, so it is pre-existing and unrelated.

CPU-only; run on the x86-64 AVX512-BF16 host.

🤖 Generated with Claude Code

…ModelCloud#4783)

`TorchCpuAvx512Linear` is what CPU `AUTO` selects for a bf16 GPTQ model on an
AVX512-BF16 host. `transform_cpu` replaces `qweight` with the packed runtime
words, but the class declared `_FUSED_TRAIN_STATE_REBUILD = None`, so
`capture_fused_train_state` kept a full clone of the checkpoint words next to
the runtime layout: +63% resident tensor bytes on an 8B model (5467 -> 9060
MiB, of which 3458 MiB was the snapshot). The AVX512 transform also never
called `finalize_fused_train_state`, so even a verified inverse could not have
dropped it.

`unpack_hf_int4` is the documented inverse of `pack_hf_int4` and is already
used by `dequantize_weight`. `rebuild_avx512_qweight` therefore restores the
checkpoint words in pure Torch:

- unpack the runtime words back to codes,
- scatter them back through `ret_idx` when the transform gathered the input
  channels (a `desc_act` checkpoint), exactly as `dequantize_weight` does,
- pack the nibbles into `[in_features // pack_factor, out_features]`.

`_can_rebuild_checkpoint_qweight` admits the new `"avx512_gptq"` kind and, unlike
the ATen path, allows a non-identity `ret_idx` because the gather is undone here.
`transform_cpu` now ends with `finalize_fused_train_state`, which drops the clone
only after the rebuilt words match the bytes captured before the transform, so a
layout this build cannot invert still keeps its snapshot.

Tests
- `test_cpu_avx512_snapshot_rebuild_4783.py` covers the identity order and a
  `desc_act` permutation. It asserts the clone is gone, the snapshot points at a
  `RebuiltCheckpointTensor`, the rebuilt words equal the captured checkpoint
  words bitwise, and `state_dict()` still emits those words. All 4 cases fail on
  the parent commit with `TorchCpuAvx512Linear cannot rebuild its checkpoint
  qweight`.
- `test_resave_dense_parameter_dtype_4466.py`,
  `test_awq_runtime_dtype_storage.py`, `test_cpu_int4_mmap_detach_4785.py` and
  the AVX512/ATen cases of `test_kernel_compile_levels_gptq.py`: 27 passed,
  4 skipped. `test_cpu_int4_snapshot_dedup_4395.py` has one failure
  (`torch_aten`), which reproduces identically on the parent commit.
@Qubitium
Qubitium merged commit 61babf4 into ModelCloud:main Nov 17, 2026
1 check passed
sLaffey pushed a commit to sLaffey/GPTQModel that referenced this pull request Nov 18, 2026
…uilding every qweight (ModelCloud#4884) (ModelCloud#4965)

ModelCloud#4845 dropped the checkpoint `qweight` clone of `TorchCpuAvx512Linear` and
verified the pure-Torch inverse against that clone inside `transform_cpu`.
With the clone gone, every `state_dict()` rebuilt every weight too, and the
inverse itself was a `codes.t()` transpose plus eight strided shift/OR passes
over an int32 expansion.

- Build the checkpoint bytes in one pass over the packed tensor and view four
  of them as one little-endian int32 word, replacing the transpose plus eight
  passes. `unpack_hf_int4` transposes the 16 x half_in block per byte lane
  instead of walking the full 4-D view twice.
- Verify the inverse once per (module class, inverse, packer, shape, layout)
  in the process; later modules of the same layout only check the captured
  shape/dtype. The key carries the inverse and packer identities, so a
  monkeypatched or overridden inverse re-verifies.
- A `desc_act` channel order is applied at the checkpoint-byte level when the
  reorder keeps input pairs adjacent, and at the code level otherwise; both
  emit the same bytes.

The emitted checkpoint stays bitwise identical. On a synthetic 4096x14336
module `rebuild_avx512_qweight` drops from 184 ms to 40 ms (identity) and
235 ms to 75 ms (desc_act); a transformed 1024x8192 module's `state_dict()`
drops from 22.4 ms to 4.6 ms.
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