Repository navigation
CUDA: dequantize q4_0/q8_0 K/V tiles in the FA MMA kernel for small multi-sequence batches (pin branch on b11642) - #303
danielhanchen wants to merge 3 commits into
Conversation
…atches For 3 or more queries per ubatch (multi-sequence decode) with a quantized KV cache, the MMA FlashAttention path first converted the entire K and V views to f16 every layer and every step. With a unified KV cache that view is the whole pool, so the conversion grows with the pool's high-water mark and costs more than the attention itself. For q4_0/q8_0 K/V (same type), head sizes 128/256/512 and up to 16 queries, on NVIDIA Ampere or newer, the MMA kernel now dequantizes the K/V tiles as it loads them, into the same f16 shared memory layout, and launch_fattn skips the conversion and its scratch buffer. The tile loader has a compile-time trip count and is unrolled by 4 so that several global loads per thread are in flight. The tile shapes are the ones the f16 path selects for the same batch. The vector/MMA/tile selection is unchanged, so single-query decode, prefill, other GPUs and the sparse kernels keep their current paths. The tile loader is adapted from ggml-org#30202.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1368b423e7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| for (int hs : { 64, 128, 256, 512, }) { | ||
| for (int nr2 : { 1, 2, 4, 6, 8, }) { | ||
| for (int kv : { 113, 512, 1024, }) { | ||
| for (int nb : { 1, 2, 3, 4, 5, 8, 9, 16, 17, }) { | ||
| for (ggml_type type_KV : { GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0, }) { | ||
| test_cases.emplace_back(new test_flash_attn_ext(hs, hs, 2, {nr2, 1}, kv, nb, true, false, 0, 0, GGML_PREC_F32, type_KV, type_KV)); |
There was a problem hiding this comment.
Reduce the exhaustive flash-attention test matrix
This Cartesian product adds 1,620 evaluation cases here, and the following loops bring the total increase to 1,782 cases. The unfiltered test-backend-ops invocation in ci/run.sh runs this evaluation matrix for applicable backends, so this change increases the suite from 3,804 to 5,586 cases (about 47%), including many costly kv=1024/hs=512 combinations. Keep a small boundary-focused set that covers each dispatch width, quant type, and relevant head size rather than multiplying every parameter value.
Useful? React with 👍 / 👎.
The quantized K/V path is only selected on Ampere or newer (ggml_cuda_flash_attn_ext_mma_q_supported), but its template instances were also compiled for older architectures. For Turing the tile widths 8 and 16 use nbatch_K2 = 96, and 256 % 96 != 0 fails the static_assert of the quantized K tile loader, which broke sm_75 builds and the default fat binary. Compile the quantized kernels as NO_DEVICE_CODE when AMPERE_MMA_AVAILABLE is not defined, keeping the host specializations so fat binaries link.
flash_attn_ext_f16_process_tile now deduces the K/V type from the type of its K pointer, so the kernel's process_tile call sites keep their upstream text and the Turing/Volta guard no longer wraps them. On architectures without quantized tile loading the kernel instantiates the f16 path for quantized K/V, which is never launched there, as before. Also move test_flash_attn_ext_multi_seq after test_flash_attn_ext_large_logits. No functional change.
|
Ran this on an RTX PRO 6000 Blackwell Server Edition (sm_120, CUDA 13.0, driver 580.82, Colab), one of the "not covered" archs. Base
ms per decode step,
The 3-stream regression from the body reproduces here (+2.6% vs +4.4% on the L4). Gains are smaller than on the L4, which is expected on a card this much faster at 512-token contexts. The gate this PR adds, as read from HIP is unchanged: A100 40GB (sm_80) and T4 (sm_75), same base, Colab, CUDA 13.0. I added 1,340 extra FLASH_ATTN_EXT cases to every tree (GQA 3/5/7/12/16, KV 1/31/255/257/4097/16384, D512, softcap, sinks, no mask, ALiBi, permuted K/V, all 10 K/V type pairs):
ms per decode step at 16 streams on the A100, q4_0 / q8_0 KV, % vs base: Qwen3-0.6B -15.1 / -9.3, Qwen3-8B -8.4 / -5.3, Llama-3.1-8B -8.9 / -5.0, Phi-4-mini -11.3 / -7.6, gemma-3-4b -9.8 / -5.7. The 3-stream regression doesn't show on A100 (Qwen3-0.6B npl 3: -3.3% q8_0, -7.0% q4_0). On Turing the gate is off, so T4 is within 0.4% of base everywhere. One mismatch with the body: it says 1-2 token steps stay on the vector kernel, but for quantized K/V that only holds on Ada and newer. On Ampere and Turing, base picks the vector kernel at 1 token only (fattn.cu#L648-L663), and the new gate only checks the upper bound (fattn.cu#L342), so a 2-stream step on an A100 takes the new kernel. It's mixed there: Qwen3-0.6B q4_0 -2.3%, Qwen3.5-0.8B q8_0 +1.9%. A Not run: H100 (Colab refused it), llama-server. |
|
@codex review |
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
LeoBorcherding
left a comment
There was a problem hiding this comment.
Codex: the only finding is a P2 at tests/test-backend-ops.cpp:11680, that the FA matrix grows by 1,782 cases. The count is right; on sm_120 FLASH_ATTN_EXT went from 3993 to 5586. ci/run.sh:643 runs test-backend-ops -b CPU unfiltered under a 3600 s timeout, so the cost lands on CPU CI too, not only on GPU nodes. I'd keep a case for each dispatch width, quant type and head dim, and drop the full kv=1024 x hs=512 cross product, which is most of the added time. The rerun asked above is against 34a66f42b after a setup failure.
Kernel side: the in-kernel path is gated to Ampere+ with K and V of the same type, head dims 128/256/512 and 3 to 16 queries; every other shape takes the old selection. On sm_120 it passes all 5586 cases, the greedy text is byte-identical to base, and Qwen3-0.6B decode is 3.4 / 5.4 / 6.5% faster at 4 / 8 / 16 streams. The 3-stream regression from the body reproduces (+2.6%), and since it's consistent across L4 and sm_120, I'd gate 3 queries back to the conversion path rather than leave it. Numbers and setup are in the comment above.
Not run: A100/H100, compute-sanitizer, llama-server.
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…timings (source line moved off the axes)

Summary
With a quantized KV cache (q4_0 / q8_0) and 3 to 16 tokens per ubatch (multi-sequence decode, e.g.
-np 4 --kv-unified), the CUDA flash-attention dispatcher leaves the vector kernel for the MMA f16 kernel, and the MMA path first converts the whole K and V views to f16 for every layer on every step. On a 4-slot server that was 56dequantize_block_q8_0_f16launches per decode step and the largest single kernel of the session (23.7% of GPU time on an L4).This change lets the MMA kernel dequantize q4_0 / q8_0 K/V tiles as it loads them, for 3 to 16 tokens, so the whole-view conversion is gone. It builds on the in-kernel dequant proof of concept in ggml-org#30202 (am17an) and addresses the points raised in its review: the head dim 512 path without GQA, the GQA check, sparse kernels for quantized KV, generated instead of hand-written instance files, and no test-only env knobs. 1-2 token steps stay on the vector kernel, and the tile loader uses a fixed trip count with unroll 4 (a per-element loader lost at 4 streams).
Pin branch: upstream b11642 (2639a3b) + this one commit. Independent of #269; it also merges cleanly onto #269's head and onto ggml-org master 36140db.
Scope
NVIDIA Ampere or newer, K and V of the same type (q4_0 or q8_0), head dims 128 / 256 / 512, 3 to 16 tokens. Kernel selection is otherwise unchanged: single-stream decode, prefill, f16 KV, Turing, Volta and AMD take exactly the same path as before.
Correctness
test-backend-ops -o FLASH_ATTN_EXT: 5586/5586 pass against CPU on B200 and L4, including 1,782 new cases (head dims 64-512, GQA 1-8, 1 to 17 tokens, f16 / q8_0 / q4_0, a mask with 4 sequences in one unified cache, sinks, softcap, ALiBi, no mask).Performance (Colab L4, one session, 3 interleaved rounds, median)
One reproducible regression: batched-bench at 3 streams with 512-token prompts is +0.23 ms (+4.4%); 3 streams with 128-token prompts and every 4+ stream cell improve. No threshold was added for it.
Not covered
T4 (gated off; compiled for sm_75 only), A100 / H100 / sm_120 at runtime (compile only), compute-sanitizer, and the interaction with #269's batch-invariant mode (checked by reading the code).