Repository navigation
fix(cpu): reject partial-group int4pack and serialize first-forward repack (#4571, #4565) - #4600
Merged
Merged
Conversation
This was referenced Nov 17, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 17, 2026
…rm (#4788) (#4798) 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). Co-authored-by: Qubitium <[email protected]>
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 #4571
#4571 — CPU AUTO selected int4pack kernels for partial groups
Root cause
_validate_cpu_int4pack_group_size()only checked that the resolved group sizewas in
{32, 64, 128, 256}. It did not checkin_features % group_size == 0, whichaten::_weight_int4pack_mm_for_cpurequires.
SUPPORTS_AUTO_PADDING = Truelet the base validation accept GPTQ'spartial final group, so AUTO selected
TorchAtenLinear/TorchFusedLinearforSmolLM2's
K=576, gs=128, and the first forward raisedexpect K to be divisible by qGroupSize.Fix
_validate_cpu_int4pack_group_size()now rejects a knownin_featuresthat isnot divisible by the resolved group size, so per-module AUTO selection falls
through to a kernel that supports the geometry.
TorchCpuAvx512Linearopts outof the new check (
CPU_INT4PACK_REQUIRES_DIVISIBLE_IN_FEATURES = False) becauseits kernel consumes the ragged final group; on an AVX512 host it remains
selectable, otherwise selection falls to
TorchLinear.Regression
tests/test_cpu_kernel_constraints_4181.py::test_auto_partial_group_geometry_avoids_int4pack_and_forwardsbuilds
K=576, N=64, gs=128, runs per-module AUTO selection, assertsAten/Fused are not selected, and runs the first forward.
Fixes #4565
#4565 — concurrent first-forward repack was unsynchronised
Root cause
TorchAtenLinear,TorchFusedLinear,TorchAtenAwqLinear,TorchFusedAwqLinear, andTorchCpuAvx512AwqLinear(plus the inheritedTorchCpuAvx512Linear.forward) checkedlinear_mode is Noneand then rewroteqweight/qzeros/scalesin place with no lock. Concurrent first forwardscould observe half-converted tensors or repack already-packed words.
Fix
BaseQuantLinearnow owns a process-wideRLock(
_cpu_transform_lock). Every one of those forwards takes it around thecheck-and-transform block, re-checks
linear_modeunder the lock, and publisheslinear_modelast. Readers that seeINFERENCEtherefore see the finishedtensors. The lock is a class attribute, so it is not pickled/deep-copied with a
module and the cold transform still runs once per module.
Regression
tests/test_cpu_lazy_transform_concurrency_4565.py:a real 8-thread barrier repro runs the first eval forward of one module and
asserts exactly one
transform()call, no thread errors, and output equal toa serial copy (
TorchAtenLinearandTorchFusedLinear);a source-level check pins
_cpu_transform_lockin all six forward methods(including the AWQ and AVX512 variants).
before fix: 8 failed
after fix: 8 passed
with
PYTHON_GIL=0: 8 passedTests run
CUDA venv (Python 3.14t, torch 2.16.0.dev20261102+cu130), thread caps = 4:
ROCm venv (torch 2.16.0.dev20261025+rocm10.1),
HIP_VISIBLE_DEVICES=3, thread caps = 4:Lint:
Final run was after
git fetch origin main && git merge --no-edit origin/main(
97c377509).Hardware
CPU-only host-side verification; AMD Eng Sample
100-000001458-01withAVX512-BF16. No GPU kernel involved.
Limitations
K=576, gs=128),not a full SmolLM2 checkpoint.
make the check-then-act deterministic; it does not rerun the issue's
Qwen2.5-0.5B checkpoint stress. AWQ variants are covered by the shared lock
and the source-level assertion, not by a dedicated threaded AWQ forward.