Skip to content

fix(hf): reselect AWQ offload kernels when the bridge is imported mid-load (#5395) - #5543

Merged
Qubitium merged 2 commits into
mainfrom
fix/5395-awq-offload-late-import
Nov 19, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/5395-awq-offload-late-import

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5395.

The load that imports gptqmodel from inside Transformers'
replace_with_awq_linear installs the AWQ bridge too late to run the prepare
hook for that same load. validate_environment still runs afterwards with the
placement resolved from device_map="auto" + max_memory, but the bridge had
no _gptqmodel_model, so _reselect_kernels_for_resolved_device_map returned
early. AUTO kept AwqMarlinLinear on a CPU/disk-offloaded layer and
post_init raised AWQ Marlin requires CUDA tensors (CPU spill) or the
disk-backed post_init refusal.

The fix captures the in-flight AwqQuantizer and model while the bridge is
being installed and publishes the same third-party reselect context the prepare
wrapper publishes. The existing validate_environment path then rebuilds the
offloaded meta modules with an offload-safe kernel before any checkpoint tensor
is assigned. Native checkpoints keep their post-weight repair.

What Changed

  • gptqmodel/__init__.py: _ensure_transformers_awq_gptqmodel_load_bridge
    calls the new _capture_awq_quantizer_from_frames before installing the
    hooks. The helper walks a bounded number of frames for the live
    AwqQuantizer/model pair of the prepare call that triggered the import,
    records _gptqmodel_model, and publishes
    _gptqmodel_hf_reselect_context for third-party checkpoints only.
  • tests/test_hf_bridge_thirdparty_offload_5262.py: a subprocess regression
    loads a third-party AWQ GEMM checkpoint with no prior gptqmodel import,
    device_map="auto", and a CPU or disk spill. It asserts the offloaded kernel
    is offload-safe and that logits match the resident load.
  • docs/hf-interop.md: the AWQ bridge page now documents the late-import
    offload selection flow, the old failure, and the before/after behavior.

No public API change. The behavior change is limited to the first AWQ load in
a process when the bridge is imported mid-prepare; later loads already took
the prepare path.

Tests

New regression (red on the base, green with the fix):

python -m pytest tests/test_hf_bridge_thirdparty_offload_5262.py -k late_import -q
# 2 passed

On the base without the fix the same nodes fail with
AUTO kept a CUDA-only kernel for a cpu-offloaded layer: ['AwqMarlinLinear', 'AwqMarlinLinear']
(and the disk variant). The user-visible failure on the reported checkpoint is
ValueError: AWQ Marlin requires CUDA tensors, got 'cpu'.

Affected bridge families:

python -m pytest \
  tests/test_hf_bridge_thirdparty_offload_5262.py \
  tests/test_hf_bridge_cpu_offload_5022.py \
  tests/test_hf_awq_bridge_import_order_4795.py \
  tests/test_hf_awq_repair_train_mode_4907.py -q
# 40 passed, 5 warnings

Lint gate:

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

Manual end-to-end check with the third-party AWQ GEMM checkpoint from
#5194/#5262, each in a fresh process without import gptqmodel:

Case Before After
device_map="auto", CPU spill ValueError: AWQ Marlin requires CUDA tensors, got 'cpu' Loads; offloaded layers use TorchAtenAwqLinear; finite logits
device_map="auto", disk spill RuntimeError: CPU-offloaded Marlin requires a writable in-memory weights map Loads; offloaded layers use TorchAtenAwqLinear; finite logits
device_map="auto", no spill Loads with Marlin Unchanged; Marlin
Explicit device map with a CPU entry Loads with an offload-safe kernel Unchanged
import gptqmodel first, CPU spill Loads Unchanged
Second load in the same process Loads Unchanged

CPU-spill logits differ from the resident load by 2.7e-2, below the
1.0e-1 fp16 tolerance for this fixture.

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

Model Support / Module Plan

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

AWQ onboarding safety

Not applicable: this changes loader kernel selection for an offloaded layer.
It does not change AWQ grouping, scale search, native replay, folding, or a
model definition.

Logic Flow Documentation

The flow changes at bridge-install time and at the resolved-map reselection
decision. Diagrams and the before/after table are in
docs/hf-interop.md.

Before

flowchart TD
    A["replace_with_awq_linear imports gptqmodel"] --> B["bridge installs for later calls only"]
    B --> C["validate_environment has no _gptqmodel_model"]
    C --> D["_reselect_kernels_for_resolved_device_map returns early"]
    D --> E["AwqMarlinLinear stays on the CPU/disk layer"]
    E --> F["post_init raises"]
    style E fill:#f8d7da,stroke:#b02a37
    style F fill:#f8d7da,stroke:#b02a37
Loading

The old mechanism is _ensure_transformers_awq_gptqmodel_load_bridge
(gptqmodel/__init__.py:982) installing only the class hooks, so
_reselect_kernels_for_resolved_device_map (gptqmodel/__init__.py:702)
returned at the missing _gptqmodel_model guard. The crash is in
_post_init_cpu_offloaded_marlin (gptqmodel/utils/model.py:2086).

After

flowchart TD
    A["plain AutoModelForCausalLM.from_pretrained"] --> B["AwqQuantizer._process_model_before_weight_loading"]
    B --> C["replace_with_awq_linear"]
    C --> D["import gptqmodel (first import, mid-load)"]
    D --> E["_ensure_transformers_awq_gptqmodel_load_bridge"]
    E --> F["_capture_awq_quantizer_from_frames"]
    F --> G{"AwqQuantizer and model on this stack?"}
    G -->|"no: package init or later entry point"| H["install the bridge hooks"]
    G -->|"yes: the load that triggered the import"| I["record _gptqmodel_model and publish the third-party reselect context"]
    I --> H
    H --> J["Transformers builds modules from the global config"]
    J --> K["_get_device_map resolves auto plus max_memory"]
    K --> L["patched AwqQuantizer.validate_environment"]
    L --> M{"offloaded layer with a CUDA-only kernel?"}
    M -->|"yes"| N["hf_gptqmodel_reselect_offloaded_kernels rebuilds the meta modules"]
    M -->|"no"| O["keep the AUTO kernel"]
    N --> P["Transformers assigns checkpoint tensors"]
    O --> P
    style N fill:#d9f2d9,stroke:#2e7d32
Loading
Decision Before After
Who can rebuild an offloaded module Only the prepare wrapper, which misses the import-triggering load The prepare wrapper, or the captured in-flight model at install time
When the rebuild runs validate_environment before tensor assignment Same
Third-party checkpoint context Missing on the first load Published from the captured model (gptqmodel/__init__.py:1018)
Native checkpoint context Post-weight repair from the serialized manifest Unchanged; no global-config context is published
No offload in the resolved map Marlin Marlin; hf_gptqmodel_reselect_offloaded_kernels finds nothing (gptqmodel/utils/hf_load.py:683)
  • This change does not alter a major logic flow, or the required diagrams and docs page are included below.
  • New-flow Mermaid diagram(s) describe the shipped code using real symbol names.
  • The removed/replaced mechanism is shown or tabulated with file:line evidence.
  • Failure and ambiguity paths are marked and visually distinct from success.
  • A reference page under docs/ repeats the diagrams, titled for the mechanism, and is linked from README.md.

Review Requirements

I or an AI agent (such as Codex or Claude) have:

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • For kernel-related changes, accuracy drift was measured against an applicable independent Torch FP32/FP64 oracle, or the reason this is not applicable is documented.
  • Fusion support changes preserve AUTO's native class and add actual-device
    FP16/BF16 tests where supported, native shape/workspace/descriptor checks,
    mutation, direct source calls, exact restoration and packed-storage checks.
  • The complete kernel inventory/device report lists unavailable dependencies,
    architecture restrictions and unqualified formats; skips are not validation.

Not applicable: no kernel math, packing, fusion, or workspace change. The fix
selects an existing offload-safe kernel; accuracy is covered by the
resident-load logit comparison in the new subprocess test and by the existing
bridge suites.

Notes

The issue suggested rebuilding at post-weight time. At that point the affected
modules are either CPU-resident (the checkpoint tensors are already assigned)
or meta with Accelerate's per-layer offload hook, so
hf_gptqmodel_reselect_offloaded_kernels would replace materialized modules
and drop their weights or hooks. Capturing the in-flight model at install time
keeps the fix in the same pre-weight-load path that #5194/#5262 already test.

Out of scope: the post-weight repair for native dynamic checkpoints and the
Transformers 5.23 mismatch gate are unchanged.

…-load (#5395)

Transformers' AWQ quantizer imports gptqmodel from inside replace_with_awq_linear, after its prepare hook has started. The bridge installed by that import only wraps later calls, so the current load's validate_environment had no model and skipped the resolved-map kernel reselection. A third-party AWQ checkpoint loaded with device_map="auto" and a max_memory CPU/disk spill kept AwqMarlinLinear and crashed in post_init.

Capture the in-flight quantizer and model from the prepare frame while installing the bridge, publish the same reselect context the prepare wrapper publishes, and let the existing validate_environment path rebuild the offloaded modules before weights are assigned. Add a subprocess regression that loads a third-party AWQ checkpoint with no prior gptqmodel import and a CPU and disk spill.
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