Skip to content

refactor(looper): share run teardown between ModuleLooper and WeightOnlyLooper - #6465

Merged
Qubitium merged 1 commit into
mainfrom
refactor/looper-shared-teardown
Nov 30, 2026
Merged

Qubitium merged 1 commit into
mainfrom
refactor/looper-shared-teardown

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Debloat refactor (behavior-preserving): ModuleLooper and WeightOnlyLooper each kept their own copy of the run-exit steps that RunExitRecovery drives, plus quant-device assignment. The copies differed only in data. This PR keeps one implementation on the bases both loopers already inherit, and each looper now declares only what is different about it. No new classes, mixins or registries.

No issue is fixed. This is a debloat refactor.

What Changed

  • gptqmodel/looper/execution_state.py (DeviceAssignmentState, already shared): now owns the single _cross_device_pool and _assign_quant_device_for_module. Telemetry event names come from _quant_device_event_prefix ("" / "weight_only_").
  • gptqmodel/looper/inflight_layer.py (InflightLayerTracker, already the run-exit base): now owns the single _bind_private_offload_namespace, _restore_offloaded_base_modules, _restore_disk_offloaded_modules, _materialize_remaining_meta_modules, _materialize_unreached_meta_modules, _release_run_workspace, _release_failed_run_processors, _fence_run_work(*extra_waits), _release_or_keep_private_offload_root and _make_run_exit_recovery(**steps).
  • Per-looper data or overrides that remain:
    • Both loopers: _run_processors() (self.processors / (self.processor,)).
    • ModuleLooper: _make_run_exit_recovery adds the finalizer-watcher fence wait and restore_deferred. The _finalize_deferred_runtime_weights hook-probe site is not touched, so it stays clear of R2.
    • WeightOnlyLooper: _quant_device_event_prefix, _fence_run_label, _kept_root_run_label, _release_processors_before_restore = False, and an override of _materialize_meta_modules that fills its base modules first. That is the same strict=False on abort and strict=True on success as before.
  • Log text, telemetry event names, step order, strictness and release order are unchanged.
  • One unification: weight-only _release_run_workspace used to probe getattr(processor, "release_run_workspace"). It now calls the method directly, as ModuleLooper already did. Every real processor is a LoopProcessor, and LoopProcessor defines the method. Only duck-typed test fakes differ, and the fake in tests/test_weight_only_looper.py now implements the method.
  • Tests that patched module globals now patch them in inflight_layer, because the shared steps look them up there: test_failed_run_fence_4595, test_failed_run_device_smi_cleanup_4813, test_weight_only_failed_run_device_5011, test_exl3_workspace_release, test_post_loop_tail_failure_5023. Each is a patch-target change only, and the assertions are unchanged. The 5011 stub processor also gets release_run_workspace, and the test now asserts that it runs last.
  • scripts/repro_issue_2326.py: its fake looper borrows ModuleLooper._assign_quant_device_for_module, so it now also declares the event prefix.
  • Out of scope: _loop_impl (genuinely different pipelines), _finalize_deferred_runtime_weights and the other processor hook probes (R2).

git diff --shortstat origin/main: 12 files changed, 330 insertions(+), 406 deletions(-). That breaks down as gptqmodel/ +249/-381 (net -132) and tests/scripts +81/-25, mostly the new guard test.

Tests

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.

New guard: tests/test_run_exit_recovery_contract_5060.py::test_shared_exit_steps_keep_each_loopers_wording[ModuleLooper-...] and [WeightOnlyLooper-...]. They assert each looper's exact fence-drain warnings (including the watcher wait for ModuleLooper only), the kept-root debug line and the *quant_device_{preferred_hint,assign,cache_hit} event names. Mutation check: changing the weight-only label to "weight only run" and the prefix to "weightonly_" makes the test fail at the wording assertion.

Family A/B. I ran all 88 non-tests/models/ test files that reference either looper, on origin/main (1c51b32) and on this branch, with an identical command (CPU, CUDA_VISIBLE_DEVICES=""):

PYTHONPATH=<tree> CUDA_VISIBLE_DEVICES="" python -P -m pytest -n 8 --dist loadfile -q -rs -p no:cacheprovider \
  --junitxml=<tree>.junit.xml $(cat tests.txt)   # list: every tests/*.py naming ModuleLooper/WeightOnlyLooper, minus tests/models and multigpu
# origin/main: 1 failed, 2013 passed, 113 skipped
# branch:      1 failed, 2015 passed, 113 skipped
# per-node outcome differences: none (extra nodes = 2 new guards; 5023 parametrize ids renamed)

Teardown files run on their own on the branch (all pass):
tests/test_failed_run_fence_4595.py tests/test_run_exit_recovery_contract_5060.py tests/test_weight_only_failed_run_device_5011.py tests/test_failed_run_device_smi_cleanup_4813.py tests/test_exl3_workspace_release.py tests/test_cancelled_quantize_default_offload_4860.py tests/test_cancelled_quantize_disk_offload_4807.py tests/test_post_loop_tail_failure_5023.py tests/test_exl3_cancelled_run_5083.py tests/test_checkpoint_devices.py tests/test_weight_only_looper.py tests/test_layer_module_coverage.py.

The one failure is tests/test_looper_processor_capabilities.py::test_gptq_never_inherits_previous_calibration. It fails the same way on clean origin/main when run alone: SimpleNamespace has no calibration_dataset_owner. It comes from a stale test after #6422 and is unrelated to this PR.

Skips (113, identical on both trees) are CUDA-only nodes, mainly:

  • test_exl3_cancelled_run_5083 (EXL3 needs NVIDIA)
  • test_weight_only_failed_run_device_5011:116 (needs an accelerator)
  • test_checkpoint_devices multi-GPU restore
  • test_calibration_data_device multi-GPU

Accelerator-placement recovery for the cancelled EXL3 / weight-only device runs is therefore not verified by these runs. The GPU A/B below covers only the success path on one GPU.

Structural A/B (teardown_ab.py, run on both trees). It patches every pool and offload helper in whichever module owns it on that tree, then records the full event sequence, rendered log text, telemetry names, RunExitRecovery wiring (which steps are set, plus release order), failures, completed and recovered. It covers both loopers × {success, raise, StopMainLoop, wedged pools, disk-kept root, unreached-materialize failure, success-path materialize failure}, device assignment (single, round-robin, preferred hint as device and as string, hint outside the pool, None device, CPU pool, cache hits), _cross_device_pool and private-namespace rebinding. Output: identical JSON (sha256 b2492c29…), excluding the gptqmodel.__file__ line.

GPU A/B on one GPU: Qwen2.5-0.5B-Instruct, fp16, group_size=128, default offload_to_disk=True, seed 0, fixed 32-row calibration. GPTQ 4-bit and RTN 4-bit each quantized on both trees, followed by one in-session forward, then save:

  • GPTQ: 794 tensors across 8 files, every tensor and every json equal, in-session logits equal → bit-identical
  • RTN: 794 tensors across 8 files, every tensor and every json equal, in-session logits equal → bit-identical
  • Run logs with timing numbers removed are identical apart from the temp offload dir and the NF12 JIT cache-hash lines.

bash format/check.sh: All checks passed (ruff 0.14.2).

Measured Impact

Not applicable: no runtime behavior change.

Model Support / Module Plan

Not applicable: no model, definition or module-plan change.

AWQ onboarding safety

Not applicable: AWQ is not touched.

Logic Flow Documentation

  • This change does not alter a major logic flow, or the required diagrams and docs page are included below.

Not applicable: the RunExitRecovery step order and the quant-device selection order are unchanged. What moved is where the step bodies live, and per-looper copies became class-attribute data. The table below maps old to new (old line numbers at 1c51b32). docs/cancelled-run-contract.md still describes the sequence, so no docs page is needed.

Step Old (ModuleLooper / WeightOnlyLooper) New
_cross_device_pool module_looper.py:506 / weight_only_looper.py:138 execution_state.py:17
_assign_quant_device_for_module :1136 / :398 (event names inline) execution_state.py:28, _quant_device_event_prefix
_bind_private_offload_namespace :1825 / :914 inflight_layer.py, _run_processors()
restore offloaded / disk, materialize remaining :1861-1880 / :930-949 inflight_layer.py
_materialize_unreached_meta_modules :1882 / :951 (adds base modules) inflight_layer.py → _materialize_meta_modules(strict); weight-only override
_fence_run_work :1921 (3 waits) / :1024 (2 waits), label inline inflight_layer.py:259 *extra_waits, _fence_run_label
_release_or_keep_private_offload_root :1972 / :1055, text inline inflight_layer.py, _kept_root_run_label
_make_run_exit_recovery :1953 / :1067 (full kwargs lists) inflight_layer.py:304 shared; ModuleLooper overrides with fence and restore_deferred; weight-only uses _release_processors_before_restore = False

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

Not applicable: no kernel change.

Notes

  • InflightLayerTracker now owns all of the looper-side exit steps, not just in-flight tracking. A rename (e.g. RunExitSteps) would be a separate, purely cosmetic follow-up.
  • ModuleLooper's success-path materialize step now resolves _materialize_remaining_meta_modules when it is called, not when the recovery object is built. This matches how weight-only already worked, and only differs if someone patches the method in the middle of a run.
  • Conflicts: this PR stays out of the processor hook-probe sites that the concurrent R2 refactor edits. _make_run_exit_recovery sits next to _finalize_deferred_runtime_weights, so a textual rebase may be needed.

🤖 Generated with Claude Code

…nlyLooper

ModuleLooper and WeightOnlyLooper each carried their own copy of the run
exit steps that RunExitRecovery drives (fence, restore offloaded/disk
modules, materialize meta modules, release processors/workspace, keep or
release the private offload root, bind the private offload namespace) and of
quant-device assignment. The copies differed only in data: log wording,
telemetry event prefix, the processor list, the fence's extra watcher wait,
ModuleLooper's deferred-weight step, WeightOnly's base-module fill and its
release order.

Move the single implementation onto the bases both loopers already inherit:
- DeviceAssignmentState: _cross_device_pool, _assign_quant_device_for_module
  (event names come from _quant_device_event_prefix).
- InflightLayerTracker: every shared exit step plus _make_run_exit_recovery,
  which builds RunExitRecovery from the shared steps and takes per-looper
  overrides as keyword steps.

Each looper now declares only its differences: _run_processors(), the
_fence_run_label/_kept_root_run_label wording, the event prefix and
_release_processors_before_restore. ModuleLooper adds its watcher-thread
fence wait and deferred runtime weights; WeightOnlyLooper overrides
_materialize_meta_modules to fill its base modules first.

Log text, telemetry event names, step order and strictness are unchanged.
Tests that patched module globals now patch them in inflight_layer, where the
shared steps resolve them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Qubitium
Qubitium marked this pull request as ready for review November 30, 2026 02:29
@Qubitium
Qubitium merged commit 9d50096 into main Nov 30, 2026
6 checks passed
@Qubitium
Qubitium deleted the refactor/looper-shared-teardown branch November 30, 2026 02:29
Qubitium added a commit that referenced this pull request Nov 30, 2026
…d teardown

Rebased fix/6148-issue onto origin/main 94ceaf7 (after #6464, #6465,
#6467, #6469, #6470). Three conflicts:
- gptqmodel/looper/forward_executor.py: import block. Main dropped the
  unused resolve_calibration_keep_mask import; kept only the PR's
  replay_records import.
- gptqmodel/looper/module_looper.py: #6465 moved the failed-run teardown
  into InflightLayerTracker (inflight_layer.py). Kept main's structure and
  re-applied the PR's release_looped_capture() as a ModuleLooper override
  of _release_failed_run_processors that calls super() first.
- gptqmodel/models/definitions/inkling2_loop.py: #6464 reduced
  pre_quantize_generate_hook_start to the batch-index/bank reset plus
  super(); the PR removes that reset, so the override is deleted.

Earlier rebase (onto 81ac176):
Rebased fix/6148-issue onto origin/main 81ac176. Two conflicts:
- gptqmodel/models/definitions/inkling2_loop.py: import block. Kept the
  PR's replay_records imports; dropped the PR's get_op_name import
  because main removed its last use and the rebased file does not use it.
- tests/module_tree/test_inkling2_loop_plan.py: main added the AWQ
  declaration block where the PR deletes _capture_inkling2_forward.
  Kept all of main's AWQ tests, removed only the helper the PR replaced
  with record-owned head banks.

Tests: 135 passed, 1 skipped on CPU (main baseline for the pre-existing
files: 124 passed).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Dec 1, 2026
…d teardown

Rebased fix/6148-issue onto origin/main 94ceaf7 (after #6464, #6465,
#6467, #6469, #6470). Three conflicts:
- gptqmodel/looper/forward_executor.py: import block. Main dropped the
  unused resolve_calibration_keep_mask import; kept only the PR's
  replay_records import.
- gptqmodel/looper/module_looper.py: #6465 moved the failed-run teardown
  into InflightLayerTracker (inflight_layer.py). Kept main's structure and
  re-applied the PR's release_looped_capture() as a ModuleLooper override
  of _release_failed_run_processors that calls super() first.
- gptqmodel/models/definitions/inkling2_loop.py: #6464 reduced
  pre_quantize_generate_hook_start to the batch-index/bank reset plus
  super(); the PR removes that reset, so the override is deleted.

Earlier rebase (onto 81ac176):
Rebased fix/6148-issue onto origin/main 81ac176. Two conflicts:
- gptqmodel/models/definitions/inkling2_loop.py: import block. Kept the
  PR's replay_records imports; dropped the PR's get_op_name import
  because main removed its last use and the rebased file does not use it.
- tests/module_tree/test_inkling2_loop_plan.py: main added the AWQ
  declaration block where the PR deletes _capture_inkling2_forward.
  Kept all of main's AWQ tests, removed only the helper the PR replaced
  with record-owned head banks.

Tests: 135 passed, 1 skipped on CPU (main baseline for the pre-existing
files: 124 passed).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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