Skip to content

[BUG] #4823 (fix of #4813) incomplete: quantize() failing before the looper (empty/too-short calibration, bad rows, checkpoint/session setup) still leaks 2 device-smi sampler threads per attempt; LoopProcessor.__init__ opens handles before validating the dataset #4862

Closed

Description

@Qubitium

Found on main @ 5cded90, RTX 4090, torch 2.15, device-smi installed.

What is wrong

#4823 (the fix for #4813) closes the processors' Device-SMI handles in the failure branch of ModuleLooper.loop() and WeightOnlyLooper.loop(). Any quantize() that fails before the looper's try still leaks the two sampler threads (device-smi-metrics-gpu and device-smi-metrics-cpu, each polling at 5 Hz) for the life of the process.

LoopProcessor.__init__ opens the handles first (gptqmodel/looper/loop_processor.py:534-535) and only then validates and prepares the calibration dataset in the same constructor (:539-583). Several ValueErrors raised there abort the constructor: "Calibration dataset must not be empty.", "... is empty after CalibrationConfig.prepare_dataset removed all samples ...", and unsupported row types raised from prepare_dataset. The half-built processor is never handed to a looper, so nothing calls release_failed_run(). Each Device thread holds its Device through the bound _fast_metrics_worker, so GC cannot reclaim it either.

The same gap applies to:

  • processors already built when a later one fails (base.py:2883-2929, e.g. GPTQ + EoRA);
  • failures between processor construction and module_looper.loop(): checkpoint_session(...) entry (resume validation), install_w4a_llama_replay, and ModuleLooper.__init__.

Repro (Qwen2.5-0.5B-Instruct truncated to 4 layers, GPTQ 4-bit)

def smi(): return len([t for t in threading.enumerate() if t.name.startswith("device-smi")])
m = GPTQModel.load(MODEL, C.GPTQConfig(bits=4, group_size=128), dtype=torch.float16)
for name, ds in [("empty list", []),
                 ("all rows < min_length", [{"input_ids": [1, 2], "attention_mask": [1, 1]}]),
                 ("bad row type", [123])]:
    for i in range(2):
        try:
            m.quantize(C.CalibrationConfig(dataset=ds, batch_size=4))
        except BaseException as e:
            err = f"{type(e).__name__}: {str(e)[:70]}"
        gc.collect(); time.sleep(0.5)
        print(f"{name} #{i+1}: {err} -> device-smi threads {smi()}")
start 0
empty list #1: ValueError: Calibration dataset must not be empty. -> device-smi threads 2
empty list #2: ValueError: Calibration dataset must not be empty. -> device-smi threads 4
all rows < min_length #1: ValueError: Calibration dataset is empty after CalibrationConfig.prepare_dataset r -> device-smi threads 6
all rows < min_length #2: ValueError: Calibration dataset is empty after CalibrationConfig.prepare_dataset r -> device-smi threads 8
bad row type #1: ValueError: Quantize: unsupported calibration example type <class 'int'> at index  -> device-smi threads 10
bad row type #2: ValueError: Quantize: unsupported calibration example type <class 'int'> at index  -> device-smi threads 12

The in-loop failures that #4823 targets are fixed. StopMainLoop, RuntimeError and KeyboardInterrupt from subset_callback at layer 1 each leave 0 threads.

Suggested fix

Open the Device-SMI handles lazily on the first metric query, or at least after the dataset checks in LoopProcessor.__init__. Also wrap processor construction and the code up to module_looper.loop() in _quantize_with_calibration (and the weight-only and EoRA-generation paths) in a try/except BaseException that calls release_failed_run() on every processor already built. Of the two, lazy opening is the more robust, because a processor that is never run then never starts a thread.

Test that should have caught it

tests/test_failed_run_device_smi_cleanup_4813.py covers only failures inside ModuleLooper.loop(). Add a case with an empty or too-short calibration dataset and assert that no device-smi-metrics* thread survives.

Severity

Low-medium. It is the same per-attempt thread and CPU leak as #4813. These are the most common user errors (a bad or too-short dataset), and retry loops in notebooks or services accumulate threads.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4906

    Finish #4823: quantize() failing before the looper (empty/short calibration, bad config) must tear down the Device-SMI monitor thread and any other pre-looper resources; add failure-path tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions