Commit 8e821c729 for llama.cpp
commit 8e821c7292c94a291e9b8e8ae4b4bf0debcb0e5b
Author: thelittlefireman <5165783+thelittlefireman@users.noreply.github.com>
Date: Sun Dec 6 18:30:23 2026 +0100
HIP: implement a dpp path for warp_prefix_inclusive_sum (#32867)
* ggml-cuda: hip: change warp_prefix_inclusive_sum to dpp
* ggml-cuda: hip: fix DPP identity permutation encoding
* Update ggml/src/ggml-cuda/common.cuh
Co-authored-by: uvos <devnull@uvos.xyz>
* Update ggml/src/ggml-cuda/common.cuh
Co-authored-by: uvos <devnull@uvos.xyz>
* Update ggml/src/ggml-cuda/common.cuh
Co-authored-by: uvos <devnull@uvos.xyz>
* ggml-cuda: assert warp prefix sum width
Assisted-by: ChatGPT
---------
Co-authored-by: uvos <devnull@uvos.xyz>
diff --git a/ggml/src/ggml-cuda/common.cuh b/ggml/src/ggml-cuda/common.cuh
index 08b0d07e9..811a8b444 100644
--- a/ggml/src/ggml-cuda/common.cuh
+++ b/ggml/src/ggml-cuda/common.cuh
@@ -637,6 +637,33 @@ static __device__ __forceinline__ float warp_reduce_max(float x) {
template<typename T, int width = WARP_SIZE>
static __device__ __forceinline__ T warp_prefix_inclusive_sum(T x) {
+ static_assert(width <= ggml_cuda_get_physical_warp_size(), "width must not exceed the physical warp size");
+#if defined(GGML_USE_HIP)
+ if constexpr (std::is_same_v<T, float> && (width == 32 || width == 64)) {
+ x += ggml_hip_dpp_update<GGML_HIP_DPP_ROW_SHR_1, 0xf, false>(x);
+ x += ggml_hip_dpp_update<GGML_HIP_DPP_ROW_SHR_2, 0xf, false>(x);
+ x += ggml_hip_dpp_update<GGML_HIP_DPP_ROW_SHR_4, 0xf, false>(x);
+ x += ggml_hip_dpp_update<GGML_HIP_DPP_ROW_SHR_8, 0xf, false>(x);
+
+#if defined(GCN) || defined(CDNA)
+ x += ggml_hip_dpp_update<GGML_HIP_DPP_ROW_BCAST_15, 0xa, false>(x);
+ if constexpr (width == 64) {
+ x += ggml_hip_dpp_update<GGML_HIP_DPP_ROW_BCAST_31, 0xc, false>(x);
+ }
+#else
+ // Propagate the previous 16-lane row sum to rows 1 and 3.
+ const float sum_16 = __uint_as_float(__builtin_amdgcn_permlanex16(0u, __float_as_uint(x), 0xffffffffu, 0xffffffffu, false, false));
+ x += ggml_hip_dpp_update<GGML_HIP_DPP_QUAD_PERM_ID, 0xa, false>(sum_16);
+
+ if constexpr (width == 64) {
+ // Propagate the lower 32-lane sum to rows 2 and 3.
+ const float sum_32 = __int_as_float(__builtin_amdgcn_readlane(__float_as_int(x), 31));
+ x += ggml_hip_dpp_update<GGML_HIP_DPP_QUAD_PERM_ID, 0xc, false>(sum_32);
+ }
+#endif // defined(GCN) || defined(CDNA)
+ return x;
+ }
+#endif // defined(GGML_USE_HIP)
const int lane_id = threadIdx.x % width;
#pragma unroll
for (int offset = 1; offset < width; offset <<= 1) {
diff --git a/ggml/src/ggml-cuda/vendors/hip.h b/ggml/src/ggml-cuda/vendors/hip.h
index e4382db86..da36b4bc7 100644
--- a/ggml/src/ggml-cuda/vendors/hip.h
+++ b/ggml/src/ggml-cuda/vendors/hip.h
@@ -337,21 +337,28 @@ static __device__ __forceinline__ uint32_t ggml_hip_perm(const uint32_t hi, cons
// 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_QUAD_PERM_ID 0x0e4 // quad_perm:[0,1,2,3]
#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_ROW_SHR_1 0x111 // row_shr:1
+#define GGML_HIP_DPP_ROW_SHR_2 0x112 // row_shr:2
+#define GGML_HIP_DPP_ROW_SHR_4 0x114 // row_shr:4
+#define GGML_HIP_DPP_ROW_SHR_8 0x118 // row_shr:8
#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>
+// Lanes excluded from the DPP operation (row_mask, out-of-bounds) receive a fallback value: x, or 0 when mask_fallback_is_x is false.
+template <int dpp_ctrl, int row_mask = 0xf, bool mask_fallback_is_x = true, 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);
+ const int fallback = mask_fallback_is_x ? value.i : 0;
+ value.i = __builtin_amdgcn_update_dpp(fallback, value.i, dpp_ctrl, row_mask, 0xf, true);
return value.t;
}