Skip to content

CUDA : add warp-per-row WKV7 kernel for single-token decode - #26111

Merged
ggerganov merged 1 commit into
ggml-org:masterfrom
123123213weqw:wangyue/rwkv7-cuda-t1
Aug 11, 2026
Merged

ggerganov merged 1 commit into
ggml-org:masterfrom
123123213weqw:wangyue/rwkv7-cuda-t1

Conversation

@123123213weqw

@123123213weqw 123123213weqw commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Follow-up to #26080.

This one speeds up the T/B == 1, C/H == 64 case of WKV7 — basically single-token decode on head size 64 models.

Right now each state row is handled by a single thread that keeps its own float state[64] and does the two 64-wide reductions serially. That's a pretty heavy thread, and there's no parallelism in the reduction. So I wrote a new kernel (rwkv_wkv7_f32_t1_warp_row) where a whole warp works on one row — each lane takes care of two columns, and reductions just use warp shuffles.

If the shape doesn't match, it falls back to the existing kernel, nothing changes there.

Additional information

How it's launched:

constexpr int rows_per_block = 4;
block = (32, 4);     // WARP_SIZE x rows_per_block
grid  = (B * H, 16); // CUDA_WKV_BLOCK_SIZE / rows_per_block

The kernel is templated on rows_per_block and carries launch_bounds(WARP_SIZE * rows_per_block, 2).

I tried rows_per_block = 2, 4 and 8 on my 4080 and got 293.1 / 293.3 / 293.4 tok/s, so it's basically noise — stuck with 4.

What changed:

  • ggml/src/ggml-cuda/wkv.cu — the new kernel and the dispatch branch
  • tests/test-backend-ops.cpp — added wkv7(F32, H=32, D=64, T=1, B=4) since multi-seq decode wasn't covered

Checked against the CPU backend with F32 inputs (NMSE < 1e-7):

head_count=32, head_size=64, n_seq_tokens=1,   n_seqs=1   PASS
head_count=32, head_size=64, n_seq_tokens=1,   n_seqs=4   PASS
head_count=32, head_size=64, n_seq_tokens=32,  n_seqs=1   PASS
head_count=32, head_size=64, n_seq_tokens=32,  n_seqs=4   PASS
head_count=32, head_size=64, n_seq_tokens=128, n_seqs=4   PASS
5/5 passed

The T=32/T=128 ones go through the old kernel, so the fallback path gets a bit of coverage too.

Benchmarks on RWKV7 1.5B Q4_K_M (gen 128, 10 runs, ngl 999, no prompt):

GPU CUDA master this PR
V100 32GB 12.8.93 146.1 tok/s 151.9 tok/s +4.0%
RTX 4080 13.0.88 283.2 tok/s 293.3 tok/s +3.5%

HIP shares this .cu file but I don't have any AMD hardware, so no ROCm numbers unfortunately.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES — I used AI to poke around the codebase and sketch early versions of the kernel. The final code and all the benchmarks are mine.

@123123213weqw
123123213weqw requested review from a team and ggerganov as code owners July 25, 2026 10:14
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Jul 25, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Jul 25, 2026

Copy link
Copy Markdown

Hi @123123213weqw, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@123123213weqw

Copy link
Copy Markdown
Contributor Author

Hi @123123213weqw, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

done

@123123213weqw
123123213weqw force-pushed the wangyue/rwkv7-cuda-t1 branch from f35ee11 to 33c0a39 Compare July 26, 2026 01:33
@am17an am17an added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Aug 8, 2026
@ggerganov
ggerganov merged commit 5988633 into ggml-org:master Aug 11, 2026
23 of 59 checks passed
gabe-l-hart added a commit to gabe-l-hart/llama.cpp that referenced this pull request Aug 12, 2026
Signed-off-by: Gabe Goodhart <[email protected]>

* origin/master: (383 commits)
  cmake :  introduce semantic versioning  (ggml-org#26839)
  gguf : harden loader against malformed tensor dims and metadata types (ggml-org#25596)
  kleidiai: Add runtime feature detection mechanism for aarch64/kleidiai (ggml-org#26076)
  model : disallow integer dflash sliding_window_pattern (ggml-org#26900)
  sync : ggml
  cmake : add config version support (ggml/1582)
  server : support slot save/restore with media inputs (ggml-org#26640)
  ui: add read_media tool (ggml-org#25877)
  opencl: default FA c8 cluster width to 16 on X1E (ggml-org#26433)
  tests : update speculative params (ggml-org#26925)
  vulkan: add TQ2_0 (ternary) support (ggml-org#25850)
  wavtokenizer-dec : bound posnet/convnext block_count against n_layer_all (ggml-org#26892)
  convert : handle per_layer_config in Gemma4 (transformers 5.15) (ggml-org#26882)
  opencl: use flat mv q5_k when weight exceeds image1d_buffer_t limit (ggml-org#26880)
  chat : fix muse-glimmer detection of tool calls after EOM (ggml-org#26879)
  ci : add missing release check (ggml-org#26923)
  CUDA: only disable CUDA graphs when mul_mat_id actually needs a stream sync (ggml-org#26802)
  cuda : add warp-per-row wkv7 kernel for single-token decode (ggml-org#26111)
  spec : update speculative-simple (ggml-org#26904)
  chat : tighten bare function parsing for Qwen models (ggml-org#26793)
  ...
huaxel pushed a commit to huaxel/strix-llama.cpp that referenced this pull request Aug 12, 2026
brittlewis12 pushed a commit to brittlewis12/llama.cpp that referenced this pull request Aug 17, 2026
thecodacus pushed a commit to thecodacus/llama.cpp that referenced this pull request Sep 7, 2026
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
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 merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants