Repository navigation
fix(cpu): keep checkpoint layout across inference transforms and fusion - #3765
Merged
Merged
Conversation
A CPU W4A16 inference forward rewrites `qweight`/`qzeros`/`scales` in place into the kernel's int4pack runtime layout, and the AVX512 pair also widen `bias` to FP32. The checkpoint values survived only in the `_fused_train_state` snapshot, so every save path serialized runtime shapes under checkpoint keys. The written file could not be loaded into a fresh model, and `load_state_dict()` into an already-transformed model failed with a size mismatch. A plain forward triggered it even with fusion disabled, and `torch_cpu_avx512_awq` never captured a snapshot at all, so its layout was unrecoverable after one forward. Reproduced on clean base be3852b, so this predates #3762/#3763. - `capture_fused_train_state()` now takes an explicit operand list and clones outside `inference_mode`; an inference-mode clone cannot be restored as a buffer that `load_state_dict` later copies into. - `_load_from_state_dict()` restores the snapshot first, but only when the incoming tensors match the checkpoint shapes; a model's own runtime `state_dict()` keeps round-tripping unchanged. - `get_state_dict_for_save()` restores every transformed module before it collects tensors, which covers `save()` and the writer paths. - `TorchCpuAvx512AwqLinear` captures and restores its snapshot, and both AVX512 classes include the widened `bias` in the captured set. - `FusionKernelSpec.prepare()` captures the snapshot before it finalizes a source for a fused group. Fusion had bypassed the forward-time capture, so a fused model exported int4pack shapes and `fuse(False)` had nothing to restore. - `_FusionGroup.remove()` skips a CPU runtime rebind once an export restored checkpoint operands; rebuilding `scales_and_zeros` from GPTQ codes produced mismatched shapes and infinities. Snapshot buffers are non-persistent and never appear in `state_dict()`. Verified on AVX512-BF16 CPU with torch 2.15.0: the 6-layout checkpoint matrix and the 6-class fused round trip (save -> fresh strict load -> forward) all match pristine shapes and baseline numerics; the 12-layout fusion sweep stays exact at err=0. New regression tests cover both paths. Suites: tests/test_kernel_audit_regressions.py + tests/test_qqq_cpu.py 79 passed; AVX512/AWQ/INT8/writer/offload 243 passed, 3 skipped; module_tree fusion, fusable and module_plan 728 passed, 633 skipped.
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
A CPU W4A16 inference forward rewrites
qweight/qzeros/scalesin place into the kernel's int4pack runtime layout (the AVX512 pair also widensbiasto FP32). The checkpoint values survived only in the_fused_train_statesnapshot, so:state_dict()/get_state_dict_for_save()/save()emitted runtime shapes under checkpoint keys, and the written file could not load into a fresh model;load_state_dict()into an already-transformed model failed with size mismatch;torch_cpu_avx512_awqnever captured a snapshot, so its layout was unrecoverable after one forward.Reproduced on clean base
be3852bf9, so this predates #3762 / #3763 and is unrelated to those merged fixes.A second, related path:
FusionKernelSpec.prepare()finalizes sources outside their ownforward, so fusing a CPU group also bypassed the forward-time capture. A fused model exported int4pack shapes, andfuse(False)had no snapshot to restore.Fix
capture_fused_train_state()takes an explicit operand list and clones outsideinference_mode; an inference-mode clone cannot be restored as a buffer thatload_state_dictlater copies into in place._load_from_state_dict()restores the snapshot before the copy, but only when the incoming tensors match the checkpoint shapes, so a model's own runtimestate_dict()still round-trips.get_state_dict_for_save()restores every transformed module before collecting tensors (coverssave()and writer paths).TorchCpuAvx512AwqLinearcaptures/restores its snapshot; both AVX512 classes include the widenedbias.FusionKernelSpec.prepare()captures the snapshot before finalizing a source;_FusionGroup.remove()skips a CPU runtime rebind once an export restored checkpoint operands (rebuildingscales_and_zerosfrom GPTQ codes produced mismatched shapes and infinities).Snapshot buffers are
persistent=Falseand never appear instate_dict().Tests
New regression tests cover all 6 CPU classes (GPTQ/AWQ × aten/fused/avx512), both for a plain forward and for a fused group:
test_cpu_inference_forward_preserves_checkpoint_save_layoutfails 6/6 without the fix.test_fused_cpu_group_preserves_checkpoint_save_layoutfails 6/6 without the fix.Verified on AVX512-BF16 CPU, torch 2.15.0+cu132, transformers 5.22.2:
save_shapes_ok,reload_ok,numerics_okall True, 0 failures.tests/test_kernel_audit_regressions.py+tests/test_qqq_cpu.py: 79 passed.tests/module_treefusion/fusable/module_plan: 728 passed, 633 skipped.ruff check: clean.