CUDA: cuBLAS path for lightning_indexer on Volta - #28888
Closed
CoREse wants to merge 1 commit into
Closed
Conversation
The op has a wmma kernel for Turing and newer and a generic vector kernel for everything else; on a V100 the latter reaches ~4.3 TFLOPS while cuBLAS does the same multiply-accumulate at ~20 TFLOPS. Compute K^T Q with cuBLAS one chunk of query rows at a time and do relu, the per-head weighting, the sum and the mask add in one fused kernel. C is laid out n_kv x n_head*nb so that each head is a contiguous stream for the reduce kernel; the row-major alternative made it 60-78% of the total time. Scratch is bounded at 128 MB and does not grow with context length. test-backend-ops on V100: 144/144 LIGHTNING_INDEXER cases pass, 4.3 -> 20 TFLOPS. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FBqdxhtLYCXWCqbGx6LJuh
|
Hi @CoREse, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
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
GGML_OP_LIGHTNING_INDEXER(DeepSeek-V4) computesscore[b,kv] = sum_h w[b,h] * relu(q[b,h] . k[kv]) + mask[b,kv]. The CUDA backend has a wmma kernel for Turing and newer and a generic vector kernel for everything else. On V100 the vector kernel reaches ~4.3 TFLOPS, while cuBLAS on the same card does the same multiply-accumulate at ~20 TFLOPS (tensor cores viaCUBLAS_COMPUTE_32F_FAST_16F).This PR adds a cuBLAS path for pre-Turing NVIDIA GPUs: for each chunk of query rows, one GEMM computes
C = K^T Q(n_kv x n_head*nb, column-major) and one fused kernel does relu, the per-head weighting, the sum and the mask add. Scratch memory is bounded (128 MB by default) and does not grow with context length. The existing kernels are untouched and still used everywhere else.The layout of
Cmatters a lot: with the products of one(b, kv)pair contiguous (n_head*nb x n_kv) the reduce kernel has neighbouring lanes gathering across an_head*nbstride and takes 60-78% of the total time; withn_kv x n_head*nbeach head is one contiguous stream and the same GEMM ends up more than twice as fast.Test
test-backend-ops -o LIGHTNING_INDEXER, V100 (sm_70), CUDA 12.9: 144/144 pass (the op'smax_nmse_erris 1e-6).test-backend-ops perf -o LIGHTNING_INDEXER,hsk=128, nh=64, type_K=f16:End to end, DeepSeek-V4-Flash (UD-Q8_K_XL, 4x V100-16GB, experts on CPU), 54,564-token prompt: the indexer was 24% of GPU time at 23K context; combined with #28887 prompt processing goes 147.1 -> 178.4 t/s.
Quality,
llama-perplexity --kl-divergence, wikitext-2,-c 32768 --chunks 2: mean KLD 0.0045, top-1 agreement 97.5%, PPL 3.9701 vs 3.9689 on master (±0.048). Note that a mathematically equivalent variant of this path (plaincublasSgemm, exact f32) gives the same 0.0043: the deviation comes from the changed summation order flipping near-ties in the model's own top-512 routing, not from the reduced-precision accumulate. Running the same binary twice gives KLD 0.000000 / 100%, so this is measured against a deterministic baseline.Notes
cc == GGML_CUDA_CC_VOLTA; I only have V100s to test on. Pascal/Maxwell would very likely benefit too (they run the same vector kernel), but I did not want to enable something I cannot measure.🤖 Generated with Claude Code
https://claude.ai/code/session_01FBqdxhtLYCXWCqbGx6LJuh