Repository navigation
fix(cpu-int4): rebuild checkpoint qweight instead of keeping a clone (#4395) - #4532
Merged
Merged
Conversation
…#4395) The CPU int4pack transform kept a byte-identical clone of `scales` in the checkpoint snapshot and a standalone `qzeros` copy of the zero column already stored in `scales_and_zeros`. Keep `scales` live and bind `qzeros` as a view of the packed affine metadata, so the resident snapshot only holds the operands the transform actually rewrites. Fused sources keep their fused views instead of re-pointing `qzeros` at a private copy. The `qweight` checkpoint snapshot stays: PyTorch's `_convert_weight_to_int4pack_for_cpu` nibble layout is internal and shape-dependent with no inverse op, and TorchFused also has a distinct XPU packing. An unverified inverse would silently corrupt saved checkpoints. `state_dict()`/save bytes and forward outputs are bitwise identical to origin/main for TorchFused, TorchAten and their AWQ variants. Resident bytes after the first forward: - 2048x8192 4-bit layer: 18,747,432 -> 18,223,144 (-512 KiB) - SmolLM2-135M, 211 int4 linears: 223,262,072 -> 214,857,080 (-8.0 MiB)
Qubitium
force-pushed
the
fix/cpu-int4-snapshot-4395
branch
from
November 17, 2026 01:09
cf20425 to
099fb7c
Compare
…4395) The fused-train snapshot kept a full checkpoint-layout qweight clone next to the int4pack runtime tensor (2.17x resident). Capture it as a candidate, prove the inverse bitwise at the end of the transform, and only then drop the clone and rebuild the checkpoint words on state_dict()/save. A layout this build cannot invert keeps the clone (fail closed).
Qubitium
force-pushed
the
fix/cpu-int4-snapshot-4395
branch
from
November 17, 2026 01:12
099fb7c to
1fae07e
Compare
This was referenced Nov 17, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 17, 2026
The CPU inference path built a hidden `TorchAtenAwqLinear` child that cloned every packed buffer, then left the checkpoint `qweight`/`qzeros` resident alongside the kernel operands. A Paro CPU layer therefore held both layouts at once: ~2.07x (fp16) / ~2.10x (bf16) the checkpoint bytes after the first forward. Reuse the existing `_fused_train_state` machinery (#4532/#4788) instead of a second module. `ParoLinear` now rebinds `qweight`/`qzeros` to the packed operands of the layout it is actually running and rebuilds the checkpoint words for `state_dict()`/save from a verified inverse; the AVX512 and ATen paths each own exactly one layout, and switching layouts, `.to()`, `train()`, the dense fallback, a checkpoint load and a weight cache clear all restore the checkpoint layout first. Scales and bias keep their checkpoint dtype and are widened per call for the AVX512 kernel. Resident bytes after the first forward (Qwen-like 512->256, g128, krot=2): ATen 1.05x quantized / 1.17x total (fp16), 1.20x (bf16); AVX512 1.02x quantized. The remaining ATen overhead is the FP32 `scales_and_zeros` operand the op requires. `state_dict()` stays bit-exact for all four tensors on both paths.
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.
Summary
Fixes #4395 item 1. The fused-train snapshot kept a full checkpoint-layout
qweightclone next to the int4pack runtime tensor (2.17x resident on the CPU int4 kernels).capture_fused_train_statenow recordsqweightas a candidate clone when the module declares an invertible int4pack layout.finalize_fused_train_staterebuilds the checkpoint words from the live packed tensor and compares them bitwise with the clone. Only an exact match drops the clone and keeps a descriptor that rebuilds onstate_dict()/save.checkpoint_layout_tensornow materializes rebuilt entries, so the [BUG] Checkpoint export still loses dense tensor dtypes and copies transformed CPU runtime operands #4466 repack path keeps using canonical checkpoint tensors.Validation
pytest tests/test_cpu_int4_snapshot_dedup_4395.py tests/test_resave_dense_parameter_dtype_4466.py -q→ 26 passedruff checkcleanResidual: AVX512/XPU layouts deliberately keep the snapshot; only the ATen CPU int4pack permutation is inverted here.
Resolves #4395.