Repository navigation
fix(cpu): keep the int4pack runtime layout during eval-mode grad forwards (#5509) - #5549
Merged
Merged
Conversation
…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.
This was referenced Nov 19, 2026
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 #5509.
Summary
Eval-mode grad forwards on CPU int4pack layers used to restore the checkpoint layout before their dense dequant. That restore rewrites
qweight/qzerosin place, so a concurrent no-grad forward reading the fused layout crashed, and the layout flipped on every grad/no-grad pair.TorchAtenLinear/TorchAtenAwqLinearwere left permanently broken;TorchFusedLinear/TorchFusedAwqLinear/TorchCpuAvx512Linearfailed transiently.This decodes the live runtime layout for the grad call, the same
runtime_int4pack_dense_weightpath 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_densenow setsruntime_denseand decodes the live layout instead of callingrestore_fused_train_state(). A grad call that races the first transform snapshots the checkpoint weight under the lock.TorchCpuAvx512Linearoverrides_runtime_dense_weight()because itsdequantize_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 useawq_weight_dequantize(), which already decodes the transformed layout; the forward takes the dense branch for that call without restoring.test_cpu_trainable_kernels_grad_after_inference_5314.pydecodes the expected weight before the first fused forward (the module intentionally stays transformed now) and adds adesc_acteager-grad case.test_cpu_lazy_transform_concurrency_4565.pyadds 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
Pre-fix, the new concurrency test fails on clean
origin/mainfor all four kernels:linear_modereturns toNone/qweightis 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
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_weightdecoder that the compiled path already ships, and the eager grad test compares against the checkpoint dequant with 2e-2 tolerance.Notes
gptq-fused18234 exceptions,gptq-aten28858 and permanently broken; post-fix 0 exceptions for all four runnable kernels (AVX512 skipped).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_statemachinery.