Repository navigation
fix(looper): restore the in-flight layer when quantize() is cancelled or fails (#4546) - #4580
Merged
Merged
Conversation
This was referenced Nov 17, 2026
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.
This was referenced Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_layerrecords the layer's home device beforepre_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_looperrestores that layer on anyBaseExceptionfrom_loop_impl, before the existing offload/meta cleanup.post_quantizerestores the layer, so behavior is unchanged.Validation
Reproduced on CUDA with a 2-layer tiny Llama:
forward_startquant_starttests/test_cancelled_quantize_layer_cleanup_4546.py→ 2 passed (fails 2/2 pre-fix)ruff check gptqmodel docs/eora tests setup.pycleanResidual: 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.