Skip to content

fix(hf): reselect a meta Marlin layer whose resolved placement is CPU (#6383) - #6387

Closed
Qubitium wants to merge 1 commit into
mainfrom
fix/6383-offload-meta
Closed

Qubitium wants to merge 1 commit into
mainfrom
fix/6383-offload-meta

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes a merge-induced regression from #6274 + #6253 that breaks published checkpoints.

Symptom

ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1 (meta.quantizer: ["gptqmodel:1.7.4"],
checkpoint_format: "gptq", bits=4, group_size=32, desc_act=true, sym=true) with

AutoModelForCausalLM.from_pretrained(path, device_map="auto",
    max_memory={0: "350MiB", "cpu": "14GiB"}, offload_folder="/tmp/off")
tree result
0dd8e8ab (before the wave) offload OK {'TorchLinear': 196}
3082b02d0 (merged main) ValueError: GPTQ Marlin requires CUDA tensors, got 'cpu'
this branch offload OK {'TorchLinear': 196}

CPU-only load is unaffected in every tree (TorchCpuAvx512Linear, finite logits).

Mechanism

device_map="auto" resolves max_memory after the kernel is selected, so at
_reselect_offloaded_marlin_kernels time an offloaded layer is still a meta placeholder with
no layer-level hook and no hf_device_map:

_marlin_needs_offload_kernel(meta=True)
  -> _marlin_offload_needs_reselection()
       -> _layer_offload_hook() raises RuntimeError   # nothing dispatched yet
       -> except RuntimeError: return False           # "the bridge does not own the map"

So the reselect declines. _post_init_cpu_offloaded_marlin then sees a payload that is no
longer meta (materialized onto the CPU device), finds no hook, and takes its resident path —
module.post_init() on CPU tensors, which Marlin rejects.

Before #6274 the shape-blind selection returned TorchLinear for these checkpoints, so the
path was never taken. #6274 keeps the AUTO kernel for group-aligned layers, and #6253's
reselect is what has to catch them.

Fix

gptqmodel/utils/model.py

This is deliberately narrow: only the "no hook yet + CPU placement" state changes.

Tests

New tests/test_marlin_offload_reselect_6383.py pins the decision table:

case 3082b02d0 this branch
meta + no hook + CPU placement -> reselect fail (returns False) pass
meta + writable in-memory snapshot -> keep Marlin (#5022) pass pass
meta + disk-backed snapshot -> reselect pass pass
resident GPU layer -> keep pass pass
resident CPU layer -> reselect pass pass

origin/main: 1 failed, 4 passed -> this branch: 5 passed.

Affected suites, CPU: test_marlin_offload_reselect_6383, test_hf_bridge_cpu_offload_5022,
test_marlin_cpu_offload_post_init, test_single_device_dispatch_4847,
test_hf_bridge_mixed_capability_5084, kernels/test_selection, test_cpu_offload_* ->
177 passed, 113 skipped (CUDA-gated).

ruff check clean.

`device_map="auto"` resolves `max_memory` only after the kernel was selected,
so at reselect time an offloaded layer is still a meta placeholder with no
layer-level hook and no `hf_device_map`. `_marlin_needs_offload_kernel` read
the missing hook as "the bridge does not own the map" and declined the
reselect; `_post_init_cpu_offloaded_marlin` then saw a payload that was no
longer meta (materialized onto the CPU device), found no hook, and called
Marlin's `post_init()` on CPU tensors:

    ValueError: GPTQ Marlin requires CUDA tensors, got `cpu`.

Since #6274's per-layer dispatcher keeps the AUTO kernel for group-aligned
layers, every published pre-7.3.4 GPTQ checkpoint loaded with
`device_map="auto"` + `max_memory` hit this. Measured on
`ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1`:

    0dd8e8a  offload OK  {'TorchLinear': 196}
    3082b02  offload FAIL ValueError: GPTQ Marlin requires CUDA tensors
    this tree offload OK  {'TorchLinear': 196}

A layer that already has a layer-level hook keeps the writable-snapshot path
(#5022) and stays on Marlin; only the "no hook yet, placement is CPU" state is
now reselected.

`tests/test_marlin_offload_reselect_6383.py` pins the decision: meta + no hook
+ CPU placement reselects, a writable in-memory snapshot keeps Marlin, a
disk-backed snapshot reselects, and resident GPU/CPU layers keep their
existing outcomes.
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Closing as superseded by #6391 (c32f2b346), which fixed the same decision point.

I filed #6383 with the real-checkpoint repro and opened this PR; while it was in review #6391 merged
a broader fix to the same function (_marlin_needs_offload_kernel now falls through from the
meta branch to runtime_device() instead of returning early). Re-measured on current main
(c32f2b346) with the same published checkpoint
(ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1):

case 0dd8e8ab 1615accf9 (before #6391) c32f2b346 (after #6391) this PR
max_memory={0: "350MiB", "cpu": "14GiB"} disk offload OK, TorchLinear×196 ValueError: GPTQ Marlin requires CUDA tensors OK, TorchLinear×196 OK
max_memory={0: "1GiB", "cpu": "14GiB"} mixed OK same ValueError OK, TorchLinear×196 OK

So main no longer reproduces #6383 and this branch's source change is redundant. I am not shipping a
duplicate edit to the same function.

One case is still broken on main (reported here for the record, and being fixed in a separate
PR): max_memory={0: "3GiB", "cpu": "14GiB"} fails with

ValueError: qzeros is on the meta device, we need a `value` to put in on 0.

That is a different defect — #6274's deferred qzeros is still a meta tensor when
accelerate.dispatch_model runs, because _materialize_hf_sym_qzeros only runs later, inside
hf_gptqmodel_post_init. It is not fixed by #6391 and not by this PR.

Thanks — #6383 can be closed with #6391.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant