Repository navigation
Description
Found on main @ 597b39b, RTX 4090 (SM89), torch 2.15 + CUDA 13.2.
What is wrong
The EXL3 CUDA inference path (ExllamaV3Linear -> LinearEXL3.forward) uses three writable scratch buffers that are shared per device, not per stream. Two ExllamaV3Linear modules running on different CUDA streams (for example, threads that each use their own torch.cuda.Stream) can execute kernels at the same time and write into the same scratch. QQQ had the same pattern; #4488 / #4524 fixed it there, but EXL3 was not changed.
| Path (taken when) | Shared scratch | Where |
|---|---|---|
bc.run -> exl3_gemm (M <= 32) |
one MAX_TILES_C int lock array per device, used for the cross-CTA split-K barriers (barrier_acquire/barrier_release on locks[slice_m * blocks_n + slice2_n]) |
gptqmodel_ext/exllamav3/quant/exl3_devctx.cu:65 DevCtx::get_locks(device), quant/exl3_gemm.cu:91, quant/exl3_gemm_inner.cuh:370-372, 519 |
hgemm (M > 32, every prefill) |
one 4 MiB cuBLAS workspace per device, installed on the caller's handle with cublasSetWorkspace |
gptqmodel_ext/exllamav3/hgemm.cu:56-57, quant/exl3_devctx.cu:52 DevCtx::get_ws(device) |
bc.run with M == 1 |
xh (the Hadamard-rotated input) comes from g_tensor_cache.get(device, (1, in_features), dtype), so it is one buffer for every module with the same in_features on that device (q/k/v/gate/up all share it) |
gptqmodel/exllamav3/modules/quant/exl3.py:80-89, gptqmodel_ext/exllamav3/libtorch/linear.cpp:30 |
The lock array is the most serious of the three. exl3_gemm is a cooperative kernel whose CTAs spin in barrier_acquire until a lock reaches an expected stage. When two such kernels run concurrently on different streams, their CTAs increment and reset the same counters. The result is either silently wrong output or a GPU-side deadlock: the kernel never finishes, the device stays at 100% utilization, and the process has to be killed. This happens whenever the kernel does not occupy every SM (small N, for example the k_proj/v_proj of a GQA model), because then two launches can be co-resident.
The cudaSetDevice calls inside get_ws/get_locks do not leak device state on the paths I could exercise. Both callers already hold an OptionalCUDAGuard for the same ordinal, and prepare_ctx() has no callers.
Repro (self-contained, random trellis, no checkpoint needed)
import sys, threading, time, os, torch
from gptqmodel.exllamav3.modules.quant.exl3 import LinearEXL3
import gptqmodel.exllamav3.modules.quant.exl3 as exl3_mod
dev = torch.device("cuda:0"); torch.manual_seed(0)
def make(k, n, K=4):
trellis = torch.randint(-32768, 32767, (k // 16, n // 16, 16 * K), dtype=torch.int16, device=dev)
sign = lambda d: (torch.randint(0, 2, (d,), device=dev) * 2 - 1).half()
mcg = torch.tensor(3417055213 - 2**32, dtype=torch.int32, device=dev)
return LinearEXL3(None, k, n, suh=sign(k), svh=sign(n), trellis=trellis, mcg=mcg, out_dtype=torch.half)
def run(M, n_threads, k=2048, n=512, iters=2000):
mods = [make(k, n) for _ in range(n_threads)] # distinct layers
xs = [torch.randn(M, k, device=dev).half() for _ in mods]
refs = [m.forward(x, {}).clone() for m, x in zip(mods, xs)] # serial reference
torch.cuda.synchronize()
bad = [0] * n_threads; done = [0] * n_threads; rel = [0.0] * n_threads
def worker(i):
s = torch.cuda.Stream()
with torch.cuda.stream(s):
for _ in range(iters):
y = mods[i].forward(xs[i], {})
if not torch.equal(y, refs[i]):
bad[i] += 1; rel[i] = max(rel[i], ((y - refs[i]).abs().max() / refs[i].abs().max()).item())
done[i] += 1
ts = [threading.Thread(target=worker, args=(i,), daemon=True) for i in range(n_threads)]
[t.start() for t in ts]; last, t0 = None, time.time()
while any(t.is_alive() for t in ts):
time.sleep(1)
if done != last: last, t0 = list(done), time.time()
elif time.time() - t0 > 30:
print(f"M={M}: HANG, calls done {done}, mismatches {bad}", flush=True); os._exit(3)
print(f"M={M} threads={n_threads}: mismatches {bad} of {iters} each, max rel err {max(rel):.3f}")
case = sys.argv[1]
if case == "serial": run(8, 1); run(1, 1, n=8192); run(256, 1)
if case == "lock": run(8, 4) # exl3_gemm, shared lock array
if case == "lock_hang": run(4, 4)
if case == "xh": run(1, 4, n=8192) # bsz=1 shared xh
if case == "hgemm": run(256, 4) # hgemm, shared cuBLAS workspaceOutput (each case in a fresh process):
serial: M=8 threads=1: mismatches [0] of 2000 each, max rel err 0.000
M=1 threads=1: mismatches [0] of 2000 each, max rel err 0.000
M=256 threads=1: mismatches [0] of 2000 each, max rel err 0.000
lock: M=8 threads=4: mismatches [1867, 1868, 1864, 1902] of 2000 each, max rel err 0.484
lock_hang: M=4: HANG, calls done [1165, 1255, 1250, 1061], mismatches [0, 1, 1, 0] (GPU stuck at 100% until the process is killed)
xh: M=1 threads=4: mismatches [46, 54, 52, 52] of 2000 each, max rel err 0.870
hgemm: M=256 threads=4: mismatches [2, 2, 0, 2] of 2000 each, max rel err 1.589
The same thing happens on a real checkpoint (Llama-3.2-1B-Instruct EXL3 W4, mcg, loaded with GPTQModel.load, AUTO -> ExllamaV3Linear). Each thread forwards a different layer's module on its own stream:
- k_proj/v_proj of layers 0-1, M=8, 2 threads: 1259/3000 and 1261/3000 wrong outputs, max rel err 0.61.
- q/o/gate/up of layers 0-3, M=1, 4 threads: 84/8000 wrong outputs, max rel err 0.95. o_proj and q_proj share
xh. - k/v/down of layers 0-3: M=8 hangs after about 700 calls per thread. M=256 hung in 3 out of 3 runs, after 1 to 43 calls.
- Large-N modules (q_proj, gate_proj, down_proj, o_proj) at M=2..32 gave 0 mismatches. Their launch fills all 128 SMs, so two launches never overlap.
Controls (same box)
- The same workload with one global lock around forward + stream sync: 0 mismatches and no hang.
- Replacing only
ext.hgemmwithtorch.matmul(a, b, out=c), which uses torch's per-stream cuBLAS workspace: M=256 with 4 threads gives 0 mismatches. - A rebuilt extension where
DevCtx::get_locks/get_wsare keyed by(device, cudaStream_t), with every other line unchanged, giveslock,lock_hang(M=4 and M=8 with K=8192) andhgemmall at 0 mismatches and no hang.xhstill fails ([1, 55, 45, 0]), as expected. - Patching
g_tensor_cache.getto allocate a privatexhper module:xhgives 0 mismatches on both builds.
Suggested fix
- Key the EXL3 lock array and the hgemm workspace by
(device, stream), as fix(qqq): own reduce scratch per CUDA stream (#4488) #4524 did for QQQ'sC. Alternatively, take the lock buffer from the PyTorch caching allocator on the current stream. It must be zeroed, and the kernel leaves it zeroed after a clean run. - For
hgemm, use the workspace torch already associates with(handle, stream)instead of overwriting it with the device-global one. - Drop the
g_tensor_cache(1, in_features)buffer. Allocatexhper call, as the M>1 path already does withat::empty_like(x), or give each stream its own. A per-module buffer is still unsafe when the same module is called from two streams. - A per-stream cache that is never pruned also needs a cleanup story. QQQ's per-stream
Ccache from fix(qqq): own reduce scratch per CUDA stream (#4488) #4524 has the same gap.
Test that should have caught it
tests/test_qqq_kernel_memory_4387.py added CUDA-stream ownership tests for QQQ only. An equivalent EXL3 test is needed: two ExllamaV3Linear/LinearEXL3 modules with N=512, two or more threads with their own streams, and M in {1, 4, 8, 256}. It should assert bitwise equality with the serial output and run under a timeout, because the failure mode includes a GPU hang.
Severity
High for anyone who runs EXL3 inference concurrently on one GPU, for example threaded serving with per-request streams. The outputs are silently wrong, with relative error up to 1.6, and the cooperative GEMM can deadlock the GPU. Single-stream inference and quantization are not affected.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Claimed. This issue is now owned by a dedicated worker; a fix PR referencing it will follow.
fix(exl3): own kernel scratch per call instead of per device (#4619) (#…
Merge origin/main; keep stream-scoped cuBLAS/EXL3 scratch and FP32 bias