Skip to content

fix(multi-gpu): release worker task refs, StopForward tracebacks, and ExLlamaV2 device (#4078) - #4088

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4078-leaks
Nov 15, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4078-leaks

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4078

TL;DR

Three process-lifetime owners survived the operation that created them. Each now releases at its own scope: the device worker clears its task locals after task_done(), every early stop raises a fresh StopForward, and both ExLlamaV2 make_q_matrix ops are wrapped in OptionalCUDAGuard. Net diff: 11 files, +361/-9 (docs and tests included). No synchronization point added.

1. The problem

Item Owner that outlived its use Where
1 fn, args, kwargs, fut, result bound in the worker frame until the next get() gptqmodel/utils/threadx.py:523 (old _run)
2 STOP_FORWARD_EXCEPTION.__traceback__ from the last raise gptqmodel/nn_modules/hooked_linear.py:27; raises at :226, :288, :350, :380, :452, auto_dtype_cast.py:308, stage_inputs_capture.py:321
3 current CUDA device set by QMatrix::QMatrix gptqmodel_ext/exllamav2/cuda/q_matrix.cu:72, q_matrix_awq.cu:72; make_q_matrix had no guard (ext_gptq.cpp:10, ext_awq.cpp:10)
flowchart TD
    A["forward_executor.submit(forward_batch_worker, replica)"] --> B["_DeviceWorker._run"]
    B --> C["fn, args, kwargs, fut, result bound in frame"]
    C --> D{"next task?"}
    D -- "yes" --> E["frame overwritten; previous released"]
    D -- "no: quantize finished" --> F["last replica pinned on every GPU"]
Loading

2. The fix

flowchart TD
    A["_DeviceWorker._run"] --> B["execute task"]
    B --> C["resolve future"]
    C --> D["finally: task_done()"]
    D --> E["fn = args = kwargs = fut = result = None"]
    E --> F["worker blocks in get() with no task payload"]
Loading
  • Item 1 (threadx.py:570-577): clear the five task locals in the loop finally, immediately after task_done(). Reference-count only; no sync.
  • Item 2: raise a fresh StopForward("Forwarding stopped") at all 7 call sites. STOP_FORWARD_EXCEPTION stays importable for compatibility but is never raised. Grep confirms no is STOP_FORWARD_EXCEPTION identity check anywhere in the repo; handlers only use except StopForward.
  • Item 3: const at::cuda::OptionalCUDAGuard device_guard(at::device_of(q_weight)); at the top of both make_q_matrix ops (ext_gptq.cpp:27, ext_awq.cpp:27), mirroring the fix(kernels): pack strided inputs, bind devices, widen offsets, handle M=0 (#4042) #4047 GGUF fix.

3. Measured before/after

torch.cuda.memory_allocated, 2x RTX 4090 (CUDA 13.0, Torch 2.16.0.dev):

Repro Before After
task holding 256 MiB, drop caller refs + gc 256.000 MiB pinned 0.000 MiB, weakref dead
same after one more no-op task 0.0000 MiB 0.0000 MiB
StopForward from a frame holding 512 MiB, caught + gc 512.000 MiB, singleton traceback not None 0.0010 MiB, singleton traceback None
Qwen2.5-0.5B GPTQ 4-bit, 2 GPUs, after quantize()+save()+gc on cuda:1 26.377 MiB 0.000 MiB
make_q_matrix with operands on cuda:1, current cuda:0 0 -> 1 (GPTQ and AWQ) 0 -> 0 (GPTQ and AWQ)

ROCm 2x R9700: the worker-frame repro and its test report 0.000 MiB and a dead weakref after the fix.
Quantize wall time: 11.9 s after vs 12.6 s before (same host, warm cache) -- no throughput regression; the cleanup adds no synchronization.

4. Tests

New regression tests (all fail on pre-fix code, pass after):

Test Invariant
tests/test_threadx.py::test_worker_frame_releases_last_task_references last task's tensor dies without a further submission
tests/test_stop_forward_leak.py::test_stop_forward_does_not_pin_the_raising_traceback singleton keeps no traceback; raising frame's tensor released
tests/kernels/test_exllamav2_device_guard.py::test_make_q_matrix_restores_current_device[gptq/awq] current device unchanged

Commands and results:

CUDA_VISIBLE_DEVICES=<two 4090 UUIDs> PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/test_threadx.py tests/test_stop_forward_leak.py tests/kernels/test_exllamav2_device_guard.py -q
-> 41 passed, 2 skipped, 1 xpassed

CUDA_VISIBLE_DEVICES=<two 4090 UUIDs> PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/test_exllamav2_jit.py tests/test_exllamav2_awq_jit.py tests/test_exllamav2_g_idx_bounds.py tests/module_tree/test_subset.py::test_qwen2_moe_routing_override_all_runs_shared_expert_before_last_expert tests/module_tree/test_subset.py::test_stage_subset_early_stop_and_callbacks -q
-> 19 passed

HIP_VISIBLE_DEVICES=0,1 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest tests/test_threadx.py::test_worker_frame_releases_last_task_references -q
-> 1 passed

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

Pre-fix failure evidence: the same tests fail on origin/main with assert tensor(...) is None (item 1), assert <traceback object> is None (item 2), and assert 1 == 0 (item 3, GPTQ and AWQ).

5. Docs

Committed docs/multi-gpu-memory.md (ownership contract, old/new flow diagrams, measurements) and linked it from README.md operational notes.

6. Risk / scope

  • No GPTQMODEL_* switches. No dense/dequantized weight cache added. CUDA and ROCm paths remain explicit.
  • STOP_FORWARD_EXCEPTION is kept as a compatibility import only; removing it is a separate cleanup.
  • Item 3 is CUDA-only because ExLlamaV2 is CUDA-only in this repo.

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