Skip to content

fix(looper): release pre-looper resources on a failed quantize (#4862) - #4906

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4862
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4862

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4862.

Root cause

LoopProcessor.__init__ opened one device_smi.Device per accelerator plus the CPU before validating/preparing the calibration dataset. Every handle owns a 5 Hz device-smi-metrics-* daemon thread. The #4823 fix only released handles in the loopers' failure branches, so:

  • an empty / too-short / malformed dataset raised in the constructor, leaving the half-built processor (and its threads) unreachable — nothing called release_failed_run();
  • with GPTQ + EoRA, the EoRA constructor failed after the GPTQ processor (and EoRA's own handles) were opened, leaking both;
  • failures between construction and loop() (checkpoint_session, install_w4a_llama_replay, ModuleLooper.__init__) had the same gap.

Each retry added two threads for the life of the process.

Fix

  1. Lazy handles: _device_smi_handles starts as an uninitialized sentinel and _ensure_device_smi_handles() opens them on the first metric query, under the existing lock. A processor that never runs starts no sampler. _close_device_smi_handles() marks the processor closed, so a released processor cannot reopen one.
  2. Prologue release: _quantize_with_calibration now wraps processor construction -> checkpoint entry -> install_w4a_llama_replay -> ModuleLooper.__init__ -> loop() in except BaseException: release_failed_run_processors(processors); raise. Same wrapper for _quantize_weight_only and _eora_generate. release_failed_run_processors() is shared with ModuleLooper._release_failed_run_processors() (same best-effort, non-masking semantics); repeated release stays idempotent, so the fix(looper): close the Device-SMI handles on a failed or cancelled run (#4813) #4823 in-loop release double-call is harmless.

Evidence

Red on parent (6ec93e63, pristine worktree + final test file): 5 failed, exit 1

  • [empty], [too-short], [bad-row]: each left ['device-smi-metrics-cpu']
  • EoRA-late case: the built processors were never released
  • never-opened release case: ImportError: _DEVICE_SMI_UNINITIALIZED

Green after fix: new file 5 passed; new file + #4823 suite 10 passed; 8 affected suites 85 passed; test_checkpoint_interop_4053.py 27 passed, 1 skipped; success-path smoke leaves 0 device-smi threads; ruff check gptqmodel tests clean.

Pre-existing failure on main (not caused here)

test_checkpoint_interop_4053.py::test_load_state_dict_round_trip_preserves_v1_model[gptq_triton] is red on pristine origin/main d8758c48 — its _logits() helper forwards CPU input_ids into a CUDA model, and #4871 deliberately removed the per-module hooks that used to move them. Filed separately.

Residual risk

  • Pre-looper release calls release_workspace_cache(), which is process-wide; a concurrent run in the same process could see its cached Hessian workspace dropped. Same semantics fix(looper): close the Device-SMI handles on a failed or cancelled run (#4813) #4823 already uses on failure; quantize runs are not designed to overlap.
  • eora_save() failures after a successful EoRA loop remain uncovered (processors are finalized by then).
  • Lazy handles mean a processor that never runs reports no memory metrics (intended).

#4823 released the processors' Device-SMI handles in the failure branch of
ModuleLooper.loop() and WeightOnlyLooper.loop(). A quantize() that failed
before a looper existed still leaked them: LoopProcessor.__init__ opened one
device_smi.Device per accelerator plus the CPU first and only then validated
the calibration dataset, so every empty, too-short, or malformed dataset
raised with two live 5 Hz sampler threads. A later processor in the chain
failing (GPTQ + EoRA) leaked the processors already built, and the half-built
one, for the same reason. Retry loops accumulated two threads per attempt.

Open the Device-SMI handles lazily on the first metric query, so a processor
that never runs never starts a sampler, and keep a closed processor closed.
Additionally wrap the whole quantize prologue -- processor construction,
checkpoint entry, install_w4a_llama_replay, ModuleLooper.__init__, and the
loop itself -- in a try/except that releases every processor built so far
(shared release_failed_run_processors helper, also used by ModuleLooper).
Weight-only and EoRA generation get the same wrapper.

Adds tests/test_pre_looper_failure_cleanup_4862.py: empty, too-short, and
malformed calibration, a later EoRA processor failing after GPTQ was built,
and release_failed_run on a never-opened processor. On the parent the three
calibration cases each leave a device-smi-metrics-cpu thread and the EoRA
case never releases the built processor.
@Qubitium
Qubitium merged commit 6eb1b01 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4862 branch November 17, 2026 18:29
sLaffey pushed a commit to sLaffey/GPTQModel that referenced this pull request Nov 18, 2026
…elCloud#4967) (ModelCloud#4971)

`tests/test_docs_contract_refs.py` was red on main again: four citations into
`gptqmodel/models/base.py` were 114 lines out of the 8-line window, because
base.py keeps growing under them (ModelCloud#4894, ModelCloud#4906, ModelCloud#4912, ModelCloud#4930):

  docs/awq-scale-folding.md        _select_prev_op                        5489 -> 5667
  docs/calibration-replay.md       capture_layer_type_replay_kwargs       4022 -> 4200
  docs/calibration-replay.md       _build_layer_type_position_embeddings  4005 -> 4183
  docs/fp8-scale-semantics.md      _decoder_fp8_scale_semantics           4752 -> 4930

Re-anchoring the numbers would rot again at the next base.py change, so the
guard now accepts a symbol-anchored citation (`path.py::symbol`) and those four
docs use it. The test still resolves the symbol in the source, so a renamed or
removed symbol fails; only the line number stops being part of the contract.
The line-number form stays for files that are not growing.

Also re-anchored the stale `:5531` on the adjacent `:postnorm` row to
`base.py::_select_prev_op` and `POST_NORM_FLAG` (base.py:560).

And the test now runs pre-merge: it is the first file in the AWQ CPU `unit`
suite, and `docs/**` plus the test itself are added to that workflow's path
filter, so a base.py or docs change runs it.

On unmodified main: 4 failed, 16 passed. With the fix: 20 passed. Copying only
the new test onto main (docs still citing line numbers) also fails all four,
which proves the new form is actually enforced.

Closes ModelCloud#4967.
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