Skip to content

fix(qqq): allocate the split-K lock array per forward call (#4665) - #4717

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4665-qqq-locks
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4665-qqq-locks

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4665. qqq_gemm.cu uses its workspace argument as the split-K barrier: each CTA spins in barrier_acquire, increments a per-tile counter, and resets it in barrier_release. The array was one buffer per QQQLinear, so two streams serving the same module (always the same layers) incremented and reset each other's counters; the kernel never completed and the device sat at 100% until the process was killed. #4626 made C per-call, but the docs' "stream-safe by construction" claim did not cover this second writable buffer.

Change

QQQLinear._split_k_locks(device) allocates a zeroed ceil(out_features / 64) * max_par INT32 array for each forward and passes it to mul. This matches EXL3's exl3_gemm, which allocates its locks per call (#4645). The registered workspace buffer is kept only so existing checkpoints keep loading; the kernel no longer reads it. The array is a few hundred int32 slots, so the per-call allocation is negligible next to the kernel call.

docs/kernel-scratch-lifecycle.md gains a "QQQ split-K lock array" section.

before after
lock array lifetime per module per call
two streams on one module counters corrupted → deadlock no aliasing possible
checkpoint compat workspace buffer unchanged (buffer kept, unused)

Tests

tests/kernels/test_qqq_workspace.py::test_forward_allocates_a_fresh_lock_array_per_call (new): calls QQQLinear.forward twice with the kernel intercepted and asserts the two workspace arguments are distinct objects, neither is the module buffer, and both are zeroed INT32 arrays of _workspace_numel(out_features, max_par). Fails on unmodified main (both calls receive the same tensor).

QQQ suites (test_qqq_workspace, test_qlinear_hierarchy, test_inference_numeric_range, test_qqq_jit, test_qqq_uncaptured_fallback_4468, test_qqq_export_scale): 280 passed, 65 skipped, 1 pre-existing test_qqq_export_scale tolerance failure also present on main. ruff check clean.

Not verified here: the two-stream CUDA deadlock repro needs two GPUs on this CPU-only host; the test asserts the aliasing property that caused it.

Resolves #4665.

`qqq_gemm.cu` spins on and resets the counters in its `workspace` argument,
so one lock array per `QQQLinear` let two streams serving the same module
increment and reset each other's counters; the kernel never completed and the
device had to be reset. The per-call reduce scratch from #4626 covered only
`C`, and the docs' "stream-safe by construction" claim did not hold for this
second writable buffer.

`QQQLinear._split_k_locks(device)` allocates a zeroed
`ceil(out_features / 64) * max_par` INT32 array for each forward, matching
EXL3's per-call locks (#4645). The registered `workspace` buffer stays only so
existing checkpoints keep loading; the kernel no longer reads it.

Tests: `tests/kernels/test_qqq_workspace.py::test_forward_allocates_a_fresh_lock_array_per_call`
fails on unmodified main (both calls receive the same tensor) and asserts two
distinct zeroed arrays of the kernel's size. QQQ suites: 280 passed, 65
skipped, 1 pre-existing `test_qqq_export_scale` tolerance failure also present
on main. The CUDA concurrency repro needs two GPUs and is not runnable here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant