Repository navigation
fix(qqq): own reduce scratch per CUDA stream (#4488) - #4524
Merged
Merged
Conversation
This was referenced Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4488
Root cause
_shared_reduce_scratch()cached one INT32Creduction buffer pertorch.deviceand released the mutex before returning a live view.QQQLinear.forward()passes that view toqqq_gemm, which reads and writesCfor the whole call and launches on the current CUDA stream. Two eager modules on different CUDA streams therefore received the identicalCstorage with no ordering between their kernels: one kernel could overwrite the other's partial reductions. The allocation lock was not a scratch-use lease.Fix
Key the process-wide scratch cache by
(device, current CUDA stream handle)instead ofdevice:flowchart LR subgraph NEW["one flat buffer per device + current stream"] R1["scratch(cuda:0, stream s1)"] R2["scratch(cuda:0, stream s2)"] L1["QQQLinear on s1"] -->|"reuse, ordered by s1"| R1 L2["QQQLinear on s1"] -->|"reuse, ordered by s1"| R1 L3["QQQLinear on s2"] -->|"own view"| R2 R1 --> K1["qqq_gemm C on s1"] R2 --> K2["qqq_gemm C on s2"] enddevice(device, stream handle)CCQQQLinear._reduce_scratch()resolvestorch.cuda.current_stream(device)(a SAFE, thread-local API perpytorch_gil_0_compat.md). The handle is used as the key rather than theStreamwrapper so the cache cannot keep a user-created stream alive.torch.compilekeeps its per-call allocation. Memory behaviour is unchanged for the normal single-stream case: one max-width buffer per stream (56 MiB at N=14336 instead of 5376 MiB per-module).Docs updated in
docs/kernel-scratch-lifecycle.md(diagram + before/after table + contract).Before / after evidence
Two eager
QQQLinearmodules, same width, separate non-default CUDA streams,qqq_gemmstubbed to recordC.data_ptr():461b42023):test_qqq_native_forward_separates_scratch_across_streamsfails withassert 70262982144 != 70262982144.test_qqq_reduce_scratch_is_exclusive_per_cuda_streamfails withassert 12918456320 != 12918456320.The issue's CPU-only AST probe still prints
same_pointer: Trueafter the change. That probe has no stream: CPU execution is synchronous and the native QQQ kernel is CUDA-only, so the CPU buffer remains shared (as #4387 requires). The new tests exercise real CUDA streams.Tests (all with
OMP/MKL/OPENBLAS/NUMEXPR_NUM_THREADS=4)CUDA (
/opt/venvs/cu130t,CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae):pytest tests/test_qqq_kernel_memory_4387.py -q->10 passedpytest tests/test_qqq_kernel_memory_4387.py tests/test_qqq_cpu.py tests/test_gsq_method_adapters.py -q -k "qqq or 4387 or kernel_memory"->62 passed, 10 deselectedROCm (
/opt/venvs/rocm101t,HIP_VISIBLE_DEVICES=0):60 passed, 2 skipped, 10 deselected(the two CUDA-stream tests are skipped because QQQ's native kernel is CUDA-only)Lint:
ruff check gptqmodel docs/eora tests setup.py->All checks passed!git fetch origin main && git merge --no-edit origin/main-> already up to date; tests and lint re-run after the merge.Hardware
NVIDIA RTX 4090 (
GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae), AMD R9700 (HIP_VISIBLE_DEVICES=0); torch2.16.0.dev+cu130and2.16.0.dev+rocm10.1.Limitations
_clear_reduce_scratch().CUDA_HOMEunset perdocs/kernel-scratch-lifecycle.md), so no real GPU kernel arithmetic was executed. The regression tests assert scratch ownership and the eager forward wiring withqqq_gemmstubbed; no numerical claim is made.