Repository navigation
fix(load): keep a repaired AWQ module in the model's mode (#4907) - #4949
Merged
Merged
Conversation
#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).
This was referenced Nov 17, 2026
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.
This was referenced Nov 19, 2026
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.
Fixes #4907.
Root cause
hf_gptqmodel_repair_awq_load(#4839) rebuildsdynamicAWQ modules viacreate_quant_module(transfer_from=...). A freshnn.Moduleistraining=True, and the rebuild runs after Transformers puts the model in eval — so every repaired linear stayedtraining=Truewhilemodel.trainingwasFalse. The CPU int4pack kernels choose their layout fromself.trainingon the first forward and latchlinear_mode="train", so every weight was dequantized on every forward, even underinference_mode().Second, independent defect: the
train()overrides intorch_aten_kernel*.py/torch_fused.pyonly cleared"inference"when re-entering training, so a latched"train"never cleared oneval()— 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 withmodel.train(model.training)after the swap.TorchAtenAwqLinear/TorchAtenLinear/TorchFusedLinear.train: clear a latchedtrainmode 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,
dynamicgs=32 on layer 1, 1x32 tokens, CPU fp16, median of 8 forwards after 2 warm-ups)linear_modetraininferenceTests
New
tests/test_hf_awq_repair_train_mode_4907.py(fresh interpreter, noimport gptqmodelfirst, so the repair path runs): 4 passed. On parent all 4 fail —21/21training,['train']vs['inference'], sticky latch aftereval(),9.77e-4delta vs native.New test +
test_hf_awq_bridge_import_order_4795.py+test_hf_gptqmodel_load_bridge.py: 36 passed, 1 failed — the failure istest_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
linear_mode, mode flags, bitwise equality) rather than wall-clock.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 existinglinear_modecontract tests, which still pass.