Skip to content

fix(awq): load packed AWQ MoE experts through the native HF bridge (#4661) - #4692

Merged
Qubitium merged 1 commit into
mainfrom
fix/4661-awq-hf-bridge
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/4661-awq-hf-bridge

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR

Fixes #4661.

transformers loads quant_method: "awq" through its own AwqQuantizer, which
#4084 never patched. A GPT-QModel AWQ MoE checkpoint therefore loaded with every
routed expert randomly initialized and no error. This wraps that quantizer with
the existing native bridge (net +72 lines in gptqmodel/__init__.py, plus tests
and docs). Non-native AWQ checkpoints keep Transformers' own path.

1. The problem

_patch_optimum_gptqmodel_load_bridge (gptqmodel/__init__.py:412) wraps
optimum.gptq.GPTQQuantizer.convert_model / post_init_model. Transformers
dispatches AWQ somewhere else entirely, so the bridge never ran:

flowchart TD
    A["AutoModelForCausalLM.from_pretrained"] --> B{"quant_method in config.json"}
    B -->|"gptq"| C["GptqHfQuantizer"]
    C --> D["optimum GPTQQuantizer.convert_model"]
    D --> E["patched by _patch_optimum_gptqmodel_load_bridge"]
    B -->|"awq"| F["AwqQuantizer._process_model_before_weight_loading"]
    F --> G["replace_with_awq_linear: nn.Linear only"]
    G --> H["fused 3-D expert params stay dense"]
    H --> I["experts.N.*.qweight UNEXPECTED"]
    I --> J["experts randomly initialized, no error"]
    style G fill:#f8d7da,stroke:#b02a37
    style I fill:#f8d7da,stroke:#b02a37
    style J fill:#f8d7da,stroke:#b02a37
Loading

Reproduced on origin/main 7395b40e0 with a 2-layer / 4-expert OLMoE model
quantized by AWQConfig(bits=4, group_size=32) and saved by GPTQModel.save,
then loaded by plain AutoModelForCausalLM.from_pretrained in a process that
imported gptqmodel first:

Observation Before
model.layers.{0,1}.mlp.experts.{0..3}.*.qweight/qzeros/scales UNEXPECTED (dropped)
model.layers.{0,1}.mlp.experts.gate_up_proj / down_proj MISSING (random init)
Module kinds TorchCpuAvx512AwqLinear=8, OlmoeExperts=2, Linear=1 — only attention quantized
Logits vs GPTQModel.load max abs diff 0.0608 on a tiny model

The save-time warning (gptqmodel/models/writer.py:346) told users to "import
gptqmodel before Transformers", a remedy that provably did nothing for AWQ
and described the gap as Optimum-GPTQ-specific.

Mechanisms replaced

Old mechanism Where New mechanism
Optimum-only bridge installation gptqmodel/__init__.py:787 (_patch_optimum_gptqmodel_load_bridge()) Also install _patch_transformers_awq_gptqmodel_load_bridge()
AwqQuantizer._process_model_before_weight_loading → replace_with_awq_linear transformers/quantizers/quantizer_awq.py:64 Bridge-first: hf_gptqmodel_prepare_model_for_load, falling back to the original when it returns None
AwqQuantizer._process_model_after_weight_loading → hf_gptqmodel_post_init transformers/quantizers/quantizer_awq.py:80 Bridge context: hf_gptqmodel_post_init_for_load
Warning naming "Optimum's GPTQ loader" and an inapplicable remedy gptqmodel/models/writer.py:346-356 Warning names both built-in GPTQ/AWQ loaders and a remedy that applies

2. The new flow

flowchart TD
    A["AutoModelForCausalLM.from_pretrained"] --> B{"quant_method"}
    B -->|"gptq"| C["patched GPTQQuantizer.convert_model"]
    B -->|"awq"| E["patched AwqQuantizer._process_model_before_weight_loading"]
    C --> G{"hf_gptqmodel_prepare_model_for_load"}
    E --> G
    G -->|"not a native GPT-QModel checkpoint"| H["original loader path"]
    G -->|"native provenance"| I["defuser expands fused blocks and MoE experts"]
    I --> J["make_quant per serialized module with the dynamic config"]
    J --> K["Transformers assigns checkpoint tensors"]
    K --> L["patched post-weight hook"]
    L --> M["hf_gptqmodel_post_init_for_load"]
    style H fill:#fff3cd,stroke:#997404
    style M fill:#d9f2d9,stroke:#2e7d32
Loading

The AWQ wrapper reuses the same bridge entry points as the GPTQ wrapper, so
defusion, per-module dynamic bits/group sizes, rotation setup and paged-attention
opt-out all apply unchanged.

3. Failure and ambiguity contract

Situation Before After
Native AWQ MoE checkpoint, import gptqmodel first Attention quantized; every routed expert random Every serialized expert module rebuilt as a packed kernel
AWQ checkpoint written by another tool Transformers' AWQ path Unchanged; the bridge returns None
Native AWQ checkpoint, no gptqmodel import Random experts, no error Unchanged; only the save-time warning applies
AwqQuantizer API moved — Bridge probe logs a warning and stays inert, same as the Optimum shim

4. Change inventory

Area File +/- Purpose
Load bridge gptqmodel/__init__.py +72 Wrap AwqQuantizer pre/post hooks; installed from package import
Save warning gptqmodel/models/writer.py +5/−5 Name both built-in loaders and a remedy that works
Consumer regression tests/test_awq_moe_hf_load_4661.py +140 End-to-end AWQ MoE save→plain-HF load
Unit regression tests/test_hf_gptqmodel_load_bridge.py +81 Bridge routing contract and non-native control
Docs docs/hf-interop.md, README.md +50/−1 AWQ bridge flow, before/after table, discovery link

5. Tests / guarantees

Test node Invariant pinned
tests/test_awq_moe_hf_load_4661.py::test_plain_transformers_loads_every_awq_expert No MISSING/UNEXPECTED expert keys; every expert projection is a BaseQuantLinear
tests/test_awq_moe_hf_load_4661.py::test_plain_transformers_load_decodes_the_same_expert_words Expert qweight/qzeros/scales are bit-identical to GPTQModel.load, then logits match
tests/test_hf_gptqmodel_load_bridge.py::test_transformers_awq_quantizer_is_routed_through_the_native_bridge Bridge context is created and consumed; non-native checkpoints take the original path

Red evidence: both test_awq_moe_hf_load_4661.py nodes fail on origin/main
7395b40e0 at the intended assertions (expert keys MISSING/UNEXPECTED; logit
diff 0.0608), not at import or collection.

6. Validation

python -m pytest tests/test_awq_moe_hf_load_4661.py tests/test_hf_gptqmodel_load_bridge.py -q
# 24 passed
python -m pytest tests/test_awq_hf_cpu.py tests/test_awq_scaling_groups.py tests/test_checkpoint_interop_4053.py -q
# 185 passed, 5 skipped (3 mamba_ssm displaced the eager Torch path, 2 unbuildable families)
python -m ruff check gptqmodel/__init__.py gptqmodel/models/writer.py tests/test_hf_gptqmodel_load_bridge.py tests/test_awq_moe_hf_load_4661.py
# All checks passed

Also verified out-of-suite with an accelerator device map
(device_map={"": 0}): no MISSING/UNEXPECTED expert keys, the native AWQ kernels
are selected instead of the portable Torch one, and logits are bit-identical
(max abs diff 0.0) to GPTQModel.load.

7. Reviewer notes

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
@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator Author

Claimed in #4661 as well: this PR is the fix for it, no duplicate work in flight.

@Qubitium
Qubitium merged commit 3276171 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/4661-awq-hf-bridge branch November 17, 2026 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants