Skip to content

fix(looper): restore the in-flight layer when quantize() is cancelled or fails (#4546) - #4580

Merged
Qubitium merged 1 commit into
mainfrom
fix/cancelled-quantize-layer-cleanup-4546
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/cancelled-quantize-layer-cleanup-4546

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4546 (#4515 incomplete off-CPU). A cancelled or failed quantize() left the in-flight layer on the compute device: the next forward raised a device mismatch and the layer's VRAM stayed pinned.

  • stage_layer records the layer's home device before pre_quantize (which itself moves CPU layers to the quantize device) and tracks the current layer across converter replacement, catch-up materialization and endpoint swaps.
  • module_looper restores that layer on any BaseException from _loop_impl, before the existing offload/meta cleanup.
  • The success path clears tracking after post_quantize restores the layer, so behavior is unchanged.

Validation

Reproduced on CUDA with a 2-layer tiny Llama:

before after
cancel at forward_start 9 modules on cuda:0; forward device-mismatch 0 off CPU; forward OK
fail at quant_start 9 modules on cuda:0; device-mismatch 0 off CPU; forward OK
  • New tests/test_cancelled_quantize_layer_cleanup_4546.py → 2 passed (fails 2/2 pre-fix)
  • Targeted looper/quantize suites → 59 passed; broader → 79 passed, 2 skipped
  • ruff check gptqmodel docs/eora tests setup.py clean

Residual: CPU-only runs cannot observe the off-CPU move (the test then checks forwardability); disk-offloaded models with other meta layers are not guaranteed fully forwardable after cancellation.

Resolves #4546.

@Qubitium
Qubitium merged commit f7e30b9 into main Nov 17, 2026
8 checks passed
@Qubitium
Qubitium deleted the fix/cancelled-quantize-layer-cleanup-4546 branch November 17, 2026 01:53
Qubitium added a commit that referenced this pull request Nov 17, 2026
…d run (#4807) (#4829)

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.
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