Skip to content

fix(cpu): keep checkpoint layout across inference transforms and fusion - #3765

Merged
Qubitium merged 3 commits into
mainfrom
fix/checkpoint-io-runtime-layout
Nov 11, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/checkpoint-io-runtime-layout

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

A CPU W4A16 inference forward rewrites qweight/qzeros/scales in place into the kernel's int4pack runtime layout (the AVX512 pair also widens bias to FP32). The checkpoint values survived only in the _fused_train_state snapshot, 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;
  • a plain forward triggered it even with fusion disabled;
  • torch_cpu_avx512_awq never 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 own forward, so fusing a CPU group also bypassed the forward-time capture. A fused model exported int4pack shapes, and fuse(False) had no snapshot to restore.

Fix

  • capture_fused_train_state() 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 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 runtime state_dict() still round-trips.
  • get_state_dict_for_save() restores every transformed module before collecting tensors (covers save() and writer paths).
  • TorchCpuAvx512AwqLinear captures/restores its snapshot; both AVX512 classes include the widened bias.
  • FusionKernelSpec.prepare() captures the snapshot before finalizing a source; _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 persistent=False and never appear in state_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_layout fails 6/6 without the fix.
  • test_fused_cpu_group_preserves_checkpoint_save_layout fails 6/6 without the fix.

Verified on AVX512-BF16 CPU, torch 2.15.0+cu132, transformers 5.22.2:

  • 6-layout checkpoint matrix: save_shapes_ok, reload_ok, numerics_ok all True, 0 failures.
  • 6-class fused round trip (fuse → save → fresh strict load → forward): shapes pristine, numerics within 2e-3.
  • 12-layout fusion sweep: exact, err=0.0e+00.
  • tests/test_kernel_audit_regressions.py + tests/test_qqq_cpu.py: 79 passed.
  • AVX512/AWQ/INT8/writer/offload: 243 passed, 3 skipped.
  • tests/module_tree fusion/fusable/module_plan: 728 passed, 633 skipped.
  • ruff check: clean.

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.
@Qubitium
Qubitium merged commit c434a93 into main Nov 11, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/checkpoint-io-runtime-layout branch November 11, 2026 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant