Skip to content

fix(cpu-int4): rebuild checkpoint qweight instead of keeping a clone (#4395) - #4532

Merged
Qubitium merged 2 commits into
mainfrom
fix/cpu-int4-snapshot-4395
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/cpu-int4-snapshot-4395

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4395 item 1. The fused-train snapshot kept a full checkpoint-layout qweight clone next to the int4pack runtime tensor (2.17x resident on the CPU int4 kernels).

  • capture_fused_train_state now records qweight as a candidate clone when the module declares an invertible int4pack layout.
  • At the end of the transform, finalize_fused_train_state rebuilds 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 on state_dict()/save.
  • A layout this build cannot invert (different PyTorch packing, fused operand order, non-int32 dtype, AVX512/XPU) keeps the resident clone — fail closed, so an unverified inverse can never reach a saved checkpoint.
  • checkpoint_layout_tensor now 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 passed
  • Wider CPU int4/AVX512/fused suite: 15 failures, identical to the untouched baseline (AVX512-BF16 unsupported on this host + one save test), 131 passed
  • Save bytes are byte-identical to the checkpoint; forward output bitwise identical
  • ruff check clean

Residual: AVX512/XPU layouts deliberately keep the snapshot; only the ATen CPU int4pack permutation is inverted here.

Resolves #4395.

…#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
Qubitium force-pushed the fix/cpu-int4-snapshot-4395 branch from cf20425 to 099fb7c Compare November 17, 2026 01:09
…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
Qubitium force-pushed the fix/cpu-int4-snapshot-4395 branch from 099fb7c to 1fae07e Compare November 17, 2026 01:12
@Qubitium
Qubitium merged commit b25a612 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/cpu-int4-snapshot-4395 branch November 17, 2026 01:13
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.
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