Skip to content

fix(load): keep a repaired AWQ module in the model's mode (#4907) - #4949

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4907
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4907

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4907.

Root cause

hf_gptqmodel_repair_awq_load (#4839) rebuilds dynamic AWQ modules via create_quant_module(transfer_from=...). A fresh nn.Module is training=True, and the rebuild runs after Transformers puts the model in eval — so every repaired linear stayed training=True while model.training was False. The CPU int4pack kernels choose their layout from self.training on the first forward and latch linear_mode="train", so every weight was dequantized on every forward, even under inference_mode().

Second, independent defect: the train() overrides in torch_aten_kernel*.py / torch_fused.py only cleared "inference" when re-entering training, so a latched "train" never cleared on eval() — the model could not recover.

Fix (29 lines across 5 files + a 164-line test)

  • create_quant_module: the rebuilt layer inherits the replaced module's mode (new_layer.train(transfer_from.training)).
  • hf_gptqmodel_repair_awq_load: normalizes the tree with model.train(model.training) after the swap.
  • TorchAtenAwqLinear/TorchAtenLinear/TorchFusedLinear.train: clear a latched train mode when leaving training mode (dense-dequant holds no derived state, so the next forward re-chooses int4pack). The AVX512 CPU kernels already reset on every mode change.

Mode comes from the replaced module object, not the config, so the native import gptqmodel-first flow and #4799's untied head are untouched.

Measurements (tiny 3-layer Qwen2 AWQ g128, dynamic gs=32 on layer 1, 1x32 tokens, CPU fp16, median of 8 forwards after 2 warm-ups)

training modules linear_mode plain load native load ratio logits vs native
before 21/21 train 57.3 ms 32.4 ms 1.77x 9.77e-4 off
after 0/21 inference 28.8 ms 27.7 ms 1.04x bit-identical

Tests

New tests/test_hf_awq_repair_train_mode_4907.py (fresh interpreter, no import gptqmodel first, so the repair path runs): 4 passed. On parent all 4 fail — 21/21 training, ['train'] vs ['inference'], sticky latch after eval(), 9.77e-4 delta vs native.

New test + test_hf_awq_bridge_import_order_4795.py + test_hf_gptqmodel_load_bridge.py: 36 passed, 1 failed — the failure is test_plain_transformers_load_honors_dynamic_awq_on_cuda, which needs the Marlin JIT that cannot build on this host; verified identical on parent. tests/test_awq_*.py: 929 passed, 82 skipped, 10 failed + 1 collection error, all identical to parent. ruff check gptqmodel tests: clean.

Residual risk

  • Timing was measured on a 21-module synthetic model, not the issue's 0.5B checkpoint; the test asserts the causal proxy (linear_mode, mode flags, bitwise equality) rather than wall-clock.
  • The eval()-recovery guard is applied to three kernels but only the AWQ aten path is exercised by the new test; the GPTQ aten and fused variants get the same one-branch guard and are covered by the existing linear_mode contract tests, which still pass.

#4839 repairs a plain `AutoModelForCausalLM.from_pretrained` load of a `dynamic`
AWQ checkpoint in the post-weight hook. `hf_gptqmodel_repair_awq_load` rebuilds
each checkpoint-backed module through `create_quant_module(transfer_from=...)`
and swaps it in. A freshly constructed `nn.Module` is in training mode, and the
rebuild runs after Transformers put the model in eval, so every repaired linear
stayed `training=True` while `model.training` was False.

The CPU int4pack kernels choose their layout from `self.training` on the first
forward and then latch `linear_mode = "train"`, so the repaired model
dequantized every weight on every forward — including under
`inference_mode()` — with no warning. The latched mode was sticky: the
`train()` overrides only cleared `"inference"` when re-entering training mode,
so a later `model.eval()` could not recover either.

Fix the mode at the swap and the latch at the kernel:

* `create_quant_module` gives the rebuilt layer the mode of the module it
  replaces (`new_layer.train(transfer_from.training)`), and the repair
  normalizes the whole tree against `model.training` after the swap.
* `TorchAtenAwqLinear.train`, `TorchAtenLinear.train` and
  `TorchFusedLinear.train` clear a latched `train` mode when leaving training
  mode. The dense-dequant path holds no derived state, so the next forward
  re-chooses the int4pack/fused layout. (The AVX512 CPU kernels already reset
  on every mode change.)

The mode is copied from the replaced module object, not from
`tie_word_embeddings` or the serialized config, so the native
`import gptqmodel`-first flow and the #4799 untied-head behavior are untouched.

Measured on this host (tiny Qwen2 3-layer AWQ 4-bit g128 with
`dynamic={".*layers\\.1\\..*": {"group_size": 32}}`, 1x32 tokens, CPU fp16,
median of 8 forwards after 2 warm-ups):

  before: 21/21 modules `training=True`, `linear_mode='train'`,
          plain 57.3 ms vs native 32.4 ms (1.77x), logits differ by 9.77e-4
  after:   0/21 modules `training=True`, `linear_mode='inference'`,
          plain 28.8 ms vs native 27.7 ms (1.04x), logits bit-identical

The issue's 4.1x is on a 0.5B checkpoint; the ratio here is smaller because the
tiny model's dense dequant is a smaller share of the forward, but the mechanism
and the mode/latch evidence are the same.

Tests: `tests/test_hf_awq_repair_train_mode_4907.py` loads the checkpoint in a
fresh interpreter (no `import gptqmodel` first, so the repair path runs) and
asserts the model mode, the int4pack layout after one inference forward, the
`eval()` recovery from a latched `train` forward, and bitwise equality with the
native load. 4 passed; on the parent all 4 fail (21/21 training, `['train']` vs
`['inference']`, sticky latch, 9.77e-4 delta).
@Qubitium
Qubitium merged commit c2a76ce into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4907 branch November 17, 2026 19:38
Qubitium added a commit that referenced this pull request Nov 18, 2026
…5018) (#5036)

`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant