Skip to content

fix(looper): keep the offload root for completed layers of a cancelled run (#4807) - #4829

Merged
Qubitium merged 1 commit into
mainfrom
root/offload-cancel-4807
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/offload-cancel-4807

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

fix(looper): keep the offload root for completed layers of a cancelled run (#4807)

A cancelled or failed quantize() with the default offload_to_disk=True
left every already-quantized layer as a meta shell behind an accelerate
disk-offload hook, then deleted the per-run offload directory those hooks read
from. The next forward raised FileNotFoundError for
<run>/model.layers.N.<proj>/module.safetensors and the completed layers were
unrecoverable: their packed and original weights only existed in that directory.

The failure branch restored the in-flight layer and the base modules, but unlike
the success path it did not bring the disk-offloaded modules back before calling
release_private_offload_root.

Fix: keep the run directory alive while any module still owns a disk-offload
hook, and release it eagerly only when nothing reads from it. This is the right
option for a cancelled run: restoring every finished layer into host memory
would defeat the memory bound that offload_to_disk=True exists for (a
cancel on a large model would materialize tens of GB), whereas the completed
layers stay lazily loadable exactly as they were during the run. The directory
is already owned by the model-lifetime weakref.finalize installed by
bind_private_offload_root, so ownership passes to the model: it is reclaimed
when the model is collected or when the next run re-binds the namespace (#4368).
The in-flight layer and base modules are still restored as before (#4546/#4580).

The same guard is applied to the identical failure branch in
WeightOnlyLooper.

Tests:

  • New tests/test_cancelled_quantize_disk_offload_4807.py (cancel via
    StopMainLoop and an injected RuntimeError at layer 1's
    forward_start) asserts the completed layer is still disk-offloaded, its
    bundle exists (or the model no longer points at the root), and a plain
    forward succeeds. It fails on unmodified main with:
    AssertionError: the per-run offload root was deleted while modules still read from it: ['model.layers.0.self_attn.q_proj', ...].
  • tests/test_cancelled_quantize_layer_cleanup_4546.py and all
    tests/test_*offload*.py: 170 passed, 9 skipped, 1 failed.
    The one failure,
    test_offload_base_modules_restored.py::test_loop_restores_base_modules_when_quantize_fails,
    is pre-existing on main (identical AttributeError: 'ModuleLooper' object has no attribute '_inflight_layer' from the test's object.__new__ stub) and
    is unrelated to this change.
  • ruff check passes on every changed file.

…d run (#4807)

A cancelled or failed ``quantize()`` with the default ``offload_to_disk=True``
left every already-quantized layer as a meta shell behind an accelerate
disk-offload hook, then deleted the per-run offload directory those hooks read
from. The next forward raised ``FileNotFoundError`` for
``<run>/model.layers.N.<proj>/module.safetensors`` and the completed layers were
unrecoverable: their packed and original weights only existed in that directory.

The failure branch restored the in-flight layer and the base modules, but unlike
the success path it did not bring the disk-offloaded modules back before calling
``release_private_offload_root``.

Fix: keep the run directory alive while any module still owns a disk-offload
hook, and release it eagerly only when nothing reads from it. This is the right
option for a *cancelled* run: restoring every finished layer into host memory
would defeat the memory bound that ``offload_to_disk=True`` exists for (a
cancel on a large model would materialize tens of GB), whereas the completed
layers stay lazily loadable exactly as they were during the run. The directory
is already owned by the model-lifetime ``weakref.finalize`` installed by
``bind_private_offload_root``, so ownership passes to the model: it is reclaimed
when the model is collected or when the next run re-binds the namespace (#4368).
The in-flight layer and base modules are still restored as before (#4546/#4580).

The same guard is applied to the identical failure branch in
``WeightOnlyLooper``.

Tests:
- New ``tests/test_cancelled_quantize_disk_offload_4807.py`` (cancel via
  ``StopMainLoop`` and an injected ``RuntimeError`` at layer 1's
  ``forward_start``) asserts the completed layer is still disk-offloaded, its
  bundle exists (or the model no longer points at the root), and a plain
  forward succeeds. It fails on unmodified main with:
  ``AssertionError: the per-run offload root was deleted while modules still
  read from it: ['model.layers.0.self_attn.q_proj', ...]``.
- ``tests/test_cancelled_quantize_layer_cleanup_4546.py`` and all
  ``tests/test_*offload*.py``: 170 passed, 9 skipped, 1 failed.
  The one failure,
  ``test_offload_base_modules_restored.py::test_loop_restores_base_modules_when_quantize_fails``,
  is pre-existing on main (identical ``AttributeError: 'ModuleLooper' object has
  no attribute '_inflight_layer'`` from the test's ``object.__new__`` stub) and
  is unrelated to this change.
- ``ruff check`` passes on every changed file.
@Qubitium
Qubitium merged commit 9d2ea51 into main Nov 17, 2026
8 checks passed
@Qubitium
Qubitium deleted the root/offload-cancel-4807 branch November 17, 2026 16:15
Qubitium added a commit that referenced this pull request Nov 17, 2026
… (#4893)

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.

Co-authored-by: Codex <codex@localhost>
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.
sLaffey pushed a commit to sLaffey/GPTQModel that referenced this pull request Nov 18, 2026
…lCloud#4978)

Seven tests went red because their doubles/oracles predate library changes
that behave as intended. This is test-only; no product code changes.

- test_fusion_kernel_backends: gc.collect() before the weakref check; ModelCloud#4871
  leaves transient Dynamo trace garbage that keeps the pre-fusion weight
  alive. The refs are still asserted dead afterwards.
- test_failed_run_fence_4595: del qmodel + gc.collect() so ModelCloud#4829's finalizer
  runs before the no-leak assertion; a late finalizer must not recreate the
  released run dir.
- test_exl3_projection_resume: add _release_deferred_hessian_sources() to the
  _Capture double (ModelCloud#4837).
- test_kernel_audit_regressions: stub _needs_torch_fallback=lambda: False on
  the SimpleNamespace self (ModelCloud#4835).
- test_moe_resave_preserves_experts_4589: accept the earlier RuntimeError
  "does not provide tensors" refusal (ModelCloud#4810); still assert no safetensors.
- test_positional_mask_capture_4660: build the native oracle with the same
  mask-derived position_ids the capture uses (ModelCloud#4930).
- test_qqq_kernel_memory_4387: snapshot the oracle weight/s_channel before
  the first forward, which may drop the checkpoint layout (ModelCloud#4856).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant