Repository navigation
Description
Found on main @ e75181d, RTX 4090 (CUDA 13.2, torch 2.15), default offload_to_disk=True.
What is wrong
ModelLoader._copy_checkpoint_tensors_into_submodule (gptqmodel/utils/structure.py:4017-4025) fills a lazy (meta) parameter in two steps. First it installs fresh, uninitialized storage on the module, then it copies the checkpoint values in:
target_param_new = _ensure_target_storage_on_device_(target_param, tensor_device) # meta -> torch.empty_like(...) (structure.py:562-563)
if target_param_new is not target_param:
setattr(t_parent, leaf, target_param_new) # module now owns garbage
...
target_param.detach().copy_(source, ...) # filled only hereIf a Ctrl-C (or any exception) lands between the setattr and the copy_, or between the parameters of one module, the module keeps torch.empty memory. That memory is no longer meta. The run-exit recovery (RunExitRecovery.abort(), _restore_inflight_layer, _materialize_unreached_meta_modules / _materialize_meta_base_modules) uses "is it meta?" as its only test for an unmaterialized module, so it treats these tensors as loaded. quantize() re-raises KeyboardInterrupt, and the in-session model then runs with uninitialized weights: NaN logits or plausible-looking wrong logits, with no error.
For the weight-only looper (RTN/GGUF/FP8) this is new. On c2b0bf64f the same interrupt left meta tensors or a device mismatch, so the next forward failed loudly. #5039 (fix of #5023, post-loop tail recovery), #5049 (fix of #5011, in-flight layer restore) and #5066 (#5060) now repair everything else (move the layer home, materialize the remaining meta modules), so the forward "succeeds" on garbage. For ModuleLooper (GPTQ) the input-capture materialization window already gave silent garbage on c2b0bf64f.
Repro (deterministic)
Qwen2.5-0.5B-Instruct truncated to 4 layers, fp16. A KeyboardInterrupt is raised at the N-th Tensor.copy_ (this stands in for a real Ctrl-C at that instant). ARM=tail arms the hook only after the loop body has returned (the #5023 window). ARM=start arms it from the start of quantize().
import torch, traceback
from gptqmodel import GPTQModel
from gptqmodel.quantization import config as C
import gptqmodel.looper.weight_only_looper as WL
state = {"armed": False, "n": 0, "N": 1}
orig_copy = torch.Tensor.copy_
def copy_(self, *a, **k):
if state["armed"]:
state["n"] += 1
if state["n"] == state["N"]:
state["armed"] = False
raise KeyboardInterrupt()
return orig_copy(self, *a, **k)
torch.Tensor.copy_ = copy_
orig_impl = WL.WeightOnlyLooper._loop_impl
def impl(self, *a, **k):
r = orig_impl(self, *a, **k); state["armed"] = True; return r # ARM=tail
WL.WeightOnlyLooper._loop_impl = impl
m = GPTQModel.load(MODEL_4L, C.RTNConfig(bits=4, group_size=128), dtype=torch.float16)
try: m.quantize(None)
except KeyboardInterrupt: pass
state["armed"] = False
print([n for n, p in m.model.named_parameters() if p.device.type == "meta"],
[n for n, p in m.model.named_parameters() if not torch.isfinite(p).all()])
ids = m.tokenizer(TEXT, return_tensors="pt").input_ids[:, :256]
with torch.inference_mode(): print(float(m.model(ids, labels=ids).loss))Measured (the completed run gives loss 15.356 for RTN and 15.2023 for GPTQ; the unquantized model gives 15.107):
| method, window | interrupt at | main e75181d |
c2b0bf64f |
|---|---|---|---|
RTN, post-loop tail (_materialize_meta_base_modules) |
1st copy (embed_tokens) |
loss 11.9312, no meta, no error | Cannot copy out of meta tensor |
| RTN, post-loop tail | 2nd copy | loss NaN (model.norm.weight NaN), no meta |
Cannot copy out of meta tensor |
RTN, pre_quantize layer materialization |
1st copy | loss 15.227 | device mismatch |
RTN, pre_quantize |
5th copy | loss NaN | device mismatch |
RTN, pre_quantize (layer 1) |
50th copy | loss 14.4993 | device mismatch |
GPTQ, prepare_input_capture_layer |
1st copy | loss NaN (layers.0.self_attn.q_proj.weight NaN) |
NaN (same) |
GPTQ, prepare_input_capture_layer |
5th copy | loss 27.2799 | 21.8176 (same bug) |
The same thing happens with a real _thread.interrupt_main() timer. In a random-time Ctrl-C sweep of RTN (33 cancel points), one point gave loss NaN with no error.
Suggested fix
Make the per-tensor publish atomic. Allocate the new storage, copy_ into it, and only then setattr it on the module (or keep the meta parameter until the copy has finished). With that, an interrupted materialization leaves either the old meta tensor, which the recovery already re-materializes, or a fully loaded one. Alternatively, mark a module "materializing" in materialize_submodule and clear the mark only after every tensor is copied. The run-exit recovery would then re-materialize any module still carrying the mark (and _restore_inflight_layer would treat the in-flight layer the same way).
Test that should have caught it
tests/test_post_loop_tail_failure_5023.py, tests/test_weight_only_failed_run_device_5011.py and tests/test_run_exit_recovery_contract_5060.py inject the failure at a helper boundary (before any tensor is touched), so they never see a half-materialized module. Add a node that raises inside _copy_checkpoint_tensors_into_submodule (for example at the first Tensor.copy_ after the body returns, and inside pre_quantize), then asserts that every floating parameter is finite and that the forward matches either the completed run or the untouched base model.
Severity
High for the weight-only path. A cancelled quantize() (the documented StopMainLoop/Ctrl-C path, whose contract is "Runnable") now hands back a model that silently produces NaN or wrong logits, where before it failed loudly. The GPTQ input-capture window has the same silent corruption and predates these PRs.
🤖 Generated with Claude Code
Activity
ZX-ModelCloud commentedon Nov 18, 2026
Agent claim released:
agent-65a6dbddcf23finished this issue.Pull request: #5132
Reproducing interrupted lazy materialization leaving torch.empty weights; verifying latest main
fix(structure): publish materialized parameters atomically (#5088) (#…
Replace LazyTurtle with weight_loader CheckpointSource (#6471)
Remove accelerate: explicit WeightLoader for weight loading, placemen…