Skip to content

[BUG] #4829 (fix of #4807) incomplete: after a cancelled/failed quantize() with default offload, layers past the cancel point stay lazy meta shells (no hook) -> next forward still fails 'Tensor on device cpu is not on the expected device meta' #4860

Closed

Description

@Qubitium

Found on main @ 5cded90, RTX 4090, torch 2.15.

What is wrong

#4829 (the fix for #4807) keeps the per-run offload root alive after a cancelled or failed quantize(), so the layers that finished before the cancel stay readable through their disk-offload hooks. That part works.

The fix misses the layers after the cancel point. With the default offload_to_disk=True, the model is loaded as a lazy shell, so every layer the looper never reached is still on meta, with no offload hook and no bundle. The success path materializes these layers with _materialize_remaining_meta_modules() (gptqmodel/looper/module_looper.py:2115). The failure branch (module_looper.py:2061-2106) still skips that call. As a result, the next forward still fails, now on the first unreached layer instead of on layer 0:

RuntimeError: Tensor on device cpu is not on the expected device meta!

#4807 already counted these modules: its ON_META 23 was 7 + 7 offloaded modules in layers 0-1 plus the 9 lazy modules of layer 3. Its suggested fix also said "materialize or rehome the rest".

The new regression test tests/test_cancelled_quantize_disk_offload_4807.py misses this. It uses a 2-layer model and cancels at layer_idx == 1, which is the last layer, so no unreached layer exists.

Repro

Qwen2.5-0.5B-Instruct truncated to 4 layers, GPTQ 4-bit g128, 16 C4 samples, default offload:

m = GPTQModel.load(MODEL, C.GPTQConfig(bits=4, group_size=128), dtype=torch.float16)  # offload_to_disk=True
def cb(**kw):
    if kw.get("stage") == "forward_start" and kw.get("layer_idx") == AT:
        return StopMainLoop          # same result with `raise RuntimeError(...)`
m.subset_callback = cb
try:
    m.quantize(C.CalibrationConfig(dataset=calib, batch_size=4))
except BaseException as e:
    print("ERR", type(e).__name__)
for i, L in enumerate(m.model.model.layers):
    ...  # count modules with meta params / with an _hf_hook
x = tok("Hello world, the capital of France is", return_tensors="pt").input_ids  # embed is on CPU
with torch.inference_mode():
    m.model(x)

Output with AT=2, on main:

ERR StopMainLoop Subset callback requested stop at layer 2 subset 0
  layer0: meta-mods=7 hooked=7 q_proj=TorchLinear      <- fixed by #4829, loadable
  layer1: meta-mods=7 hooked=7 q_proj=TorchLinear
  layer2: meta-mods=0 hooked=0 q_proj=AutoNF12HookedLinear   (in-flight, restored by #4580)
  layer3: meta-mods=9 hooked=0 q_proj=Linear           <- never materialized
FWD_FAIL RuntimeError Tensor on device cpu is not on the expected device meta!

With AT=1 and an injected RuntimeError, layers 2 and 3 are both left with meta-mods=9 hooked=0, and the forward fails the same way.

With AT=3 (cancel at the last layer, the case the PR test covers), the forward works (FWD_OK). Before #4829, at 9d2ea51^, AT=2 failed with FileNotFoundError .../model.layers.0.self_attn.q_proj/module.safetensors.

The offload_to_disk=False control gives ON_META 0 and FWD_OK for the same cancel.

Suggested fix

In the failure branch, after _restore_offloaded_base_modules(strict=False), call materialize_remaining_meta_modules(self.gptq_model, device=CPU) (non-strict, best effort). It touches only modules that are still on meta and have no offload hook, so it does not undo #4829's choice to keep the finished layers lazily loaded. Apply the same change to WeightOnlyLooper's failure branch.

Extend test_cancelled_quantize_disk_offload_4807.py to at least 3 layers and cancel at a layer that is not the last one.

Severity

Medium. This is the default config and the documented cancel path. The in-session model still cannot run a forward after a cancel or failure anywhere except the last layer, which is what #4546, #4807 and #4829 set out to guarantee.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4893

    Finish #4829: a cancelled/failed quantize() with default offload must release layers past the cancelled point (offload root cleanup); add a cancellation test with default offload.

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