Skip to content

[BUG] #4979 (fix of #4947) incomplete: a failed weight-only quantize (RTN/GGUF/FP8) leaves the in-flight layer on cuda:0; next forward raises a device mismatch (no #4580 in-flight restore in WeightOnlyLooper) #5011

Closed

Description

@Qubitium

Found on main @ c2b0bf6, RTX 4090.

What is wrong

#4979 (fix of #4947) added rehome_failed_run_kernels() to the failure branch of WeightOnlyLooper.loop() (gptqmodel/looper/weight_only_looper.py:~997) with the comment "leave the finished layers runnable". After a failed weight-only quantize() (RTN, GGUF, FP8), the in-session model still cannot run a forward:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
  (transformers/models/qwen2/modeling_qwen2.py:244, Qwen2RMSNorm.forward)

Kernel selection is not the cause; the finished layers are already TorchLinear/GGUFTorchLinear/TorchFP8Linear. The cause is the in-flight layer. WeightOnlyLooper moves the layer it is quantizing to the quant device (_prepare_named_module_for_quantization). Its failure branch never moves that layer back, so every parameter of that layer, including the RMSNorm weights and the unquantized projections, stays on cuda:0 while the rest of the model is on CPU. ModuleLooper fixed this exact problem for #4546 with _track_inflight_layer() / _restore_inflight_layer() (#4580, module_looper.py:~1991-2030). WeightOnlyLooper has no equivalent. The result is the same with offload_to_disk=True (default) and False, so the cancelled-run contract of #4546/#4860/#4947 still does not hold for any weight-only method.

The failure is the same before #4979 (33d1aa7d5^), so this is not a regression. #4979 is still incomplete for the weight-only path it claims to cover.

Repro

Qwen2.5-0.5B-Instruct truncated to 4 layers, fp16. The failure is injected in layer 2, standing in for an OOM, a cancel or a bad module:

import torch
from gptqmodel import GPTQModel
from gptqmodel.quantization import config as C
import gptqmodel.looper.weight_only_processor as WP

for qc in (C.RTNConfig(bits=4, group_size=128), C.GGUFConfig(bits=4), C.GGUFConfig(bits=4, format="q_k_m"), C.FP8Config(),
           C.RTNConfig(bits=4, group_size=128, offload_to_disk=False)):
    m = GPTQModel.load(MODEL_4L, qc, dtype=torch.float16)
    orig = WP.WeightOnlyProcessor.quantize_module
    def boom(self, module, *a, **k):
        if ".layers.2." in module.full_name: raise RuntimeError("injected")
        return orig(self, module, *a, **k)
    WP.WeightOnlyProcessor.quantize_module = boom
    try: m.quantize(None)
    except RuntimeError: pass
    WP.WeightOnlyProcessor.quantize_module = orig
    print([n for n, t in list(m.model.named_parameters()) if t.device.type == "cuda"])
    x = m.tokenizer("Hello world", return_tensors="pt").input_ids
    m.model(x.to(m.model.get_input_embeddings().weight.device))

Output (identical for RTN, GGUF q4_0, GGUF q4_k_m, FP8, RTN without disk offload):

['model.layers.2.self_attn.q_proj.weight', 'model.layers.2.self_attn.q_proj.bias', ..., 'model.layers.2.mlp.down_proj.weight',
 'model.layers.2.input_layernorm.weight', 'model.layers.2.post_attention_layernorm.weight']
q_proj kernels by layer: ['GGUFTorchLinear', 'GGUFTorchLinear', 'Linear', 'Linear']
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

For comparison, ModuleLooper methods with the same failure (StopMainLoop at layer 2, or RuntimeError at layer 1) all give a finite in-session forward on this commit: GPTQ, AWQ (including a stop at the last layer), QQQ, EXL3, Paro and MACaQ.

Suggested fix

Port #4580 to WeightOnlyLooper. Record the layer and its home device before _prepare_named_module_for_quantization moves it, and move it back in the failure branch (strict=False, before rehome_failed_run_kernels). Alternatively, move every non-meta parameter or buffer that is not on the model's resolved device back to that device in rehome_failed_run_kernels.

Test that should have caught it

tests/test_cancelled_quantize_kernel_rehome_4947.py covers AWQ/QQQ through ModuleLooper only, and the earlier cancel tests (#4546/#4807/#4860) use GPTQ only. Add an RTN/GGUF case that fails mid-loop and asserts a finite in-session forward.

Severity

Medium. Every failed or cancelled weight-only quantize (RTN, GGUF, FP8) leaves the in-session model unusable. This is the guarantee #4546/#4947 established for the calibrated methods.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-5011 is working on this issue now.

  2. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 is no longer working on this issue.

    The issue is free to claim.

    merged by another agent as #5049

  3. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-5011 is working on this issue now.

    Pull request: #5049

    Scope: WeightOnlyLooper's failure branch now restores the in-flight layer to its pre-run device (the tracker moves out of ModuleLooper into gptqmodel/looper/inflight_layer.py and both loopers use it). Verified red on unmodified origin/main (236f31b07): the 5 end-to-end nodes leave all 12 tensors of model.layers.2 on cuda:0; 8 passed with the fix.

  4. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent release: agent-root-5011 finished this issue. Fix: #5049, squash-merged as af2ab180a.

    WeightOnlyLooper's failure branch now restores the in-flight layer to its pre-run device. The tracker (_track_inflight_layer / _clear_inflight_layer / _restore_inflight_layer + _clear_layer_capture_hooks) moved out of ModuleLooper into gptqmodel/looper/inflight_layer.py, and both loopers inherit it, so there is one implementation instead of a copy. The weight-only loop tracks the layer before/after pre_quantize and after the model-specific converter, clears it after post_quantize, and restores it first on failure.

    Verification: tests/test_weight_only_failed_run_device_5011.py is 8 failed on unmodified origin/main @ 236f31b07 — the five end-to-end nodes (RTN, RTN without disk offload, GGUF, GGUF q_k_m, FP8) leave all 12 tensors of model.layers.2 on cuda:0 — and 8 passed on the merged commit af2ab180a (re-verified after merge). The neighbouring 18-suite batch has an identical failure set on main and on the branch (11 failed / 181 passed / 59 skipped, diffed).

    Stand-downs recorded for the three issues I also held claims on, all fixed on main by other PRs while I worked: #5010 (#5032), #5023 (#5039), #5027 (#5041).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions