Repository navigation
cuda : take the transposed concat kernel from two tokens, not 32 - #291
Conversation
Adds test_concat cases for the qwen4exp conv-state concat as the decode and MTP-verify graphs build it: state [3 x 10240] concatenated along dim 0 with the transposed qkv_mixed [10240 x n_tokens] (conv kernel 4; 16 key heads and 48 value heads of 128), for 1 to 16 tokens in the eval list and 1 to 32 in the perf list. The existing transposed-src1 cases start at 31 tokens. Assisted-by: Claude Fable 5.1 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
concat_cuda sent a dim-0 concat with a transposed src1 to the transposed tile kernel only from 32 tokens up. Below that it fell to the generic non-contiguous kernel, which launches one block of 256 threads per output row and writes a handful of elements per block. For the qwen4exp conv-state concat (state [3 x 10240] ++ transpose(qkv_mixed [10240 x n_tokens]), 36 layers per graph) that is every MTP verify batch and every multi-row decode. Isolated on gfx1151 (test-backend-ops perf, ROCm0, us per op): tokens before after 1 3.74 3.74 (contiguous kernel, unchanged) 2 24.47 7.87 4 24.50 7.88 8 24.56 7.90 16 24.21 8.03 32 9.11 9.12 (tile kernel, unchanged) The one-token case stays on the contiguous kernel: a one-column transpose is contiguous and that path is faster than the tile kernel for it (7.85 us). test-backend-ops -o CONCAT on ROCm0: 193/193. Assisted-by: Claude Fable 5.1 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
c00bb06 to
39b617d
Compare
dzannotti
left a comment
There was a problem hiding this comment.
Verdict
The change is correct and does what it says. The verify-batch concat on gfx1151 drops from about 24 us to 8 us per layer, outputs are byte-identical, and end-to-end MTP decode is 0.6 to 0.9 % faster. Two small cleanups are below; none of them blocks the merge, which needs a maintainer approval.
Direction
Accept. The tile kernel checks bounds on tokens and channels, so the old 32-token floor only picked a kernel and had no correctness role. Note for ordering: per its diff, #297 fuses this concat into the Gated DeltaNet decode chain at 2-8 tokens. After #297 lands, the gain here stays for 9-31 token batches and for the other conv models (qwen35, qwen3next, lfm2 and others), not for qwen4exp MTP verify at 2-8 tokens.
Asks
- Should fix:
tests/test-backend-ops.cpp:18162and:19398. The comments say "below the transposed kernel's token minimum" and "the transposed kernel's token minimum". After this PR the minimum is 2, so both are wrong. Describe the shapes (decode and verify widths) instead. - Should fix:
ggml/src/ggml-cuda/concat.cu:199-200. The comment tells the history and the benchmark numbers, which are already in the commit message. One line about why 1 token stays on the contiguous kernel is enough. - Optional, not needed for merge: the tile path is slower than the old path for shapes that no model builds today.
{4096,32}at 2 tokens goes from 4.4 to 28.2 us, andsrc1->ne[1] == 1goes from 2.1 to 2.4 us. Asrc1->ne[1] > 1check, or a cap onsrc0->ne[0], would keep those on the old path.
Correctness
PASS on ROCm. Prompt token IDs and f32 final logits are byte-identical, base vs head, on all four corpora at -ub 16, which runs the new path, and at -ub 512. The .kld files are byte-identical for the short case (-c 2048 -ub 16) and the long case (-c 16384). Same top p is 100 %. Greedy decode, 256 tokens, is identical, and base A/A is stable. With HIP_LAUNCH_BLOCKING=1 the logits are also identical. test-backend-ops -o CONCAT passes 251/251 on ROCm0 with HIP_LAUNCH_BLOCKING unset and set to 1, including your 6 cases and 21 extra edge cases that we added (f16, bf16, i8, i64, ne2 > 1, empty src0, 31/32/33 tokens). The full ROCm0 suite passes 56968/56968 on both sides. The -Werror build is clean. VGPR and SGPR spill counts are equal on both sides. Vulkan is not changed; its CONCAT tests pass with the new cases.
Performance
Qwen3.8-Flash-Next (Gyro-M TQ2_0) with the shared MTP draft, --spec-draft-n-max 3, 256 generated tokens, server arms alternated base/head/head/base, 3 requests each (6 per side):
| prompt | base tok/s | head tok/s | spread (sd) | gain |
|---|---|---|---|---|
| 2.3k | 29.27 | 29.55 | 0.03 / 0.06 | +0.93 % |
| 11.8k | 47.94 | 48.32 | 0.06 / 0.08 | +0.78 % |
| 45k | 39.44 | 39.69 | 0.07 / 0.07 | +0.64 % |
The ranges do not overlap. Generated text, drafted tokens and accepted tokens are identical, and TTFT does not change. A kernel trace of one request shows the 2072 verify concats going from 23.7 to 7.5 us each. PP2048 at depths 0 to 64000 (+-0.05 %), pp2 to pp16 and tg128 do not change, as expected: they do not run the 2-31 token concat, or they stay inside the spread.
Verification
Strix Halo (gfx1151), ROCm 10.1. We tested the PR merged into master f2230a8 (5 commits behind, clean merge) against that master, with the same build flags.
Not tested
NVIDIA and non-RDNA3.5 AMD hardware (the gate is not device-specific), your UD-IQ4_XS model files, and the wide ops-perf.sh list (only CONCAT perf ran).
Thanks for the clear measurements and the test cases.
Review made with AI on behalf of the strix-llama.cpp maintainers.
Full verification (for reproduction and agents)
Environment
Ubuntu 26.04.1, Linux 7.0.0-43-generic. ROCm 10.1.0 (hipconfig 7.16.26385), AMD clang 24.0.0git d6f6cb69. Mesa RADV 26.0.8 (Radeon 8060S, STRIX_HALO). cmake 4.2.3, gcc 15.2.0. GPU: AMD Radeon 8060S (gfx1151), 128 GB unified memory.
Compared
- Master tip (base): f2230a8
- PR head: 39b617d
- Merge base: f5e7a5d (5 commits behind master)
- Head under test: local merge of the PR into master, b980a1810 (clean, not pushed)
- Op-level base: master plus only the test commit 74d8770, so that both sides run the same test list.
Build
HIP: cmake -G Ninja -S <tree> -B <tree>/build-hip -DCMAKE_BUILD_TYPE=Release -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R)
VK: cmake -G Ninja -S <tree> -B <tree>/build-vk -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=ON -DGGML_HIP=OFF
-Werror: HIP flags + -DCMAKE_HIP_FLAGS="-Werror -Wno-tautological-compare", target ggml-hip (head): clean
Spill gate: HIP flags + -DGGML_HIP_EXPORT_METRICS=On -DCMAKE_HIP_FLAGS="", scripts/hip/gcn-cdna-vgpr-check.py against gfx1151-vgpr-baseline.json
Models
- Target: agentionai/Qwen3.8-Flash-Next-Gyro-GGUF,
Qwen3.8-Flash-Next-Gyro-M-TQ2_0.gguf(sha256 7aa36d15131d...), loaded with-lm none -lzm on-direct. - Draft: unsloth/Qwen3.8-Flash-Next-GGUF,
MTP/mtp-Qwen3.8-Flash-Next-shared-Q4_K_M.gguf(sha256 f521868a9e143718...). - Path check only: ggml-org/Qwen3.5-0.8B-Base-GGUF
Qwen3.5-0.8B-Base-BF16.gguf(8db389e421ecf9fb...), LiquidAI LFM2-350MLFM2-350M-Q4_K_M.gguf(a4d000c7064bd3b2...). - Corpus sha256: code dec1127a..., numeric d9f08d94..., prose c90398f2..., structured c21947b3...
Commands
test-backend-ops test -b ROCm0 -o CONCAT (HIP_LAUNCH_BLOCKING unset and =1)
test-backend-ops test -b Vulkan0 -o CONCAT
GGML_CUDA_DISABLE_GRAPHS=1 test-backend-ops perf -b ROCm0 -o CONCAT (2 rounds, alternated)
test-backend-ops test -b ROCm0 -j 4 (full suite)
llama-debug -m <model> -f tests/corpus/correctness-<t>.txt -c 16384 -b 16384 -ub {16,512} -ngl 99 -fa on -ctk f16 -ctv f16 -lm none -lzm on-direct --save-logits --logits-output-dir <dir>
llama-perplexity -m <model> -f <4 corpora joined> -c 2048 -b 2048 -ub 16 --chunks 6 <same flags> --kl-divergence-base <side>.kld
llama-perplexity -m <model> -f <4 corpora joined> -c 16384 -b 512 -ub 512 --chunks 1 <same flags> --kl-divergence-base <side>.kld
llama-completion -m <model> -f correctness-prose.txt -n 256 --temp 0 --seed 1 -no-cnv --ignore-eos -c 16384 <same flags> (base twice, head once)
llama-server -m <model> -md <mtp draft> --spec-type draft-mtp --spec-draft-n-max 3 -ngl 99 -ngld 99 -fa on -ctk f16 -ctv f16 -lm none -lzm on-direct -c 49152 -b 2048 -ub 2048 -np 1
/completion, temperature 0, cache_prompt false, ignore_eos, n_predict 256; 1 warmup + 3 timed per prompt; arms base, head, head, base
rocprofv3 --kernel-trace --stats -- llama-server ... (one 2.3k-token request, n_predict 128, both sides)
llama-bench -m <model> -p 2,4,8,16 -n 128 -b 2048 -ub 2048 -r 5 <same flags> (2 rounds, alternated)
llama-bench -m <model> -p 2048 -d 0,12000,32000,64000 -b 2048 -ub 2048 -n 0 -r 5 <same flags> (2 rounds, alternated)
Results
Op time, ROCm0, us/run, 2 or 4 samples per side:
| shape (f32 unless noted), tokens | base | head |
|---|---|---|
| {3,10240}, 1 | 3.60-3.63 | 3.60-3.62 |
| {3,10240}, 2 | 24.36-24.37 | 7.95-7.97 |
| {3,10240}, 4 | 24.37 | 7.98 |
| {3,10240}, 8 | 24.37-24.38 | 8.01-8.09 |
| {3,10240}, 16 | 24.39-24.44 | 8.08-8.10 |
| {3,10240}, 31 | 24.99 | 9.14 |
| {3,10240}, 32 | 9.18-9.21 | 9.18-9.19 |
| {3,10240}, 33 | 11.59-11.74 | 11.74-11.84 |
| {3,10240,2}, 4 | 46.38-46.44 | 12.81-12.94 |
| {3,10240,4}, 2 | 95.02 | 23.99-24.85 |
| {3,8192}, 2 / 8 | 19.95 / 19.97 | 6.55 / 6.58 |
| f16 {3,1000,2}, 4 | 6.78-6.84 | 3.12-3.14 |
| bf16 / i8 / i64 {3,77}, 5 | 2.33-2.35 | 2.50-2.59 |
| {64,127,2}, 2 | 2.75 | 2.84 |
| {4096,32}, 2 | 4.39-4.40 | 28.16 |
| {3,1}, 8 | 2.07-2.09 | 2.38-2.39 |
| {3,10240}, 512 / 2048 / 4096 | 174 / 1286 / 2595 | 170 / 1289 / 2587 |
Kernel trace, one server request (2.3k prompt, 128 tokens, MTP draft): base runs concat_non_cont 2072 times, mean 23.65 us (49.0 ms in total); head runs the same 2072 concats as concat_transposed_src1_dim0, mean 7.46 us (15.5 ms in total). Prefill concats (512 tokens) and the 1-token concat_cont calls do not change. With llama-debug at -ub 16 the new path also runs for qwen35 (14.9 -> 5.6 us) and lfm2 (3.7 -> 2.0 us); their logits are byte-identical.
Server tok/s per request (B1 / H1 / H2 / B2):
2.3k : 29.27 29.29 29.30 | 29.62 29.60 29.57 | 29.49 29.50 29.50 | 29.27 29.23 29.28 draft 328 / accepted 144 on every arm
11.8k: 48.02 47.96 47.97 | 48.47 48.33 48.30 | 48.26 48.26 48.28 | 47.97 47.86 47.88 draft 196 / accepted 189
45k : 39.48 39.46 39.41 | 39.76 39.76 39.65 | 39.69 39.58 39.70 | 39.50 39.46 39.31 draft 239 / accepted 175
TTFT ms 2.3k 3087-3133 vs 3077-3120; 11.8k 14244-14285 vs 14194-14265; 45k 55542-55639 vs 55438-55634
llama-bench small batches (t/s, avg +- sd over 5, round 1 / round 2):
pp2 base 42.60+-2.8 / 42.59+-3.0 head 42.90+-3.1 / 42.98+-3.0
pp4 base 72.01+-0.99 / 72.02+-0.91 head 72.30+-1.35 / 72.62+-0.76
pp8 base 86.09+-1.04 / 85.89+-1.07 head 86.70+-0.70 / 86.81+-0.53
pp16 base 119.36+-0.91 / 120.20+-0.20 head 119.75+-0.99 / 120.09+-0.93
tg128 base 28.09+-0.03 / 28.51+-0.02 head 28.51+-0.02 / 28.51+-0.02 (base round 1 was the first run of the session)
PP2048 depth sweep (t/s, avg +- sd over 5, round 1 / round 2):
d0 base 1108.4+-32.8 / 1106.4+-34.6 head 1108.5+-35.4 / 1107.1+-35.5 +0.04 %
d12000 base 1008.2+-1.8 / 1006.5+-2.1 head 1008.0+-1.9 / 1007.1+-2.1 +0.02 %
d32000 base 994.6+-5.5 / 993.7+-5.2 head 993.5+-5.6 / 994.0+-6.1 -0.04 %
d64000 base 986.2+-9.3 / 985.7+-9.6 head 985.9+-9.4 / 987.0+-9.7 +0.05 %
Correctness
| check | method | result |
|---|---|---|
| changed op | test-backend-ops -o CONCAT, ROCm0, HLB unset and =1 | 251/251 both sides, both settings |
| Vulkan op | test-backend-ops -o CONCAT, Vulkan0 | 240/240 base, 266/266 head (new cases) |
| full backend suite | test-backend-ops -j 4, ROCm0 | 56968/56968 both sides |
| test-vk-exact | n/a | no Vulkan change |
| prompt tokens + f32 final logits | llama-debug, 4 corpora, -ub 16 and -ub 512 | byte-identical (16/16 pairs) |
| HLB=1 | llama-debug prose -ub 16 | byte-identical to HLB unset, both sides |
| KLD/PPL short | -c 2048 -ub 16, 6 chunks | .kld byte-identical; PPL 7.0900 both; same top p 100 % |
| KLD/PPL long | -c 16384 -ub 512, 1 chunk | .kld byte-identical; PPL 3.1005 both |
| greedy decode | 256 tokens, base A/A + head | identical (A/A stable) |
| speculative output | server, 3 prompts x 4 arms | identical text and acceptance |
| -Werror | ggml-hip target | clean |
| spill gate | gcn-cdna-vgpr-check.py | 0 VGPR spills, 11 baseline SGPR entries on both sides; concat kernel metadata identical. The script's separate exl3 coverage check fails on base and head alike (exl3 kernels absent in this build config), so it is not a PR result |
| performance | llama-bench, server, op perf | see above |
Findings in full
concat.cu: the tile kernel bounds-checks token and channel, uses every stride it gets, and copies src0 from theblockIdx.y == 0row, so anysrc1->ne[0] >= 1is safe. At 1 token,ggml_is_contiguous_to_3is true and the contiguous kernel still runs. Quantized types are rejected before this point.- The 32-token floor came with the tile kernel and has no recorded measurement. The changed line is fork-only code; no upstream-merge cost.
- Asks 1-3 above.
Claims
- 2-16 tokens 24.5 -> 7.9 us: reproduced (24.37 -> 7.95-8.10).
- 1 token and 32 tokens unchanged: reproduced.
- 193/193 CONCAT: reproduced as 251/251 with extra cases; full suite passes (56968 on current master).
- Greedy output identical across servers: reproduced.
- About 1-2 % end-to-end MTP tok/s: reproduced as +0.64 to +0.93 % on a different qwen4exp quant; the "+2.3 %" first-request effect was not seen.
- pp2-pp16 inside the spread: reproduced.
Not tested
NVIDIA CUDA and other AMD architectures (tile_y 8 there); the author's UD-IQ4_XS files (87 GiB); Vulkan model runs (no Vulkan code change); server pytest (not available); ops-perf.sh over the full perf-cases list (only -o CONCAT perf ran, the one changed op).
dzannotti
left a comment
There was a problem hiding this comment.
Approving after the review above: byte-identical on ROCm, small measured gain, remaining asks are optional cleanups. Merging.
Overview
Two commits: test-backend-ops cases, then a one-line dispatch change in
concat_cuda.The dim-0 concat with a transposed
src1went to the transposed tile kernel only from 32 tokens up. Below that it fell to the generic non-contiguous kernel, which launches one 256-thread block per output row and writes 4 to 7 numbers per block. For the qwen4exp conv-state concat (state [3 x 10240] ++ transpose(qkv_mixed [10240 x n]), 36 layers per graph) that is every MTP verify batch and every multi-row decode. The threshold moves from 32 to 2, not 1: a one-column transpose is contiguous and its kernel is faster than the tile kernel for it (3.74 vs 7.85 us).Measurements
Isolated,
test-backend-ops perf -b ROCm0 -o CONCAT, us per op at the shape the graph builds:llama-bench -p 2,4,8,16 -n 128 -b 2048 -ub 1024 -r 5 -ngl 99 -fa on -ctk f16 -ctv f16 -lm none -dev ROCm0, four runs alternated baseline, change, change, baseline, one session. The change acts only on 2 to 31 token batches, so the default-p 512 -n 128cannot show it; pp2 to pp16 are the axis it acts on, tg128 is the single-token path that must not move.Summary (t/s, mean ± sd over 5 repetitions; raw tables below):
The change is ahead at pp2 to pp16 in all four comparisons (0.7 to 2 %), but inside the five-run spread, so by the rules this table does not measure a speedup on its own; pp8 in run 4 has one outlier repetition. tg128 (one-token decode, contiguous kernel) is unchanged. The size of the effect is set by the isolated op numbers above and confirmed end to end below.
Raw llama-bench tables, four runs
Run 1, baseline:
build: c495f90 (12477)
Run 2, change:
build: c00bb06 (12479)
Run 3, change:
build: c00bb06 (12479)
Run 4, baseline:
build: c495f90 (12477)
End to end,
llama-serverwith the MTP draft,--spec-draft-n-max 3,-c 16384 -b 1024 -ub 1024, 8k prompts (prose, code, agentic) x 2 reps x 256 tokens, baseline and change alternated (B, C, C, B), one session, round split from a timing print both arms carried:Over the run: verify -1.5 ms per round, round wall -1.7 ms, +2.3 % tok/s. Most of that is the first request of each prompt, where the baseline's rounds run 2.6 ms slower than on its cached repeat and the change's do not; I have not found why. On cached repeats alone the difference is -0.36 ms verify (60.22 vs 59.86, ranges 59.91-60.47 vs 59.64-60.00), which is what the isolated numbers predict (16.6 us x 36 layers). Read it as a 1 % change with an unexplained first-request bonus.
Correctness:
test-backend-ops -b ROCm0 -o CONCAT193/193 including the six new shapes; fulltest-backend-ops -b ROCm0on the change: 40554/40554 passed, 2/2 backends OK (24 min). Greedy output streams identical between all four servers above. A concat copies values, sollama-perplexitywas not run.Not run: Vulkan (the change is in the CUDA/HIP backend only), the four
tests/corpustop-1 and logit comparisons,llama-benchy, other models that concat a transposedsrc1below 32 columns (they take the same faster path).Additional information
Found while profiling the MTP verify round for this model on gfx1151; the tile kernel comes from f5bf10f.
AI usage: the code, the tests, the measurements and this text were produced by an AI agent (Claude Fable 5.1) under my direction, on my box; I reviewed them and own the result.
🤖 Generated with Claude Code