Repository navigation
Description
Found on main @ 597b39b, RTX 4090, torch 2.15 + CUDA 13.2.
What is wrong
#4524 (fix for #4488) changed the QQQ reduce scratch cache from one buffer per device to one buffer per (device, stream handle): gptqmodel/nn_modules/qlinear/qqq.py:49-83 _REDUCE_SCRATCH / _shared_reduce_scratch. That fixed the race. However, the cache is module-global and is never pruned, so:
- Each distinct stream that ever ran a QQQ forward keeps a max-width buffer (
max_par * 64 * max(out_features) * 4bytes, which is 56 MiB for Llama-3-8B's N=14336) for the life of the process. - Nothing releases the cache when the model goes away.
del model; gc.collect(); torch.cuda.empty_cache()leaves all of it allocated._clear_reduce_scratch()exists, but only tests call it.
torch.cuda.Stream() takes handles round-robin from PyTorch's per-device stream pool: 32 handles per priority, so 64 per device including priority=-1. Any workload that creates streams per request or per worker therefore ends up pinning 32 x 56 MiB = 1.75 GiB per device, or 3.5 GiB if it also uses high-priority streams. A single-stream run pins 56 MiB. Before #4524 the maximum was 56 MiB per device. #4524 lists "Buffers for destroyed streams remain in the process-wide cache" as a limitation, but docs/kernel-scratch-lifecycle.md still says "the default single-stream case is unchanged" and does not mention this retention or the fact that del model does not free it.
Repro
import gc, sys, torch
from concurrent.futures import ThreadPoolExecutor
from gptqmodel import GPTQModel
import gptqmodel.nn_modules.qlinear.qqq as qqq
mode, N = sys.argv[1], int(sys.argv[2])
model = GPTQModel.load("QQQ-Llama-3-8b-g128", device="cuda:0") # AUTO -> QQQLinear
lin = model.model.model.layers[0].mlp.up_proj # 4096 -> 14336, max_par 16
x = torch.randn(16, lin.in_features, device="cuda:0", dtype=torch.half)
with torch.inference_mode(): lin(x); torch.cuda.synchronize()
base, n0 = torch.cuda.memory_allocated(), len(qqq._REDUCE_SCRATCH)
def call(_):
s = torch.cuda.Stream()
with torch.inference_mode(), torch.cuda.stream(s): lin(x)
s.synchronize(); return s.cuda_stream
if mode == "pool":
with ThreadPoolExecutor(8) as ex: handles = list(ex.map(call, range(N)))
else:
handles = [call(i) for i in range(N)]
torch.cuda.synchronize()
print(f"mode={mode} calls={N} distinct stream handles={len(set(handles))} cache entries {n0}->{len(qqq._REDUCE_SCRATCH)} "
f"retained: {(torch.cuda.memory_allocated()-base)/2**20:.0f} MiB")
del model, lin, x; gc.collect(); torch.cuda.empty_cache()
print(f"after del model + gc + empty_cache: allocated {torch.cuda.memory_allocated()/2**20:.0f} MiB, "
f"cache entries {len(qqq._REDUCE_SCRATCH)}")Output:
mode=percall calls=10 distinct stream handles=10 cache entries 1->11 retained: 560 MiB
after del model + gc + empty_cache: allocated 616 MiB, cache entries 11
mode=percall calls=200 distinct stream handles=32 cache entries 1->33 retained: 1792 MiB
after del model + gc + empty_cache: allocated 1848 MiB, cache entries 33
mode=pool calls=200 (8 workers, new stream per task) distinct stream handles=32 cache entries 1->33 retained: 1792 MiB
after del model + gc + empty_cache: allocated 1848 MiB, cache entries 33
With no model left in the process, 1848 MiB is still allocated (33 x 56 MiB). Handle counts from the stream pool: 500 x torch.cuda.Stream() gives 32 distinct handles, and 500 x torch.cuda.Stream(priority=-1) gives another 32 (64 in the union). Growth is therefore bounded at about 64 buffers per device for pool streams, but unbounded for torch.cuda.ExternalStream / raw handles.
Suggested fix
- Tie the cache lifetime to the model or kernel instance, for example a per-model cache that is dropped when the last
QQQLinearthat uses it is collected, or clear it on model unload. At minimum,del modelshould free it. - Bound or reclaim per-stream entries instead of keeping one forever per handle ever seen. Options: allocate
Cfrom the caching allocator on the current stream per call (it is stream-ordered and the allocator recycles it). Or keep a small per-stream LRU. Or release entries whose streams have been idle, afterrecord_stream/ an event confirms the last use has completed. - Document the retention in
docs/kernel-scratch-lifecycle.md.
Test that should have caught it
tests/test_qqq_kernel_memory_4387.py checks per-stream exclusivity, but not the steady-state footprint across many streams or release after the model is dropped. Add a test that runs N>=8 distinct streams, then del the module, and asserts that the cache is released (or that memory_allocated returns to the baseline).
Severity
Medium ([MEM]). There is no correctness impact. But a threaded or multi-stream server on an 8B QQQ model permanently loses about 1.75 GiB per GPU (up to 3.5 GiB with priority streams), 32x the pre-#4524 footprint, and loading a second model in the same process does not get it back.
🤖 Generated with Claude Code
Activity
ZX-ModelCloud commentedon Nov 17, 2026
Claimed. I'm working on this issue and will submit a fix PR. Please avoid duplicate work in the meantime.
fix(qqq): allocate the reduce scratch per call instead of caching it (#…
Qubitium commentedon Nov 17, 2026
Fixed by #4626 (merged), which allocates the QQQ reduce scratch per call instead of caching it per (device, stream). The PR body had a
FIXStypo so the issue was not auto-closed on merge.