Repository navigation
fix(awq): clear the int4pack training latch in TorchFusedAwqLinear (#5018) - #5036
Merged
Merged
Conversation
…5018) `TorchFusedAwqLinear.train()` only reset `linear_mode` when switching *to* training from `INFERENCE`. The CPU int4pack kernels latch the layout on the first forward, so a forward taken while the module was in training mode (`model.train()`, a LoRA step) pinned it to `LinearMode.TRAIN` for the rest of the process: `eval()` could not recover and every later `inference_mode` forward dequantized the full weight (AWQ-Llama-3.2-1B-g128-gemm fp16: 6.56 s/fwd -> 208.8 s/fwd). #4949 taught the three sibling kernels (`TorchAtenLinear`, `TorchAtenAwqLinear`, `TorchFusedLinear`) to clear the latch when leaving training; this applies the same rule to the fourth member of the family. The dense-dequant path holds no derived state, so clearing the latch lets the next forward choose the fused layout again. Entering training still restores the fused train state exactly as before. A sweep of the rest of the family found no other one-way transition: the AVX512 siblings already reset on any mode change, and `TorchFusedLinear`, `TorchAtenLinear` and `TorchAtenAwqLinear` carry the #4949 rule.
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.
Summary
Fixes #5018.
TorchFusedAwqLinear.train()only reset its latchedlinear_modewhen switching to training fromINFERENCE:The CPU int4pack kernels choose their layout from
self.trainingon the firstforward and then latch it, so a forward taken while the module was in training
mode pinned it to
LinearMode.TRAINfor the rest of the process.eval()couldnever recover, and every later
inference_modeforward dequantized the fullweight — the issue measured 6.56 s/fwd → 208.8 s/fwd (32x) on
AWQ-Llama-3.2-1B-g128-gemm fp16, while
TorchAtenAwqLinearstayed at6.71 → 6.90 s/fwd.
#4949 fixed this one-way transition in
TorchAtenLinear,TorchAtenAwqLinearand
TorchFusedLinear;TorchFusedAwqLinear— the fourth member of the family —was missed.
What Changed
gptqmodel/nn_modules/qlinear/torch_fused_awq.py:train(False)now clearsa
TRAINlatch, mirroring the fix(load): keep a repaired AWQ module in the model's mode (#4907) #4949 rule and its comment. The dense-dequantpath holds no derived state, so clearing the latch lets the next forward
choose the fused layout again; entering training still calls
restore_fused_train_state()exactly as before.tests/test_awq_torch_fused_mode_5018.py(new, 4 test functions / 7 nodes +2 AVX512 skips) and the AWQ CPU
unitCI suite runs it; the workflow's pathfilter now includes
torch_fused_awq.pyso the file is not skipped on achange to the module it covers.
I also swept the rest of the int4pack family for the same one-way transition
(
grep -n "LinearMode.TRAIN" gptqmodel/nn_modules/qlinear/*.py):eval()after atrain()forwardTorchFusedAwqLinearTRAINTorchFusedLinearTorchAtenLinearTorchAtenAwqLinearTorchCpuAvx512Linear/TorchCpuAvx512AwqLinearNo other kernel needed a change; the test parametrizes all six so the family
contract is pinned rather than just the one bug.
Tests
tests/test_awq_torch_fused_mode_5018.pyon unmodifiedorigin/main(
0ff0c0ebc, file copied into a separate worktree): 3 failed, 4 passed,2 skipped.
test_int4pack_family_clears_the_train_latch_on_eval[TorchFusedAwqLinear]—TorchFusedAwqLinear kept the TRAIN latch across eval(). The other fiveparametrizations pass (four sibling kernels + the two AVX512 nodes skip on
this non-AVX512-BF16 host).
test_torch_fused_awq_recovers_the_fused_path_after_a_training_forward—a forward taken in training mode pinned the module to the dense path.test_torch_fused_awq_alternating_modes_stay_correct— the same, over threetrain/eval cycles.
test_torch_fused_awq_training_forward_still_uses_the_dense_pathpasses onboth trees: it guards that the fix does not change training behaviour (no
torch_fused_op/scales_and_zerosbuilt on the dense path).On this branch: 7 passed, 2 skipped (the AVX512 kernels cannot run on this
host; they are covered by their own suites elsewhere).
The end-to-end node is stronger than a mode assertion: it compares the
recovered
inference_modeforward bitwise against a freshly constructedeval()module (torch.equal), so it fails both when the latch sticks and ifthe fused path were rebuilt against stale derived state.
Neighbouring suites (branch vs unmodified
origin/main, same command):origin/maintest_awq_torch_fused_mode_5018.py,test_cpu_kernel_optimize.py,test_hf_awq_repair_train_mode_4907.py,test_train_mode_atomicity.py,test_torch_fused_asym_qzeros.py,test_inference_fp16_overflow.py,kernels/test_awq_cpu_fused_post_init.py,kernels/test_awq_runtime_dtype_storage.pykernels/test_awq.py,kernels/test_awq_torch_fused.py,kernels/test_kernel_compile_levels_awq.py,test_cpu_lazy_transform_concurrency_4565.py,test_torch_cpu_avx512.py,test_torch_cpu_avx512_awq.pyAfter merging
origin/mainup to0ff0c0ebc(which includes #5033'stest_cpu_int4_snapshot_5013.pyand #5035), the combined set(
test_awq_torch_fused_mode_5018.py,test_cpu_int4_snapshot_5013.py,test_cpu_kernel_optimize.py,test_hf_awq_repair_train_mode_4907.py,test_train_mode_atomicity.py,test_torch_fused_asym_qzeros.py,test_inference_fp16_overflow.py,kernels/test_awq_cpu_fused_post_init.py,kernels/test_awq_runtime_dtype_storage.py,kernels/test_awq.py,kernels/test_kernel_compile_levels_awq.py) is 72 passed, 41 skipped, andthe red proof still holds at that revision.
The only delta in the first row is the three new nodes; no pre-existing failure
was touched.
ruff checkclean on both changed Python files.Model Support / Module Plan
No model or module-plan change. The fix is kernel-local
(
gptqmodel/nn_modules/qlinear/torch_fused_awq.py) and applies to any modelloaded with
backend="awq_torch_fused"on CPU.Logic Flow Documentation
No new doc page: this restores the documented
linear_modecontract that#4949 already wrote down (
train()clears the latch when leaving training), sothe existing comment block is extended with the #5018 reference instead of
duplicating the rule.
Kernel Accuracy Requirements
No numerical change. The training forward still takes the dense dequant path
(
awq_weight_dequantize), and the recovered inference forward is assertedbitwise equal to a fresh
eval()module. Inference-only workloads neverobserve a difference: the latch is only cleared when it was set.
Notes
skip with a precise reason rather than reporting a pass.
checkpoint and ~200 s/fwd); the test pins the mode transition and the
resulting path choice, which is the mechanism behind the 32x.