Commit ec3dac264 for llama.cpp

commit ec3dac26499ca2d5958d4560b4d76386d361b169
Author: uvos <carl@uvos.xyz>
Date:   Sat Nov 28 15:28:54 2026 +0100

    HIP: implement a ddp path for sum reduction operator (#32215)

    * HIP: implement a ddp path for sum reduction operator

    * Support wdiths 4,8,16

    * Might as well support width 2 also

    * Fix width 8 case

    * hip: change from ddp move to dpp update

    * hip: static assert

    * hip: change width == 32 to ds_swizzle

    * hip: when lane0 only is used, handle fast warp_reduce_sum

    * hip: ds swizzle static assert

    * hip: typo

    * hip: extend to warp_reduce_sum(float2 a)

    * hip: small comment mmvf.cu to avoid concurrent shared memory writes in MMVF, extends lane0 to block_size > warp_size

    * hip: add mmvq to warp_reduce_sum lane0 only

    * Update ggml/src/ggml-cuda/common.cuh

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

    ---------

    Co-authored-by: thelittlefireman <thelittlefireman3@gmail.com>
    Co-authored-by: thelittlefireman <5165783+thelittlefireman@users.noreply.github.com>
    Co-authored-by: Johannes Gäßler <johannesg@5d6.de>

diff --git a/ggml/src/ggml-cuda/common.cuh b/ggml/src/ggml-cuda/common.cuh
index 05f15448f..5b09dbc57 100644
--- a/ggml/src/ggml-cuda/common.cuh
+++ b/ggml/src/ggml-cuda/common.cuh
@@ -517,35 +517,64 @@ struct ggml_cuda_unroll<1> {
     }
 };

-template<int width = WARP_SIZE>
+// If only_lane0_is_used is set to true, then the performance can be better but only lane 0 is guaranteed to have the correct result.
+template<typename T, int width, bool only_lane0_is_used = false>
+static __device__ __forceinline__ T warp_reduce_sum_impl(T x) {
+#if defined(GGML_USE_HIP)
+    if constexpr (width <= 64 && width <= ggml_cuda_get_physical_warp_size()) {
+        static_assert(width >= 2 && width <= 64 && (width & (width - 1)) == 0, "width must be a power of two between 2 and 64");
+        x += ggml_hip_dpp_update<GGML_HIP_DPP_QUAD_PERM_1032>(x);
+        if constexpr (width >= 4) {
+            x += ggml_hip_dpp_update<GGML_HIP_DPP_QUAD_PERM_2301>(x);
+        }
+        if constexpr (width >= 8) {
+            x += ggml_hip_dpp_update<GGML_HIP_DPP_ROW_HALF_MIRROR>(x);
+        }
+        if constexpr (width >= 16) {
+            x += ggml_hip_dpp_update<GGML_HIP_DPP_ROW_ROR_8>(x);
+        }
+        if constexpr (width == 64) {
+            x += ggml_hip_dpp_update<GGML_HIP_DPP_ROW_BCAST_15>(x);
+            x += ggml_hip_dpp_update<GGML_HIP_DPP_ROW_BCAST_31>(x);
+            if constexpr (only_lane0_is_used) {
+                return ggml_hip_dpp_update<GGML_HIP_DPP_WAVE_ROR_1>(x);
+            } else {
+                return ggml_cuda_shfl(x, 63, 64);
+            }
+        }
+        if constexpr (width == 32) {
+            return x + ggml_hip_swizzle<GGML_HIP_SWIZZLE_SWAP16>(x);
+        }
+        return x;
+    } else
+#endif // defined(GGML_USE_HIP)
+    {
+#pragma unroll
+        for (int offset = width/2; offset > 0; offset >>= 1) {
+            x += ggml_cuda_shfl_xor(x, offset, width);
+        }
+        return x;
+    }
+}
+
+template<int width = WARP_SIZE, bool only_lane0_is_used = false>
 static __device__ __forceinline__ int warp_reduce_sum(int x) {
 #if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA) && __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE
     return __reduce_add_sync(GGML_CUDA_FULL_THREAD_MASK, x);
 #else
-#pragma unroll
-    for (int offset = width/2; offset > 0; offset >>= 1) {
-        x += ggml_cuda_shfl_xor(x, offset, width);
-    }
-    return x;
+    return warp_reduce_sum_impl<int, width, only_lane0_is_used>(x);
 #endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA) && __CUDA_ARCH__ >= GGML_CUDA_CC_AMPERE
 }

-template<int width = WARP_SIZE>
+template<int width = WARP_SIZE, bool only_lane0_is_used = false>
 static __device__ __forceinline__ float warp_reduce_sum(float x) {
-#pragma unroll
-    for (int offset = width/2; offset > 0; offset >>= 1) {
-        x += ggml_cuda_shfl_xor(x, offset, width);
-    }
-    return x;
+    return warp_reduce_sum_impl<float, width, only_lane0_is_used>(x);
 }

-template<int width = WARP_SIZE>
+template<int width = WARP_SIZE, bool only_lane0_is_used = false>
 static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
-#pragma unroll
-    for (int offset = width/2; offset > 0; offset >>= 1) {
-        a.x += ggml_cuda_shfl_xor(a.x, offset, width);
-        a.y += ggml_cuda_shfl_xor(a.y, offset, width);
-    }
+    a.x = warp_reduce_sum_impl<float, width, only_lane0_is_used>(a.x);
+    a.y = warp_reduce_sum_impl<float, width, only_lane0_is_used>(a.y);
     return a;
 }

diff --git a/ggml/src/ggml-cuda/count-equal.cu b/ggml/src/ggml-cuda/count-equal.cu
index 411d08c0d..f09a3c68a 100644
--- a/ggml/src/ggml-cuda/count-equal.cu
+++ b/ggml/src/ggml-cuda/count-equal.cu
@@ -16,7 +16,7 @@ static __global__ void count_equal(const T * __restrict__ x, const T * __restric
         nequal += xi == yi;
     }

-    nequal = warp_reduce_sum(nequal);
+    nequal = warp_reduce_sum<WARP_SIZE, true>(nequal);

     if (threadIdx.x != 0) {
         return;
diff --git a/ggml/src/ggml-cuda/cross-entropy-loss.cu b/ggml/src/ggml-cuda/cross-entropy-loss.cu
index 0c8b08197..ee7826605 100644
--- a/ggml/src/ggml-cuda/cross-entropy-loss.cu
+++ b/ggml/src/ggml-cuda/cross-entropy-loss.cu
@@ -40,7 +40,7 @@ static __global__ void cross_entropy_loss_f32(
         const float logit_i = use_shared ? tmp[i] : logits[i];
         loss += (logit_i - max_logit - sum) * labels[i];
     }
-    loss = -warp_reduce_sum(loss) / (float)k;
+    loss = -warp_reduce_sum<WARP_SIZE, true>(loss) / (float)k;

     if (threadIdx.x != 0) {
         return;
diff --git a/ggml/src/ggml-cuda/fattn-vec.cuh b/ggml/src/ggml-cuda/fattn-vec.cuh
index 6e5a09ea5..bbe6901ee 100644
--- a/ggml/src/ggml-cuda/fattn-vec.cuh
+++ b/ggml/src/ggml-cuda/fattn-vec.cuh
@@ -513,7 +513,7 @@ static __global__ void flash_attn_ext_vec(
 #endif // V_DOT2_F32_F16_AVAILABLE

         KQ_sum[j_VKQ] *= kqmax_scale;
-        KQ_sum[j_VKQ] = warp_reduce_sum(KQ_sum[j_VKQ]);
+        KQ_sum[j_VKQ] = warp_reduce_sum<WARP_SIZE, true>(KQ_sum[j_VKQ]);
         if (threadIdx.x == 0) {
             KQ_sum_shared[j_VKQ][threadIdx.y] = KQ_sum[j_VKQ];
         }
diff --git a/ggml/src/ggml-cuda/gated_delta_net.cu b/ggml/src/ggml-cuda/gated_delta_net.cu
index 526030007..c38684e8f 100644
--- a/ggml/src/ggml-cuda/gated_delta_net.cu
+++ b/ggml/src/ggml-cuda/gated_delta_net.cu
@@ -203,7 +203,7 @@ gated_delta_net_cuda(const float * q,
                 attn_partial += s_shard[r] * q_reg[r];
             }

-            float attn_col = warp_reduce_sum<lanes_per_col>(attn_partial);
+            float attn_col = warp_reduce_sum<lanes_per_col, true>(attn_partial);

             if (lane_in_col == 0) {
                 attn_data[col] = attn_col * scale;
@@ -237,7 +237,7 @@ gated_delta_net_cuda(const float * q,
                 attn_partial += s_shard[r] * q_reg[r];
             }

-            float attn_col = warp_reduce_sum<lanes_per_col>(attn_partial);
+            float attn_col = warp_reduce_sum<lanes_per_col, true>(attn_partial);

             if (lane_in_col == 0) {
                 attn_data[col] = attn_col * scale;
diff --git a/ggml/src/ggml-cuda/lightning-indexer.cu b/ggml/src/ggml-cuda/lightning-indexer.cu
index ff6402e47..b5a5b1491 100644
--- a/ggml/src/ggml-cuda/lightning-indexer.cu
+++ b/ggml/src/ggml-cuda/lightning-indexer.cu
@@ -290,7 +290,7 @@ static __global__ void lightning_indexer_kernel_vec(

 #pragma unroll
             for (int k = 0; k < K_VECS_PER_WARP; ++k) {
-                float sum = warp_reduce_sum(qk[k]);
+                float sum = warp_reduce_sum<WARP_SIZE, true>(qk[k]);

                 // ReLU, weight
                 if (i_lane == 0) {
diff --git a/ggml/src/ggml-cuda/mmvf.cu b/ggml/src/ggml-cuda/mmvf.cu
index f8b03c9d9..600c92a8f 100644
--- a/ggml/src/ggml-cuda/mmvf.cu
+++ b/ggml/src/ggml-cuda/mmvf.cu
@@ -358,29 +358,32 @@ static __global__ void mul_mat_vec_f(
     ggml_cuda_pdl_lc();
 #pragma unroll
     for (int j = 0; j < ncols_dst; ++j) {
-        sumf[j] = warp_reduce_sum<warp_size>(sumf[j]);
+        sumf[j] = warp_reduce_sum<warp_size, (block_size > warp_size || ncols_dst == 1)>(sumf[j]);

         if constexpr (has_fusion) {
             if (use_gate) {
-                sumf_gate[j] = warp_reduce_sum<warp_size>(sumf_gate[j]);
+                sumf_gate[j] = warp_reduce_sum<warp_size, (block_size > warp_size || ncols_dst == 1)>(sumf_gate[j]);
             }
         }

         if (block_size > warp_size) {
-            buf_iw[tid/warp_size] = sumf[j];
-            if constexpr (has_fusion) {
-                if (use_gate) {
-                    buf_iw_gate[tid/warp_size] = sumf_gate[j];
+            // one write per warp
+            if (tid % warp_size == 0) {
+                buf_iw[tid/warp_size] = sumf[j];
+                if constexpr (has_fusion) {
+                    if (use_gate) {
+                        buf_iw_gate[tid/warp_size] = sumf_gate[j];
+                    }
                 }
             }
             __syncthreads();
             if (tid < warp_size) {
                 sumf[j] = buf_iw[tid];
-                sumf[j] = warp_reduce_sum<warp_size>(sumf[j]);
+                sumf[j] = warp_reduce_sum<warp_size, ncols_dst == 1>(sumf[j]);
                 if constexpr (has_fusion) {
                     if (use_gate) {
                         sumf_gate[j] = buf_iw_gate[tid];
-                        sumf_gate[j] = warp_reduce_sum<warp_size>(sumf_gate[j]);
+                        sumf_gate[j] = warp_reduce_sum<warp_size, ncols_dst == 1>(sumf_gate[j]);
                     }
                 }
             }
diff --git a/ggml/src/ggml-cuda/mmvq.cu b/ggml/src/ggml-cuda/mmvq.cu
index 5fa37ae77..fad806d76 100644
--- a/ggml/src/ggml-cuda/mmvq.cu
+++ b/ggml/src/ggml-cuda/mmvq.cu
@@ -904,10 +904,11 @@ static __global__ void mul_mat_vec_q(
                     }
                 }
             }
-            tmp[j][i] = warp_reduce_sum<warp_size>(tmp[j][i]);
+
+            tmp[j][i] = i == 0 ? warp_reduce_sum<warp_size, true>(tmp[j][i]) : warp_reduce_sum<warp_size>(tmp[j][i]);
             if constexpr (has_fusion) {
                 if (use_gate) {
-                    tmp_gate[j][i] = warp_reduce_sum<warp_size>(tmp_gate[j][i]);
+                    tmp_gate[j][i] = i == 0 ? warp_reduce_sum<warp_size, true>(tmp_gate[j][i]) : warp_reduce_sum<warp_size>(tmp_gate[j][i]);
                 }
             }

@@ -1053,10 +1054,10 @@ static __global__ void mul_mat_vec_q_moe(
     // Warp-level reduction only - no shared memory needed
 #pragma unroll
     for (int i = 0; i < c_rows_per_block; ++i) {
-        tmp[i] = warp_reduce_sum<warp_size>(tmp[i]);
+        tmp[i] = i == 0 ? warp_reduce_sum<warp_size, true>(tmp[i]) : warp_reduce_sum<warp_size>(tmp[i]);
         if constexpr (has_fusion) {
             if (use_gate) {
-                tmp_gate[i] = warp_reduce_sum<warp_size>(tmp_gate[i]);
+                tmp_gate[i] = i == 0 ? warp_reduce_sum<warp_size, true>(tmp_gate[i]) : warp_reduce_sum<warp_size>(tmp_gate[i]);
             }
         }
     }
diff --git a/ggml/src/ggml-cuda/norm.cu b/ggml/src/ggml-cuda/norm.cu
index 02cca5afd..f6462713c 100644
--- a/ggml/src/ggml-cuda/norm.cu
+++ b/ggml/src/ggml-cuda/norm.cu
@@ -290,8 +290,8 @@ static __global__ void rms_norm_back_f32(
     }

     // sum up partial sums
-    sum_xx = warp_reduce_sum(sum_xx);
-    sum_xg = warp_reduce_sum(sum_xg);
+    sum_xx = warp_reduce_sum<WARP_SIZE, (block_size > WARP_SIZE)>(sum_xx);
+    sum_xg = warp_reduce_sum<WARP_SIZE, (block_size > WARP_SIZE)>(sum_xg);
     if constexpr (block_size > WARP_SIZE) {
         static_assert(block_size == 1024, "unexpected block_size");
         __shared__ float s_sum_xx[32];
diff --git a/ggml/src/ggml-cuda/quantize.cu b/ggml/src/ggml-cuda/quantize.cu
index 526dabdea..9d89d6a41 100644
--- a/ggml/src/ggml-cuda/quantize.cu
+++ b/ggml/src/ggml-cuda/quantize.cu
@@ -122,7 +122,7 @@ static __global__ void quantize_q8_1(
     const float  d = amax / 127.0f;
     const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);

-    const int sum = warp_reduce_sum<QK8_1>((int) q);
+    const int sum = warp_reduce_sum<QK8_1, true>((int) q);

     y[ib].qs[iqs] = q;

@@ -680,7 +680,7 @@ static __device__ __forceinline__ void quantize_mmq_few_store(const float4 xi, i
     // as in quantize_mmq_q8_1: the sum of the quantized values; the int warp_reduce_sum reduces the whole warp on Ampere+
     [[maybe_unused]] float sum = 0.0f;
     if constexpr (ds_layout != MMQ_Q8_1_DS_LAYOUT_D4) {
-        sum = d*warp_reduce_sum<8>(float(q.x + q.y + q.z + q.w));
+        sum = d*warp_reduce_sum<8, true>(float(q.x + q.y + q.z + q.w));
     }

     const int s = iu / QK8_1; // sub-block
diff --git a/ggml/src/ggml-cuda/ssm-scan.cu b/ggml/src/ggml-cuda/ssm-scan.cu
index b88c9d6ea..10de9aa62 100644
--- a/ggml/src/ggml-cuda/ssm-scan.cu
+++ b/ggml/src/ggml-cuda/ssm-scan.cu
@@ -218,7 +218,7 @@ __global__ void __launch_bounds__(d_state, 1)
         }

         // parallel accumulation for output
-        state_sum = warp_reduce_sum(state_sum);
+        state_sum = warp_reduce_sum<WARP_SIZE, true>(state_sum);

         if (lane == 0) {
             y_warp[i * stride_y] = state_sum;
diff --git a/ggml/src/ggml-cuda/vendors/hip.h b/ggml/src/ggml-cuda/vendors/hip.h
index a43814799..a97233c1e 100644
--- a/ggml/src/ggml-cuda/vendors/hip.h
+++ b/ggml/src/ggml-cuda/vendors/hip.h
@@ -333,3 +333,33 @@ static __device__ __forceinline__ uint32_t ggml_hip_perm(const uint32_t hi, cons
     return __builtin_amdgcn_perm(hi, lo, sel);
 #endif // __has_builtin(__builtin_amdgcn_is_invocable)
 }
+
+// DPP control codes.
+#define GGML_HIP_DPP_QUAD_PERM_1032  0x0b1 // quad_perm:[1,0,3,2]
+#define GGML_HIP_DPP_QUAD_PERM_2301  0x04e // quad_perm:[2,3,0,1]
+#define GGML_HIP_DPP_ROW_HALF_MIRROR 0x141 // row_half_mirror
+#define GGML_HIP_DPP_ROW_ROR_8       0x128 // row_ror:8
+#define GGML_HIP_DPP_ROW_BCAST_15    0x142 // row_bcast:15, GCN/CDNA only
+#define GGML_HIP_DPP_ROW_BCAST_31    0x143 // row_bcast:31, GCN/CDNA only
+#define GGML_HIP_DPP_WAVE_ROR_1      0x13c // wave_ror:1
+
+// DS swizzle mask codes.
+#define GGML_HIP_SWIZZLE_SWAP16      0x401F // XOR16 within each 32-lane group
+
+template <int dpp_ctrl, typename T>
+static __device__ __forceinline__ T ggml_hip_dpp_update(T x) {
+    static_assert(sizeof(T) == 4, "DPP updates require 32-bit values");
+    union { T t; int i; } value;
+    value.t = x;
+    value.i = __builtin_amdgcn_update_dpp(value.i, value.i, dpp_ctrl, 0xf, 0xf, true);
+    return value.t;
+}
+
+template <int swizzle_mask, typename T>
+static __device__ __forceinline__ T ggml_hip_swizzle(T x) {
+    static_assert(sizeof(T) == 4, "DS swizzle requires 32-bit values");
+    union { T t; int i; } value;
+    value.t = x;
+    value.i = __builtin_amdgcn_ds_swizzle(value.i, swizzle_mask);
+    return value.t;
+}
diff --git a/ggml/src/ggml-cuda/wkv.cu b/ggml/src/ggml-cuda/wkv.cu
index 0bf997760..809b2d599 100644
--- a/ggml/src/ggml-cuda/wkv.cu
+++ b/ggml/src/ggml-cuda/wkv.cu
@@ -180,7 +180,7 @@ rwkv_wkv7_f32_t1_warp_row(const int T, const int C, const int H, const float * r
     const float vt  = v[t];
     const float st0 = s0 * _w[lane]             + _k[lane]             * vt + sa * _b[lane];
     const float st1 = s1 * _w[lane + half_head] + _k[lane + half_head] * vt + sa * _b[lane + half_head];
-    const float y   = warp_reduce_sum(st0 * _r[lane] + st1 * _r[lane + half_head]);
+    const float y   = warp_reduce_sum<WARP_SIZE, true>(st0 * _r[lane] + st1 * _r[lane + half_head]);

     dst[T * C + state_base + lane]             = st0;
     dst[T * C + state_base + lane + half_head] = st1;