Skip to content

CUDA: replace GGML_FA_ALL_QUANTS with GGML_FA_QUANTS, more control over what is compiled - #28079

Merged
pwilkin merged 4 commits into
ggml-org:masterfrom
pwilkin:fa-select-quants
Sep 9, 2026
Merged

pwilkin merged 4 commits into
ggml-org:masterfrom
pwilkin:fa-select-quants

Conversation

@pwilkin

@pwilkin pwilkin commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Overview

Currently, to use a cross-quant combination of KV cache quants, you have to build with -DGGML_CUDA_FA_ALL_QUANTS. However, that forces compilation of large package of quant combinations, some of which are totally unviable (eg. Q_X-F16 KV quants or generally X-Y quants with X < Y) and others are never selected in practice. Thus, adding a new option GGML_CUDA_FA_QUANTS, where you pick the quants you want built in X-Y format, eg.:

-DGGML_CUDA_FA_QUANTS="q8_0-q8_0;q8_0-q5_1;q5_1-q5_1"

Additional information

This should greatly reduce compilation time on CUDA/HIP boxes.

Requirements

@pwilkin
pwilkin requested review from a team, IMbackK and ggerganov as code owners August 31, 2026 09:41
@github-actions github-actions Bot added documentation Improvements or additions to documentation ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Aug 31, 2026

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

Instead of adding a new compilation option, refactor the currently existing one into one like GGML_CUDA_FA_QUANTS that has a default of f16-f16;q8_0-q8_0;q4_0-q4_0.

Instead of reporting FA as not supported if the specific quant combination has not been compiled, fall back to the f16-f16 variant. This should automatically result in the correct type conversions to produce correct results. The speed will still be bad though so add a one-time warning that a quant combination is missing.

Please keep the code style consistent, particularly as it relates to closing #ifdef X ... #endif with // X.

@pwilkin pwilkin changed the title CUDA: add GGML_FA_SELECT_QUANTS to build a subset of FA quants CUDA: replace GGML_FA_ALL_QUANTS with GGML_FA_QUANTS, more control over what is compiled Sep 3, 2026
@pwilkin

pwilkin commented Sep 3, 2026 •

Copy link
Copy Markdown
Member Author

@JohannesGaessler all right, as per new doc:

Option Type Defaults Description
GGML_CUDA_FA_QUANTS Preset or type combinations minimal Select FlashAttention KV cache type combinations. Presets: minimal compiles f16-f16, bf16-bf16, q4_0-q4_0, and q8_0-q8_0; all compiles every combination; symmetric compiles every X-X combination; viable compiles combinations where K is at least as precise as V. A custom type_K-type_V list separated by ; is also accepted. Custom lists always include f16-f16. Legal types: f16, bf16, q4_0, q4_1, q5_0, q5_1, q8_0.
GGML_CUDA_FA_ALL_QUANTS Boolean false Deprecated alias for GGML_CUDA_FA_QUANTS=all.

Comment thread docs/build.md Resolved
@JohannesGaessler JohannesGaessler self-assigned this Sep 4, 2026
@pwilkin

pwilkin commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

@JohannesGaessler okay - removed all the flags but "all", added the fallback to f16-f16 + an explicit warning displayed once about the combination not being compiled in.

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

As I said off-site, please keep the

Comment thread ggml/cmake/common.cmake Resolved
Comment thread ggml/cmake/common.cmake Resolved
Comment thread docs/build.md Resolved
Comment thread ggml/src/ggml-cuda/fattn.cu Resolved
Comment thread ggml/src/ggml-cuda/fattn.cu Resolved
pwilkin and others added 2 commits September 7, 2026 13:21
Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
@pwilkin

pwilkin commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

@JohannesGaessler done.

@pwilkin pwilkin added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Sep 7, 2026
@pwilkin
pwilkin merged commit 5a4d0fe into ggml-org:master Sep 9, 2026
30 of 31 checks passed
@seadra

seadra commented Sep 9, 2026

Copy link
Copy Markdown

With iq4_nl, getting the following error:

  GGML_CUDA_FA_QUANTS: unknown type "iq4_nl" in "iq4_nl-iq4_nl", must be one
  of: q4_0;q4_1;q5_0;q5_1;q8_0;bf16;f16

Just wanted to confirm if this is expected.

@pwilkin

pwilkin commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

@seadra looks like a bug, going to add a quick PR for it.

@pwilkin

pwilkin commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

@seadra never mind - it looks like iq4_nl was a false positive in the previous build and it was never actually an accepted KV cache quant (it fell back to f16 silently).

@JohannesGaessler

Copy link
Copy Markdown
Contributor

The CUDA backend does not and never has had any support for quantized iq4_nl KV cache.

zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
…er what is compiled (ggml-org#28079)

* CUDA: add configurable FA quant combinations

Assisted-by: Codex

* remove all flags but , add runtime fallback with warning for uncompiled combination

* Update docs/build.md

Co-authored-by: Johannes Gäßler <johannesg@5d6.de>

* apply code review comments

---------

Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
@BrewTestBot BrewTestBot mentioned this pull request Sep 14, 2026
1 task done
OllyJohnston added a commit to OllyJohnston/llama.cpp that referenced this pull request Sep 26, 2026
Port upstream PR ggml-org#28079 (fcaeeff) so the compiled FlashAttention K/V
combinations are selectable at configure time instead of the
all-or-nothing GGML_CUDA_FA_ALL_QUANTS switch:

    -DGGML_CUDA_FA_QUANTS=f16-f16,q4_0-q4_0,q8_0-q8_0,q5_0-q5_0

A combination with no compiled kernel falls back to the f16 kernel
instead of silently taking a slow path. f16-f16 is always compiled.

Fork delta: GGML_CUDA_FA_ALL_QUANTS is still defined when FA_QUANTS is
"all". The KV-stream native path keys off it, so this keeps its trigger
unchanged.

Measured on the dense 27B (522-token prompt, -c 100000 --ubatch 256):
q4_0/q4_0 758 tok/s, q8_0/q4_0 877, q5_1/q5_1 905, q5_0/q5_0 934.
q8_0/q4_0 was 186 tok/s before, because its kernel was not compiled.
q6_K has no FA kernel and stays on the conversion path.
bri-prism pushed a commit to PrismML-Eng/llama.cpp that referenced this pull request Oct 9, 2026
…er what is compiled (ggml-org#28079) (#317)

* CUDA: add configurable FA quant combinations

Assisted-by: Codex

* remove all flags but , add runtime fallback with warning for uncompiled combination

* Update docs/build.md



* apply code review comments

---------

Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com>
Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
roverdrubber added a commit to roverdrubber/llama.cpp that referenced this pull request Oct 15, 2026
…etune kernel selection

Three numeric defects made long prompts return NaN logits on an RX 7900 XTX.
The tile and vector kernels summed the V-weighted softmax numerator in F16, and
that sum is unnormalized: over a long KV cache it saturates and then overflows.
Both now accumulate in F32, including the shared buffer the vector kernel
combines through. The MMA kernel had the same problem twice over: RDNA3 used an
F16 accumulator for every head size except 80 and 112, and even the F32 path
writes the unnormalized sum into F16 shared memory before dividing by the row
sum. RDNA3 now uses the head-80 tile layout everywhere, and an F32 accumulator
is divided by its row sum before that write, with the row sum folded back
through the meta data. Upstream carries the second half of that defect too.

Kernel selection on RDNA3 with a quantized KV cache at head size 256 was also
wrong for the small batches speculative decoding verifies with. The vector
kernel's cost grows with batch times GQA ratio while the tile kernel packs the
GQA columns, so the vector kernel now only runs below a product of 6 (below a
GQA ratio of 16 for a single token) and WMMA only takes over past 32 packed
columns instead of 16. Measured with test-backend-ops perf over 85 shapes
(GQA 1 to 16, batch 1 to 8, 16K to 131K cells, q8_0 and q4_0): 48.1 ms to
41.8 ms in total, no shape more than 1 percent worse than before, and within
0.5 percent of the best kernel per shape. A batch of two is 1.3 to 2.8 times
faster; the 35B's verify batch is 10 percent faster at 65K and beyond.

Also ports the upstream flash-attention work this sits on: the kernel
refactor (ggml-org#28079, hand-merged to keep the TurboQuant KV types and their
fallback), the head-256 and gfx1201 tuning (ggml-org#28102), and the MFMA F32
accumulator (ggml-org#28576).

FLASH_ATTN_EXT passes on gfx1100 (3975/3975 at the tip of this series, with
the regression cases the last commit adds), and the 35B answers a 112K-token
prompt with finite logits. The 27B's deep-context prefill costs 7 to 14
percent more than the saturating F16 path it replaces.

Assisted-by: Claude Opus 5
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 documentation Improvements or additions to documentation ggml changes relating to the ggml tensor library for machine learning merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants