perf(ggml-cuda): skip shared reduction and barrier for single-wave mul_mat_vec_q#521
Open
cheese-cakee wants to merge 1 commit into
Open
perf(ggml-cuda): skip shared reduction and barrier for single-wave mul_mat_vec_q#521cheese-cakee wants to merge 1 commit into
cheese-cakee wants to merge 1 commit into
Conversation
…l_mat_vec_q Wrap the cross-wave reduction epilogue of mul_mat_vec_q in if constexpr (nwarps > 1). For single-warp instantiations this removes a block-wide __syncthreads() and the shared-memory staging that the reduction would otherwise emit, without changing any computed result. The reduction is a no-op when there is only one warp: the shared array is only written under threadIdx.y > 0 (which never holds), the combine loop runs nwarps-1 == 0 iterations, and the barrier synchronizes a single warp. Guarding the block on nwarps > 1 makes that explicit so the compiler no longer has to emit the barrier for the single-wave decode path. For nwarps > 1 the statements are unchanged, only nested inside the guard.
howard0su
approved these changes
Jul 15, 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.
Summary
mul_mat_vec_qends with a cross-wave reduction that combines each warp'spartial sums through shared memory and a block barrier. When the kernel is
instantiated with a single warp (
nwarps == 1) that whole step is inert: thereare no other warps to combine. This change wraps the reduction in
if constexpr (nwarps > 1)so the single-wave path no longer carries a__syncthreads()that synchronizes one warp. The change is byte-for-byteidentical on output; it only affects which instructions the compiler emits for
single-wave instantiations.
Changes
ggml/src/ggml-cuda/mmvq.cu: guard the shared-memory cross-wave reduction inmul_mat_vec_qwithif constexpr (nwarps > 1). One file, no API change, nonew flags.
How it works
For
nwarps > 1the statements are unchanged, only nested inside the guard, soevery multi-warp instantiation behaves exactly as before (same shared array,
same store, same barrier, same combine loop).
For
nwarps == 1the guarded block is provably dead on clean main:tmp_shared/tmp_shared_gateare only written underthreadIdx.y > 0, andwith a single warp
threadIdx.yis always 0, so the store never runs.lin[0, nwarps - 1), which is zero iterations.__syncthreads()synchronizes a block that contains one warp.The shared arrays were already sized with a
nwarps-1 > 0 ? nwarps-1 : 1guard,so removing them for the single-wave case is not what changes; the barrier is.
ptxasdead-code-eliminates the unused shared array on its own, but it cannotremove the barrier, because a barrier has cross-thread semantics it must
preserve. Guarding on
nwarps > 1states the intent in source and lets thesingle-wave decode path drop the barrier.
Performance
Validated locally on an RTX 4050 Laptop (Ada, sm_89), CUDA 12.6.85.
nwarps == 1instantiation ofmul_mat_vec_qdrops from oneBAR.SYNCto zero. Multi-warp instantiationskeep their single
BAR.SYNC.ptxas -vfor clean vs patched over378 compiled
mul_mat_vec_qinstantiations, shared-memory usage is identicalfor every one of them (0 differences). Register counts move by small amounts
in both directions (mean -1.0 registers, i.e. net neutral) as a result of the
reordering; no instantiation changes occupancy tier.
parameter table only selects
nwarpsin {2, 4} for the batch sizes that routeto
mul_mat_vec_q(ncols_dst <= 8), so the removed barrier is neverexecuted here and there is no measurable NVIDIA runtime change. An isolated
microbench that forces the single-wave epilogue measures the removed barrier
at within +/- 3 percent, i.e. below noise, because a one-warp
__syncthreads()is nearly free on this architecture.
The intended beneficiary is decode on architectures whose parameter tables
select a single warp for small-batch mat-vec (AMD GCN and RDNA), where this is
the common path. That end to end measurement is not included here because it
needs the AMD hardware.
Limitations
GPU with synthetic inputs, not a full model run.
not instantiate the single-wave kernel for this op; the AMD measurement is
deferred.
existing
if constexprstyle in this file, so a runtime toggle would addcomplexity with no upside.
Verification
All local, on RTX 4050 (sm_89), CUDA 12.6.
Byte-identity, real kernel. Built the CUDA backend twice from identical
synthetic inputs, once with clean
mmvq.cuand once with the patch, anddumped every
mul_mat_vec_qresult across a matrix of{Q4_0, Q8_0, Q4_K, Q5_K, Q6_K} x K in {256, 4096} x N in {1024, 1000} x
ncols_dst in {1, 2, 4, 5, 8} (100 cases, run with
LUCE_MMVQ_MAX_NCOLS=8sobatches 1..8 route to
mul_mat_vec_q, exercising nwarps=4 for ncols 1..4 andnwarps=2 for ncols 5..8). The two dumps are byte-identical (same SHA-256,
1,619,200 bytes).
Single-wave and fusion coverage. NVIDIA never instantiates the
nwarps == 1path, so an isolated probe reproduces the exact reductionepilogue verbatim for both the clean and patched structure and compares
output for nwarps in {1, 2, 4} with fusion off and on. All identical.
Barrier removal.
cuobjdump -sassconfirms theBAR.SYNCis present in theclean single-wave epilogue and absent in the patched one, and unchanged for
multi-warp.