Commit 5b1ba66b5 for llama.cpp
commit 5b1ba66b59ef0e53d614ef525e6c1541855d3ca2
Author: Aman Gupta <amangupta052@gmail.com>
Date: Tue Dec 1 12:40:27 2026 +0800
CUDA : read K/V through kv_rows in the vec flash attention kernel (#32520)
* CUDA : read K/V through kv_rows in the vec flash attention kernel
Without a bias only the MMA kernel read K/V through kv_rows. With a quantized, BF16 or F8_E4M3 cache it converted the whole K/V view to F16 on every call, so decode in a unified cache slowed down with the cells of all sequences, idle ones included.
The vec kernel already reads K/V through kv_rows for the indexed bias. Its kv_rows variant is now also instantiated without a bias (one query per slice, no softcap), and the kv_rows dispatch takes it for non-F16 K/V with one query per slice. F16 K/V and slices of several queries keep the MMA kernel.
tests : kv_rows cases with one query per slice for f16/bf16/q8_0/q4_0
ref #32181
* CUDA : take the vec kv_rows path only for K/V pairs with a compiled vec kernel
Without a compiled vec kernel for the K/V pair (the default GGML_CUDA_FA_QUANTS has only same-type pairs), the vec kernel falls back to f16 and converts the whole cache, which is slower than the MMA kernel. Mixed pairs such as q8_0/q4_0 keep the MMA kernel as on master. The lookup also covers the head sizes of the vec kernel.
diff --git a/ggml/src/ggml-cuda/fattn-vec.cuh b/ggml/src/ggml-cuda/fattn-vec.cuh
index 1811e8b37..0ea46cc9a 100644
--- a/ggml/src/ggml-cuda/fattn-vec.cuh
+++ b/ggml/src/ggml-cuda/fattn-vec.cuh
@@ -574,6 +574,11 @@ void ggml_cuda_flash_attn_ext_vec_case_impl(ggml_backend_cuda_context & ctx, ggm
const int nthreads = ggml_cuda_fattn_vec_get_nthreads_host(cc);
const int nwarps = nthreads / WARP_SIZE;
fattn_kernel_t fattn_kernel = flash_attn_ext_vec<D, cols_per_block, type_K, type_V, use_logit_softcap, false, false>;
+ if constexpr (cols_per_block == 1 && !use_logit_softcap) {
+ if (dst->src[5]) {
+ fattn_kernel = flash_attn_ext_vec<D, cols_per_block, type_K, type_V, use_logit_softcap, false, true>;
+ }
+ }
if constexpr (cols_per_block == 1 && (D == 64 || D == 128) && !use_logit_softcap) {
if (ggml_flash_attn_ext_bias_is_indexed(dst)) {
fattn_kernel = dst->src[5] ? flash_attn_ext_vec<D, cols_per_block, type_K, type_V, use_logit_softcap, true, true>
@@ -594,6 +599,9 @@ void ggml_cuda_flash_attn_ext_vec_case(ggml_backend_cuda_context & ctx, ggml_ten
float logit_softcap;
memcpy(&logit_softcap, (const float *) KQV->op_params + 2, sizeof(float));
+ // the kv_rows variant exists for one query per sequence without softcap
+ GGML_ASSERT(!dst->src[5] || (Q->ne[1] == 1 && logit_softcap == 0.0f));
+
if (Q->ne[1] == 1) {
constexpr int cols_per_block = 1;
if (logit_softcap == 0.0f) {
diff --git a/ggml/src/ggml-cuda/fattn.cu b/ggml/src/ggml-cuda/fattn.cu
index a0a58ef99..3f2fb0193 100644
--- a/ggml/src/ggml-cuda/fattn.cu
+++ b/ggml/src/ggml-cuda/fattn.cu
@@ -765,10 +765,23 @@ static best_fattn_kernel ggml_cuda_get_best_fattn_kernel(const int device, const
}
}
- // without a bias only the MMA kernel reads K/V through kv_rows
+ // without a bias only the MMA and vec kernels read K/V through kv_rows
if (kv_rows) {
- return GGML_CUDA_CC_IS_NVIDIA(cc) && turing_mma_available(cc) && mask && Q->ne[0] != 40 && Q->ne[0] != 72 ?
- BEST_FATTN_KERNEL_MMA_F16 : BEST_FATTN_KERNEL_NONE;
+ if (!(GGML_CUDA_CC_IS_NVIDIA(cc) && turing_mma_available(cc) && mask)) {
+ return BEST_FATTN_KERNEL_NONE;
+ }
+ float logit_softcap = 0.0f;
+ memcpy(&logit_softcap, (const float *) KQV->op_params + 2, sizeof(float));
+
+ // the vec kernel reads quantized/bf16/f8 K/V in place, the MMA kernel would convert the whole cache to f16
+ // only for K/V pairs with a compiled vec kernel, the f16 fallback of the vec kernel also converts the whole cache
+ const bool vec_rows = n_kv % FATTN_KQ_STRIDE == 0 &&
+ (K->type != GGML_TYPE_F16 || V->type != GGML_TYPE_F16) && K->type != GGML_TYPE_F32 && V->type != GGML_TYPE_F32 &&
+ Q->ne[1] == 1 && logit_softcap == 0.0f && ggml_cuda_get_fattn_vec_case(Q->ne[0], K->type, V->type) != nullptr;
+ if (vec_rows) {
+ return BEST_FATTN_KERNEL_VEC;
+ }
+ return Q->ne[0] != 40 && Q->ne[0] != 72 ? BEST_FATTN_KERNEL_MMA_F16 : BEST_FATTN_KERNEL_NONE;
}
// For small batch sizes the vector kernel may be preferable over the kernels optimized for large batch sizes:
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 0880d57a3..37519b2e8 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -15074,6 +15074,13 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
test_cases.emplace_back(new test_flash_attn_ext_kv_rows(256, 8, 2, 4096, 1280, nb, n_slices));
}
}
+ // decode: one token per slice, non-f16 K/V take the vec kernel
+ for (ggml_type type_KV : {GGML_TYPE_F16, GGML_TYPE_BF16, GGML_TYPE_Q8_0, GGML_TYPE_Q4_0}) {
+ for (int64_t n_slices : {1, 3, 8}) {
+ test_cases.emplace_back(new test_flash_attn_ext_kv_rows(256, 2, 12, 8192, 2304, 1, n_slices, type_KV));
+ test_cases.emplace_back(new test_flash_attn_ext_kv_rows(128, 4, 4, 5000, 512, 2, n_slices, type_KV));
+ }
+ }
// flash attention with an indexed bias (Inkling 2 relative-position bias)
for (bool kv_rows : {false, true}) {