Repository navigation
fix(hf): run native MoE bridge, remap legacy VL keys, retune AWQ groups (#4073) - #4084
Merged
Merged
Conversation
This was referenced Nov 15, 2026
Qubitium
pushed a commit
that referenced
this pull request
Nov 17, 2026
#4692) Transformers loads `quant_method: "awq"` through its own AwqQuantizer, not Optimum. That path only replaces `nn.Linear` modules, so a GPT-QModel AWQ checkpoint storing per-expert `experts.<n>.*.qweight` tensors against fused 3-D expert parameters loaded with every routed expert randomly initialized and no error. #4084 patched the Optimum GPTQ path only. Wrap AwqQuantizer._process_model_before_weight_loading and _process_model_after_weight_loading with the same contract as the Optimum wrapper: a native GPT-QModel checkpoint is rebuilt from the manifest by hf_gptqmodel_prepare_model_for_load, and any other checkpoint keeps Transformers' own AWQ path because the bridge returns None. The save-time warning no longer claims Optimum's GPTQ loader is the only gap, and it now points at a remedy that applies to both methods. Refs #4661
Qubitium
added a commit
that referenced
this pull request
Nov 17, 2026
…ges (#4680) (#4702) Both HF load bridges called `hf_gptqmodel_prepare_model_for_load` without the caller's backend, so `_resolve_backend_for_checkpoint` always saw `None` and resolved AUTO. `docs/peft.md` tells users to set `quantization_config["backend"] = "gptq_torch"` to get a trainable kernel; for a GPT-QModel-made checkpoint that setting was silently dropped and AUTO picked a different kernel (Marlin on CUDA, which does not support training). An impossible choice such as `marlin` on a CPU-only host was accepted without a word. Optimum-owned checkpoints kept honoring it, so behavior depended on who made the checkpoint. Forward `getattr(self, "backend", None)` from both bridges — the GPTQ one (`GPTQQuantizer.convert_model`, #4084) and the AWQ one (`AwqQuantizer._process_model_before_weight_loading`), which had the identical defect. A quantizer without the attribute still forwards `None`. Closes #4680.
This was referenced Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4073
TL;DR
Four HF-interop defects are fixed in one change:
model.layers.*,visual.*) load.in_featuresbefore calibration instead of raising after the whole pass.auto_mapwhen the.pyfiles are not copied.New reference page:
docs/hf-interop.md, linked fromREADME.md.1. MoE checkpoints loaded by plain Transformers
Root cause
hf_gptqmodel_prepare_model_for_load(gptqmodel/utils/hf_load.py:242) andhf_gptqmodel_post_init_for_load(:361) were exported but never called by Optimum. Optimum'sGPTQQuantizer.convert_modelonly replacesnn.Linear(optimum/gptq/quantizer.py), while Transformers 5 builds fused 3-D experts. Everyexperts.<n>.*.qweightwas therefore UNEXPECTED and every fused expert MISSING, with no error.Fix
_patch_optimum_gptqmodel_load_bridge(gptqmodel/__init__.py:228, installed at the end of package import) wrapsGPTQQuantizer.convert_model/post_init_model. Native GPT-QModel checkpoints go through the bridge; Optimum-owned checkpoints keep Optimum's path because the bridge returnsNonefor them.hf_compat_warnings(gptqmodel/models/writer.py:232) now also emits a loud save-time warning for per-expert checkpoints.flowchart TD A["AutoModelForCausalLM.from_pretrained"] --> B["GptqHfQuantizer"] B --> C["patched GPTQQuantizer.convert_model"] C --> D{"hf_gptqmodel_prepare_model_for_load"} D -->|"Optimum-owned checkpoint"| E["original Optimum convert_model"] D -->|"native GPT-QModel checkpoint"| F["defuser expands fused blocks and MoE experts"] F --> G["make_quant per serialized module with dynamic config"] G --> H["Transformers assigns checkpoint tensors"] H --> I["patched post_init_model"] I --> J["hf_gptqmodel_post_init_for_load"] style E fill:#fff3cd,stroke:#997404Before / after (real OLMoE-1B-7B checkpoint)
experts.gate_up_proj MISSING, PPL 39570,"domest domest domest..."model.layers.0.mlp.experts.0present,"The capital of France is Paris."(1, 5, 50304),"The capital of France is Paris."Save-time warning reproduced on the same real checkpoint:
2. Legacy-layout quantized VL checkpoints
Root cause
models/loader.py:2561(validation) and:2850/:2958(load) read checkpoint keys verbatim. The HF conversion map was only applied on the LazyTurtle quantization path (loader.py:1786), soQwen2-VL-2B-Instruct-GPTQ-Int8(keysmodel.layers.*,visual.*) failed validation.Fix
_checkpoint_key_converter(gptqmodel/models/loader.py:133) resolves the model's checkpoint conversion map from the live model unless a definition declares an explicitcheckpoint_key_for_load/checkpoint_key_for_save. It is used for validation (runtime -> checkpoint) and both load calls (checkpoint -> runtime). The map is idempotent for runtime-layout checkpoints.flowchart LR A["checkpoint: model.layers.0.mlp.down_proj.qweight"] --> B["HF conversion map"] B --> C["runtime: model.language_model.layers.0.mlp.down_proj.qweight"] D["checkpoint: visual.blocks.0.attn.qkv.weight"] --> B B --> E["runtime: model.visual.blocks.0.attn.qkv.weight"] F["already-runtime key"] --> B B --> G["unchanged"] style G fill:#d1e7dd,stroke:#146c43Before / after
ValueError: Missing required quantized weights ... model.language_model.layers.0.mlp.down_proj.qweight(1, 5, 151936), mean abs 7.31(1, 5, 151936), mean abs 7.363. AWQ with
in_features % group_size != 0Root cause
_accumulate_awq_weight_mean(gptqmodel/looper/awq_processor.py:93) reshapes the input axis into fixed groups and raised forintermediate_size=448withgroup_size=128only after calibration completed.Fix
AWQProcessor._ensure_group_size_divisibility(awq_processor.py:540) runs in__init__, before any calibration forward. For each non-excluded linear it picks the largest kernel-supported group size that dividesin_features(else-1per-channel), preserves the module's other resolved overrides, and prepends an exactdynamicrule so staging, packing, save, and reload all resolve the same value. A warning names every retuned module.flowchart TD A["AWQProcessor.__init__"] --> B["_ensure_group_size_divisibility"] B --> C{"in_features divisible by group_size"} C -->|"yes"| D["keep configured value"] C -->|"no"| E["largest supported divisor, else -1"] E --> F["prepend exact dynamic override"] F --> G["calibration, packing, save and reload agree"] style F fill:#cfe2ff,stroke:#084298Before / after
ValueError: Expected in_features (448) to be divisible by group_size (128).down_proj.group_size == 64, override+:^model.layers.0.mlp.down_proj$ -> {"group_size": 64}down_proj.group_size == 64,in_features == 4484. Dangling
auto_mapafter the nativetrust_remote_codeoverrideRoot cause
resolve_trust_remote_code(gptqmodel/utils/hf.py:749) turns remote code off for natively supported types, and the writer skips copying.pyfiles._save_model_configs_without_weightsthen merged the sourceauto_mapback into the savedconfig.json, pointing at files that were not exported.Fix
_save_model_configs_without_weights(writer.py:290) andsave_quantized_embeddingsdropauto_mapwhenevertrust_remote_codeis false, i.e. exactly when.pyfiles are not copied.Before / after (real Phi-tiny-MoE source config)
config.jsonkeepsauto_map -> configuration_slimmoe.PhiMoEConfig;AutoConfig.from_pretrained(out, trust_remote_code=True)raisesOSError: ... configuration_slimmoe.pyauto_mapisNone;AutoConfig.from_pretrained(out, trust_remote_code=True)returnsPhimoeConfig(model_type=phimoe)Tests
Exact commands and results:
Real-model runs (not committed tests) are the before/after tables above. The real VL checkpoint test is committed and skips when
/monster/data/model/Qwen2-VL-2B-Instruct-GPTQ-Int8is absent.Pre-existing failure
tests/test_awq_dynamic_group_size.py::test_dynamic_group_size_selects_compatible_kernel_and_packsfails identically on cleanorigin/main(AttributeError: 'Stub' object has no attribute '_module_format'). It is deselected above and is not caused by this PR.Hardware
GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae(CUDA venv, torch 2.16.0.dev+cu130)HIP_VISIBLE_DEVICES=3(ROCm venv, torch 2.16.0.dev+rocm10.1)Notes / limitations
gptqmodel. A process that loads a native GPT-QModel checkpoint through Transformers without importinggptqmodelcannot be intercepted; the save-time warning now tells users to import it or useGPTQModel.load.modules_to_not_convertwas not added for GPTQ: Optimum's GPTQ loader does not read it. The bridge keeps unquantized gates such as Qwen'sshared_expert_gateasnn.Linear, which removes the MarlinK=2048, N=1crash structurally.