Skip to content

refactor(looper): give both quantization loops one run-exit recovery owner (#5060) - #5066

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-5060
Nov 18, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-5060

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Implements the bounded migration in #5060: the run, not each looper's exception handler, now owns the model-state obligations of a quantization run's exit. Design decision accepted as written in the issue; no checkpoint or public-API change.

What changed

New gptqmodel/looper/run_exit_recovery.py — RunExitRecovery, a run-scoped owner with run(body) / complete() / abort():

  • run(body) executes the layer loop; success calls complete(), whose steps are the shared success tail. _completed is published only after every restore step succeeds.
  • A body failure or a failure inside complete() enters the same abort() path, which re-raises the original exception.
  • abort() is idempotent and never raises. Each step is best effort and independent; a secondary repair failure is appended to recovery.failures and logged, never raised in place of the exception that ended the run.
  • Obligations are driven by the callbacks the looper actually supplies: fence submitted work, restore the in-flight layer and hooks, restore offloaded/meta base modules, materialize unreached layers, rehome kernels, repair version counters, retain/release the private offload root, release the run workspace.
  • release_processors_before_restore keeps the two existing orderings (calibrated loop releases processors first; weight-only restores its in-flight layer first per [BUG] #4979 (fix of #4947) incomplete: a failed weight-only quantize (RTN/GGUF/FP8) leaves the in-flight layer on cuda:0; next forward raises a device mismatch (no #4580 in-flight restore in WeightOnlyLooper) #5011). The owner still runs the release exactly once.

ModuleLooper.loop() and WeightOnlyLooper.loop() now both delegate to it. ModuleLooper._recover_post_loop_tail_failure and the ~145-line weight-only failure branch are retired; the same obligations can no longer be forgotten in one loop only (#5031, #5039, #5049 were each that failure mode).

Verification (this host, origin/main 468c201d)

New tests/test_run_exit_recovery_contract_5060.py — 16 conformance cases against both loopers, covering the issue's acceptance list:

  • tiny GPTQ (calibrated) and tiny RTN (weight-only) runs that complete a layer, then fail during the next layer and during the final restore; disk offload on and off where supported;
  • the calibrated post-forward stop (StopMainLoop after a forward);
  • the intended fault is proven to have fired; on recovery the original exception is the one raised; an ordinary grad-enabled forward works afterwards; completed / in-flight / unreached layers are usable; no late worker mutation; live disk shells keep their backing until release;
  • a secondary repair failure is recorded but does not replace the primary error; successful-run control.
pytest tests/test_run_exit_recovery_contract_5060.py                     -> 16 passed
same file with both loopers reverted to origin/main                      -> 16 failed
probe: abort() returns early                                             -> 11 failed / 5 passed
probe: complete() skips the restore                                      -> 8 failed / 8 passed
probe: best-effort step re-raises                                        -> 2 failed (secondary error leaked)

pytest <new file> + 4546/4860/4947 + weight_only_looper + processor_error
       + stage_modules + looper_helpers + looper_concurrency             -> 151 passed
ruff check gptqmodel tests                                               -> All checks passed

Deferred / residual risk

  • Two exit orderings remain, selected by release_processors_before_restore; unifying them is a further contract change, not part of this slice.
  • abort() deliberately does not restore disk-offloaded modules (matches both previous failure branches); complete() does.
  • Weight-only _fence_run_work drains device/stream pools but has no dangling-watcher registry (it never had one).
  • Secondary failures are in-memory (recovery.failures) plus a log line, not surfaced through a public API.

Part of #5060.

…owner

The calibrated and weight-only loops each ordered the same failure repairs in
their own exception handler, and the calibrated loop had a second recovery
path for a failing success tail. Every new obligation (fence, in-flight
restore, base-module restore, meta materialization, kernel rehome, version
counters, offload-root retention) had to be added twice.

Add `gptqmodel/looper/run_exit_recovery.py::RunExitRecovery`, a run-scoped
owner with one success sequence (`complete()`) and one abort sequence
(`abort()`). Each looper supplies its processor-specific callbacks; steps the
run did not acquire are skipped. Success is published only after every restore
step succeeds. Stop, exception and interrupt enter `abort()`, which is
idempotent, fences submitted work first, and records secondary repair failures
on `failures` without ever replacing the primary exception. The calibrated
looper's separate `_recover_post_loop_tail_failure` is retired into the same
abort path; the weight-only loop gains the fence and run-workspace release it
previously skipped, while keeping its in-flight restore before processor
release to preserve the #5011 contract.

Add `tests/test_run_exit_recovery_contract_5060.py`, the shared conformance
fixture: tiny GPTQ and RTN runs with a next-layer fault and a final-restore
fault, disk offload on/off, the calibrated post-forward stop, a secondary
repair failure, a successful-run control, and assertions for the original
error, a grad-enabled forward, usable completed/in-flight/unreached layers,
no hooks/inference tensors/late mutation, and retained disk-shell backing.
Update the cancelled-run contract page to name the shared owner.

Part of #5060
@Qubitium
Qubitium merged commit 2ea230c into main Nov 18, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-5060 branch November 18, 2026 01:36
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Independent note from a parallel attempt at this issue (I closed my narrower #5067 as superseded by this PR).

Before this PR, WeightOnlyLooper.loop() submitted submodule finalizers to DEVICE_THREAD_POOL (weight_only_looper.py pack/replace steps) and its failure branch never drained them — the #4595 defect class in the second looper. Your WeightOnlyLooper._fence_run_work() closes that.

Two properties that matter for it, both of which my branch pinned with a unit test that drives the exit path directly (pool still writing into the run directory when the run fails):

  1. The fence must run first in the abort path — before _restore_inflight_layer() and before release_private_offload_root() — otherwise a finalizer still running can re-create the directory that was just removed.
  2. A wedged drain must be logged, not raised: the remaining waits and the repairs still have to run, and the caller must still see the exception that failed the run.

If RunExitRecovery.abort() already guarantees both, nothing to do. Worth a look at release_private_offload_root ordering in the abort sequence either way.

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