Skip to content

fix(looper): materialize unreached layers after a cancelled run (#4860) - #4893

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4860
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4860

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4860.

Root cause

The except BaseException cleanup in ModuleLooper.loop() (and WeightOnlyLooper.loop()) restored the in-flight layer and base modules, but never ran the success path's _materialize_remaining_meta_modules(). With the default offload_to_disk=True the model is a lazy meta shell, so every layer past the cancel point still owned meta tensors and had no offload hook. The next forward failed on the first unreached layer with Tensor on device cpu is not on the expected device meta!.

Repro on base 6ec93e63 (which already includes #4829), 4-layer tiny Llama, cancel at layer 2:

layer0: meta-mods=10 hooked=7
layer1: meta-mods=10 hooked=7
layer2: meta-mods=0  hooked=0     # in-flight, restored by #4580
layer3: meta-mods=12 hooked=0     # never reached
FWD_FAIL RuntimeError Tensor on device cpu is not on the expected device meta!

Fix

  • module_looper.py: new _materialize_unreached_meta_modules() (best effort, logs and never masks the original exception) called in the failure branch after _restore_offloaded_base_modules(strict=False), before the offload-root keep/release decision.
  • weight_only_looper.py: same helper, which also runs the weight-only success path's _materialize_meta_base_modules(strict=False) so base modules are covered.
  • The materializer only touches modules that directly own meta tensors and skips packed quantized modules, so completed layers stay disk-offloaded and their bundles/root are preserved; a resumable checkpoint is untouched.

After the fix the same repro shows layer3: meta-mods=0, root kept for layers 0-1, FWD_OK. Same result for StopMainLoop at layers 1/2/3 and an injected RuntimeError.

Tests

New tests/test_cancelled_quantize_default_offload_4860.py (4-layer tiny Llama via GPTQModel.load, default offload):

  • cancels at layer 2 (not last), asserts no unreached layer owns meta tensors, completed layers remain disk-offloaded with module.safetensors bundles in the kept root, and a plain forward works
  • cancels a checkpointed run and resumes on a fresh model (success + forward)

Results: 3 passed, exit 0. Red on parent: 2 failed / 3 passed with AssertionError: layer 3 was never reached ... but still owns meta tensors.

Also: tests/test_cancelled_quantize_disk_offload_4807.py passes; ruff check gptqmodel tests clean.

Pre-existing failure (not caused here)

tests/test_failed_run_fence_4595.py::test_failed_quantize_does_not_leak_offload_bundles fails identically on pristine origin/main d8758c48 — the test still asserts the run root is deleted, but #4829 intentionally keeps it while completed layers remain hooked. Left untouched to keep this PR scoped.

Residual risk

  • Failure-path materialization is intentionally best-effort; if it fails, the model can still be unusable, but the original cancellation exception is preserved and a warning is logged.
  • Weight-only has no deterministic cancel hook, so its branch was validated with an injected pre_quantize failure in scratch, not in the committed test.

Codex and others added 2 commits November 17, 2026 17:59
The failure branch kept the per-run offload root for the layers that finished
before a cancel (#4829) but never filled the layers the loop never reached.
With the default offload_to_disk=True those layers still owned meta tensors and
had no offload hook, so the next forward failed on the first unreached layer
with "Tensor on device cpu is not on the expected device meta!".

Call the success path's remaining-meta materialization best effort on the
cancel/failure path (ModuleLooper and WeightOnlyLooper). It only touches
modules that own meta tensors and skips packed quantized modules, so the
completed layers stay readable through the kept offload root and a resumable
checkpoint is untouched.
@Qubitium
Qubitium merged commit 0facf5f into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4860 branch November 17, 2026 18:05
Qubitium added a commit that referenced this pull request Nov 17, 2026
…4947) (#4979)

#4893 materialized the layers a cancelled run never reached, and #4829/#4807
kept the offload root of the layers it did finish, but the failure branch never
rehomed the finished layers' kernels. A run packed with an accelerator-only
kernel (AwqGEMMLinear, QQQLinear, GGUF Triton) therefore left CUDA-only modules
behind a CPU model and the next in-session forward raised

  NotImplementedError: Could not run 'gptqmodel_awq::gemm_forward_fp32_reduce'
  with arguments from the 'CPU' backend

The success path rehomes them in rehome_quantized_kernel; the failure path now
does the same, best effort:

- rehome_failed_run_kernels() (loop_processor) resolves the device from a
  materialized packed layer, else from the rest of the model (finished layers
  of a disk-offloaded run are meta shells), and calls
  rehome_quantized_kernel(model, device=...) so the probe can be overridden.
  Rehome errors such as the save-and-reload guidance are logged and swallowed
  so the original cancellation still propagates.
- ModuleLooper and WeightOnlyLooper call it right after the #4893
  materialization, so the #4893/#4829/#4807 behaviour is unchanged.
- _switch_quant_linear_class rebinds the accelerate offload hook's captured
  bound ``forward`` (_old_forward) to the target kernel; otherwise the hook
  kept calling the accelerator-only class even after the swap.

Adds tests/test_cancelled_quantize_kernel_rehome_4947.py: cancelled and
RuntimeError-failed AWQ and QQQ runs, a cancel at the last layer, and a
checkpoint resume. Each asserts the finished layers use the portable kernel,
model.qlinear_kernel is updated, and an in-session forward is finite. The QQQ
case forces the CUDA-only class when the native extension cannot build. On the
parent all kernel/forward cases fail with AwqGEMMLinear/QQQLinear left behind.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant