Repository navigation
Conversation
|
is this similar to #30202? |
Hi Mine goes through the TILE kernel and targets the ne[1] <= 2 branch that upstream hands The only place we actually overlap is NVIDIA Ampere, D in {128,256,512}, q8_0/q8_0, ne[1] <= 2. #30202 FWIW MMA isn't a good fit for our target anyway: on gfx1151, sending bs=1 decode to MMA measured -3% and I We are happy to (a) keep this as the AMD/tile-side change, (b) narrow it so it never fires where your gate wins. Thanks |
|
FYI — I adapted this PR onto a 0.7.0 fork and tested on an RDNA3.5 iGPU Setup: llama-batched-bench, -c 65536 -b 4096 -ub 2048 -npp 16384 -ntg 128 PP/TG t/s, 16384 ctx:
63241 ctx, q8_0/q8_0 (PP/TG):
The gemma4 prefill drop only shows up when native V is used for prefill Note: the numbers above were measured by a DeepSeek agent as a reference |
JohannesGaessler
left a comment
There was a problem hiding this comment.
A PR like this should be made in such a way that it establishes abstractions for any alternative KV type, not just a single one.
Fair point — the current patch wires only q8_0. Restructuring it so any KV type
That makes q4_0/q5_0 KV caches — the far more common legacy configuration — Meanwhile, CI on this branch only ran the labeler, so the CUDA build/tests |
3c25375 to
40f0d07
Compare
…throughput (local fork) Local to ftulabs/momonga-llama.cpp fork. Do not upstream directly without multi-arch review. Measured on /mnt/ssd/models/K2-Horizon-4B-Q4_K_M.gguf (-ngl 99 -fa 1 -b 2048): - RMS_NORM+MUL+ADD fusion & direct src1 conversion cache (norm.cuh/.cu, convert.cu, unary.cuh/.cu, ggml-cuda.cu): eliminates all 70 per-layer residual k_bin_bcast<&op_add> launches and cuts cudaGraphLaunch overhead (579.4 -> 334.5 us/call). - Volta cublasGemmEx deep-K split-K fix (ggml-cuda.cu): dispatches CUBLAS_GEMM_ALGO4/5/6_TENSOR_OP for aligned K >= 3*M, K >= 6144, N >= 64 FP16 GEMM (avoiding slow split-K 128x3x2 on ffn_down). Pushes pp2048 (-ub 2048) to 771.66 +/- 4.19 tok/s (+44.8% vs 533.06 baseline, +5.8% vs 729.41 round-2) and pp512 (-ub 1024) to 729.59 +/- 8.24 tok/s. - PR ggml-org#30792 + coalesced q8_0 KV tile loader (fattn-tile.cuh, fattn.cu): warp-coalesced uint32_t + __shfl_sync + magic-bias half conversion (REG: 73, STACK: 0 at DKQ=128) lifts q8_0/q8_0 tg128 from 19.52 -> 22.88 (d4096), 15.33 -> 19.09 (d8192), and 10.74 -> 14.06 tok/s (d16384, +30.9%); f16/f16 remains fastest across all depths (tg128 = 30.61 +/- 0.19 at d0; 17.55 +/- 0.04 at d16384). - PR ggml-org#29796 stream sync skip (common.cuh, ggml-cuda.cu) + runner -t 2 -tb 2 tuning (runbest/run-k2-horizon-4b.sh): server (-bs, NPRED=400) hits 29.60 tok/s decode median and 672.2 tok/s prefill (2,213 tok / 3,308 ms TTFT); ServerBench hits 304.72 gross tok/s (42.96 s). - Perplexity & regression parity: PPL = 2.5997 +/- 0.04012 (f16 KV, bit-identical) and 2.5998 +/- 0.04013 (q8_0 KV); Cyber-Tiel-Coder-35B-A3B-tg.gguf zero regression (23.48 tok/s std / 28.06 tok/s runbest).
|
I've tried this on 2x RX6800 (gfx1030) at 10k-128k context depth. My setup is quite unusual in a lot of ways, and my benchmark is questionable, but I just wanted to confirm that it works even for me, and roughly how much happier I am now. Qwen 3.8 27BComparing q8_0 upstream vs. patch:
Comparing q8_0 upstream/patch vs. unquantized cache:
Qwen 3.8 Flash-NextWith the patch, prefill is from 9% faster to 11% slower, and decode is 2-41% faster. |
|
@Woody-Hu This PR as currently submitted does not compile for pre-Volta NVIDIA (tested with
Fix (~30 lines): gate the raw readers on Measured on Tesla P40 (sm_61) with the fix applied: neutral as expected (fp32 tile keeps the f16-scratch path), q8_0 KV FA eval cases all pass. The PR's real benefit is for fp16-capable GPUs. One heads-up for the interaction with #32741: both PRs rewrite |
|
Thanks @sorasoras for the detailed report and for the verified patch — your analysis was spot on, and we implemented the gating on top of the current head (fa60cc0) rather than cherry-picking the gist, so it stays a minimal diff against what is in the PR. What changed:
On fp16-capable arches ( One caveat we want to be honest about: our development machines are macOS without a CUDA toolchain, so we could not compile And thanks @dark-penguin for the 2×RX6800 measurements — that data was very useful for confirming the GQA-reuse win holds on RDNA2. |
|
Thanks for the report and the reference patch, @sorasoras. The gating fix is now pushed as sm_61 cross-compile (NVIDIA path)
AMD/ROCm sanity (no regression on the native path)
Still outstanding: I have no P40-class hardware, so this is compile-level verification only — a runtime smoke on real sm_61 (your FA eval) would still be the gold standard. Happy to iterate if the cross-compile here diverges from anything you see. Build logs kept at the author's remote ( |
|
Runtime verification on real sm_61 here — this is the piece your cross-compile couldn't cover. Built 1. Build — clean. Same result as your Linux cross-compile: the pre-fix 2. Full Every case passes, including the KV types this PR touches:
The q8_0 cases run up to 3. Real-model bench (Qwen3.5-9B Q6_K, q8_0 KV, fa on,
Everything within ~1%, which is the expected result: on pre-Volta So: compile-verified on two toolchains, runtime-verified on real sm_61 hardware, functionally inert (as designed) on Pascal. From my side this is good to merge. If you want any specific shape added to the eval before it lands, tell me and I'll run it on the P40. |
For bs=1 decode with a quantized KV cache, ggml_cuda_get_best_fattn_kernel() falls back to BEST_FATTN_KERNEL_VEC (Q->ne[1] <= 2), which does not reuse K/V across the GQA heads that share a KV head, so it is much slower at long context. Route q8_0 K/V straight into the GQA-optimized tile kernel and dequantize the q8_0 blocks to half2 in registers during the global->shared copy. The shared layout is identical to the f16 path, so the downstream KQ/VKQ math is unchanged. The same applies to asymmetric K=f16 with V=q8_0. On the native path get_alloc_size() no longer requests the f16 K/V scratch, which drops a redundant per-layer 2x KV write and read. The VEC fallback is narrowed with gqa_opt_applies, so the native path is only taken in GQA-reuse regimes. Measured on MiniCPM-V 4.7 35B-A3B (qwen35moe, head_dim=256, n_head_kv=2, gqa_ratio=8, Q4_K_M), bs=1, single sequence, -fa on, base 31288c9, llama-batched-bench tg t/s: ctx 16384: q8_0 50.05 -> 59.56, K=f16/V=q8_0 48.30 -> 59.11 ctx 63241: q8_0 31.19 -> 50.51, K=f16/V=q8_0 27.88 -> 49.38 f16 control: 16K 56.72 -> 56.66, 64K 43.46 -> 43.39, i.e. unchanged Quality: test-backend-ops -o FLASH_ATTN_EXT 3998/3998 (incl. the new (f16, q8_0) case); perplexity on a 512-token corpus unchanged (f16 12.1948 vs q8_0 12.1436). Assisted-by: DeepSeek-V4.1-Flash
The asymmetric (K=f16, V=q8_0) tile path is not exercised by the existing FLASH_ATTN_EXT cases, which only cover matching K/V types. Add decode and prompt, KV padding, permuted KV and long-context cases for that combination. test-backend-ops -o FLASH_ATTN_EXT: 3993 -> 3998 cases. Assisted-by: DeepSeek-V4.1-Flash
…ta build) -DCMAKE_CUDA_ARCHITECTURES=61 fails to build: the q8_0 reader dequantizes to half2, and the fp32 (pre-Volta) tile kernel instantiates the q8_0 branch with float accumulation, which the fp16-only reader cannot serve. In addition, get_alloc_size() and the launch path set need_f16_K/V from the q8_0 type check alone, so a q8_0 KV cache would skip the f16 scratch and hand raw q8_0 pointers to the fp32 kernel. Give the block_q8_0 -> float instantiation a NO_DEVICE_CODE stub so the fp32 kernels still compile, and gate the raw readers on fast_fp16_available(cc) at the three dispatch sites (ggml_cuda_flash_attn_ext_tile in fattn-tile.cu, ggml_cuda_get_best_fattn_kernel and ggml_cuda_flash_attn_ext_get_alloc_size), plus the need_f16_K/V decision in launch_fattn_tile_switch_ncols1. Pre-Volta keeps the previous behavior (f16 scratch conversion plus the VEC kernel, which reads q8_0 natively via MMVQ); fp16-capable arches are unchanged. Reported-by: sorasoras Assisted-by: DeepSeek-V4.1-Flash
fa60cc0 to
fbb6a26
Compare
|
Rebased onto
Verification:
|
|
@sorasoras thanks for taking this to a real P40 with nvcc/MSVC — the Windows build and the full One heads-up so your measurements stay meaningful against the current head: I rebased onto Post-rebase checks on my side, for completeness: sm_61 cross-compile clean ( On #32741: agreed, we both rewrite |
Overview
At bs=1 decode with a quantized KV cache and
-fa on,ggml_cuda_get_best_fattn_kernel()falls back toBEST_FATTN_KERNEL_VEC(Q->ne[1] <= 2). The VEC kernel does not reuse K/V across the GQA heads that share a KV head, and quantized K/V only reaches the TILE kernel after being expanded into an f16 scratch buffer, so a quantized KV cache ends up slower than an f16 one at long context.This PR routes q8_0 K/V (and asymmetric f16 K / q8_0 V) straight into the GQA-optimized tile kernel, dequantizing the q8_0 blocks to half2 in registers during the global->shared copy. The shared-memory layout is identical to the f16 path, so the KQ/VKQ math is unchanged. On the native path
get_alloc_size()no longer requests the f16 K/V scratch, which drops a redundant per-layer 2x KV write and read. The VEC fallback is narrowed withgqa_opt_applies, so non-GQA-reuse regimes keep the current behaviour.Closes #30790.
Benchmarks
ROCm 7.2.4, gfx1151, base 31288c9, MiniCPM-V 4.7 35B-A3B (qwen35moe, Q4_K_M), single sequence,
llama-batched-bench -c 262144 -b 2048 -ub 512 -npp <N> -ntg 64 -npl 1 -fa on, tg t/s:Short context (
llama-bench -p 512 -n 128 -r 3) is on par for all three.Tests
test-backend-ops -o FLASH_ATTN_EXT: 4375/4375 cases on07ba74bab, including the new (f16, q8_0) casestest-backend-ops: 2/2 backends passed OKGGML_CUDA_FA_QUANTS:q8_0-q8_0is in the default list,f16-q8_0has to be added explicitly (-DGGML_CUDA_FA_QUANTS="q4_0-q4_0;q8_0-q8_0;f16-f16;bf16-bf16;f8_e4m3-f8_e4m3;f16-q8_0") to get the asymmetric path; without it that case keeps using the f16 scratchAdditional information
The SYCL backend already merged the same idea for Intel BMG (#26689). Earlier related discussion: #20866 (closed).
Requirements