Skip to content

fix(cpu): keep the int4pack runtime layout during eval-mode grad forwards (#5509) - #5549

Merged
Qubitium merged 1 commit into
mainfrom
fix/cpu-int4pack-concurrent-layout-5509
Nov 19, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/cpu-int4pack-concurrent-layout-5509

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5509.

Summary

Eval-mode grad forwards on CPU int4pack layers used to restore the checkpoint layout before their dense dequant. That restore rewrites qweight/qzeros in place, so a concurrent no-grad forward reading the fused layout crashed, and the layout flipped on every grad/no-grad pair. TorchAtenLinear/TorchAtenAwqLinear were left permanently broken; TorchFusedLinear/TorchFusedAwqLinear/TorchCpuAvx512Linear failed transiently.

This decodes the live runtime layout for the grad call, the same runtime_int4pack_dense_weight path the compiled branch already uses (#5369), and leaves the fused layout installed. A first-call race is also closed by snapshotting the checkpoint weight under _cpu_transform_lock.

What Changed

  • gptqmodel/nn_modules/qlinear/torch_aten_kernel.py: add _dense_weight_for_activation() / _runtime_dense_weight(). force_dense now sets runtime_dense and decodes the live layout instead of calling restore_fused_train_state(). A grad call that races the first transform snapshots the checkpoint weight under the lock. TorchCpuAvx512Linear overrides _runtime_dense_weight() because its dequantize_weight() already returns checkpoint input order.
  • torch_fused.py, torch_aten_kernel_awq.py, torch_fused_awq.py: same eager-branch change. The fused kernels keep the checkpoint-restore fallback for non-CPU (XPU) devices, where no int4pack runtime decode exists.
  • torch_cpu_avx512_awq.py: grad calls use awq_weight_dequantize(), which already decodes the transformed layout; the forward takes the dense branch for that call without restoring.
  • Tests: test_cpu_trainable_kernels_grad_after_inference_5314.py decodes the expected weight before the first fused forward (the module intentionally stays transformed now) and adds a desc_act eager-grad case. test_cpu_lazy_transform_concurrency_4565.py adds the concurrent grad/no-grad regression for all four runnable kernels.

No API or user-facing behavior changes: a grad call still returns a differentiable output with the same math, and the module now stays on the fast fused layout afterwards.

Tests

# New regression + existing coverage
sh /tmp/t.sh tests/test_cpu_trainable_kernels_grad_after_inference_5314.py \
  tests/test_cpu_fused_eval_grad_latch_5392.py \
  tests/test_cpu_lazy_transform_concurrency_4565.py \
  tests/test_cpu_fused_compiled_grad_5369.py -q
# 54 passed, 9 skipped (AVX512 unavailable on this aarch64 host)

# Wider CPU kernel set
sh /tmp/t.sh tests/kernels/test_intel_cpu_xpu.py tests/kernels/test_awq_cpu_fused_post_init.py \
  tests/test_cpu_fused_eval_grad_latch_5392.py tests/test_cpu_fused_compiled_grad_5369.py tests/test_cpu_pin.py -q
# 42 passed, 18 skipped

# Transform/state invariant set
sh /tmp/t.sh tests/kernels/test_cpu_int4pack_compile_warmup_5515.py \
  tests/kernels/test_cpu_int4pack_scale_offsets_5372.py \
  tests/test_cpu_int4_snapshot_5013.py tests/test_cpu_int4pack_dispatch_contracts.py \
  tests/test_checkpoint_state_invariant.py tests/test_dequantize_model_compute_dtype_4370.py \
  tests/test_dequantize_model_device_4455.py tests/test_packable.py \
  tests/test_training_kernel_inplace_4974.py tests/kernels/test_cpu_fp32_fused_numerics.py -q
# 124 passed, 14 skipped; the 4 AVX512 failures and 5 test_packable FileNotFound errors are identical on clean origin/main
  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

Pre-fix, the new concurrency test fails on clean origin/main for all four kernels: linear_mode returns to None / qweight is rebound (layout flip), and the issue repro raised 18k-29k exceptions (rshift_cpu ... Float, expect B to be uint8). Post-fix: 0 exceptions and the runtime layout stays installed.

Model Support / Module Plan

Not applicable: no model definitions, MODEL_MAP, module trees, or fusion configs change.

AWQ onboarding safety

Not applicable: no model onboarding and no AWQ grouping/search/replay/fold change. The AWQ CPU kernels only change which layout a grad forward decodes.

Logic Flow Documentation

Not applicable as a new flow: this is the existing eager forward branch (force_dense -> dense dequant -> matmul) with the mutating restore replaced by a read-only decode; no resolution or selection order changes.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

Not applicable as a kernel math change: no kernel or quant/dequant math changes. The grad path uses the existing runtime_int4pack_dense_weight decoder that the compiled path already ships, and the eager grad test compares against the checkpoint dequant with 2e-2 tolerance.

Notes

  • Repro on this host (aarch64 GB10, torch 2.11.0): pre-fix gptq-fused 18234 exceptions, gptq-aten 28858 and permanently broken; post-fix 0 exceptions for all four runnable kernels (AVX512 skipped).
  • XPU behavior is unchanged (checkpoint restore remains).
  • The module intentionally keeps linear_mode="inference" after a grad call now; train()/load_state_dict()/state_dict() still restore or rebuild the checkpoint layout through the existing _fused_train_state machinery.

…ards (#5509)

An eval-mode grad forward restored the checkpoint layout before its dense dequant, which rewrote qweight/qzeros while a concurrent no-grad forward was reading the fused layout. Decode the live runtime layout instead, and snapshot the checkpoint weight under _cpu_transform_lock for a first-call race.
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