Skip to content

[BUG] QQQ: one module on two CUDA streams deadlocks the GPU: per-module split-K lock workspace still shared after #4626 (fix claims stream-safe by construction) #4665

Closed

Description

@Qubitium

Found on main @ 12c1273, RTX 5090 (sm_120), torch 2.15 + CUDA 13.2.

What is wrong

#4626 (fix for #4621, after #4488) moved QQQ's reduce scratch C to a per-call allocation. Both the PR and the comment at gptqmodel/nn_modules/qlinear/qqq.py:36-52 describe the forward as "stream-safe by construction" and say "cross-stream aliasing is impossible". docs/kernel-scratch-lifecycle.md says the same.

That only covers C. The kernel's other writable scratch is the split-K lock array self.workspace, which is still one buffer per module. It is registered in __init__/pack (qqq.py:268, :562) and passed to every call at qqq.py:622. qqq_gemm.cu CTAs spin in barrier_acquire(&locks[slice_col], slice_idx) and reset the counter in barrier_release (qqq_gemm.cu:229-252). When the same QQQLinear runs on two streams at once (two threads each with their own torch.cuda.Stream serving one model, which always hits the same layers), the two launches increment and reset each other's counters. The GPU then deadlocks: the kernel never completes, the device sits at 100% utilization, and the process has to be killed.

#4488 / #4619 tested distinct modules only. That case is clean now (0 mismatches below). EXL3's fix #4645 handled the same-module case by allocating the lock array per call (exl3_gemm uses at::zeros for the locks). QQQ did not get the equivalent change.

Repro

import sys, threading, time, os, torch
from gptqmodel.nn_modules.qlinear.qqq import QQQLinear

def build(i, o, seed):
    torch.manual_seed(seed)
    lin = torch.nn.Linear(i, o, bias=False, dtype=torch.float16); lin.weight.data.normal_(0, 0.02)
    m = QQQLinear(bits=4, group_size=-1, sym=True, desc_act=False, in_features=i, out_features=o, bias=False, register_buffers=True)
    m.pack(lin, torch.rand((o, 1), dtype=torch.float16) * 0.02 + 0.01, None)
    m.post_init(); return m.eval().cuda()

mode, M, T, iters = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), 500   # mode: same | distinct
mods = [build(4096, 1024, 11 + k) for k in range(T if mode == "distinct" else 1)]
xs = [torch.randn(M, 4096, device="cuda", dtype=torch.float16) for _ in range(T)]
with torch.inference_mode():
    refs = [mods[k % len(mods)](xs[k]).clone() for k in range(T)]
torch.cuda.synchronize()
done, bad, bar = [0] * T, [0] * T, threading.Barrier(T)
def worker(k):
    s = torch.cuda.Stream(); bar.wait()
    with torch.inference_mode(), torch.cuda.stream(s):
        for _ in range(iters):
            bad[k] += not torch.equal(mods[k % len(mods)](xs[k]), refs[k]); done[k] += 1
ts = [threading.Thread(target=worker, args=(k,), daemon=True) for k in range(T)]
[t.start() for t in ts]; last, t0 = None, time.time()
while any(t.is_alive() for t in ts):
    time.sleep(0.5)
    if done != last: last, t0 = list(done), time.time()
    elif time.time() - t0 > 20: print(f"{mode} M={M} T={T}: HANG, calls done {done}"); os._exit(3)
print(f"{mode} M={M} T={T}: mismatches {bad} of {iters}")

Output (each in a fresh process):

distinct M=16 T=4: mismatches [0, 0, 0, 0] of 500      # #4488 case: fixed
same     M=16 T=2: HANG, calls done [4, 4]
same     M=8  T=2: HANG, calls done [99, 98]
same     M=1  T=2: HANG, calls done [191, 189]          # decode

All four "same" runs at 4096x1024 hung. A run at 4096x4096 M=16 T=4 completed, so whether it hangs depends on the shape, which matches #4619: launches that do not fill every SM can be co-resident. If the forward is patched to pass torch.zeros_like(self.workspace) per call (no other change), the same cases give mismatches [0, 0] of 500 (M=16 T=2, M=1 T=2) and [0, 0, 0, 0] (M=1 T=4), with no hang.

Suggested fix

Marlin (MarlinLinear, AwqMarlinLinear) has the same per-module workspace and also hangs in the same harness (4096x4096 M=16 T=4: HANG, calls done [74, 76, 74, 78]). However, docs/marlin_scratch.md:29-31 explicitly says ordinary Marlin forwards are not safe across streams, so this report covers only QQQ, whose code and docs now claim the opposite.

Test that should have caught it

tests/test_qqq_kernel_memory_4621.py and tests/test_qqq_kernel_memory_4387.py check scratch release and distinct-call buffers, but never run one module on two concurrent streams. Add a GPU test that runs the same QQQLinear (for example 4096x1024, M=1 and M=16) from 2-4 threads with private streams, compares the output against the serial reference, and fails on a progress watchdog instead of hanging CI.

Severity

High for multi-stream serving: a GPU-side deadlock on a supported kernel that requires killing the process. Single-stream inference is not affected.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Taking this. The per-call reduce scratch from #4626 left the split-K lock array per module; qqq_gemm increments and resets those counters, so two streams on the same QQQLinear corrupt them and deadlock. I'll allocate the lock array per call, like EXL3's exl3_gemm (#4645), and keep the module buffer only for checkpoint compatibility.

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