Skip to content

CUDA: native q8_0 KV (and f16 K / q8_0 V) in the flash-attn tile kernel - #30792

Open
Woody-Hu wants to merge 3 commits into
ggml-org:masterfrom
Woody-Hu:rdna-fattn-quant-kv
Open

Woody-Hu wants to merge 3 commits into
ggml-org:masterfrom
Woody-Hu:rdna-fattn-quant-kv

Conversation

@Woody-Hu

@Woody-Hu Woody-Hu commented Oct 19, 2026 •

Copy link
Copy Markdown

Overview

At bs=1 decode with a quantized KV cache and -fa on, ggml_cuda_get_best_fattn_kernel() falls back to BEST_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 with gqa_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:

npp type_k type_v before after delta
16384 f16 f16 56.72 56.66 -0.1% (control)
16384 q8_0 q8_0 50.05 59.56 +19.0%
16384 f16 q8_0 48.30 59.11 +22.4%
63241 f16 f16 43.46 43.39 -0.2% (control)
63241 q8_0 q8_0 31.19 50.51 +61.9%
63241 f16 q8_0 27.88 49.38 +77.1%

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 on 07ba74bab, including the new (f16, q8_0) cases
  • full test-backend-ops: 2/2 backends passed OK
  • perplexity on a 512-token corpus unchanged (f16 12.1948 vs q8_0 12.1436)
  • the tile q8_0 instances are built per GGML_CUDA_FA_QUANTS: q8_0-q8_0 is in the default list, f16-q8_0 has 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 scratch

Additional information

The SYCL backend already merged the same idea for Intel BMG (#26689). Earlier related discussion: #20866 (closed).

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES - the code, tests and text were written with AI assistance (DeepSeek-V4.1-Flash). Every change was reviewed line by line and built and tested on ROCm/gfx1151 by me; I am responsible for the submitted content.

@Woody-Hu
Woody-Hu requested review from a team and ggerganov as code owners October 19, 2026 06:02
@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 Oct 19, 2026
@am17an

am17an commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

is this similar to #30202?

@thelittlefireman thelittlefireman self-assigned this Oct 19, 2026
@Woody-Hu

Copy link
Copy Markdown
Author

is this similar to #30202?

Hi
I'd say same idea, different kernel — not duplicates. #30202 dequantizes on the fly inside the MMA kernel and is
gated to NVIDIA (GGML_CUDA_CC_IS_NVIDIA + ampere_mma_available), requires K and V to be the same type, and only
covers D = 128/256/512.

Mine goes through the TILE kernel and targets the ne[1] <= 2 branch that upstream hands
to VEC; it has no NVIDIA gate (I measured it on ROCm/gfx1151), also handles the asymmetric f16-K / q8_0-V case,
and is narrowed by gqa_opt_applies.

The only place we actually overlap is NVIDIA Ampere, D in {128,256,512}, q8_0/q8_0, ne[1] <= 2. #30202
ggml_cuda_fattn_mma_use_q() check sits earlier in ggml_cuda_get_best_fattn_kernel(), so you'd win there — on
NVIDIA the two aren't additive.

FWIW MMA isn't a good fit for our target anyway: on gfx1151, sending bs=1 decode to MMA measured -3% and I
reverted it. SYCL already merged the same idea for BMG (#26689).

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

@LZQ-2020

Copy link
Copy Markdown

FYI — I adapted this PR onto a 0.7.0 fork and tested on an RDNA3.5 iGPU
(gfx1151, ROCm 7.2 / LLVM 21), in case the numbers are useful for the PR.

Setup: llama-batched-bench, -c 65536 -b 4096 -ub 2048 -npp 16384 -ntg 128
-npl 1 -fa on, serial runs, first (warm-up) run discarded.
Models: gemma-4-26B-A4B Q4_K_XL (hybrid, 256-head SWA + 512-head global)
and occamy Q5_K_M (Qwen3.6-35B-A3B architecture, 128-head).

PP/TG t/s, 16384 ctx:

  • gemma4 q8_0/q8_0:
    0.7.0, no patch 1494.8 / 42.3
    native V for all queries 335.7 / 47.3 (prefill -77.5%)
    native V for decode only 1497.3 / 47.8
  • Qwen q8_0/q8_0: 46.1 -> 52.6 (+14.1%)
  • Qwen f16/q8_0: 50.1 -> 52.3 (+4.4%)

63241 ctx, q8_0/q8_0 (PP/TG):

  • Qwen: 875.2 / 31.5 -> 873.5 / 46.6 (TG +48.2%)
  • gemma4: 849.5 / 28.2 -> 845.5 / 33.3 (TG +18.2%)

The gemma4 prefill drop only shows up when native V is used for prefill
(Q->ne[1] > 2); it does not appear on the 128-head model.

Note: the numbers above were measured by a DeepSeek agent as a reference
only — please verify independently before relying on them.

@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.

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.

@Woody-Hu

Copy link
Copy Markdown
Author

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
is a one-line addition, following the pattern the VEC kernel already uses
(get_vec_dot_KQ / get_dequantize_V in fattn-common.cuh):

  1. Turn flash_attn_tile_load_tile_q8_0 into a get_load_tile<type_K>() /
    get_load_tile_V<type_V>() constexpr selector with the same shape as
    get_dequantize_V<type_V, T, ne>(). q8_0 is the first specialization;
    q4_0/q5_0/q5_1/q4_1/bf16 are the same dequant-in-registers form.
  2. Replace ggml_cuda_fattn_kv_native_q8_0() / _v_q8_0() with a single
    fattn_tile_supports(type_K, type_V, ne0_K, ne0_V) derived from
    ggml_blck_size() and the tile row length (FATTN_KQ_STRIDE % ggml_blck_size(type) == 0), so no q8_0-specific constant remains.
  3. Feed that one query to all three places that currently hardcode the pair:
    ggml_cuda_get_best_fattn_kernel(), ggml_cuda_flash_attn_ext_get_alloc_size()
    and the dispatch in ggml_cuda_flash_attn_ext_tile_case().
  4. f16/f16 stays the fallback and the scratch-copy path is untouched, so types
    without a native loader behave exactly as today.

That makes q4_0/q5_0 KV caches — the far more common legacy configuration —
fall out of the same path instead of needing a new patch per type. I'll push the
refactor to this PR and keep the q8_0 numbers as the first data point. If you'd
rather see the abstraction land as a standalone PR with the q8_0 enablement on
top, I can split it.

Meanwhile, CI on this branch only ran the labeler, so the CUDA build/tests
haven't been exercised yet.

sourenaraya added a commit to sourenaraya/llama.cpp that referenced this pull request Oct 27, 2026
sourenaraya added a commit to sourenaraya/llama.cpp that referenced this pull request Oct 28, 2026
sourenaraya added a commit to sourenaraya/llama.cpp that referenced this pull request Nov 2, 2026
@Woody-Hu
Woody-Hu force-pushed the rdna-fattn-quant-kv branch from 3c25375 to 40f0d07 Compare November 16, 2026 07:42
SpikeVN added a commit to ftulabs/momonga-llama.cpp that referenced this pull request Nov 18, 2026
…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).
@dark-penguin

Copy link
Copy Markdown

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 27B

Comparing q8_0 upstream vs. patch:

  • Prefill is slower by 1-6%
  • Decode is faster by 5-45%
  • As context depth increases, decode drops by: 46% (upstream) -> only 22% (patch)

Comparing q8_0 upstream/patch vs. unquantized cache:

  • Normally, quantizing the cache to q8_0 drops prefill by 2-7%; with patch, by 3-13% (a little more)
  • Normally, quantizing the cache to q8_0 drops decode by 8-35%; with patch, only by 3-6% (a lot less)

Qwen 3.8 Flash-Next

With the patch, prefill is from 9% faster to 11% slower, and decode is 2-41% faster.

@sorasoras

Copy link
Copy Markdown

@Woody-Hu This PR as currently submitted does not compile for pre-Volta NVIDIA (tested with -DCMAKE_CUDA_ARCHITECTURES=61):

  1. static_assert(std::is_same<T_vec_dot, half2>::value, ...) in the q8_0 flash_attn_tile_iter_KQ / flash_attn_tile_iter branches is unguarded. On sm_61 the tile kernel computes in fp32 (T_vec_dot = float), and since native_q8_0 is a runtime decision, the GGML_TYPE_Q8_0 instantiation is compiled regardless and the assert fires.
  2. ggml_cuda_flash_attn_ext_get_alloc_size / the launch path set need_f16_K = !ggml_cuda_fattn_kv_native_q8_0(K, V) unconditionally, so on pre-Volta a q8_0 KV cache would skip the f16 scratch and hand raw q8_0 pointers to an fp32 kernel.

Fix (~30 lines): gate the raw readers on fast_fp16_available(cc) at the three dispatch sites (ggml_cuda_flash_attn_ext_tile_case, get_alloc_size, and the best-kernel VEC/TILE decision for quantized decode) and wrap the two static_asserts in #ifdef FAST_FP16_AVAILABLE. On pre-Volta this keeps the current behavior (f16 scratch conversion + the VEC kernel, which reads q8_0 natively via MMVQ); on fp16-capable arches nothing changes. Patch on top of 0e5f569, compile-verified for sm_61 (the previously-failing fattn-tile-instance-* objects build): https://gist.github.com/sorasoras/ebcabe437071f3752e41dcf7b7c61497

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 fattn-tile.cuh and they conflict. I resolved the 3-way merge (the q8_0 loader branches need && !use_sparse, the template signatures union, and the instantiation sites need the extra template args): https://gist.github.com/sorasoras/5744379227b229b71a1a68deaf864d21 — both PRs build and pass the full FLASH_ATTN_EXT eval with the merge applied, so whichever lands second has a working reference.

@Woody-Hu

Copy link
Copy Markdown
Author

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:

  • ggml/src/ggml-cuda/fattn-tile.cuh: the two static_assert(std::is_same<T_vec_dot, half2>::value, ...) in flash_attn_tile_iter_KQ and flash_attn_tile_iter are now wrapped in #ifdef FAST_FP16_AVAILABLE, so the q8_0 instantiations compile on pre-Volta where T_vec_dot is float.
  • Three dispatch sites are now gated on fast_fp16_available(cc):
    1. ggml_cuda_flash_attn_ext_tile_case (fattn-tile.cuh) — native_q8_0 / native_v_q8_0 are forced off on pre-Volta, so those builds select the F16/F16 instantiation backed by the f16 scratch.
    2. ggml_cuda_get_best_fattn_kernel (fattn.cu) — quantized decode keeps the upstream BEST_FATTN_KERNEL_VEC decision (Q->ne[1] <= 2), i.e. the MMVQ path that reads q8_0 natively.
    3. ggml_cuda_flash_attn_ext_get_alloc_size (fattn.cu) — need_f16_K/V are computed as !(fast_fp16 && native_...), so the f16 scratch is allocated exactly when the fp32 kernel will be used.

On fp16-capable arches (fast_fp16_available(cc) true, which is also the exact condition under which FAST_FP16_AVAILABLE is defined per __CUDA_ARCH__) the added term is a constant true, so the selected kernel, the f16-scratch decision and the numerical results are bit-identical to before.

One caveat we want to be honest about: our development machines are macOS without a CUDA toolchain, so we could not compile -DCMAKE_CUDA_ARCHITECTURES=61 locally — this change has only been statically reviewed here. @sorasoras, could you re-run your sm_61 (P40) build/test on fa60cc05a to confirm? We also avoided touching anything that #32741 rewrites, so the conflict should stay confined to the two static_assert blocks and the three gating sites you listed.

And thanks @dark-penguin for the 2×RX6800 measurements — that data was very useful for confirming the GQA-reuse win holds on RDNA2.

@Woody-Hu

Copy link
Copy Markdown
Author

Thanks for the report and the reference patch, @sorasoras. The gating fix is now pushed as fa60cc05a (same ~30-line shape as your suggestion) and I have a compile-level verification from the Linux side, so I can take that uncertainty off your plate:

sm_61 cross-compile (NVIDIA path)

  • Host: x86_64 Linux (Ryzen AI MAX 395 box), container nvidia/cuda:12.6.3-devel-ubuntu22.04 (nvcc 12.6.85, cmake 3.22, ninja 1.10)
  • Configure: cmake -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=61 -DCMAKE_BUILD_TYPE=Release
  • Result: the entire ggml-cuda target compiles clean — all 149 .cu objects, including every fattn-tile.cu / fattn-mma / fattn-vec template instance, and libggml-cuda.so links. Zero static_assert errors (before the fix, the q8_0 tile instantiation dies at static_assert(std::is_same<T_vec_dot, half2>)).
  • The only failing target in a full cmake --build is app/llama.cpp.o missing build-info.h — an artifact of my source tree (git archive without .git), unrelated to this change.

AMD/ROCm sanity (no regression on the native path)

  • Same source tree with -DGGML_HIP=ON on ROCm 7.2 (gfx1151): ggml-hip target builds 162/162 and libggml-hip.so links, exit 0.

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 (llama-cpp-sm61-1125/{configure.log,build.log,build2.log}) if anyone wants to audit the flags.

@sorasoras

Copy link
Copy Markdown

Runtime verification on real sm_61 here — this is the piece your cross-compile couldn't cover. Built fa60cc05a natively on the P40 box (Windows 11, CUDA 12.6.85, MSVC 19.44, -DGGML_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES=61, Release, no cross-compile), so this is nvcc targeting the actual device it runs on.

1. Build — clean. Same result as your Linux cross-compile: the pre-fix static_assert at the q8_0 tile instantiation is gone, all fattn-tile-instance-* objects compile and link into ggml-cuda.dll. Confirms the fix works for both nvcc-on-Linux and nvcc-on-Windows/MSVC.

2. Full FLASH_ATTN_EXT eval on the P40 (test-backend-ops -o FLASH_ATTN_EXT -b CUDA0):

5776 test cases, 2/2 backends passed, OK

Every case passes, including the KV types this PR touches:

KV types cases failures
q8_0 / q8_0 (new native path) 374 0
f16 / q8_0 (new native path) 29 0

The q8_0 cases run up to kv=32768, with nb from 1 to 64, hsk/hsv 64/128/192/256 and gqa 4–8, mask/sinks/logit_softcap variants, and kv_view/v_is_view_of_k permutations — i.e. the n_kv_max=0 dense cases that actually reach the tile kernel dispatch. Your 40f0d0735 test additions are what make this a meaningful pass rather than a vacuous one; thanks for adding them.

3. Real-model bench (Qwen3.5-9B Q6_K, q8_0 KV, fa on, -r 3):

test this PR master-equivalent baseline
pp512 853.45 ± 1.94 856.9
pp4096 821.29 ± 0.19 823.3
pp512 @ 8k 680.75 ± 0.36 687.5
pp4096 @ 8k 659.53 ± 0.33 665.6
tg256 @ 8k 27.41 ± 0.02 27.97

Everything within ~1%, which is the expected result: on pre-Volta fast_fp16_available(cc) is false, so native_q8_0/native_v_q8_0 are false and the code takes exactly the pre-PR path (f16 scratch conversion + the VEC kernel, which reads q8_0 natively via MMVQ). The 1% spread is cross-build/thermal, not this change — P40 is passively cooled and drifts on sustained runs. No regression, and the new fast path lights up only on fp16-capable hardware.

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
@Woody-Hu
Woody-Hu force-pushed the rdna-fattn-quant-kv branch from fa60cc0 to fbb6a26 Compare November 26, 2026 07:52
@Woody-Hu

Copy link
Copy Markdown
Author

Rebased onto 07ba74bab to pick up #32901 ("refactor & deduplicate FA data loading"). That refactor moved the tile KV loads behind flash_attn_load_tile<...>, so the q8_0 reader is now a flash_attn_load_tile_impl specialization in fattn-common.cuh instead of dedicated load functions. No behavior change vs. the previous revision, but the wiring is different on purpose:

  • ggml_cuda_fattn_kv_dtype<GGML_TYPE_Q8_0>() returns block_q8_0, so T_K/T_V are block pointers and stride_K2/stride_V2 are block counts (s11 = nb11/sizeof(block_q8_0)). The shared-memory tile is still half2 with the same layout/padding as the f16 path, so nothing downstream changes.
  • The dequantization loop is the one from the previous revision (one q8_0 block per thread), guarded by if constexpr (J % QK8_0 == 0) so head sizes that cannot hold a q8_0 tensor don't instantiate it (the leftover tile, nbatch_K == 0, is skipped too).
  • The kernel instances now follow the existing GGML_CUDA_FA_<type_K>_<type_V> scheme: q8_0-q8_0 tile instances are built when that combination is enabled (it is in the default GGML_CUDA_FA_QUANTS list). The asymmetric f16-q8_0 tile instances need f16-q8_0 in that list; without it, that case falls back to the f16 scratch path exactly like before this PR.
  • Pre-Volta (sorasoras' report): the block_q8_0 -> float instantiation is a NO_DEVICE_CODE stub, and the raw readers are gated on fast_fp16_available(cc), including the need_f16_K/V decision in the launch path so it stays consistent with ggml_cuda_flash_attn_ext_get_alloc_size().

Verification:

  • sm_61 cross compile (nvidia/cuda:12.6.3, -DCMAKE_CUDA_ARCHITECTURES=61 -DGGML_CUDA=ON): ggml-cuda builds clean, including the new fattn-tile-instance-q8_0-q8_0-* objects.
  • gfx1151 (ROCm 7.2.4, HIP build with f16-q8_0 added to GGML_CUDA_FA_QUANTS): test-backend-ops -o FLASH_ATTN_EXT -> 4375/4375 passed.

@Woody-Hu

Copy link
Copy Markdown
Author

@sorasoras thanks for taking this to a real P40 with nvcc/MSVC — the Windows build and the full FLASH_ATTN_EXT sweep are exactly the part my Linux cross-compile couldn't cover, and the q8_0 374/374 + f16/q8_0 29/29 breakdown is the number I wanted to see.

One heads-up so your measurements stay meaningful against the current head: I rebased onto 07ba74bab to pick up #32901 ("refactor & deduplicate FA data loading"), so the branch tip is now fbb6a26b0. That refactor moved the tile KV loads behind flash_attn_load_tile<...>, which turns the q8_0 reader into a flash_attn_load_tile_impl specialization in fattn-common.cuh instead of dedicated load functions. The dispatch still goes through fast_fp16_available(cc) exactly as before, so on Pascal native_q8_0 stays false and the kernel takes the same f16-scratch + VEC path you benchmarked — your build/runtime results and the <1% deltas still describe the current head, no re-run needed on my account.

Post-rebase checks on my side, for completeness: sm_61 cross-compile clean (ggml-cuda 170/170, including the new q8_0-q8_0 tile instance) and gfx1151 HIP test-backend-ops -o FLASH_ATTN_EXT 4375/4375.

On #32741: agreed, we both rewrite fattn-tile.cuh and they collide. It's still open, so whoever lands second picks up the resolution — thanks for leaving the gist with a working 3-way merge. If #32741 goes in first I'll re-resolve on top of it, re-run the AMD regression, and ping you for a look before asking for a re-review.

This branch has not been deployed

No deployments
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 testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: native q8_0 KV in the CUDA flash-attention TILE kernel (bs=1 decode)

7 participants