Repository navigation
feat(model): add native GPTQModel load bridge for Transformers/Optimum - #3503
Conversation
Transformers hands GPTQ loading to Optimum, which replaces every linear layer with one homogeneous kernel derived from the global quantization config. That loses per-module `dynamic` settings and cannot rebuild structural rewrites such as fused blocks or expanded MoE experts, so native GPTQModel checkpoints load with the wrong kernels. Add a stable bridge in `gptqmodel.utils.hf_load`, re-exported from `gptqmodel.utils.model`: - `HFGPTQModelLoadContext` carries state between the two hooks. - `hf_gptqmodel_prepare_model_for_load` resolves the checkpoint manifest, applies fused/MoE rewrites on the meta device, and installs one supported kernel per serialized module while honoring the full `dynamic` config. - `hf_gptqmodel_post_init_for_load` runs the v1->v2 conversion and backend post-init once the caller has assigned the checkpoint tensors. Checkpoints that Optimum saved also record a `gptqmodel:<version>` entry, because Optimum uses GPTQModel for the quantization math. Only checkpoints whose provenance names GPTQModel without naming Optimum are treated as native, so Optimum keeps its existing loading path. Relax the `create_quant_module` device guard to accept the meta device, which Transformers uses while building a quantized model shell. Tests cover manifest-driven kernel selection, dense-module preservation, the Optimum-owned and unresolvable-path cases, missing-module errors, Laguna MoE expansion on meta, post-init config restoration, and an end-to-end Transformers load.
github-code-quality flags the bridge re-export in utils/model.py as an unused import and the test-local imports as import-style noise. Both are deliberate: - The re-export exists so transformers/optimum can keep importing the bridge from the stable `gptqmodel.utils.model` entry point. Dropping it raises ImportError there, so keep it and spell out why next to the noqa. - `test_hf_load_post_init_restores_gptqmodel_config_fields` needs the module object for monkeypatch.setattr, matching the local-import convention already used by other tests in this repo. Hoist only GPTQConfig, which transformers already exports at module scope. Verified: ruff 0.14.2 (CI gate) clean, PLC0415 and F401 clean, bridge tests 10 passed on CUDA and ROCm.
|
Reviewed commit 1. [P1] Restore the online Hadamard transform for rotated checkpoints — hf_load.py:324–329 The bridge accepts manifests with I reproduced this with a one-layer Llama shell, a native manifest containing Please share the normal loader's rotation backend validation and online-transform setup with this bridge. Add a rotated-checkpoint load-and-forward regression, rather than checking only restored metadata. 2. [P2] Apply the saved activation policy before selecting kernels — hf_load.py:277–285 The bridge passes the caller's backend directly to With a native Please normalize the requested backend and apply Validation
|
The native load bridge skipped two post-processing steps the normal loader performs, so two classes of native checkpoints loaded incorrectly. Rotation (P1): rotated checkpoints fuse an orthogonal Q and an exact Hadamard H into the weights, and H has to be re-applied to activations at inference. The bridge accepted `rotation=hadamard|random` but never called `_setup_rotation_online_had`, leaving `mlp.down_proj.online_full_had` False. The module then consumes untransformed activations, so the load succeeds and inference is silently wrong. Call the shared helper after post-init, and reject rotation on backends that cannot apply the transform instead of accepting them. Activation policy (P2): W4AFP8/NVFP4 kernels carry no AUTO-selection priority, so an AUTO load of a W4A checkpoint installed ordinary GPTQ kernels and post-init then failed with "No complete W4A decoder layer was found". Resolve the backend through `backend_for_activation` before `make_quant`, keeping the normal loader's rejection of incompatible explicit backends. Both regressions are covered by tests that fail when the fix is removed: - rotated post-init leaves `online_full_had` armed on `mlp.down_proj` - AUTO resolves W4AFP8/NVFP4 from the saved activation mode, and still rejects a mismatched explicit backend - AUTO resolves a rotation-aware kernel, and rotation on MARLIN is rejected Verified: 16 passed on CUDA and ROCm, bridge integration 1 passed, Optimum bridge tests 4 passed, ruff 0.14.2 clean.
The bridge was the only load path that skipped `_set_paged_attention_safe_cuda_graphs`, which the normal loader applies on every quantized load. Paged Flash Attention is not CUDA-graph capture safe, so models declaring a paged attn implementation kept transformers' continuous-batching default `use_cuda_graph=True` and captured an unsafe graph. Apply the same helper after post-init. It is a no-op for models whose attn implementation does not list `paged`, so non-paged loads are unchanged. Tests drive the real post-init hook and cover both sides: a paged model gains `continuous_batching_config.use_cuda_graph == (False, False)`, and a non-paged model gains no `generation_config` at all. Verified: 18 passed on CUDA and ROCm, bridge integration 1 passed, Optimum bridge tests 4 passed, ruff 0.14.2 clean.
|
Both issues reproduced and fixed in P1 — rotation state restoredConfirmed your finding: Fix, shared with the normal loader rather than reimplemented:
P2 — saved activation policy applied before kernel selectionConfirmed: Applied at Additional fix — paged attention
On the test gap you called outYou were right that metadata-only assertions let P1 through. I wrote the regression against the real
Coverage added (
Validation
One limitation I did not resolveThe rotation and paged-attention tests are structural, not numerical: they assert the transform is armed and graph capture is disabled, but do not run a forward pass against a normalized-Hadamard reference like your @Qubitium Requesting another review. |
Summary
Transformers hands GPTQ loading to Optimum, which replaces every linear layer with a single homogeneous kernel derived from the global quantization config. That loses per-module
dynamicsettings and cannot rebuild structural rewrites such as fused blocks or expanded MoE experts, so native GPTQModel checkpoints load with the wrong kernels (or fail outright).This adds a small stable bridge that GPTQModel checkpoints can use through Transformers/Optimum.
New API —
gptqmodel/utils/hf_load.pyRe-exported from
gptqmodel.utils.model, so existing import paths keep working.HFGPTQModelLoadContexthf_gptqmodel_prepare_model_for_loaddynamicconfig.hf_gptqmodel_post_init_for_loadis_native_gptqmodel_checkpointAlso exposes
find_checkpoint_dirfor manifest resolution.Provenance gating
Optimum's own saved checkpoints also record a
gptqmodel:<version>entry, because Optimum delegates the quantization math to GPTQModel. Treating anygptqmodelentry as native would hijack Optimum's own loading path and silently swap its selected kernel.So a checkpoint is only native when its provenance names GPTQModel without naming Optimum:
Supporting change
create_quant_modulenow accepts{CPU, meta}instead of CPU only. Transformers builds a quantized model shell on the meta device, so the meta path is required for the bridge to work; the replacement layer still lands on whichever device the original layer occupied.Tests
tests/test_hf_gptqmodel_load_bridge.py(new):dynamicrule names itdynamic/lm_head/method/pack_dtypetests/test_integration.py: end-to-endAutoModelForCausalLM.from_pretrainedover a saved checkpoint, asserting kernel class, per-module group size, preserved dense layer, and no residual meta tensors.Validation
The skip needs a machine-local
/monster/datacheckpoint path.Cross-checked against the Optimum-side integration from huggingface/optimum#2469: the 5 Optimum bridge tests pass against this implementation.
Notes
tests/test_laguna_support.py::test_laguna_module_tree_expands_dense_attention_and_sparse_moe_pathsfails on unmodifiedmaintoo; unrelated to this change.