Skip to content

fix(awq): clear the int4pack training latch in TorchFusedAwqLinear (#5018) - #5036

Merged
Qubitium merged 2 commits into
mainfrom
root/torchfusedawq-mode-5018
Nov 18, 2026
Merged

Qubitium merged 2 commits into
mainfrom
root/torchfusedawq-mode-5018

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5018. TorchFusedAwqLinear.train() only reset its latched
linear_mode when switching to training from INFERENCE:

if mode and self.linear_mode == LinearMode.INFERENCE:
    restore_fused_train_state(self)
    self.linear_mode = None
    ...
return super().train(mode=mode)

The CPU int4pack kernels choose their layout from self.training on the first
forward and then latch it, so a forward taken while the module was in training
mode pinned it to LinearMode.TRAIN for the rest of the process. eval() could
never recover, and every later inference_mode forward dequantized the full
weight — the issue measured 6.56 s/fwd → 208.8 s/fwd (32x) on
AWQ-Llama-3.2-1B-g128-gemm fp16, while TorchAtenAwqLinear stayed at
6.71 → 6.90 s/fwd.

#4949 fixed this one-way transition in TorchAtenLinear, TorchAtenAwqLinear
and TorchFusedLinear; TorchFusedAwqLinear — the fourth member of the family —
was missed.

What Changed

  • gptqmodel/nn_modules/qlinear/torch_fused_awq.py: train(False) now clears
    a TRAIN latch, mirroring the fix(load): keep a repaired AWQ module in the model's mode (#4907) #4949 rule and its comment. The dense-dequant
    path 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 unit CI suite runs it; the workflow's path
    filter now includes torch_fused_awq.py so the file is not skipped on a
    change 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):

kernel eval() after a train() forward status
TorchFusedAwqLinear stayed TRAIN fixed here
TorchFusedLinear cleared #4949
TorchAtenLinear cleared #4949
TorchAtenAwqLinear cleared #4949
TorchCpuAvx512Linear / TorchCpuAvx512AwqLinear clear on any mode change already correct

No 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.py on unmodified origin/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 five
    parametrizations 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 three
    train/eval cycles.
  • test_torch_fused_awq_training_forward_still_uses_the_dense_path passes on
    both trees: it guards that the fix does not change training behaviour (no
    torch_fused_op / scales_and_zeros built 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_mode forward bitwise against a freshly constructed
eval() module (torch.equal), so it fails both when the latch sticks and if
the fused path were rebuilt against stale derived state.

Neighbouring suites (branch vs unmodified origin/main, same command):

files origin/main this branch
test_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.py 3 failed, 45 passed, 14 skipped 48 passed, 14 skipped
kernels/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.py — 42 passed, 99 skipped

After merging origin/main up to 0ff0c0ebc (which includes #5033's
test_cpu_int4_snapshot_5013.py and #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, and
the 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 check clean 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 model
loaded with backend="awq_torch_fused" on CPU.

Logic Flow Documentation

No new doc page: this restores the documented linear_mode contract that
#4949 already wrote down (train() clears the latch when leaving training), so
the 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 asserted
bitwise equal to a fresh eval() module. Inference-only workloads never
observe a difference: the latch is only cleared when it was set.

Notes

  • The AVX512 siblings could not be exercised on this host (no AVX512-BF16); they
    skip with a precise reason rather than reporting a pass.
  • The issue's wall-clock table was not re-measured (it needs an 8B AWQ
    checkpoint and ~200 s/fwd); the test pins the mode transition and the
    resulting path choice, which is the mechanism behind the 32x.

…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.
@Qubitium
Qubitium merged commit 84ab448 into main Nov 18, 2026
9 checks passed
@Qubitium
Qubitium deleted the root/torchfusedawq-mode-5018 branch November 18, 2026 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PERF] #4949 (fix of #4907) incomplete: TorchFusedAwqLinear still latches the dense TRAIN path across eval() (fp16 CPU AWQ 6.6 -> 209 s/forward)

1 participant