Repository navigation
fix(hf): reselect offload-safe kernels for third-party checkpoints (#5262) - #5277
Merged
Merged
Conversation
Qubitium
marked this pull request as ready for review
November 18, 2026 16:16
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.
fix(hf): re-select offload-safe kernels for third-party checkpoints
Fixes #5262.
Problem
Loading an AutoAWQ / AutoGPTQ checkpoint through Transformers with
device_map="auto"+ amax_memorybudget that spills to CPU or disk stillcrashed after #5247:
The same checkpoints load with
GPTQModel.load(..., device_map="auto", max_memory=...), which picksAwqGEMMTritonLinear/TritonV2Linear.Root cause
#5247 re-selects kernels after Transformers resolves the map, but only for
checkpoints
hf_gptqmodel_prepare_model_for_loadtreats as native(
_is_native_gptqmodel_config). Third-party checkpoints have no GPT-QModelmeta.quantizer, so the bridge returnsNone:self._gptqmodel_model;_reselect_kernels_for_resolved_device_mapreturns early when the model orcontext is missing, and
hf_gptqmodel_reselect_offloaded_kernelsreturnsFalseoncontext is None.Transformers/Optimum therefore keep the Marlin kernel selected before
max_memorywas applied. Once the map spills a layer,post_initruns on CPUor a disk-backed weights map and fails.
Before
flowchart TD A["AutoModelForCausalLM.from_pretrained<br/>device_map='auto' + max_memory"] --> B["AwqQuantizer / GptqHfQuantizer<br/>preprocess_model"] B --> C{"hf_gptqmodel_prepare_model_for_load<br/>_is_native_gptqmodel_config?"} C -- "native" --> D["native context<br/>model._gptqmodel_hf_load_context"] C -- "third-party" --> E["original_prepare / Optimum convert_model<br/>AUTO picks MarlinLinear / AwqMarlinLinear"] E --> F["_gptqmodel_model unset (AWQ)<br/>or no context (GPTQ)"] D --> G["resolve map from max_memory<br/>validate_environment(device_map)"] F --> G G --> H{"_reselect_kernels_for_resolved_device_map"} H -- "native context" --> I["rebuild offloaded modules<br/>Triton / TorchAten"] H -- "model or context missing" --> J["return early"] J --> K["post_init on CPU/disk<br/>Marlin requires CUDA / writable map"] K --> L["load fails"]After
flowchart TD A["AutoModelForCausalLM.from_pretrained<br/>device_map='auto' + max_memory"] --> B["AwqQuantizer / GptqHfQuantizer<br/>preprocess_model"] B --> C{"hf_gptqmodel_prepare_model_for_load<br/>_is_native_gptqmodel_config?"} C -- "native" --> D["native context<br/>model._gptqmodel_hf_load_context"] C -- "third-party" --> E["original_prepare / Optimum convert_model<br/>AUTO picks Marlin initially"] C -- "third-party" --> F["hf_gptqmodel_reselect_context<br/>from HF quantization_config"] F --> G["model._gptqmodel_hf_reselect_context<br/>separate from the native post-init context"] D --> H["resolve map from max_memory<br/>validate_environment(device_map)"] E --> H G --> H H --> I{"_reselect_kernels_for_resolved_device_map"} I -- "native context" --> J["rebuild offloaded modules"] I -- "fallback context" --> J J --> K["offload-safe kernel on CPU/disk<br/>Triton / TorchAten; resident layers keep Marlin"] K --> L["post_init + forward OK"]Fix
gptqmodel/utils/hf_load.pyhf_gptqmodel_reselect_context(): builds the minimalHFGPTQModelLoadContext(quantize config from the HFquantization_config, normalized backend, dtype,lm_head_name) thathf_gptqmodel_reselect_offloaded_kernelsreads.HFGPTQModelLoadContext.quant_linearbecomes optional; only the nativepath installs modules.
gptqmodel/__init__.pyquantizer._gptqmodel_model = model.original_prepare/original_convert_modelfallback they publishthe minimal context under
model._gptqmodel_hf_reselect_context._reselect_kernels_for_resolved_device_mapprefers the native context andfalls back to the reselect context.
_gptqmodel_hf_load_context, sothird-party loads keep Transformers'/Optimum's own post-init path.
Resident maps are not re-routed (no offloaded modules -> helper returns
False).Tests
New
tests/test_hf_bridge_thirdparty_offload_5262.py(11 tests):metaremoved from bothconfig.jsonandquantize_config.json(synthetic third-party; no real AutoAWQ/AutoGPTQcheckpoint is required in CI);
device_map="auto"+max_memoryspills to CPU and disk, forthird-party and native controls: asserts the offloaded projection uses
TritonV2Linear/AwqGEMMTritonLinear, logits are finite, and match theresident load;
device_map={"": 0}still selectsMarlinLinear/AwqMarlinLinear;hf_gptqmodel_reselect_context.Red -> green
c1807f889: 5 failed, 6 passed(third-party AWQ/GPTQ CPU+disk nodes fail; native and no-offload controls
pass). Log:
.scratch/pytest_5262_red_full2.log..scratch/pytest_5262_after2.log.The issue's real checkpoints were also reproduced locally (offline copies at
/monster/data/model):gptq_nometafail on CPU and disk with the reported Marlinerrors; native controls load;
TorchAten*, mixed600/800 MiB maps use
AwqGEMMTritonLinear/TritonV2Linearfor offloadedlayers and keep Marlin on resident layers.
Verification
tests/test_hf_bridge_thirdparty_offload_5262.py: 11 passed.tests/test_hf_bridge_cpu_offload_5022.py+tests/test_hf_gptqmodel_load_bridge.py:46 passed.
pre-existing/environmental and reproduce identically on pristine
(
test_hf_awq_bridge_import_order_4795.py,test_hf_awq_repair_train_mode_4907.py);the
test_awq_hf_cpu.py[moe]failure is an offline cache miss.ruff check gptqmodel docs/eora tests setup.py: All checks passed(ruff 0.14.2).
Not verified
gptqmodelimport insidereplace_with_awq_linear) is not covered for third-party checkpoints: theprepare hook never runs for that load, so no fallback context is published.
Native checkpoints still use the existing repair path.