Skip to content

fix(load): install the Optimum GPTQ bridge regardless of import order (#4749) - #4771

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

Qubitium merged 2 commits into
mainfrom
fix/issue-4749

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Closes #4749.

The Optimum GPTQ load bridge (#4084) was installed only when the user's script imported gptqmodel before Transformers loaded Optimum. In the plain-Transformers flow the order is reversed: GptqHfQuantizer imports optimum.gptq, which imports gptqmodel while optimum.gptq is still initializing, so from optimum.gptq import GPTQQuantizer raised a partially-initialized ImportError that gptqmodel/__init__.py swallowed as "Optimum not installed". The bridge was never installed, and Optimum built every module with the global bits, so mixed-bit dynamic GPTQ checkpoints loaded silently wrong (NLL 3.45 -> 16.49 on the reporter's fixture).

Change

  • _patch_optimum_gptqmodel_load_bridge() now reports success/failure and records install state instead of treating a partial-module ImportError as "Optimum missing".
  • New _ensure_optimum_gptqmodel_load_bridge(): cheap and idempotent. It caches success and genuine absence, and while optimum.gptq is in sys.modules without GPTQQuantizer it returns without importing and without caching, so a later call retries.
  • New _patch_transformers_gptq_quantizer_bridge() wraps GptqHfQuantizer._process_model_before/after_weight_loading, which always run after optimum.gptq finished importing and before convert_model replaces layers. This is what makes the plain-Transformers flow work.
  • hf_select_quant_linear_v2 / hf_gptqmodel_post_init call the ensure function as a backstop for direct-Optimum use.

Tests

tests/test_hf_optimum_bridge_import_order.py builds a tiny 2-layer Qwen2 mixed-bit RTN checkpoint (bits 4, layer 1 bits 8) and loads it in a fresh interpreter that never imports gptqmodel, asserting the bridge flag is set and each layer's bits/qweight shape matches GPTQModel.load (layer 1: bits 8, (16, 64); layer 0: bits 4, (8, 64)).

Red on parent: the new test fails with assert False is True (bridge not installed).

Docs

  • docs/hf-interop.md: the import-order paragraph now describes the retry hooks and entry points; partial initialization means "retry later", not "missing".
  • docs/peft.md: states explicitly that no import gptqmodel is needed.

Verification

  • pytest tests/test_hf_gptqmodel_load_bridge.py tests/test_hf_optimum_bridge_import_order.py -> 28 passed.
  • Related checkpoint/load suites -> 128 passed, 7 skipped, 1 failed (test_untied_lm_head_quantizes_and_reloads, a CUDA_HOME Marlin JIT failure that also fails on the parent commit).
  • ruff check gptqmodel tests docs clean.

…rder (#4749)

`optimum.gptq` imports `gptqmodel` while it is still initializing, so the
package-init probe for `optimum.gptq.GPTQQuantizer` raised a partially
initialized ImportError and was swallowed as "Optimum not installed". In the
plain-Transformers flow the bridge was therefore never installed: a mixed-bit
`dynamic` checkpoint kept its 8-bit qweight inside a 4-bit module and loaded
with silently wrong logits (NLL 3.45 -> 16.49).

- Distinguish a deferred (partial-module) failure from a genuine absence.
  `_ensure_optimum_gptqmodel_load_bridge` retries the install, caches success
  and genuine absence, and never retries while `optimum.gptq` is visibly
  mid-import.
- Retry from Transformers' `GptqHfQuantizer` pre/post weight-loading hooks,
  which always run after `optimum.gptq` finished importing and before
  `convert_model` replaces any layer.
- Also retry from the `hf_select_quant_linear_v2` / `hf_gptqmodel_post_init`
  entry points Optimum resolves from gptqmodel.
- Add a subprocess regression test that builds a tiny mixed-bit dynamic RTN
  checkpoint and loads it with plain `AutoModelForCausalLM.from_pretrained`,
  without importing gptqmodel first; it asserts the bridge is installed and
  each layer's bits/qweight shape matches `GPTQModel.load`.
- Update docs/hf-interop.md and docs/peft.md: no `import gptqmodel` is needed
  for the plain-Transformers GPTQ path.
@Qubitium
Qubitium merged commit 3eb422b into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4749 branch November 17, 2026 14:33
Qubitium added a commit that referenced this pull request Nov 17, 2026
#4779)

#4771 fixed #4749 - the Optimum GPTQ bridge is now retried from Transformers'
`GptqHfQuantizer` hooks instead of being latched off when `optimum.gptq` is
observed mid-initialization - but it shipped without a test. Nothing in the
suite distinguishes "Optimum is not installed" from "optimum.gptq is importing
gptqmodel right now", which is exactly the state that silently decoded every
`dynamic` override with the global bits (NLL 3.45 -> 16.49, no error, no
warning).

Three tests pin the contract of `_ensure_optimum_gptqmodel_load_bridge`:

* a stand-in `optimum.gptq` that has no `GPTQQuantizer` yet (the real import
  window) must return `False` and must **not** set
  `_OPTIMUM_GPTQ_BRIDGE_UNAVAILABLE`, so the retry stays possible;
* once the class appears, the same call must install the bridge, set
  `_OPTIMUM_GPTQ_BRIDGE_INSTALLED`, wrap `convert_model`/`post_init_model`, and
  stay idempotent on a second call;
* a genuinely missing Optimum (find_spec -> None) must set the sticky
  `_OPTIMUM_GPTQ_BRIDGE_UNAVAILABLE` latch so a load does not retry the failed
  import.

Verified: all three fail on the pre-#4771 tree (`9a63019a8`, where the retry
path and its state flags do not exist yet) and `tests/test_hf_gptqmodel_load_bridge.py`
passes 30/30 on current main. Test-only, no production change.
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