Skip to content

[BUG] HF load bridge is never installed in the plain-Transformers flow (optimum imports gptqmodel mid-init; ImportError swallowed): mixed-bit dynamic GPTQ ckpts load silently wrong (NLL 3.45 -> 16.49), docs/peft.md path affected #4749

Description

@Qubitium

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 first hf_select_quant_linear_v2 / hf_gptqmodel_post_init call that Optimum makes into gptqmodel. Do not treat a partially-initialized-module ImportError as "Optimum missing".
  • Until then, add a dynamic bits/group-size override to hf_compat_warnings, add import gptqmodel to the docs/peft.md examples, and correct the hf-interop.md sentence.
  • Optionally refuse a quantized module whose loaded qweight shape does not match its bits.

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

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4771

    claiming: will fix on a worktree and open a PR

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Taking this. Fix: install the Optimum GPTQ bridge through an import hook when optimum.gptq is still mid-initialization, so the plain-Transformers flow gets the per-module dynamic reader instead of the global-bits one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions