Skip to content

fix(awq): load dynamic AWQ checkpoints on transformers 5.23 (#5322) - #5335

Merged
Qubitium merged 1 commit into
mainfrom
fix/5322-awq-dynamic-transformers523
Nov 19, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5322-awq-dynamic-transformers523

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Summary

Transformers 5.23 tightened core_model_loading.set_param_for_module: a
checkpoint tensor whose shape differs from the meta module is now recorded as a
mismatched key unless the active quantizer reports param_needs_quantization
for it.

- if ref is not None and param_value.shape != expected_shape and hf_quantizer is None:
+ if (ref is not None and param_value.shape != expected_shape
+     and (hf_quantizer is None or not hf_quantizer.param_needs_quantization(model, target_name))):

AwqQuantizer inherits the base implementation, which always returns False
because a pre-quantized checkpoint has nothing to quantize. A GPT-QModel AWQ
checkpoint with dynamic per-module group_size therefore builds its modules
from the global group_size, the checkpoint's wider qzeros/scales are
recorded as MISMATCH, and _finalize_model_loading raises
You set ignore_mismatched_sizes to False ... before postprocess_model can
run hf_gptqmodel_repair_awq_load. The documented plain
AutoModelForCausalLM.from_pretrained flow in docs/peft.md fails for every
dynamic AWQ checkpoint on 5.23.

Fixes #5322

What Changed

  • gptqmodel/utils/hf_load.py
    • _hf_quantization_config_is_native_awq: reads the live
      quantization_config and decides provenance (explicit gptqmodel
      quantizer entry, meta.uri, or a dynamic map). An explicit optimum or
      third-party quantizer entry wins, so another tool's dynamic field is not
      reinterpreted.
    • hf_gptqmodel_awq_tolerated_mismatch: accepts a mismatch only for one of
      qweight/qzeros/scales/g_idx/bias on a BaseQuantLinear in a
      native AWQ checkpoint. Dense parameters and unknown paths stay reported.
  • gptqmodel/__init__.py: the AWQ bridge wrapper also wraps
    AwqQuantizer.param_needs_quantization with that guard. The accepted tensors
    are then rebuilt from the serialized per-module config by the existing
    post-weight repair, so the result still matches GPTQModel.load.
  • docs/hf-interop.md: new "Transformers 5.23 mismatch gate" section with
    before/after flow and situation table; failure-contract and test tables
    extended.
  • docs/peft.md: notes that the repair now survives the 5.23 load check.
  • tests/test_hf_awq_dynamic_mismatch_5322.py: new fast unit test.

No API change. Transformers <= 5.22 behavior is unchanged (the old check was
already skipped whenever a quantizer was active), and the guard is inert for
GPTQ and third-party AWQ checkpoints.

Tests

# Reproduced first on the issue's own test, unmodified (red):
pytest tests/test_hf_awq_bridge_import_order_4795.py -q
# 2 failed, 1 passed
#   test_plain_transformers_load_honors_dynamic_awq_on_cpu
#   test_plain_transformers_load_honors_dynamic_awq_on_cuda
#   RuntimeError: You set `ignore_mismatched_sizes` to `False` ...
#   model.layers.1.self_attn.q_proj.qzeros | MISMATCH | ckpt [4, 16] vs model [1, 16]

# Green, transformers 5.23.0 (wheel unpacked beside the environment, not installed):
PYTHONPATH=/path/to/transformers-5.23.0:$PWD pytest \
  tests/test_hf_awq_bridge_import_order_4795.py \
  tests/test_hf_awq_repair_train_mode_4907.py \
  tests/test_hf_awq_dynamic_mismatch_5322.py \
  tests/test_awq_moe_hf_load_4661.py \
  tests/test_hf_optimum_bridge_import_order.py -q
# 19 passed

# Control, transformers 5.22.2 (environment default):
pytest tests/test_hf_awq_bridge_import_order_4795.py \
  tests/test_hf_awq_repair_train_mode_4907.py \
  tests/test_hf_awq_dynamic_mismatch_5322.py \
  tests/test_awq_moe_hf_load_4661.py \
  tests/test_hf_optimum_bridge_import_order.py \
  tests/test_awq_position_kwargs.py tests/test_awq_scale_search_safety.py -q
# 66 passed

bash format/check.sh
# ruff 0.14.2, All checks passed

The 5.23 red/green pair runs the CPU and CUDA nodes of the issue's test: both
fail on the unpatched tree and pass with it, with the rebuilt modules'
group_size/qzeros shape and NLL matching GPTQModel.load.

Skips: none. Both nodes ran on their real device.

Model Support / Module Plan

Not applicable: no model type, alias, definition, or module plan change.

AWQ onboarding safety

Not applicable to quantization math/grouping: this changes only how an HF load
accepts an already-serialized checkpoint's tensors. No source/consumer
relationship, scale search, or replay context changes. The repaired modules are
still built from the manifest and verified against GPTQModel.load in
tests/test_hf_awq_bridge_import_order_4795.py.

Logic Flow Documentation

The change alters a load decision (which shape mismatches are accepted), so the
flow is documented in docs/hf-interop.md ("Transformers 5.23 mismatch gate"),
already linked from the interop section of README.md.

flowchart TD
    A["plain AutoModelForCausalLM.from_pretrained"] --> B["AwqQuantizer builds modules from the global config"]
    B --> C["core_model_loading.set_param_for_module"]
    C --> D{"shape differs and param_needs_quantization false?"}
    D -->|"before 5.23: quantizer active"| E["assign tensor, continue"]
    D -->|"5.23: not quantizing"| F["mismatched_keys"]
    F --> G["_finalize_model_loading raises"]
    G --> H["post-weight repair never runs"]
    C --> I["patched param_needs_quantization"]
    I --> J{"native GPT-QModel AWQ and a packed buffer of a packed module?"}
    J -->|"yes"| E
    J -->|"no"| D
    style G fill:#f8d7da,stroke:#b02a37
    style E fill:#d9f2d9,stroke:#2e7d32
Loading

Replaced mechanism: the unconditional hf_quantizer is None early-exit in
transformers/core_model_loading.py:1458-1461 (5.23), whose new condition
not hf_quantizer.param_needs_quantization(...) is now answered by the bridge.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

Not applicable: no kernel, fusion, or dispatch change.

Notes

  • Scope is the dense (non-MoE) dynamic AWQ path on Transformers 5.23. The
    pre-existing contract is unchanged: a per-expert AWQ checkpoint still needs
    import gptqmodel before from_pretrained, because those tensors are
    UNEXPECTED rather than shape-mismatched.
  • The Optimum GPTQ path does not expose the same failure: its
    convert_model builds the per-module geometry before tensors are assigned.
    That path was not modified.

@ZX-ModelCloud
ZX-ModelCloud marked this pull request as ready for review November 19, 2026 02:44
@Qubitium
Qubitium merged commit 2dce572 into main Nov 19, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5322-awq-dynamic-transformers523 branch November 19, 2026 02:50
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