Skip to content

fix(qqq): own reduce scratch per CUDA stream (#4488) - #4524

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4488-qqq-scratch-stream
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4488-qqq-scratch-stream

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4488

Root cause

_shared_reduce_scratch() cached one INT32 C reduction buffer per torch.device and released the mutex before returning a live view. QQQLinear.forward() passes that view to qqq_gemm, which reads and writes C for the whole call and launches on the current CUDA stream. Two eager modules on different CUDA streams therefore received the identical C storage 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 of device:

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"]
    end
Loading
Before After
Cache key: device Cache key: (device, stream handle)
Two modules on different streams share C Each stream has its own C
Mutex only guards allocation Stream order is the completion order for same-stream reuse
— CPU/no-stream callers keep one buffer (synchronous path; native QQQ is CUDA-only)

QQQLinear._reduce_scratch() resolves torch.cuda.current_stream(device) (a SAFE, thread-local API per pytorch_gil_0_compat.md). The handle is used as the key rather than the Stream wrapper so the cache cannot keep a user-created stream alive. torch.compile keeps 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 QQQLinear modules, same width, separate non-default CUDA streams, qqq_gemm stubbed to record C.data_ptr():

  • Before (qqq.py from 461b42023): test_qqq_native_forward_separates_scratch_across_streams fails with assert 70262982144 != 70262982144.
  • Before: test_qqq_reduce_scratch_is_exclusive_per_cuda_stream fails with assert 12918456320 != 12918456320.
  • After: both pass; same-stream calls still return the same pointer.

The issue's CPU-only AST probe still prints same_pointer: True after 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 passed
  • pytest 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 deselected

ROCm (/opt/venvs/rocm101t, HIP_VISIBLE_DEVICES=0):

  • same combined command -> 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); torch 2.16.0.dev+cu130 and 2.16.0.dev+rocm10.1.

Limitations

  • One retained buffer per active stream. A workload using many streams retains one max-width buffer per stream; the default single-stream case is unchanged. Buffers for destroyed streams remain in the process-wide cache until _clear_reduce_scratch().
  • The native QQQ CUDA extension is not built in this environment (CUDA_HOME unset per docs/kernel-scratch-lifecycle.md), so no real GPU kernel arithmetic was executed. The regression tests assert scratch ownership and the eager forward wiring with qqq_gemm stubbed; no numerical claim is made.
  • ROCm skips the CUDA-stream tests because the native kernel is CUDA-only; HIP exposes the CUDA stream namespace, so this is a coverage limitation, not a behaviour claim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] QQQ shared reduction scratch has no in-flight CUDA stream ownership

1 participant