Repository navigation
fix(hf): install and repair the AWQ load bridge independent of import order (#4795) - #4839
Merged
Merged
Conversation
… order (#4795) Transformers' `AwqQuantizer` imports `gptqmodel` from inside `replace_with_awq_linear`, i.e. while its own prepare hook is already executing, so the AWQ bridge never ran the manifest-driven module build for the load that triggered the import. Every module was built from the global `bits`/`group_size`: a `dynamic` AWQ checkpoint decoded with the wrong group size on CPU (silently wrong logits) and raised `AWQ qzeros shape must be ...` on CUDA, and AWQ MoE checkpoints kept randomly initialized experts. - Retry the idempotent AWQ bridge install from the quantizer's post-weight hook, `hf_select_quant_linear_v2`, and the package init (`_ensure_transformers_awq_gptqmodel_load_bridge`), so every later AWQ load in the process gets the native prepare step. - Repair the load that triggered the import: the post-weight hook rebuilds each checkpoint-backed module from its resolved per-module config and kernel class, moves the already-assigned checkpoint tensors into it (`create_quant_module(transfer_from=...)`), and then runs the native post-init. Dense `dynamic` AWQ checkpoints now match `GPTQModel.load` on CPU and CUDA. - A per-expert checkpoint cannot be repaired after Transformers dropped the expert tensors, so the post hook raises an actionable error instead of returning randomly initialized experts. - Add an `hf_compat_warnings` entry for `dynamic` AWQ checkpoints and correct `docs/peft.md` / `docs/hf-interop.md`: no `import gptqmodel` is only true for GPTQ, AWQ MoE checkpoints still need it. - New fresh-interpreter regression test, plus coverage for the idempotent install and the save-time warning.
This was referenced Nov 17, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 17, 2026
) #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 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 #4795.
replace_with_awq_linearimportsgptqmodelmid-load, so the prepare hook cannever run for that load. A plain
AutoModelForCausalLMon adynamicAWQcheckpoint therefore built the modules from the global config: on CPU the
per-module
group_sizewas silently ignored (NLL drift), on CUDA the shapemismatch raised
ValueError: AWQ qzeros shape must be (1, 16), got (4, 16).Change
gptqmodel/__init__.py:_ensure_transformers_awq_gptqmodel_load_bridge()(idempotent, success/absent latches); the post-weight hook now ensures the
bridge and calls a repair when no prepare context exists.
gptqmodel/utils/hf_load.py: newhf_gptqmodel_repair_awq_load()— detects anative AWQ checkpoint whose modules were built from the global config,
rebuilds each checkpoint-backed module from its resolved per-module
config/kernel class (same
create_quant_layerpath as the native loader),moves the resident checkpoint tensors in, and runs
hf_gptqmodel_post_init_for_load. Later loads use the normal prepare path.gptqmodel/utils/model.py:create_quant_module(..., transfer_from=...)/create_quant_layer(..., transfer_from=...).gptqmodel/utils/importer.py: the entry-point path retries the AWQ install.gptqmodel/models/writer.py:hf_compat_warningsentry fordynamicAWQ.docs/peft.md,docs/hf-interop.md: the "noimport gptqmodel" guarantee isGPTQ-only; AWQ import order/repair and the MoE refusal are documented.
Evidence
Tiny Qwen2 (hidden 128, 3 layers), AWQ 4-bit, global
group_size=128,dynamic={"+:.*layers\.1\..*": {"group_size": 32}}, NLL over 64 tokens:AutoModelForCausalLMTorchAtenAwqLineargs=128, NLL 6.2413AutoModelForCausalLMValueError(qzeros shape)AwqMarlinLineargs=32, NLL 6.2393GPTQModel.loadLogits are bit-identical to
GPTQModel.loadon both devices (max |diff| 0.0).Plain-flow AWQ MoE now raises an actionable error instead of silently
quantizing dropped experts.
Tests
New
tests/test_hf_awq_bridge_import_order_4795.py(fresh interpreter, CPU +CUDA, idempotent install);
test_awq_moe_hf_load_4661.pycovers the plain-flowMoE refusal;
test_checkpoint_compat.pycovers the save-time warning.On this GB10 host the Marlin JIT build fails (
nvccbuild error), so the twoMarlin-backed cases (
test_plain_transformers_load_honors_dynamic_awq_on_cuda,test_untied_lm_head_quantizes_and_reloads) fail here for that environmentalreason; they run in CI where Marlin builds.
Red on parent: 3 failed — CPU built
group_size=128vs native 32; CUDA raisedthe qzeros shape
ValueError; the install helper was absent.ruff: clean.