Skip to content

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

Closed

Description

@Qubitium

Found on main @ c2b0bf6, x86-64 CPU (RTX 4090 host, no GPU used).

What is wrong

#4949 (fix of #4907) made three CPU int4pack kernels clear their latched linear_mode == "train" in train(False), because "a forward taken while the module was in training mode pinned it to dense dequantization for the rest of its life, so a later eval() could never recover": TorchAtenLinear, TorchAtenAwqLinear and TorchFusedLinear. The fourth sibling with the same latch, TorchFusedAwqLinear (gptqmodel/nn_modules/qlinear/torch_fused_awq.py, train() ~line 422), was not changed. Its train() only resets the latch when switching to training from INFERENCE; eval() after a training-mode forward keeps LinearMode.TRAIN forever, so every later inference_mode forward dequantizes the full weight.

(TorchCpuAvx512Linear / TorchCpuAvx512AwqLinear reset on any mode change, so they are fine.)

Scenario: load an AWQ checkpoint on CPU with the torch-fused AWQ kernel, run any forward in training mode (LoRA fine-tune step, model.train() evaluation loop, etc.), then model.eval() for generation.

Repro

import time, torch
from gptqmodel import GPTQModel, BACKEND
from gptqmodel.nn_modules.qlinear import BaseQuantLinear
m = GPTQModel.load("AWQ-Llama-3.2-1B-g128-gemm", backend=BACKEND.AWQ_TORCH_FUSED, device="cpu", dtype=torch.float16)
q = [x for x in m.model.modules() if isinstance(x, BaseQuantLinear)]
ids = torch.randint(100, 1000, (1, 32))
m.model.train()
with torch.no_grad(): m.model(ids)          # one forward in training mode
m.model.eval()
with torch.inference_mode():
    m.model(ids); t = time.perf_counter(); m.model(ids); m.model(ids)
print(type(q[0]).__name__, (time.perf_counter() - t) / 2, {str(x.linear_mode) for x in q})

8 threads, M=32 tokens, same process setup for both rows:

kernel / dtype fresh load, eval() train() forward, then eval()
TorchFusedAwqLinear fp16 6.56 s/fwd, {INFERENCE} 208.8 s/fwd, {TRAIN} (32x)
TorchFusedAwqLinear bf16 6.69 s/fwd, {INFERENCE} 1.87 s/fwd, {TRAIN}
TorchAtenAwqLinear bf16 (fixed by #4949) 6.71 s/fwd, {inference} 6.90 s/fwd, {inference}

Suggested fix

Apply the #4949 change to TorchFusedAwqLinear.train() too:

elif not mode and self.linear_mode == LinearMode.TRAIN:
    self.linear_mode = None

(or reset on any mode change, as the AVX512 kernels do).

Test that should have caught it

tests/test_hf_awq_repair_train_mode_4907.py covers the three patched kernels; parametrize the "train forward, then eval recovers the inference layout" node over every CPU int4pack kernel class, including TorchFusedAwqLinear.

Severity

Medium: silent 32x slowdown for fp16 CPU AWQ inference after any training-mode forward on the torch-fused AWQ kernel; outputs stay correct.

Side note from the same measurement, not part of this bug: bf16 TorchFusedAwqLinear/TorchAtenAwqLinear int4pack inference (6.7 s/fwd) was 3.6x slower than the dense-dequant path (1.87 s/fwd) at M=32 on this host (load average ~40, so treat the absolute numbers as indicative).

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-5018 is working on this issue now.

  2. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent release: agent-root-5018 finished this issue. Fix: #5036, squash-merged as 84ab448e9.

    TorchFusedAwqLinear.train() now clears the latched LinearMode.TRAIN when the module leaves training mode, mirroring the #4949 rule already present in TorchAtenLinear, TorchAtenAwqLinear and TorchFusedLinear. The dense-dequant path holds no derived state, so eval() lets the next forward choose the fused layout again instead of pinning the module to full weight dequantization for the rest of the process. Entering training still calls restore_fused_train_state() unchanged.

    Family sweep (grep -n "LinearMode.TRAIN" gptqmodel/nn_modules/qlinear/*.py): TorchFusedAwqLinear was the only remaining one-way transition; the AVX512 siblings already reset on any mode change and the other three carry #4949, so no further kernel needed a change.

    New tests/test_awq_torch_fused_mode_5018.py parametrizes all six int4pack kernels and adds an end-to-end node that compares the recovered inference_mode forward bitwise (torch.equal) against a fresh eval() module, plus a node pinning that the training forward still takes the dense path. Red on unmodified main @ 0ff0c0ebc (3 failed, 4 passed, 2 skipped — the AVX512 nodes skip on this non-AVX512-BF16 host) -> 7 passed, 2 skipped with the fix. Neighbouring suites after merging main (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): 72 passed, 41 skipped. All 9 CI checks pass, and the new file runs in the AWQ CPU unit lane (its path filter now includes torch_fused_awq.py).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions