Skip to content

fix(hf): reselect offload-safe kernels for third-party checkpoints (#5262) - #5277

Merged
Qubitium merged 1 commit into
mainfrom
fix/5262-reselect-thirdparty
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5262-reselect-thirdparty

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

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" + a max_memory budget that spills to CPU or disk still
crashed after #5247:

ValueError: AWQ Marlin requires CUDA tensors, got `cpu`.
RuntimeError: CPU-offloaded Marlin requires a writable in-memory weights map: [...]

The same checkpoints load with GPTQModel.load(..., device_map="auto", max_memory=...), which picks AwqGEMMTritonLinear / TritonV2Linear.

Root cause

#5247 re-selects kernels after Transformers resolves the map, but only for
checkpoints hf_gptqmodel_prepare_model_for_load treats as native
(_is_native_gptqmodel_config). Third-party checkpoints have no GPT-QModel
meta.quantizer, so the bridge returns None:

  • the AWQ bridge never sets self._gptqmodel_model;
  • the GPTQ bridge sets it but has no load context;
  • _reselect_kernels_for_resolved_device_map returns early when the model or
    context is missing, and hf_gptqmodel_reselect_offloaded_kernels returns
    False on context is None.

Transformers/Optimum therefore keep the Marlin kernel selected before
max_memory was applied. Once the map spills a layer, post_init runs on CPU
or 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"]
Loading

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"]
Loading

Fix

  • gptqmodel/utils/hf_load.py
    • Add hf_gptqmodel_reselect_context(): builds the minimal
      HFGPTQModelLoadContext (quantize config from the HF
      quantization_config, normalized backend, dtype, lm_head_name) that
      hf_gptqmodel_reselect_offloaded_kernels reads.
    • HFGPTQModelLoadContext.quant_linear becomes optional; only the native
      path installs modules.
  • gptqmodel/__init__.py
    • Both bridges always publish quantizer._gptqmodel_model = model.
    • On the original_prepare / original_convert_model fallback they publish
      the minimal context under model._gptqmodel_hf_reselect_context.
    • _reselect_kernels_for_resolved_device_map prefers the native context and
      falls back to the reselect context.
    • The fallback context is kept separate from _gptqmodel_hf_load_context, so
      third-party loads keep Transformers'/Optimum's own post-init path.
  • Native-checkpoint behavior is unchanged; the resolved map is not modified.
    Resident maps are not re-routed (no offloaded modules -> helper returns
    False).

Tests

New tests/test_hf_bridge_thirdparty_offload_5262.py (11 tests):

  • tiny Qwen2 quantized to AWQ-GEMM and GPTQ v1 with GPTQModel;
  • a copy of each checkpoint with meta removed from both config.json and
    quantize_config.json (synthetic third-party; no real AutoAWQ/AutoGPTQ
    checkpoint is required in CI);
  • device_map="auto" + max_memory spills to CPU and disk, for
    third-party and native controls: asserts the offloaded projection uses
    TritonV2Linear / AwqGEMMTritonLinear, logits are finite, and match the
    resident load;
  • no-offload control: third-party + device_map={"": 0} still selects
    MarlinLinear / AwqMarlinLinear;
  • unit test for hf_gptqmodel_reselect_context.

Red -> green

  • On pristine 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.
  • After the fix: 11 passed. Log: .scratch/pytest_5262_after2.log.

The issue's real checkpoints were also reproduced locally (offline copies at
/monster/data/model):

  • before: AWQ and gptq_nometa fail on CPU and disk with the reported Marlin
    errors; native controls load;
  • after: all 8 cases load and forward; all-CPU maps use TorchAten*, mixed
    600/800 MiB maps use AwqGEMMTritonLinear / TritonV2Linear for offloaded
    layers 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.
  • HF bridge batch (15 files): 223 passed, 3 skipped; 3 failures + 4 errors are
    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.
  • Offload/device-map batch 1: 156 passed, 1 skipped.
  • Offload batch 2: 59 passed.
  • ruff check gptqmodel docs/eora tests setup.py: All checks passed
    (ruff 0.14.2).

Not verified

  • No CI run (local only).
  • No host other than the shared local GPU 1 (RTX 4090) was used.
  • The import-order-late AWQ path (first gptqmodel import inside
    replace_with_awq_linear) is not covered for third-party checkpoints: the
    prepare hook never runs for that load, so no fallback context is published.
    Native checkpoints still use the existing repair path.
  • No third-party AWQ GEMV / other formats; the issue is AWQ GEMM and GPTQ v1.
  • No performance measurement; only kernel identity and correctness.

@Qubitium
Qubitium marked this pull request as ready for review November 18, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant