Commit 386e41466 for llama.cpp

commit 386e41466bcb547203abdbf700a3bab45c7a2067
Author: issixx <46835150+issixx@users.noreply.github.com>
Date:   Tue Dec 1 22:17:24 2026 +0900

    CUDA: optimize f16 conversion when using kv_rows (#33427)

diff --git a/ggml/src/ggml-cuda/convert.cu b/ggml/src/ggml-cuda/convert.cu
index 601c63232..4d8bb792d 100644
--- a/ggml/src/ggml-cuda/convert.cu
+++ b/ggml/src/ggml-cuda/convert.cu
@@ -40,14 +40,23 @@ static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __
     }
 }

-template <bool need_check>
-static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int64_t k) {
+// has_rows: rows of k elements, block (x, y) converts part y of row rows[x] (row 0 for x == n_rows, nothing for negative entries)
+template <bool need_check, bool has_rows>
+static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int64_t k,
+        const int32_t * __restrict__ rows, const int64_t n_rows) {
 #if __CUDA_ARCH__ >= GGML_CUDA_CC_PASCAL
     constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;

-    const int64_t   i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
-    const int * x0 = ((int *) vx) + blockIdx.x * nint;
-    half2 * y2 = (half2 *) (y + i0);
+    const int64_t i01   = has_rows && blockIdx.x < n_rows ? rows[blockIdx.x] : 0;
+    const int64_t ipart = has_rows ? blockIdx.y : blockIdx.x;
+
+    if (i01 < 0) {
+        return; // padding
+    }
+
+    const int64_t   i0 = CUDA_Q8_0_NE_ALIGN*ipart;
+    const int * x0 = ((int *) vx) + i01*(k/QK8_0)*sizeof(block_q8_0)/sizeof(int) + ipart * nint;
+    half2 * y2 = (half2 *) (y + i01*k + i0);

     __shared__ int vals[nint];

@@ -76,7 +85,7 @@ static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, h
         y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
     }
 #else
-    GGML_UNUSED_VARS(vx, y, k);
+    GGML_UNUSED_VARS(vx, y, k, rows, n_rows);
     NO_DEVICE_CODE;
 #endif // __CUDA_ARCH__ >= GGML_CUDA_CC_PASCAL
 }
@@ -268,10 +277,31 @@ static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half *
     const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
     if (k % CUDA_Q8_0_NE_ALIGN == 0) {
         const bool need_check = false;
-        dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
+        dequantize_block_q8_0_f16<need_check, false><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k, nullptr, 0);
+    } else {
+        const bool need_check = true;
+        dequantize_block_q8_0_f16<need_check, false><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k, nullptr, 0);
+    }
+}
+
+// for ne01 contiguous rows of ne00 elements, converts only the rows in rows[0..n_rows) and row 0
+static void dequantize_block_q8_0_f16_rows_cuda(const void * vx, half * y, const int64_t ne00, const int64_t ne01,
+        const int32_t * rows, const int64_t n_rows, cudaStream_t stream) {
+    const int64_t nparts     = (ne00 + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
+    const int64_t nparts_all = (ne00*ne01 + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
+    // convert all of the data if that needs fewer blocks (not elements), so this never launches more blocks than the full conversion
+    // the kernel reads int so a row has to start at a multiple of sizeof(int)
+    if ((n_rows + 1)*nparts >= nparts_all || nparts > 65535 || (ne00/QK8_0*sizeof(block_q8_0)) % sizeof(int) != 0) {
+        dequantize_block_q8_0_f16_cuda(vx, y, ne00*ne01, stream);
+        return;
+    }
+    const dim3 num_blocks(n_rows + 1, nparts, 1);
+    if (ne00 % CUDA_Q8_0_NE_ALIGN == 0) {
+        const bool need_check = false;
+        dequantize_block_q8_0_f16<need_check, true><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, ne00, rows, n_rows);
     } else {
         const bool need_check = true;
-        dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
+        dequantize_block_q8_0_f16<need_check, true><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, ne00, rows, n_rows);
     }
 }

@@ -673,6 +703,13 @@ to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
     }
 }

+to_fp16_rows_cuda_t ggml_get_to_fp16_rows_cuda(ggml_type type) {
+    if (type == GGML_TYPE_Q8_0 && fp16_available(ggml_cuda_info().devices[ggml_cuda_get_device()].cc)) {
+        return dequantize_block_q8_0_f16_rows_cuda;
+    }
+    return nullptr;
+}
+
 to_fp16_nc_cuda_t ggml_get_to_fp16_nc_cuda(ggml_type type) {
     switch (type) {
         case GGML_TYPE_F32:
diff --git a/ggml/src/ggml-cuda/convert.cuh b/ggml/src/ggml-cuda/convert.cuh
index fb2a1b3bc..53bdb86d4 100644
--- a/ggml/src/ggml-cuda/convert.cuh
+++ b/ggml/src/ggml-cuda/convert.cuh
@@ -31,6 +31,12 @@ to_fp32_nc_cuda_t ggml_get_to_fp32_nc_cuda(ggml_type type);
 to_fp16_nc_cuda_t ggml_get_to_fp16_nc_cuda(ggml_type type);
 to_bf16_nc_cuda_t ggml_get_to_bf16_nc_cuda(ggml_type type);

+// for ne01 contiguous rows of ne00 elements, converts only the rows in rows[0..n_rows) and row 0, negative entries are skipped
+typedef void (*to_fp16_rows_cuda_t)(const void * x, half * y,
+    int64_t ne00, int64_t ne01, const int32_t * rows, int64_t n_rows, cudaStream_t stream);
+
+to_fp16_rows_cuda_t ggml_get_to_fp16_rows_cuda(ggml_type type);
+
 template<typename dst_t, typename src_t>
  __host__ __device__ inline dst_t ggml_cuda_cast(src_t x) {
     if constexpr (std::is_same_v<dst_t, src_t>) {
diff --git a/ggml/src/ggml-cuda/fattn-common.cuh b/ggml/src/ggml-cuda/fattn-common.cuh
index a3a99c86e..065330903 100644
--- a/ggml/src/ggml-cuda/fattn-common.cuh
+++ b/ggml/src/ggml-cuda/fattn-common.cuh
@@ -1373,8 +1373,16 @@ void launch_fattn(
         GGML_ASSERT(f16_extra.K != 0);
         half * K_f16 = (half *) f16_extra.K;
         if (ggml_is_contiguously_allocated(K)) {
-            to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(K->type);
-            to_fp16(K_data, K_f16, ggml_nelements(K), main_stream);
+            // with kv_rows the kernel reads only the listed rows and row 0 (for padding), so convert only those
+            const to_fp16_rows_cuda_t to_fp16_rows = kv_rows && nb12*bs == K->ne[0]*ts && nb11 == K->ne[2]*nb12 ?
+                ggml_get_to_fp16_rows_cuda(K->type) : nullptr;
+            if (to_fp16_rows) {
+                to_fp16_rows(K_data, K_f16, K->ne[0]*K->ne[2], K->ne[1],
+                    (const int32_t *) kv_rows->data, ggml_nelements(kv_rows), main_stream);
+            } else {
+                to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(K->type);
+                to_fp16(K_data, K_f16, ggml_nelements(K), main_stream);
+            }

             nb11 = nb11*bs*sizeof(half)/ts;
             nb12 = nb12*bs*sizeof(half)/ts;
@@ -1407,8 +1415,15 @@ void launch_fattn(
             GGML_ASSERT(f16_extra.V != 0);
             half * V_f16 = (half *) f16_extra.V;
             if (ggml_is_contiguously_allocated(V)) {
-                to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(V->type);
-                to_fp16(V_data, V_f16, ggml_nelements(V), main_stream);
+                const to_fp16_rows_cuda_t to_fp16_rows = kv_rows && nb22*bs == V->ne[0]*ts && nb21 == V->ne[2]*nb22 ?
+                    ggml_get_to_fp16_rows_cuda(V->type) : nullptr;
+                if (to_fp16_rows) {
+                    to_fp16_rows(V_data, V_f16, V->ne[0]*V->ne[2], V->ne[1],
+                        (const int32_t *) kv_rows->data, ggml_nelements(kv_rows), main_stream);
+                } else {
+                    to_fp16_cuda_t to_fp16 = ggml_get_to_fp16_cuda(V->type);
+                    to_fp16(V_data, V_f16, ggml_nelements(V), main_stream);
+                }
                 V_data = (char *) V_f16;

                 nb21 = nb21*bs*sizeof(half)/ts;
diff --git a/tests/test-backend-ops.cpp b/tests/test-backend-ops.cpp
index 466effd67..c33cbc3a6 100644
--- a/tests/test-backend-ops.cpp
+++ b/tests/test-backend-ops.cpp
@@ -15074,6 +15074,8 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
             test_cases.emplace_back(new test_flash_attn_ext_kv_rows( 64, 4, 2, 3000, 768,  nb, n_slices, GGML_TYPE_Q8_0));
             test_cases.emplace_back(new test_flash_attn_ext_kv_rows(512, 2, 8, 4096, 1024, nb, n_slices));
             test_cases.emplace_back(new test_flash_attn_ext_kv_rows(256, 8, 2, 4096, 1280, nb, n_slices));
+            test_cases.emplace_back(new test_flash_attn_ext_kv_rows(512, 2, 8, 4096, 1024, nb, n_slices, GGML_TYPE_Q8_0));
+            test_cases.emplace_back(new test_flash_attn_ext_kv_rows(256, 8, 2, 8192, 1280, nb, n_slices, GGML_TYPE_Q8_0));
         }
     }
     // decode: one token per slice, non-f16 K/V take the vec kernel