Repository navigation
CUDA: tune fp16 tile FlashAttention configs for head sizes 40-112 - #26289
Merged
ggerganov merged 2 commits intoSep 27, 2026
Merged
Conversation
|
Hi @animeshsri14, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
Contributor
Author
|
Previous pr has been converted to draft for now, as I would like to allot my one-pr slot to this pr. |
JohannesGaessler
approved these changes
Sep 24, 2026
pierreguillot
pushed a commit
to Ircam-Partiels/llama.cpp
that referenced
this pull request
Oct 1, 2026
edwardyoon
pushed a commit
to edwardyoon/focus-llama
that referenced
this pull request
Oct 1, 2026
edwardyoon
pushed a commit
to edwardyoon/focus-llama
that referenced
this pull request
Oct 2, 2026
1 task
frostyautumnleaf
pushed a commit
to frostyautumnleaf/llama.cpp
that referenced
this pull request
Oct 5, 2026
edwardyoon
pushed a commit
to edwardyoon/focus-llama
that referenced
this pull request
Oct 7, 2026
…ml-org#26289) (cherry picked from commit 36d7b08)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR optimizes the FlashAttention FP16 tile configurations for NVIDIA GPUs, addressing the header TODO in
ggml/src/ggml-cuda/fattn-tile.cuhto "optimize kernel parameters for FP16 NVIDIA (P100)" for head sizes 40, 64, 72, 80, 96, and 112.By retuning exactly 13 rows in
ggml_cuda_fattn_tile_get_config_nvidia_fp16, this PR achieves a mean kernel-level speedup of +14.5% on P100 (up to +31.5%), with zero regressions across Pascal (P100), Volta (V100), and Blackwell architectures.End-to-end, this yields a solid +2% decode speedup for phi-2 at short contexts, scaling to +9.4% at 8K context, while remaining neutral for other models and prefill workloads.
Additional information
1. Hardware & Methodology
Methodology:
LD_LIBRARY_PATH(becauseRUNPATHwould otherwise override).2. Microbenchmark Gains (Isolated Kernel)
Rows were timed via
test-backend-ops perf -o FLASH_ATTN_EXT(4 heads, KV 4096, F16 K/V, F32 accum).ncolsmaps to the batch size / GQA ratio.Noise Floor: In-run noise floors were established by the 13 unmodified guard rows at 2.6% for P100 and 1.8% for V100. We define "neutral" as any delta falling within this noise floor. Under this definition, every modified row is win-or-neutral across all architectures it runs on.
(Note: Every tensor-core branch is explicitly guarded by ne[0] != 40 && ne[0] != 72, meaning head sizes 40 and 72 fall through to the TILE kernel on all fast-fp16 NVIDIA GPUs through Blackwell.)
(*row does not execute on the arch)
3. Tuning Mechanisms
nbatch_fa64 -> 128: Amortizes softmax/rescale overhead by halving the KQ-tile loop iteration count. Main driver for small-ncols rows.occupancy2 -> 3: Hides more latency with a third resident block per SM, taking advantage of the shared-memory headroom left by small heads.nthreads64 -> 128: Doubles per-block parallelism at small batch sizes (ncols=2), fixing severe SM underutilization.nbatch_Kfull-DV: Halves K-loop overhead for several rows by loading the full DV per iteration instead of DV/2.4. End-to-End Scaling & Cross-Model Safety (P100)
Tested via
llama-bench.Cross-model Safety:
Models hitting untuned batch-1 rows showed flat performance within the run-to-run noise margin for both prefill and decode, indicating zero regressions in production scenarios.
Phi-2 (hs 80) Decode Scaling:
Single-batch decode hits the tuned
80/2row. The speedup scales effectively with context length as FlashAttention consumes a larger share of the decode step relative to weight-streaming:Caveat: Depth > 2048 exceeds phi-2's native context; these are throughput measurements, not a generation-quality claim.
5. Correctness
Passed
test-backend-ops test -o FLASH_ATTN_EXT:cudaFuncSetAttributeinvalid argument abort on the hs 320/256 MLA test atfattn-mma-f16.cuh:1945because it exceeds Volta's 96 KB smem limit. Verified on the baseline(untuned branch) to be identical error, unrelated to this pr.Requirements