Skip to content

fix(qlinear): serialize state_dict with the CPU int4pack cold transform (#4788) - #4798

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4788-state-dict-lock
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4788-state-dict-lock

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4788.

Problem

The CPU int4pack kernels (TorchAtenLinear, TorchFusedLinear, TorchCpuAvx512Linear and the AWQ variants) rewrite
_buffers on the first eval forward: capture_fused_train_state registers the snapshot buffers, the transform rebinds
qweight/qzeros/scales_and_zeros, and finalize_fused_train_state pops the qweight clone.

Forward has taken _cpu_transform_lock since #4600, but state_dict() did not, so a save racing the first request
could:

  • raise RuntimeError: dictionary changed size during iteration while nn.Module._save_to_state_dict iterated a dict
    being resized - with the GIL enabled, in ~25% of first forwards;
  • under free-threading, emit the runtime int4pack qweight under the checkpoint key, or drop
    qzeros/scales/bias - a corrupt checkpoint whose qzeros_format marker still says 2.

Fix

  • PackedGroupedQuantLinear._save_to_state_dict takes the same re-entrant lock (it covers the GPTQQuantLinear
    override, which calls super()).
  • PackedGroupedQuantLinear._load_from_state_dict takes it too: restore_fused_train_state rebinds the same buffers,
    so a save must not interleave with a concurrent load.
  • finalize_fused_train_state publishes the RebuiltCheckpointTensor descriptor before dropping the clone, so a
    reader outside the lock never resolves a snapshot name whose buffer is already gone.

The lock is the process-wide _cpu_transform_lock that the forwards already share (one transform per module, not a hot
path), so no new lock object, no pickling concerns, no per-forward cost.

Tests (tests/test_cpu_lazy_transform_concurrency_4565.py)

  • test_state_dict_during_first_forward_is_consistent - ATen/Fused x GPTQ/AWQ: two threads hammer state_dict()
    while the first forward runs the transform; every tensor must match the checkpoint layout (no exception, no runtime
    words, no missing keys).
  • test_state_dict_waits_for_the_shared_transform_lock - all six CPU int4pack classes: hold the lock and prove a
    concurrent state_dict() blocks instead of returning a half-built dict (AVX512 skips itself via validate_once()
    on hosts without it).

Red on unmodified main (b22245774): test_state_dict_during_first_forward_is_consistent[aten],
[fused], [aten-awq] fail with dictionary changed size during iteration, and all four lock cases fail because the
reader returns immediately.

Green on this branch: 16 passed, 2 skipped (AVX512 unavailable on this aarch64 host). The neighbouring suites
(test_cpu_int4_snapshot_dedup_4395, test_save_state_dict_buffer_collection, test_checkpoint_state_invariant,
test_cpu_int4pack_dispatch_contracts, test_resave_dense_parameter_dtype_4466) give 77 passed with only the four
pre-existing torch_cpu_avx512 failures, which are identical on main here. ruff check clean.

…rm (#4788)

The CPU int4pack kernels rewrite `_buffers` on their first eval forward:
`capture_fused_train_state` registers the snapshot buffers, the transform
rebinds `qweight`/`qzeros`/`scales_and_zeros`, and `finalize_fused_train_state`
pops the `qweight` clone. Forward has taken `_cpu_transform_lock` since #4600,
but `state_dict()` did not, so a save racing the first request could

* raise `RuntimeError: dictionary changed size during iteration` while
  `nn.Module._save_to_state_dict` iterated a dict being resized (GIL on, ~25%
  of first forwards), or
* under free-threading, emit the runtime int4pack `qweight` under the
  checkpoint key, or drop `qzeros`/`scales`/`bias` - a corrupt checkpoint that
  still carries `qzeros_format=2`.

`PackedGroupedQuantLinear._save_to_state_dict` now takes the same re-entrant
lock, and `_load_from_state_dict` (which runs `restore_fused_train_state`) does
too, so a save cannot interleave with a concurrent load either.

`finalize_fused_train_state` publishes the `RebuiltCheckpointTensor` descriptor
before dropping the clone, so even a reader outside the lock never resolves a
snapshot name whose buffer is already gone.

Tests: `tests/test_cpu_lazy_transform_concurrency_4565.py` gains

* `test_state_dict_during_first_forward_is_consistent` (ATen/Fused, GPTQ and
  AWQ): hammers `state_dict()` from two threads while the first forward runs
  the transform, and requires every tensor to match the checkpoint layout -
  no exception, no runtime-layout words, no missing keys.
* `test_state_dict_waits_for_the_shared_transform_lock` (all six CPU int4pack
  classes): holds the lock and proves a concurrent `state_dict()` blocks.

Red on unmodified main (b222457): both new cases fail - the two GPTQ
concurrency cases and the AWQ one with `dictionary changed size during
iteration`, and all four lock cases with the reader returning immediately.
Green here: 16 passed, 2 skipped (AVX512 kernels are unavailable on this
aarch64 host; those four `test_checkpoint_state_invariant` AVX512 failures are
identical on main).
@Qubitium
Qubitium merged commit b364a08 into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4788-state-dict-lock branch November 17, 2026 15:19
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