Repository navigation
fix(hf): reselect AWQ offload kernels when the bridge is imported mid-load (#5395) - #5543
Merged
Merged
Conversation
…-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.
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.
Summary
Fixes #5395.
The load that imports
gptqmodelfrom inside Transformers'replace_with_awq_linearinstalls the AWQ bridge too late to run the preparehook for that same load.
validate_environmentstill runs afterwards with theplacement resolved from
device_map="auto"+max_memory, but the bridge hadno
_gptqmodel_model, so_reselect_kernels_for_resolved_device_mapreturnedearly. AUTO kept
AwqMarlinLinearon a CPU/disk-offloaded layer andpost_initraisedAWQ Marlin requires CUDA tensors(CPU spill) or thedisk-backed
post_initrefusal.The fix captures the in-flight
AwqQuantizerand model while the bridge isbeing installed and publishes the same third-party reselect context the prepare
wrapper publishes. The existing
validate_environmentpath then rebuilds theoffloaded 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_bridgecalls the new
_capture_awq_quantizer_from_framesbefore installing thehooks. 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_contextfor third-party checkpoints only.tests/test_hf_bridge_thirdparty_offload_5262.py: a subprocess regressionloads a third-party AWQ GEMM checkpoint with no prior
gptqmodelimport,device_map="auto", and a CPU or disk spill. It asserts the offloaded kernelis offload-safe and that logits match the resident load.
docs/hf-interop.md: the AWQ bridge page now documents the late-importoffload 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 passedOn 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 warningsLint gate:
Manual end-to-end check with the third-party AWQ GEMM checkpoint from
#5194/#5262, each in a fresh process without
import gptqmodel:device_map="auto", CPU spillValueError: AWQ Marlin requires CUDA tensors, got 'cpu'TorchAtenAwqLinear; finite logitsdevice_map="auto", disk spillRuntimeError: CPU-offloaded Marlin requires a writable in-memory weights mapTorchAtenAwqLinear; finite logitsdevice_map="auto", no spillimport gptqmodelfirst, CPU spillCPU-spill logits differ from the resident load by
2.7e-2, below the1.0e-1fp16 tolerance for this fixture.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:#b02a37The 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_modelguard. 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:#2e7d32validate_environmentbefore tensor assignmentgptqmodel/__init__.py:1018)hf_gptqmodel_reselect_offloaded_kernelsfinds nothing (gptqmodel/utils/hf_load.py:683)file:lineevidence.docs/repeats the diagrams, titled for the mechanism, and is linked fromREADME.md.Review Requirements
I or an AI agent (such as Codex or Claude) have:
Kernel Accuracy Requirements
FP16/BF16 tests where supported, native shape/workspace/descriptor checks,
mutation, direct source calls, exact restoration and packed-storage checks.
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_kernelswould replace materialized modulesand 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
dynamiccheckpoints and theTransformers 5.23 mismatch gate are unchanged.