Skip to content

CUDA: FA MMA in-kernel K/V dequant for all quantized K/V types, head dims 64/96/192 and MLA, and Turing - #315

Open
danielhanchen wants to merge 4 commits into
fa-q8-small-batchfrom
fa-q8-small-batch-gen
Open

danielhanchen wants to merge 4 commits into
fa-q8-small-batchfrom
fa-q8-small-batch-gen

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

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:

  • head dims 64 (gpt-oss-20b, LFM2, Llama-3.2), 96 (Phi-3/3.5), 192/128 (MiMo-V2), and MLA 576/512 with GQA up to 16 on Ampere or newer (DeepSeek-V2-Lite, Moonlight);
  • K/V types q4_0, q4_1, q5_0, q5_1, q8_0, including mixed K/V (q8_0/q4_0, f16/q8_0, q8_0/f16, ...). A default build compiles only q4_0-q4_0 and q8_0-q8_0, the same opt-in rule the vec kernel uses (GGML_CUDA_FA_QUANTS); other combinations convert as before unless enabled;
  • Turing (sm_75): the D 512 static_assert in 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;
  • the sparse (indexed) path, and 1 stream for D 96 and the covered MLA shapes, which have no vec kernel and used to convert at every step.

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; nstages is 0 when either is quantized; launch_fattn converts 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 in convert.cu apart 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).
  • Partial K tiles: when nbatch_K2 does not divide DKQ/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_supported requires 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 in ggml_cuda_get_best_fattn_kernel is untouched.
  • Instances: generate_cu_files.py writes 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

  • d491f33: the MLA 576 case of ggml_cuda_flash_attn_ext_mma_q_switch_D no longer instantiates mixed-type kernels the generator and the runtime already exclude. The default build is unchanged (identical SASS for all 41 kernels in fattn.cu.o on sm_75/89/100); with mixed pairs enabled 58 unreachable kernels per arch go and fattn.cu.o shrinks from 3.49 MB to 1.02 MB. FLASH_ATTN_EXT 10615/10615 on that build (2,113 mixed-type cases).

Tests

Timing (ms per decode step, 3 interleaved rounds, llama-batched-bench -fa on -ctk/-ctv q8_0 -kvu, npp 512)

model streams L4: b11642 / #303 / this T4: change vs #303
Phi-3.5-mini (D 96) 1 13.78 / 13.77 / 12.39 -6%
16 57.38 / 57.39 / 29.46 -43%
Llama-3.2-1B (D 64) 8 9.25 / 9.26 / 8.48
16 11.71 / 11.76 / 10.40 -17%
Qwen3-0.6B (D 128) 16 14.66 / 10.10 / 10.02 -37% (#303 is compiled out on Turing)
gemma-4-E2B (D 256 + 512) 16 18.55 / 17.06 / 17.23 -3%
Moonlight-16B-A3B (MLA, gqa 16) 16 36.49 / 36.53 / 35.95 kept on conversion

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-cuda builds in 456 s against 454 s for #303, and libggml-cuda.so grows 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.json here. 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_EXT 10626/10626 and FLASH_ATTN_EXT_BANDED 13/13 on the composed tree (B200).

Not run

  • A100/H100/sm_120 timing (A100 tests only), MiMo-V2 and the sparse path with a real model (test shapes only), mixed and q4_1/q5_x timing.
  • Volta, AMD/HIP, MUSA (device code untouched there).
  • The full 21-pin tree build and test (merge sim only).

…, 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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T04:21:57.994255Z d491f33 Manual request
🔒 Security Review ❌ Failed 2026-10-21T16:22:39.809980Z
We couldn’t complete this review. Comment @codex security review to try again.
549d0be PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

LeoBorcherding commented Nov 17, 2026 •

Copy link
Copy Markdown

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 2639a3b4e, #303 34a66f42b and this PR d491f33d6, default build with -DCMAKE_CUDA_ARCHITECTURES=120.

ms per decode step, llama-batched-bench -fa on -ctk q8_0 -ctv q8_0 -kvu -npp 512 -ntg 64, median of 3 interleaved rounds:

model streams base #303 this PR vs #303
Phi-3.5-mini (D 96) 1 4.36 4.36 4.20 -3.6%
4 5.89 5.89 5.49 -6.8%
8 7.10 7.09 6.57 -7.4%
16 10.88 10.88 8.86 -18.6%
Llama-3.2-1B (D 64) 3 1.86 1.86 1.89 +1.4%
4 1.98 1.98 1.92 -3.0%
16 3.49 3.49 3.39 -2.7%
Qwen3-0.6B (D 128) 4-16 within 0.2% of #303

#315 decode, sm_120 and T4

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: test-backend-ops -o FLASH_ATTN_EXT -b ROCm0 passes 10615/10615 on an RX 9060 XT (gfx1200, Linux, ROCm 10.1), on a Radeon 8060S (gfx1151, Linux, HIP 7.16) and on an R9700 (gfx1201, Windows 11, ROCm 7.1; base 3993/3993, #303 5586/5586), and the ggml-hip/CMakeLists.txt change builds on all three. It also stops the HIP build from compiling #303's CUDA-only instance files.

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):

A100 T4
test-backend-ops -o FLASH_ATTN_EXT, default build 11955/11955 11955/11955
same, GGML_CUDA_FA_QUANTS with q4_1/q5_0/q5_1 and mixed pairs 11955/11955 11955/11955
compute-sanitizer memcheck / racecheck 0 errors / 0 hazards 0 errors / 0 hazards
same, on the all-types build (q4_1/q5_x and mixed-pair cases: memcheck 1851, racecheck 234) 0 errors / 0 hazards 0 errors / 0 hazards
KLD vs base, 5 models, q8_0/q4_0, plus q4_1/q5_0/q5_1/mixed on the all-types build 0 (base vs base floor) 0, except ub 16 below

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: llama-batched-bench -m Llama-3.2-1B-Instruct-Q8_0.gguf -fa on -ctk q4_0 -ctv q4_0 -kvu -npp 512 -ntg 64 -npl 2 -c 10240 -ub 512 -ngl 99).

Not run: H100 and L4 timing (Colab refused them).

@LeoBorcherding

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: d491f33d67

ℹ️ 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".

@LeoBorcherding LeoBorcherding left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.so grows 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.

LeoBorcherding added a commit to LeoBorcherding/llama.cpp that referenced this pull request Nov 17, 2026
…timings (source line moved off the axes)

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants