Repository navigation
fix(qlinear): serialize state_dict with the CPU int4pack cold transform (#4788) - #4798
Merged
Merged
Conversation
…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).
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.
Fixes #4788.
Problem
The CPU int4pack kernels (
TorchAtenLinear,TorchFusedLinear,TorchCpuAvx512Linearand the AWQ variants) rewrite_bufferson the first eval forward:capture_fused_train_stateregisters the snapshot buffers, the transform rebindsqweight/qzeros/scales_and_zeros, andfinalize_fused_train_statepops theqweightclone.Forward has taken
_cpu_transform_locksince #4600, butstate_dict()did not, so a save racing the first requestcould:
RuntimeError: dictionary changed size during iterationwhilenn.Module._save_to_state_dictiterated a dictbeing resized - with the GIL enabled, in ~25% of first forwards;
qweightunder the checkpoint key, or dropqzeros/scales/bias- a corrupt checkpoint whoseqzeros_formatmarker still says2.Fix
PackedGroupedQuantLinear._save_to_state_dicttakes the same re-entrant lock (it covers theGPTQQuantLinearoverride, which calls
super()).PackedGroupedQuantLinear._load_from_state_dicttakes it too:restore_fused_train_staterebinds the same buffers,so a save must not interleave with a concurrent load.
finalize_fused_train_statepublishes theRebuiltCheckpointTensordescriptor before dropping the clone, so areader outside the lock never resolves a snapshot name whose buffer is already gone.
The lock is the process-wide
_cpu_transform_lockthat the forwards already share (one transform per module, not a hotpath), 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 hammerstate_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 aconcurrent
state_dict()blocks instead of returning a half-built dict (AVX512 skips itself viavalidate_once()on hosts without it).
Red on unmodified
main(b22245774):test_state_dict_during_first_forward_is_consistent[aten],[fused],[aten-awq]fail withdictionary changed size during iteration, and all four lock cases fail because thereader 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 fourpre-existing
torch_cpu_avx512failures, which are identical onmainhere.ruff checkclean.