Skip to content

fix(cpu): reject partial-group int4pack and serialize first-forward repack (#4571, #4565) - #4600

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4571-4565-cpu-auto
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4571-4565-cpu-auto

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4571

#4571 — CPU AUTO selected int4pack kernels for partial groups

Root cause

_validate_cpu_int4pack_group_size() only checked that the resolved group size
was in {32, 64, 128, 256}. It did not check
in_features % group_size == 0, which aten::_weight_int4pack_mm_for_cpu
requires. SUPPORTS_AUTO_PADDING = True let the base validation accept GPTQ's
partial final group, so AUTO selected TorchAtenLinear/TorchFusedLinear for
SmolLM2's K=576, gs=128, and the first forward raised
expect K to be divisible by qGroupSize.

Fix

_validate_cpu_int4pack_group_size() now rejects a known in_features that is
not divisible by the resolved group size, so per-module AUTO selection falls
through to a kernel that supports the geometry. TorchCpuAvx512Linear opts out
of the new check (CPU_INT4PACK_REQUIRES_DIVISIBLE_IN_FEATURES = False) because
its 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_forwards
builds K=576, N=64, gs=128, runs per-module AUTO selection, asserts
Aten/Fused are not selected, and runs the first forward.

  • before fix: 1 failed (Aten/Fused selected)
  • after fix: 1 passed

Fixes #4565

#4565 — concurrent first-forward repack was unsynchronised

Root cause

TorchAtenLinear, TorchFusedLinear, TorchAtenAwqLinear,
TorchFusedAwqLinear, and TorchCpuAvx512AwqLinear (plus the inherited
TorchCpuAvx512Linear.forward) checked linear_mode is None and then rewrote
qweight/qzeros/scales in place with no lock. Concurrent first forwards
could observe half-converted tensors or repack already-packed words.

Fix

BaseQuantLinear now owns a process-wide RLock
(_cpu_transform_lock). Every one of those forwards takes it around the
check-and-transform block, re-checks linear_mode under the lock, and publishes
linear_mode last. Readers that see INFERENCE therefore see the finished
tensors. 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 to
    a serial copy (TorchAtenLinear and TorchFusedLinear);

  • a source-level check pins _cpu_transform_lock in all six forward methods
    (including the AWQ and AVX512 variants).

  • before fix: 8 failed

  • after fix: 8 passed

  • with PYTHON_GIL=0: 8 passed

Tests run

CUDA venv (Python 3.14t, torch 2.16.0.dev20261102+cu130), thread caps = 4:

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_cpu_kernel_constraints_4181.py tests/test_cpu_lazy_transform_concurrency_4565.py -q
-> 16 passed
PYTHON_GIL=0 PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_cpu_lazy_transform_concurrency_4565.py -q
-> 8 passed

ROCm venv (torch 2.16.0.dev20261025+rocm10.1), HIP_VISIBLE_DEVICES=3, thread caps = 4:

PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest \
  tests/test_cpu_kernel_constraints_4181.py tests/test_cpu_lazy_transform_concurrency_4565.py -q
-> 16 passed

Lint:

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

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-01 with
AVX512-BF16. No GPU kernel involved.

Limitations

@Qubitium
Qubitium merged commit dcb68ce into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4571-4565-cpu-auto branch November 17, 2026 02:15
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]>
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