Skip to content

cuda : CUDA GGML_OP_LIGHTNING_INDEXER implementation (generic vector kernel + wmma kernel) - #25545

Merged
fairydreaming merged 13 commits into
ggml-org:masterfrom
fairydreaming:deepseek-lid-cuda
Jul 15, 2026
Merged

cuda : CUDA GGML_OP_LIGHTNING_INDEXER implementation (generic vector kernel + wmma kernel)#25545
fairydreaming merged 13 commits into
ggml-org:masterfrom
fairydreaming:deepseek-lid-cuda

Conversation

@fairydreaming

@fairydreaming fairydreaming commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Overview

This PR adds CUDA implementation of GGML_OP_LIGHTNING_INDEXER. It contains two kernels: generic portable vector kernel and second faster optimized kernel using WMMA.

Requires #24231 (Edit: already merged) to work.

Additional information

I originally wrote this months ago for #21149, now only updated it to match the CPU implementation (scales removed, added mask).

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES, AI was used to discuss possible optimizations.

@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Jul 10, 2026
ddh0 added a commit to ddh0/llama.cpp that referenced this pull request Jul 11, 2026
including:
- ggml-org/master at `4f37f51`
- ggml-org#24231 at `428831b`
- ggml-org#25545 at `50c49c83`
@fairydreaming
fairydreaming marked this pull request as ready for review July 11, 2026 11:19
@fairydreaming
fairydreaming requested a review from a team as a code owner July 11, 2026 11:19

@JohannesGaessler JohannesGaessler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a cursory look at the code I get the impression that it would benefit from the use of the primitives in mma.cuh. The interface in nvcuda::wmma does not have a defined memory layout in registers so any non-scalar operations need to go through shared memory. To be clear: from my side this is advice regarding the implementation, not a requirement for a review/merge.

@edenfunf

Copy link
Copy Markdown

Tested PR #25545 at e624d0f on an RTX 5070 under Windows 11.

Environment:

  • NVIDIA GeForce RTX 5070, sm_120, 12 GB, VMM enabled
  • CUDA 13.3 (nvcc V13.3.73), Visual Studio 2026, CMake VS generator
  • CMAKE_CUDA_ARCHITECTURES=120a

Correctness:

test-backend-ops test -o LIGHTNING_INDEXER -b CUDA0
96/96 tests passed
Backend CUDA0: OK

Both kernel paths were covered on sm_120: the nh=64 cases with F16/quantized K types used the WMMA kernel, while the remaining cases used the vector kernel. I also observed approximately 14.5–15.0 TFLOPS for the larger nh=64 q4_0 test cases:

LIGHTNING_INDEXER(hsk=128,nh=64,kv=256,nb=512,ns=1,nm=1,type_K=q4_0):   6721 runs -  149.06 us/run -  14.52 TFLOPS
LIGHTNING_INDEXER(hsk=128,nh=64,kv=256,nb=2048,ns=4,nm=4,type_K=q4_0):   435 runs - 2309.64 us/run -  14.99 TFLOPS
LIGHTNING_INDEXER(hsk=128,nh=32,kv=256,nb=512,ns=1,nm=1,type_K=q4_0):  11904 runs -   84.32 us/run -  12.83 TFLOPS

@fairydreaming

Copy link
Copy Markdown
Contributor Author

From a cursory look at the code I get the impression that it would benefit from the use of the primitives in mma.cuh. The interface in nvcuda::wmma does not have a defined memory layout in registers so any non-scalar operations need to go through shared memory. To be clear: from my side this is advice regarding the implementation, not a requirement for a review/merge.

@JohannesGaessler I'm aware of that, but I haven't gotten that far down the CUDA matrix multiplication rabbit hole yet. Leaving that as something for the future.

@am17an am17an left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @JohannesGaessler that mma would be much faster but this that can addressed in a later PR.

Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cuh Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
Comment thread ggml/src/ggml-cuda/lightning-indexer.cu Outdated
@JohannesGaessler

Copy link
Copy Markdown
Contributor

To clarify my position: the only thing that I consider a hard requirement for my approval is a check of the compilation time since that can potentially lead to a stall. Other than that I would consider my comments to only be suggestions.

@fairydreaming

fairydreaming commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

To clarify my position: the only thing that I consider a hard requirement for my approval is a check of the compilation time since that can potentially lead to a stall. Other than that I would consider my comments to only be suggestions.

@JohannesGaessler On my machine (Epyc 9374F):

$ time /usr/local/cuda-12.8/bin/nvcc -forward-unknown-to-host-compiler -DGGML_BACKEND_BUILD -DGGML_BACKEND_SHARED -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128 -DGGML_CUDA_USE_GRAPHS -DGGML_SCHED_MAX_COPIES=4 -DGGML_SHARED -D_GNU_SOURCE -D_XOPEN_SOURCE=600 -Dggml_cuda_EXPORTS --options-file CMakeFiles/ggml-cuda.dir/includes_CUDA.rsp -O3 -DNDEBUG -std=c++17 "--generate-code=arch=compute_120a,code=[sm_120a]" -Xcompiler=-fPIC -use_fast_math -extended-lambda -compress-mode=size -Xcompiler "-Wmissing-declarations -Wmissing-noreturn -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-array-bounds -Wextra-semi -Wno-pedantic" -MD -MT ggml/src/ggml-cuda/CMakeFiles/ggml-cuda.dir/lightning-indexer.cu.o -MF CMakeFiles/ggml-cuda.dir/lightning-indexer.cu.o.d -x cu -c /home/phm/projects/llama.cpp-deepseek-lid-cuda/ggml/src/ggml-cuda/lightning-indexer.cu -o CMakeFiles/ggml-cuda.dir/lightning-indexer.cu.o

real	0m4.357s
user	0m3.737s
sys	0m0.620s

@am17an am17an left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!

@fairydreaming

fairydreaming commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Good job!

In case anyone wants to use it in the meantime it's currently broken. Good job indeed, me.

Edit: should be OK now

ddh0 added a commit to ddh0/llama.cpp that referenced this pull request Jul 14, 2026
@fairydreaming
fairydreaming merged commit 3b53219 into ggml-org:master Jul 15, 2026
20 of 22 checks passed
@FullstackSensei

Copy link
Copy Markdown

Just wanted to share that that this PR works on Mi50 using ROCm and P40 CUDA. Model is fully in VRAM. Pulled and compiled just before the merge

zengde pushed a commit to zengde/llama.cpp that referenced this pull request Jul 16, 2026
…kernel + wmma kernel) (ggml-org#25545)

* cuda : CUDA GGML_OP_LIGHTNING_INDEXER implementation (generic vector kernel + wmma kernel)

* chore : remove indentation of #pragma unroll

* cuda : remove unnecessary kernel template declarations

* cuda : add WARPS_PER_BLOCK and K_VECS_PER_BLOCK template parameters in lightning indexer kernels to avoid duplication of constants.

* cuda : relax MMA architecture requirements to Turing in lightning indexer implementation

* chore : renamed variables

* chore : rename ggml_cuda_op_lightning_indexer() to ggml_cuda_lightning_indexer()

* chore : TODO for AMD rocWMMA

* chore : whitespace formatting

* chore : another variable rename to fix problems caused by shadowing

* chore : yet another rename, this time uppercased all constants

* cuda : added alignment checks for Q and K tensors in lightning indexer implementation

---------

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
CowboyTim pushed a commit to aardbeiplantje/llama.cpp that referenced this pull request Jul 21, 2026
…kernel + wmma kernel) (ggml-org#25545)

* cuda : CUDA GGML_OP_LIGHTNING_INDEXER implementation (generic vector kernel + wmma kernel)

* chore : remove indentation of #pragma unroll

* cuda : remove unnecessary kernel template declarations

* cuda : add WARPS_PER_BLOCK and K_VECS_PER_BLOCK template parameters in lightning indexer kernels to avoid duplication of constants.

* cuda : relax MMA architecture requirements to Turing in lightning indexer implementation

* chore : renamed variables

* chore : rename ggml_cuda_op_lightning_indexer() to ggml_cuda_lightning_indexer()

* chore : TODO for AMD rocWMMA

* chore : whitespace formatting

* chore : another variable rename to fix problems caused by shadowing

* chore : yet another rename, this time uppercased all constants

* cuda : added alignment checks for Q and K tensors in lightning indexer implementation

---------

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
gianni-cor pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Jul 25, 2026
…kernel + wmma kernel) (ggml-org#25545)

* cuda : CUDA GGML_OP_LIGHTNING_INDEXER implementation (generic vector kernel + wmma kernel)

* chore : remove indentation of #pragma unroll

* cuda : remove unnecessary kernel template declarations

* cuda : add WARPS_PER_BLOCK and K_VECS_PER_BLOCK template parameters in lightning indexer kernels to avoid duplication of constants.

* cuda : relax MMA architecture requirements to Turing in lightning indexer implementation

* chore : renamed variables

* chore : rename ggml_cuda_op_lightning_indexer() to ggml_cuda_lightning_indexer()

* chore : TODO for AMD rocWMMA

* chore : whitespace formatting

* chore : another variable rename to fix problems caused by shadowing

* chore : yet another rename, this time uppercased all constants

* cuda : added alignment checks for Q and K tensors in lightning indexer implementation

---------

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
ggerganov pushed a commit to am17an/llama.cpp that referenced this pull request Jul 28, 2026
…kernel + wmma kernel) (ggml-org#25545)

* cuda : CUDA GGML_OP_LIGHTNING_INDEXER implementation (generic vector kernel + wmma kernel)

* chore : remove indentation of #pragma unroll

* cuda : remove unnecessary kernel template declarations

* cuda : add WARPS_PER_BLOCK and K_VECS_PER_BLOCK template parameters in lightning indexer kernels to avoid duplication of constants.

* cuda : relax MMA architecture requirements to Turing in lightning indexer implementation

* chore : renamed variables

* chore : rename ggml_cuda_op_lightning_indexer() to ggml_cuda_lightning_indexer()

* chore : TODO for AMD rocWMMA

* chore : whitespace formatting

* chore : another variable rename to fix problems caused by shadowing

* chore : yet another rename, this time uppercased all constants

* cuda : added alignment checks for Q and K tensors in lightning indexer implementation

---------

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
@jaholmesuk

jaholmesuk commented Jul 30, 2026

Copy link
Copy Markdown

Datapoint from Ampere multi-GPU, since this landed with a big effect for us.

DeepSeek-V4-Flash UD-Q4_K_XL (145GB) fully resident on 4x A100 80GB PCIe, -fa on -ngl 999 --parallel 1, single stream:

  • b9986 (pre-merge): 18.8 t/s decode / ~400 t/s prefill on a 1.9k-token prompt. The log showed resolve_fused_ops: Lightning Indexer not supported, set to disabled.
  • master 86a9c79 (post-merge): 41.0 t/s decode / ~492 t/s prefill. 2.2x decode, and the warning is gone.

Same box, model, and args. The delta spans about two days of commits so not all of it is necessarily this PR, but the indexer was the only fused op reported as disabled on the earlier build. Worth noting we also measured 2-GPU vs 4-GPU as flat pre-merge (18.9 vs 18.8), which ruled out hop overhead and is consistent with the unfused indexer being the per-token bottleneck rather than interconnect.

Happy to run further tests on this hardware if it is useful.

smalinin pushed a commit to smalinin/llama.cpp that referenced this pull request Aug 4, 2026
…kernel + wmma kernel) (ggml-org#25545)

* cuda : CUDA GGML_OP_LIGHTNING_INDEXER implementation (generic vector kernel + wmma kernel)

* chore : remove indentation of #pragma unroll

* cuda : remove unnecessary kernel template declarations

* cuda : add WARPS_PER_BLOCK and K_VECS_PER_BLOCK template parameters in lightning indexer kernels to avoid duplication of constants.

* cuda : relax MMA architecture requirements to Turing in lightning indexer implementation

* chore : renamed variables

* chore : rename ggml_cuda_op_lightning_indexer() to ggml_cuda_lightning_indexer()

* chore : TODO for AMD rocWMMA

* chore : whitespace formatting

* chore : another variable rename to fix problems caused by shadowing

* chore : yet another rename, this time uppercased all constants

* cuda : added alignment checks for Q and K tensors in lightning indexer implementation

---------

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
NighmareGit pushed a commit to NighmareGit/atomic-llama-cpp-turboquant that referenced this pull request Aug 8, 2026
Merges 251 upstream commits on top of the fork's 392. Base was 22b208b
(2026-07-15).

What this brings in for DeepSeek V4:

- CUDA kernels for the hyper-connection ops and the lightning indexer
  (dsv4-hc.cu, lightning-indexer.cu, upstream ggml-org#25585 and ggml-org#25545). These
  landed upstream after our base, so the graph no longer needs a CPU
  fallback for those ops.
- MTP and DSpark support (ggml-org#25784), the wo_a reshape fix on load, and the
  same-K/V-cache-type enforcement (ggml-org#25871).
- Exclusion of the i32 ffn_gate_tid2eid routing table from quantization,
  which the fork did not carry.

Conflict resolution kept both architectures everywhere the two sides
touched the same code:

- llama-kv-cache: kept the fork's default-off attention-rotation policy
  and its env overrides, took upstream's GLM_DSA addition to the DSA
  indexer arch list.
- llama-context: moved the TurboQuant flash-attention auto-enable above
  upstream's generic quantized-V check, which would otherwise reject
  turbo cache types under -fa off, and dropped the fork's older V-cache
  check in favour of upstream's.
- mmq.cuh: kept the fork's int64 offsets in all three of upstream's new
  NVFP4 branches.
- fattn.cu: dropped the WMMA block, since upstream removed that kernel
  and its helpers entirely; kept the RDNA4 turbo path.
- ggml-cuda.cu: kept the host-staged cross-device copy and routed its
  peer copy through upstream's new virtual-to-physical device mapping.
- chat.cpp: rebuilt on upstream's file with the fork's Inkling and
  Laguna parsers and the leading-whitespace tolerance reapplied;
  thinking_end_tag became thinking_end_tags upstream.
- laguna.cpp/laguna.py and mtmd-image.cpp: took upstream, which already
  carries the fork's own upstreamed review fixes plus later refinements.
- Removed the inherited upstream workflows again, per 0c9a069.

GGML_OP_COUNT is 103: upstream's 101 plus the fork's TURBO_WHT and
FLASH_ATTN_EXT_BANDED.

Also drops a duplicate LLM_ARCH_LAGUNA case in test-llama-archs that the
merge would otherwise have left in moe_mandatory.
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
…kernel + wmma kernel) (ggml-org#25545)

* cuda : CUDA GGML_OP_LIGHTNING_INDEXER implementation (generic vector kernel + wmma kernel)

* chore : remove indentation of #pragma unroll

* cuda : remove unnecessary kernel template declarations

* cuda : add WARPS_PER_BLOCK and K_VECS_PER_BLOCK template parameters in lightning indexer kernels to avoid duplication of constants.

* cuda : relax MMA architecture requirements to Turing in lightning indexer implementation

* chore : renamed variables

* chore : rename ggml_cuda_op_lightning_indexer() to ggml_cuda_lightning_indexer()

* chore : TODO for AMD rocWMMA

* chore : whitespace formatting

* chore : another variable rename to fix problems caused by shadowing

* chore : yet another rename, this time uppercased all constants

* cuda : added alignment checks for Q and K tensors in lightning indexer implementation

---------

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Eval bug: DeepSeek-V4-Flash lightning-indexer materialises a ctx-proportional (4 GiB/layer) score buffer

7 participants