Repository navigation
Description
Found on main @ b225a43, CPU (x86-64 AVX512), transformers 5.22.1, optimum 2.3.0.
What is wrong
The Optimum GPTQ load bridge (#4084) is installed only if the user's script imports gptqmodel before Transformers loads Optimum. In the plain-Transformers flow, the order is reversed: GptqHfQuantizer imports optimum.gptq, and optimum/gptq/quantizer.py:50 imports gptqmodel while optimum.gptq is still initializing. gptqmodel/__init__.py:900 then calls _patch_optimum_gptqmodel_load_bridge(), which runs from optimum.gptq import GPTQQuantizer. That import raises ImportError (the module is partially initialized), and gptqmodel/__init__.py:457-463 swallows it as "Optimum not installed". The bridge is never installed for the rest of the process.
The comment at gptqmodel/__init__.py:897 and docs/hf-interop.md:51-52 ("installed at the end of package import because optimum.gptq imports gptqmodel while it loads") describe exactly this situation as handled. It is not handled.
Without the bridge, Optimum 2.3.0 builds every quantized module with the global bits/group_size, because it has no dynamic support. In a GPT-QModel GPTQ checkpoint with per-module dynamic bit-width overrides, each overridden module then holds the checkpoint's 8-bit qweight (shape (224, 896)) inside a module that decodes it as 4-bit (bits=4). There is no MISMATCHED report, no error and no save-time warning (hf_compat_warnings returns [] for this config). The output is silently wrong.
docs/peft.md's train and load examples (AutoConfig / AutoModelForCausalLM.from_pretrained(quant_path, ...), no import gptqmodel) are exactly this flow. The hf-interop table presents the GPTQ bridge as the path for every native checkpoint, with no import-order caveat (one exists only for AWQ).
Repro (CPU)
# 1) make a mixed-bit checkpoint (weight-only, fast)
import torch
from gptqmodel import GPTQModel
from gptqmodel.quantization import RTNConfig
m = GPTQModel.load("Qwen/Qwen2.5-0.5B-Instruct",
RTNConfig(bits=4, group_size=128, dynamic={r"+:.*layers\.(1|5|9)\..*": {"bits": 8}}),
device="cpu", dtype=torch.float16)
m.quantize(); m.save("rtn_dyn")# 2) fresh process, plain transformers (as in docs/peft.md)
import sys, torch
from transformers import AutoModelForCausalLM
mm = AutoModelForCausalLM.from_pretrained("rtn_dyn", device_map="cpu", dtype=torch.float16)
from optimum.gptq import GPTQQuantizer
print(getattr(GPTQQuantizer.convert_model, "_gptqmodel_load_bridge", False), "gptqmodel" in sys.modules)
q = mm.model.layers[1].self_attn.q_proj
print(type(q).__name__, q.bits, tuple(q.qweight.shape))False True # gptqmodel imported (by optimum), bridge NOT installed
TorchCpuAvx512Linear 4 (224, 896) # 8-bit payload in a 4-bit module
NLL on 511 C4 tokens, same checkpoint:
| load | layer-1 q_proj | NLL |
|---|---|---|
GPTQModel.load |
TorchLinear bits=8 | 3.4496 |
import gptqmodel first, then AutoModelForCausalLM |
TorchLinear bits=8 | 3.4496 |
plain AutoModelForCausalLM (docs/peft.md) |
TorchCpuAvx512Linear bits=4 | 16.4914 |
| control: uniform 4-bit checkpoint, plain | bits=4 | 3.5021 (vs 3.5022 native) |
#4077 noted that "mixed-bit dynamic checkpoints fail loudly in HF" (Marlin on CUDA). On the CPU AUTO kernel they load silently. The same missing bridge also leaves plain-HF MoE GPTQ checkpoints on the random-expert path #4073 describes. The save warning does mention that case, but docs/peft.md does not.
Suggested fix
- Install the bridge independently of import order. For example, patch from
GptqHfQuantizer(transformers, always fully imported before it touches Optimum), or retry_patch_optimum_gptqmodel_load_bridge()lazily from the firsthf_select_quant_linear_v2/hf_gptqmodel_post_initcall that Optimum makes into gptqmodel. Do not treat a partially-initialized-moduleImportErroras "Optimum missing". - Until then, add a
dynamicbits/group-size override tohf_compat_warnings, addimport gptqmodelto thedocs/peft.mdexamples, and correct the hf-interop.md sentence. - Optionally refuse a quantized module whose loaded
qweightshape does not match itsbits.
Test that should have caught it
tests/test_hf_gptqmodel_load_bridge.py imports gptqmodel at module top, so it never exercises the plain-Transformers import order. Add a subprocess test that runs AutoModelForCausalLM.from_pretrained on a tiny mixed-bit (and an MoE) GPT-QModel checkpoint without importing gptqmodel first, and compares logits to GPTQModel.load.
Severity
High: a documented load path (docs/peft.md) silently produces wrong logits (NLL 3.45 -> 16.49) for any mixed-precision dynamic checkpoint, with no warning at save or load.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Agent claim released:
agent-53ba17eb4d83finished this issue.Pull request: #4771
claiming: will fix on a worktree and open a PR
Qubitium commentedon Nov 17, 2026
Taking this. Fix: install the Optimum GPTQ bridge through an import hook when
optimum.gptqis still mid-initialization, so the plain-Transformers flow gets the per-module dynamic reader instead of the global-bits one.fix(hf): install the Optimum GPTQ load bridge independent of import o…
test(hf): pin the import-order-independent Optimum GPTQ bridge (#4749) (