Repository navigation
CUDA: FA MMA in-kernel K/V dequant for all quantized K/V types, head dims 64/96/192 and MLA, and Turing - #315
danielhanchen wants to merge 4 commits into
Conversation
…, head dims and Turing Extends the in-kernel dequantization of K/V tiles for small batches (up to 16 queries) so that the whole-view f16 conversion of the K/V cache is avoided for every common model shape: - K/V types: q4_0, q4_1, q5_0, q5_1, q8_0 and f16, with separate K and V types (mixed K/V), for the K-V combinations selected by GGML_CUDA_FA_QUANTS (default: q4_0-q4_0, q8_0-q8_0). The dequantized values are bit-identical to those of the f16 conversion. - Head dims: 64, 96, 128, 256, 512 (with GQA), 192/128 (MiMo-V2), 320/256 (Mistral Small 4) and 576/512 (DeepSeek MLA, V is a view of K), with the same ncols1/ncols2 tiles as the f16 path. - Turing: partial K tiles (nbatch_K2 not dividing DKQ/2) are loaded with a second compile-time width, so the quantized path is enabled for Turing or newer instead of Ampere or newer. - Sparse attention (n_kv_max hint) gathers the quantized K/V rows directly. One instance file per K type and tile, CMake only compiles the files of K types that are used.
…r DKQ 320 DeepSeek MLA has one K/V head, so the converted view is small next to the attention. The in-kernel loads gained 0.4-1.6% on an L4 at GQA 16 but were 6% slower on a T4 at 16 queries, so DKQ 576 uses them only on Ampere or newer up to GQA 16. Mistral Small 4 (DKQ 320) and GQA > 16 keep the conversion. Also restricts the 576 instances to K type == V type (V is a view of K) and drops the now unused ncols2 32 instances.
Carry the K and V types in the K pointer type of the FA MMA kernel, so the kernel's process_tile call sites keep their upstream text.
Codex Review Summary: ❌ FailedThis 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. |
|
The account paying for this security review has reached its Codex usage limits. The payer can check the Codex usage dashboard. For personal accounts, using credits requires enabling “Use credits for security reviews” in Code review settings. If you do not manage the paying account, contact this repository's admins. |
ggml_cuda_flash_attn_ext_mma_q_switch_D instantiated the DKQ 576 kernels for every compiled K/V combination. MLA is only selected for K type == V type (V is a view of K) and generate_cu_files.py has no mixed-type 576 instances, so fattn.cu compiled them implicitly and they were never used. Only builds with mixed combinations in GGML_CUDA_FA_QUANTS are affected.
|
Ran this on an RTX PRO 6000 Blackwell Server Edition (sm_120, CUDA 13.0, driver 580.82, Colab), which isn't in the tested list. Base
ms per decode step,
Same direction as the L4 table, smaller wins on this card. Llama-3.2-1B at 3 streams is slightly slower than #303 (+1.4%, same pattern as #303's 3-stream cell). HIP: A100 40GB (sm_80) and T4 (sm_75), same base, Colab, CUDA 13.0, with 1,340 extra FLASH_ATTN_EXT cases added to every tree (including D512 on Turing for the partial first K tile, D96, mixed K/V types, sinks, softcap):
ms per decode step at 16 streams, q4_0 / q8_0 KV, % vs base. A100: Phi-3.5-mini (D96) -26.6 / -20.5, gemma-2-2b (D256, softcap) -11.2 / -4.7, Llama-3.2-1B (D64) -6.0 / -2.2. T4 (Turing, new in this PR), Qwen3-0.6B: -39.0 / -35.6, and -13.0 / -10.4 already at 2 streams. T4 at ub 16 isn't bit-identical to base for Llama-3.2-1B and Phi-3.5 (KLD 0.0007-0.0008, top-1 98.3%). nsys shows a different stream-k split (base 120 blocks with the general fixup, this PR 80 with the uniform one). Against an f16-KV reference this PR is as close as base (Llama-1B 0.000763 vs 0.000793, Phi-3.5 0.000853 vs 0.000876), so that's reordering, not error. gpt-oss-20b at np 16 on the A100 also diverges after 250+ characters, likely the same thing; I didn't trace that one. Two small perf notes on the A100: this PR is 0.3-0.8% slower than #303 on the D128 shapes both cover, in every round. The 2-stream steps now take the new kernel there too, and that's +3.4% for Llama-3.2-1B q4_0 (repro: Not run: H100 and L4 timing (Colab refused them). |
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. 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". |
LeoBorcherding
left a comment
There was a problem hiding this comment.
Codex: clean on d491f33d6.
Tested on sm_120, which isn't in the body's list: FLASH_ATTN_EXT 10615/10615 on the default build, greedy text byte-identical to #303 and base at 4 and 16 streams for Qwen3-0.6B, Llama-3.2-1B and Phi-3.5-mini. The new head dims pay off: Phi-3.5 (D 96) decodes 18.6% faster than #303 at 16 streams and Llama-3.2-1B (D 64) 2.7% faster, while D 128 stays within 0.2% of #303.
Two things from the run:
- Llama-3.2-1B at 3 streams is 1.4% slower than #303, the same shape as #303's 3-stream cell. If #303 gates 3 queries back to the conversion path, this PR should follow.
libggml-cuda.sogrows 52 MiB (+15.6%) across the default prebuilt archs, per the body. That's the real cost of this PR, worth a sentence on whether the prebuilt release keeps all seven archs.
Not run: the GGML_CUDA_FA_QUANTS mixed-type builds, KLD, A100/H100.
…timings (source line moved off the axes)

Overview
Stacked on #303 (base branch
fa-q8-small-batch). #303 stopped the FA MMA kernel from converting the whole quantized K/V view to f16 on every layer and step for small batches (3 to 16 queries per ubatch), but only for q4_0/q8_0, head dims 128/256/512 and Ampere or newer. This extends the same in-kernel dequantization to:GGML_CUDA_FA_QUANTS); other combinations convert as before unless enabled;static_assertin CUDA: dequantize q4_0/q8_0 K/V tiles in the FA MMA kernel for small multi-sequence batches (pin branch on b11642) #303 is replaced by a narrower first K tile, so T4 takes the path too;Kept on the conversion path on purpose: MLA with GQA above 16 (GLM-4.7-Flash, Kimi-K2, DeepSeek-V3), MLA on Turing and 320/256 (Mistral Small 4). For MLA the converted view is a single KV head, so the conversion is cheap; in-kernel measured +0.4 to 1.6% on an L4 and 6.3% slower on a T4 at 16 streams. Volta, HIP and MUSA, bf16/f32 KV and prefill (more than 16 queries) are unchanged.
Design
fattn-mma-f16.cuh: the K and V types are separate template parameters. Each tile load picks the f16 or the quantized loader from its own type;nstagesis 0 when either is quantized;launch_fattnconverts only an f16 side.flash_attn_ext_f16_dequantize_8<type>handles q4_0, q4_1, q5_0, q5_1 and q8_0, identical to the conversion inconvert.cuapart from the sign of zero (a q4_0 zero comes out as -0 where the conversion gives +0, which leaves every dot product unchanged; this arithmetic is CUDA: dequantize q4_0/q8_0 K/V tiles in the FA MMA kernel for small multi-sequence batches (pin branch on b11642) #303's) (q4_1/q5_1 are computed in f32 with the same contraction and rounded to f16 once).nbatch_K2does not divideDKQ/2(Turing D 512, Ampere+ D 576 at 32/64 columns), the first tile of the reverse K loop is loaded with a second compile-time width.fattn.cu:ggml_cuda_flash_attn_ext_mma_q_supportedrequires NVIDIA Turing MMA, a compiled K-V combination, nb <= 16 and an instance for the f16 path's own ncols2 choice. The vec / MMA / tile selection inggml_cuda_get_best_fattn_kernelis untouched.generate_cu_files.pywrites one file per (K type, ncols1, ncols2); CMake compiles only the files of the selected combinations, so a default build adds 36 files (q4_0/q8_0).Review fixes
ggml_cuda_flash_attn_ext_mma_q_switch_Dno longer instantiates mixed-type kernels the generator and the runtime already exclude. The default build is unchanged (identical SASS for all 41 kernels infattn.cu.oon sm_75/89/100); with mixed pairs enabled 58 unreachable kernels per arch go andfattn.cu.oshrinks from 3.49 MB to 1.02 MB.FLASH_ATTN_EXT10615/10615 on that build (2,113 mixed-type cases).Tests
test-backend-ops -o FLASH_ATTN_EXTvs CPU, 10615/10615 pass on T4 (sm_75) and A100 (sm_80) at this head's code, default build and a build with q4_1, q5_0, q5_1, q8_0/q4_0, f16/q8_0, q8_0/f16 enabled; also 10615/10615 on B200 and L4 before the MLA gate commit, and 422/422 on the gated shapes after it.llama-batched -kvu --temp 0) CUDA: dequantize q4_0/q8_0 K/V tiles in the FA MMA kernel for small multi-sequence batches (pin branch on b11642) #303 vs this: 26/26 byte-identical on B200.GGML_CUDA_FA_QUANTS=allon sm_75/80, 0 warnings.Timing (ms per decode step, 3 interleaved rounds,
llama-batched-bench -fa on -ctk/-ctv q8_0 -kvu, npp 512)Prefill is unchanged within about 1.5% on both GPUs.
Cost
Default prebuilt archs (
cuda13-portable, 75 80 86 89 90 100 120), Colab G4,-j48:ggml-cudabuilds in 456 s against 454 s for #303, andlibggml-cuda.sogrows from 333.0 MiB to 385.1 MiB (+52.1 MiB, +15.6%, about 7.4 MiB per arch). On 12 vCPUs the extra instance files make the build about 3x longer.Pin set
Not added to
scripts/unsloth/pr-set.jsonhere. With ggml-org#25731 at e1b89cf and this head in #303's slot, the pin-set sim on b11785 and b11796 is ok (21 pins intact);FLASH_ATTN_EXT10626/10626 andFLASH_ATTN_EXT_BANDED13/13 on the composed tree (B200).Not run