Skip to content

[PERF] #4839 (fix of #4795): plain-Transformers dynamic AWQ repair leaves every rebuilt quant module in training mode under an eval model; CPU kernels latch the dense 'train' path (4.1x slower forward, never int4pack) #4907

Closed

Description

@Qubitium

Found on main @ 5cded90, x86-64 AVX512-BF16 CPU + RTX 4090, transformers 5.22.

What is wrong

#4839 (fix of #4795) repairs a plain AutoModelForCausalLM.from_pretrained load of a dynamic AWQ checkpoint in the post-weight hook. hf_gptqmodel_repair_awq_load (gptqmodel/utils/hf_load.py) builds fresh quant modules through create_quant_layer/create_quant_module(transfer_from=...) and swaps them into the model. A freshly constructed nn.Module has training=True, and nothing resets it to the mode of the module it replaces or of the model. The model itself reports model.training == False, while every repaired quant linear stays in training mode.

Output is now correct (see below). But the CPU int4pack kernels decide their path on the first forward from self.training (not self.training and not x.requires_grad ...), and then latch linear_mode = "train". The repaired model therefore never packs to int4pack and dequantizes every weight on every forward, even under torch.inference_mode(). Because the mode is latched, a later model.eval() does not recover it either. The import gptqmodel-first flow is unaffected.

Measured (Qwen2.5-0.5B-Instruct AWQ 4-bit g128 with dynamic={"+:.*layers\.(1|5|9)\..*": {"group_size": 32}}, 16 C4 rows, fp16)

NLL on the same 512 C4 tokens:

load device layer-1 q_proj NLL quant modules with training=True forward of 32 tokens (steady)
GPTQModel.load cpu TorchAtenAwqLinear gs=32 3.25389 0/168
import gptqmodel first cpu TorchAtenAwqLinear gs=32, qweight (896, 448) int4pack 3.25389 0/168 2.43 s
plain AutoModelForCausalLM (#4839 repair) cpu TorchAtenAwqLinear gs=32, qweight (896, 112) checkpoint layout, linear_mode='train' 3.25370 168/168 9.92 s (4.1x)
plain AutoModelForCausalLM (#4839 repair) cuda AwqMarlinLinear gs=32 3.25351 (= native) 168/168

So the fix's correctness holds on both devices. The same holds for a checkpoint that also has a -: exclusion (.*layers\.3\.mlp\..* stays a dense Linear): plain 3.22938 / native 3.22971 on CPU, and identical on CUDA. Only the module mode is wrong.

Minimal repro

import time, torch
from transformers import AutoModelForCausalLM          # no `import gptqmodel` (docs/peft.md flow)
m = AutoModelForCausalLM.from_pretrained("awq_dynamic_ckpt", device_map="cpu", dtype=torch.float16)
from gptqmodel.nn_modules.qlinear import BaseQuantLinear
q = [x for x in m.modules() if isinstance(x, BaseQuantLinear)]
print(m.training, sum(x.training for x in q), len(q))   # False 168 168   (import-first: False 0 168)
ids = torch.randint(100, 1000, (1, 32))
with torch.inference_mode():
    for _ in range(3):
        t = time.perf_counter(); m(ids); print(time.perf_counter() - t)   # ~9.9 s each (import-first: 2.4 s)
print({x.linear_mode for x in q})                        # {'train'}

Suggested fix

In hf_gptqmodel_repair_awq_load (or in create_quant_module when transfer_from is given), copy the replaced module's mode: new.train(old.training). Then call model.train(model.training) once after the swap.

Test that should have caught it

tests/test_hf_awq_bridge_import_order_4795.py compares group size and logits only. It should also assert not any(m.training for m in quant_modules) after a plain from_pretrained, and that the CPU module reached its inference layout (linear_mode == "inference") after one forward under inference_mode.

Severity

Medium. The documented plain-Transformers path for dynamic AWQ checkpoints now gives correct logits on CPU, but runs about 4x slower, on the dense-dequant training path, with no warning.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4949

    Fix the #4839 plain-Transformers dynamic AWQ repair leaving rebuilt quant modules in training mode (dropout/BN active): set eval() on rebuilt modules and add a regression test.

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