Repository navigation
fix(awq): load packed AWQ MoE experts through the native HF bridge (#4661) - #4692
Merged
Merged
Conversation
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
Collaborator
Author
|
Claimed in #4661 as well: this PR is the fix for it, no duplicate work in flight. |
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.
TL;DR
Fixes #4661.
transformersloadsquant_method: "awq"through its ownAwqQuantizer, 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 testsand docs). Non-native AWQ checkpoints keep Transformers' own path.
1. The problem
_patch_optimum_gptqmodel_load_bridge(gptqmodel/__init__.py:412) wrapsoptimum.gptq.GPTQQuantizer.convert_model/post_init_model. Transformersdispatches 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:#b02a37Reproduced on
origin/main7395b40e0with a 2-layer / 4-expert OLMoE modelquantized by
AWQConfig(bits=4, group_size=32)and saved byGPTQModel.save,then loaded by plain
AutoModelForCausalLM.from_pretrainedin a process thatimported
gptqmodelfirst:model.layers.{0,1}.mlp.experts.{0..3}.*.qweight/qzeros/scalesUNEXPECTED(dropped)model.layers.{0,1}.mlp.experts.gate_up_proj/down_projMISSING(random init)TorchCpuAvx512AwqLinear=8, OlmoeExperts=2, Linear=1— only attention quantizedGPTQModel.load0.0608on a tiny modelThe save-time warning (
gptqmodel/models/writer.py:346) told users to "importgptqmodelbefore Transformers", a remedy that provably did nothing for AWQand described the gap as Optimum-GPTQ-specific.
Mechanisms replaced
gptqmodel/__init__.py:787(_patch_optimum_gptqmodel_load_bridge())_patch_transformers_awq_gptqmodel_load_bridge()AwqQuantizer._process_model_before_weight_loading→replace_with_awq_lineartransformers/quantizers/quantizer_awq.py:64hf_gptqmodel_prepare_model_for_load, falling back to the original when it returnsNoneAwqQuantizer._process_model_after_weight_loading→hf_gptqmodel_post_inittransformers/quantizers/quantizer_awq.py:80hf_gptqmodel_post_init_for_loadgptqmodel/models/writer.py:346-3562. 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:#2e7d32The AWQ wrapper reuses the same bridge entry points as the GPTQ wrapper, so
defusion, per-module
dynamicbits/group sizes, rotation setup and paged-attentionopt-out all apply unchanged.
3. Failure and ambiguity contract
import gptqmodelfirstNonegptqmodelimportAwqQuantizerAPI moved4. Change inventory
gptqmodel/__init__.pyAwqQuantizerpre/post hooks; installed from package importgptqmodel/models/writer.pytests/test_awq_moe_hf_load_4661.pytests/test_hf_gptqmodel_load_bridge.pydocs/hf-interop.md,README.md5. Tests / guarantees
tests/test_awq_moe_hf_load_4661.py::test_plain_transformers_loads_every_awq_expertBaseQuantLineartests/test_awq_moe_hf_load_4661.py::test_plain_transformers_load_decodes_the_same_expert_wordsqweight/qzeros/scalesare bit-identical toGPTQModel.load, then logits matchtests/test_hf_gptqmodel_load_bridge.py::test_transformers_awq_quantizer_is_routed_through_the_native_bridgeRed evidence: both
test_awq_moe_hf_load_4661.pynodes fail onorigin/main7395b40e0at the intended assertions (expert keys MISSING/UNEXPECTED; logitdiff
0.0608), not at import or collection.6. Validation
Also verified out-of-suite with an accelerator device map
(
device_map={"": 0}): no MISSING/UNEXPECTED expert keys, the native AWQ kernelsare selected instead of the portable Torch one, and logits are bit-identical
(max abs diff
0.0) toGPTQModel.load.7. Reviewer notes
import gptqmodelhappens before Transformersbuilds the model, exactly like the existing GPTQ bridge. A process that never
imports
gptqmodelstill gets only the save-time warning; changing that wouldrequire upstream Transformers support.
qzeros_formatremains reported MISSING on this path: it is optional layoutmetadata that is deliberately excluded from checkpoints
(
gptqmodel/nn_modules/qlinear/__init__.py:1345), and_load_from_state_dictfilters it from strict loads.
ScaledActivation,used by the
starcoder2/bloom/mpt-style families) already round-trip:saving is unaffected because this change only touches the load path. That
family was not added here to keep the change scoped to [BUG] AWQ MoE checkpoints load in plain transformers with random experts (KL 3.26): #4084's HF bridge only patches Optimum GPTQ, and the save warning's 'import gptqmodel first' remedy does not apply to AWQ #4661.