Repository navigation
Description
Found on main @ dba142d, RTX 4090, torch 2.15.
What is wrong
#4580 (the fix for #4546) restores the in-flight layer after a cancelled or failed quantize(), so that the model can still run a forward. Its regression test (tests/test_cancelled_quantize_layer_cleanup_4546.py:67) builds the model with offload_to_disk=False. With the default offload_to_disk=True, a cancelled or failed run still leaves the model unusable, and its completed layers cannot be recovered.
The failure branch of ModuleLooper.loop() (gptqmodel/looper/module_looper.py:2042-2063) does this:
- fences the work,
- restores the in-flight layer,
- restores the base (unquantized) offloaded modules,
- calls
release_private_offload_root(), which deletes the per-run offload directory.
The success path additionally runs _restore_disk_offloaded_modules() and _materialize_remaining_meta_modules() (:2070-2071), which bring the already quantized, disk-offloaded layers back into memory. The failure path skips both. Every layer finished before the cancel therefore stays a meta shell whose offload hook points at a module.safetensors that was just deleted.
Repro (Qwen2.5-0.5B-Instruct truncated to 4 layers, GPTQ 4-bit g128, 16 C4 samples)
Cancel through the supported path, StopMainLoop from subset_callback at layer 2:
from gptqmodel import GPTQModel
from gptqmodel.quantization import config as C
from gptqmodel.looper.module_looper import StopMainLoop
m = GPTQModel.load(MODEL, C.GPTQConfig(bits=4, group_size=128), dtype=torch.float16) # offload_to_disk defaults to True
def cb(**kw):
if kw.get("stage") == "forward_start" and kw.get("layer_idx") == 2:
return StopMainLoop
m.subset_callback = cb
try:
m.quantize(C.CalibrationConfig(dataset=calib, batch_size=4))
except StopMainLoop:
pass
meta = {n.rsplit(".", 1)[0] for n, p in m.model.named_parameters() if p.device.type == "meta"}
print(len(meta), sorted(meta)[:3])
with torch.inference_mode():
m.model(m.tokenizer("Hello world", return_tensors="pt").input_ids.cuda())Output:
ERR StopMainLoop: Subset callback requested stop at layer 2 subset 0
OFFLOAD_DIR at_return (exists=True, files=0, 0.0 MiB)
ON_META 23 ['model.layers.0.mlp.down_proj', 'model.layers.0.mlp.gate_proj', 'model.layers...']
FWD_FAIL FileNotFoundError No such file or directory: <offload_to_disk_path>/run-0f1b0da843484b54/model.layers.0.self_attn.q_proj/module.safetensors
Every failure mode gives the same result: KeyboardInterrupt or an exception raised inside GPTQ.quantize at the 10th module, a layer_callback that raises RuntimeError or KeyboardInterrupt, an exception from a subset callback, and an exception in a layer forward during calibration. The number of meta modules is 23-30 and the forward always fails with FileNotFoundError.
Controls:
- The same
StopMainLoopcancel withoffload_to_disk=FalsegivesON_META 0,FWD_OK. StopMainLoopreturned fromlayer_callback(which ends the run normally) givesFWD_OK.
Everything else after the failure is clean: global state (dtype, threads, TF32, BLAS, grad mode), no hooks left, no CUDA memory pinned, and a second quantize() in the same process succeeds.
Impact
With the default config, the only in-memory copy of the completed layers' quantized and original weights is deleted. The original weights were released when the layers were packed and offloaded. The in-memory model cannot run a forward, cannot be re-quantized and cannot be saved, so the caller has to reload from disk. After a Ctrl-C or a callback cancel in a notebook, this looks like a broken model rather than a cancelled run. It is also the opposite of what #4546/#4580 set out to guarantee ("a plain forward still works").
Suggested fix
In the failure branch, before release_private_offload_root(), call _restore_disk_offloaded_modules() (non-strict) for layers that finished packing, and materialize or rehome the rest. Alternatively, keep the run directory alive until the model no longer references it (for example, tie it to the model's lifetime the way #4450 handled the success path).
Extend test_cancelled_quantize_layer_cleanup_4546.py to the default offload_to_disk=True with the cancel placed at layer >= 1, so that at least one completed layer has been offloaded.
Severity
Medium-high. It affects the default configuration and the documented cancellation path, and the in-session model is permanently lost.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Taking this.
fix(looper): keep the offload root for completed layers of a cancelle…
Qubitium commentedon Nov 17, 2026
Fixed by #4829 (merged as 9d2ea51). The failure branch no longer deletes the per-run offload root while any module still owns a disk-offload hook; the root stays owned by the model-lifetime finalizer from
bind_private_offload_rootand is released eagerly only when nothing reads from it. Completed layers stay lazily loadable, so a plain forward works after a cancel. Regression test:tests/test_cancelled_quantize_disk_offload_4807.py(fails on unmodified main with the reported dangling-hook state).fix(looper): rehome finished layers' kernels after a failed quantize (#…
fix(looper): leave a cancelled quantize() with a runnable model (Mode…
Offload finalized modules as file-backed tensors instead of meta + ho…
tests: pin file-backed offload instead of meta + hook offload (#6471)
Remove accelerate: explicit WeightLoader for weight loading, placemen…