Repository navigation
fix(cpu): rebuild the AVX512 checkpoint qweight instead of cloning it (#4783) - #4845
Merged
Qubitium merged 1 commit intoNov 17, 2026
Merged
Conversation
…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.
This was referenced Nov 17, 2026
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.
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 #4783.
TorchCpuAvx512Linearis what CPUAUTOselects for a bf16 GPTQ model on an AVX512-BF16 host. Itstransform_cpureplacesqweightwith the packed runtime words, but the class declared_FUSED_TRAIN_STATE_REBUILD = None, socapture_fused_train_statekept a full clone of the checkpoint words next to the runtime layout. The AVX512 transform also never calledfinalize_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_int4is the documented inverse ofpack_hf_int4and is already used bydequantize_weight. The newrebuild_avx512_qweight()restores the checkpoint words in pure Torch:ret_idxwhen the transform gathered the input channels (adesc_actcheckpoint), exactly asdequantize_weightdoes;[in_features // pack_factor, out_features]._can_rebuild_checkpoint_qweightadmits the new"avx512_gptq"kind and — unlike the ATen path, which drops the gather — allows a non-identityret_idxbecause the gather is undone here.transform_cpunow ends withfinalize_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.pycovers the identity order and adesc_actpermutation. It asserts the clone is gone, the snapshot points at aRebuiltCheckpointTensor, the rebuilt words equal the captured checkpoint words bitwise, andstate_dict()still emits those words.9cd6f78b5)identityTorchCpuAvx512Linear cannot rebuild its checkpoint qweightdesc_act(non-identityret_idx)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)test_cpu_int4_snapshot_dedup_4395.pyhas 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