Skip to content

fix(hf): install and repair the AWQ load bridge independent of import order (#4795) - #4839

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4795
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4795

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4795.

replace_with_awq_linear imports gptqmodel mid-load, so the prepare hook can
never run for that load. A plain AutoModelForCausalLM on a dynamic AWQ
checkpoint therefore built the modules from the global config: on CPU the
per-module group_size was silently ignored (NLL drift), on CUDA the shape
mismatch 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: new hf_gptqmodel_repair_awq_load() — detects a
    native 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_layer path 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_warnings entry for dynamic AWQ.
  • docs/peft.md, docs/hf-interop.md: the "no import gptqmodel" guarantee is
    GPTQ-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:

load device before after
plain AutoModelForCausalLM cpu TorchAtenAwqLinear gs=128, NLL 6.2413 gs=32, NLL 6.2393
plain AutoModelForCausalLM cuda ValueError (qzeros shape) AwqMarlinLinear gs=32, NLL 6.2393
GPTQModel.load cpu/cuda gs=32, 6.2393 unchanged

Logits are bit-identical to GPTQModel.load on 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.py covers the plain-flow
MoE refusal; test_checkpoint_compat.py covers the save-time warning.

pytest <7 bridge/AWQ/checkpoint suites> -> 74 passed (with CUDA_HOME)
pytest tests/test_hf_awq_bridge_import_order_4795.py -> 3 passed
pytest <dynamic/group-size/lifecycle suites> -> 139 passed

On this GB10 host the Marlin JIT build fails (nvcc build error), so the two
Marlin-backed cases (test_plain_transformers_load_honors_dynamic_awq_on_cuda,
test_untied_lm_head_quantizes_and_reloads) fail here for that environmental
reason; they run in CI where Marlin builds.

Red on parent: 3 failed — CPU built group_size=128 vs native 32; CUDA raised
the qzeros shape ValueError; the install helper was absent.

ruff: clean.

… 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.
@Qubitium
Qubitium merged commit 174d3cb into main Nov 17, 2026
8 checks passed
@Qubitium
Qubitium deleted the fix/issue-4795 branch November 17, 2026 16:35
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).
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