Skip to content

CUDA: tune FA for Gemma 4 on Ampere or newer - #29152

Merged
IMbackK merged 1 commit into
ggml-org:masterfrom
JohannesGaessler:cuda-fa-tune-2
Sep 20, 2026
Merged

IMbackK merged 1 commit into
ggml-org:masterfrom
JohannesGaessler:cuda-fa-tune-2

Conversation

@JohannesGaessler

Copy link
Copy Markdown
Contributor

Overview

This PR tunes the CUDA FlashAttention code for Ampere or newer for head sizes 256 and 512 (relevant for Gemma 4) and batch sizes 1-4 to favor larger CUDA blocks and to preferably use the mma kernel for batch size 1. This yields a bit of performance, primarily for the small models.

Performance

Table generated with scripts/compare-llama-bench.py

GPU Model Microbatch size Test t/s master t/s a3cec6a Speedup
RTX 3090 gemma4 26B.A4B Q4_0 1 pp256 189.63 190.09 1.00
RTX 3090 gemma4 26B.A4B Q4_0 1 pp256@d32768 152.52 151.95 1.00
RTX 3090 gemma4 26B.A4B Q4_0 2 pp256 331.55 334.79 1.01
RTX 3090 gemma4 26B.A4B Q4_0 2 pp256@d32768 277.40 280.19 1.01
RTX 3090 gemma4 26B.A4B Q4_0 4 pp256 464.93 465.39 1.00
RTX 3090 gemma4 26B.A4B Q4_0 4 pp256@d32768 403.22 410.03 1.02
RTX 3090 gemma4 12B Q4_0 1 pp256 98.77 99.80 1.01
RTX 3090 gemma4 12B Q4_0 1 pp256@d32768 86.67 86.54 1.00
RTX 3090 gemma4 12B Q4_0 2 pp256 187.78 190.09 1.01
RTX 3090 gemma4 12B Q4_0 2 pp256@d32768 165.59 165.27 1.00
RTX 3090 gemma4 12B Q4_0 4 pp256 281.44 288.91 1.03
RTX 3090 gemma4 12B Q4_0 4 pp256@d32768 248.92 250.81 1.01
RTX 3090 gemma4 E2B Q4_0 1 pp256 298.02 298.24 1.00
RTX 3090 gemma4 E2B Q4_0 1 pp256@d32768 246.37 245.23 1.00
RTX 3090 gemma4 E2B Q4_0 2 pp256 555.26 565.66 1.02
RTX 3090 gemma4 E2B Q4_0 2 pp256@d32768 455.79 469.93 1.03
RTX 3090 gemma4 E2B Q4_0 4 pp256 932.25 954.86 1.02
RTX 3090 gemma4 E2B Q4_0 4 pp256@d32768 787.87 809.68 1.03
RTX 4090 gemma4 26B.A4B Q4_0 1 pp256 237.10 237.82 1.00
RTX 4090 gemma4 26B.A4B Q4_0 1 pp256@d32768 192.34 192.96 1.00
RTX 4090 gemma4 26B.A4B Q4_0 2 pp256 419.71 420.48 1.00
RTX 4090 gemma4 26B.A4B Q4_0 2 pp256@d32768 344.69 347.62 1.01
RTX 4090 gemma4 26B.A4B Q4_0 4 pp256 677.74 676.71 1.00
RTX 4090 gemma4 26B.A4B Q4_0 4 pp256@d32768 583.54 589.05 1.01
RTX 4090 gemma4 31B Q4_0 1 pp256 49.49 49.53 1.00
RTX 4090 gemma4 31B Q4_0 1 pp256@d32768 41.53 41.54 1.00
RTX 4090 gemma4 31B Q4_0 2 pp256 95.78 95.71 1.00
RTX 4090 gemma4 31B Q4_0 2 pp256@d32768 80.65 80.93 1.00
RTX 4090 gemma4 31B Q4_0 4 pp256 188.39 188.48 1.00
RTX 4090 gemma4 31B Q4_0 4 pp256@d32768 158.20 159.28 1.01
RTX 4090 gemma4 12B Q4_0 1 pp256 115.85 116.11 1.00
RTX 4090 gemma4 12B Q4_0 1 pp256@d32768 103.83 103.96 1.00
RTX 4090 gemma4 12B Q4_0 2 pp256 223.78 223.96 1.00
RTX 4090 gemma4 12B Q4_0 2 pp256@d32768 198.34 200.05 1.01
RTX 4090 gemma4 12B Q4_0 4 pp256 438.92 439.00 1.00
RTX 4090 gemma4 12B Q4_0 4 pp256@d32768 386.80 391.98 1.01
RTX 4090 gemma4 E2B Q4_0 1 pp256 356.15 358.66 1.01
RTX 4090 gemma4 E2B Q4_0 1 pp256@d32768 288.06 290.76 1.01
RTX 4090 gemma4 E2B Q4_0 2 pp256 667.73 678.80 1.02
RTX 4090 gemma4 E2B Q4_0 2 pp256@d32768 540.35 553.82 1.02
RTX 4090 gemma4 E2B Q4_0 4 pp256 1296.61 1299.27 1.00
RTX 4090 gemma4 E2B Q4_0 4 pp256@d32768 1030.16 1064.82 1.03
RTX 5090 gemma4 26B.A4B Q4_0 1 pp256 320.21 320.02 1.00
RTX 5090 gemma4 26B.A4B Q4_0 1 pp256@d32768 272.90 273.05 1.00
RTX 5090 gemma4 26B.A4B Q4_0 2 pp256 563.93 566.73 1.00
RTX 5090 gemma4 26B.A4B Q4_0 2 pp256@d32768 477.07 493.04 1.03
RTX 5090 gemma4 26B.A4B Q4_0 4 pp256 920.93 923.17 1.00
RTX 5090 gemma4 26B.A4B Q4_0 4 pp256@d32768 787.41 801.19 1.02
RTX 5090 gemma4 31B Q4_0 1 pp256 80.22 80.27 1.00
RTX 5090 gemma4 31B Q4_0 1 pp256@d32768 68.34 68.35 1.00
RTX 5090 gemma4 31B Q4_0 2 pp256 154.50 154.64 1.00
RTX 5090 gemma4 31B Q4_0 2 pp256@d32768 132.40 133.02 1.00
RTX 5090 gemma4 31B Q4_0 4 pp256 284.02 283.49 1.00
RTX 5090 gemma4 31B Q4_0 4 pp256@d32768 243.53 243.29 1.00
RTX 5090 gemma4 12B Q4_0 1 pp256 176.03 176.03 1.00
RTX 5090 gemma4 12B Q4_0 1 pp256@d32768 161.07 161.44 1.00
RTX 5090 gemma4 12B Q4_0 2 pp256 340.44 341.68 1.00
RTX 5090 gemma4 12B Q4_0 2 pp256@d32768 305.45 310.22 1.02
RTX 5090 gemma4 12B Q4_0 4 pp256 653.56 657.89 1.01
RTX 5090 gemma4 12B Q4_0 4 pp256@d32768 572.15 579.69 1.01
RTX 5090 gemma4 E2B Q4_0 1 pp256 470.11 470.47 1.00
RTX 5090 gemma4 E2B Q4_0 1 pp256@d32768 384.62 389.77 1.01
RTX 5090 gemma4 E2B Q4_0 2 pp256 860.15 884.87 1.03
RTX 5090 gemma4 E2B Q4_0 2 pp256@d32768 707.94 758.59 1.07
RTX 5090 gemma4 E2B Q4_0 4 pp256 1652.63 1651.68 1.00
RTX 5090 gemma4 E2B Q4_0 4 pp256@d32768 1300.18 1350.16 1.04

The RTX 3090 has cooling issues with the thermal pads which interfered with the measurements for 31b.

Additional information

Requirements

@JohannesGaessler
JohannesGaessler requested a review from a team as a code owner September 19, 2026 19:45
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Sep 19, 2026
@am17an

am17an commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

Deepseek also has a 512 head size.

@JohannesGaessler

Copy link
Copy Markdown
Contributor Author

Deepseek has DKQ == 576 && DV == 512, Gemma 4 has DKQ == 512 && DV == 512. I tried doing the same changes for Deepseek and it was a bit slower, that's why I changed the key from DV to DKQ so Deepseek would be unaffected.

@JohannesGaessler JohannesGaessler added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Sep 20, 2026
@am17an

am17an commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

@JohannesGaessler for the deepseek 4 family DKQ == 512 && DV == 512

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

Performance DGX-SPARK
GPU Model Microbatch size Test t/s master t/s a3cec6a Speedup
DGX-SPARK gemma4 26B.A4B Q4_0 1 pp256 104.74 104.39 1
DGX-SPARK gemma4 26B.A4B Q4_0 1 pp256@d32768 75.67 75.37 1
DGX-SPARK gemma4 26B.A4B Q4_0 2 pp256 158.76 158.82 1
DGX-SPARK gemma4 26B.A4B Q4_0 2 pp256@d32768 122.27 122.5 1
DGX-SPARK gemma4 26B.A4B Q4_0 4 pp256 230.22 230.03 1
DGX-SPARK gemma4 26B.A4B Q4_0 4 pp256@d32768 190.11 190.09 1
DGX-SPARK gemma4 31B Q4_0 1 pp256 13.2 13.21 1
DGX-SPARK gemma4 31B Q4_0 1 pp256@d32768 11.19 11.12 0.99
DGX-SPARK gemma4 31B Q4_0 2 pp256 26.19 26.08 1
DGX-SPARK gemma4 31B Q4_0 2 pp256@d32768 22.12 22.05 1
DGX-SPARK gemma4 31B Q4_0 4 pp256 51.59 51.2 0.99
DGX-SPARK gemma4 31B Q4_0 4 pp256@d32768 43.49 43.34 1
DGX-SPARK gemma4 12B Q4_0 1 pp256 36.79 36.66 1
DGX-SPARK gemma4 12B Q4_0 1 pp256@d32768 32.42 32.36 1
DGX-SPARK gemma4 12B Q4_0 2 pp256 70.21 70.08 1
DGX-SPARK gemma4 12B Q4_0 2 pp256@d32768 61.87 62.01 1
DGX-SPARK gemma4 12B Q4_0 4 pp256 139.79 139.44 1
DGX-SPARK gemma4 12B Q4_0 4 pp256@d32768 122.65 122.75 1
DGX-SPARK gemma4 E2B Q4_0 1 pp256 162.3 162.41 1
DGX-SPARK gemma4 E2B Q4_0 1 pp256@d32768 120.35 120.35 1
DGX-SPARK gemma4 E2B Q4_0 2 pp256 315.53 319.75 1.01
DGX-SPARK gemma4 E2B Q4_0 2 pp256@d32768 233.18 238.19 1.02
DGX-SPARK gemma4 E2B Q4_0 4 pp256 616.71 615.92 1
DGX-SPARK gemma4 E2B Q4_0 4 pp256@d32768 450.88 456.45 1.01

@IMbackK

IMbackK commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

@JohannesGaessler please rebase

@IMbackK

IMbackK commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

your rebase erroneously removed the const bool sparse_decode = ... line

@IMbackK
IMbackK merged commit ce8caa6 into ggml-org:master Sep 20, 2026
20 of 24 checks passed
hariag added a commit to hariag/llama.cpp that referenced this pull request Sep 23, 2026
Merges the danielhanchen qwen38-mtp branch (004b547) and the three
gemma4-related commits on top of it into upstream master (e6ab7c1):

- 6f29477 CUDA: tune FA for Gemma 4 on Ampere or newer (backport ggml-org#29152)
- 6cee122 model: fix gemma4-assistant SWA pattern array length (backport ggml-org#28183)
- 372d950 ggml: port INT8 ConvRot support (stable-diffusion vendor build)

Conflicts resolved in favor of upstream where it already contains an
equivalent fix (gemma4-assistant via ggml-org#28868, fattn.cu >=256 condition),
keeping convrot additions merged alongside upstream Hadamard logic.

# Conflicts:
#	ggml/src/ggml-cpu/ggml-cpu.cpp
#	ggml/src/ggml-cuda/fattn.cu
#	ggml/src/ggml-vulkan/ggml-vulkan.cpp
#	src/models/gemma4-assistant.cpp
#	src/models/qwen4exp.cpp
@BrewTestBot BrewTestBot mentioned this pull request Sep 23, 2026
1 task done
turbo-tan pushed a commit to turbo-tan/llama.cpp-tq3 that referenced this pull request Sep 23, 2026
ggml-org#28102 replay dropped fork's vec_dot_fattn_vec_KQ_{tq3_0,turbo3_0,turbo4_0},
dequantize_V_{tq3_0,turbo3_0,turbo4_0}, turbo4_decode_element + getter arms
(static_assert 'bad type' on GB10 builds). Re-landed from main onto the
post-ggml-org#28102/ggml-org#29152 upstream shape (185 lines, no other deltas).
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 1, 2026
# Conflicts:
#	ggml/src/ggml-cuda/fattn.cu
LadislavSopko pushed a commit to 0ics-srls/llama.cpp that referenced this pull request Oct 5, 2026
frostyautumnleaf pushed a commit to frostyautumnleaf/llama.cpp that referenced this pull request Oct 5, 2026
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 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