Commit 9e213cd93 for llama.cpp
commit 9e213cd93379af130455fd4e8f2b80e5efd7a42c
Author: Aman Gupta <amangupta052@gmail.com>
Date: Wed Dec 2 15:06:26 2026 +0800
CUDA: do not use __restrict__ in FA vec helpers with PDL (#33384)
ggml_cuda_memcpy_1 and the vec FA dot product / dequantize helpers take
__restrict__ pointers. Once inlined, nvcc emits the Q loads of
flash_attn_ext_vec as read-only (ld.global.nc) loads and hoists them
above ggml_cuda_pdl_sync() (griddepcontrol.wait). With PDL the vec
kernel calls ggml_cuda_pdl_lc() at its start, so it can then read Q
before the producing kernel has finished.
On GB10 this shows up as a Gemma-4 MTP draft acceptance drop with an
F8_E4M3 KV cache (single-token draft decode uses the F8 vec kernel),
fixed by GGML_CUDA_PDL=0 or by not launching the vec kernel with PDL.
Use GGML_CUDA_RESTRICT for these helpers, as was done for the kernel
pointers in #24030. No vec instance loads global memory before the
dependency wait any more.
Fixes #33126
Assisted-by: Claude Opus 5.5
diff --git a/ggml/src/ggml-cuda/common.cuh b/ggml/src/ggml-cuda/common.cuh
index 5b09dbc57..08b0d07e9 100644
--- a/ggml/src/ggml-cuda/common.cuh
+++ b/ggml/src/ggml-cuda/common.cuh
@@ -155,6 +155,13 @@ static __device__ __forceinline__ void ggml_cuda_pdl_lc() {
#endif // defined(GGML_CUDA_USE_PDL) && __CUDA_ARCH__ >= GGML_CUDA_CC_HOPPER
}
+// PDL and __restrict__ need to be mutually exclusive, see https://github.com/ggml-org/llama.cpp/pull/24030
+# if (defined(GGML_CUDA_USE_PDL) && __CUDA_ARCH__ >= GGML_CUDA_CC_HOPPER)
+# define GGML_CUDA_RESTRICT
+# else
+# define GGML_CUDA_RESTRICT __restrict__
+# endif // defined(GGML_CUDA_USE_PDL) && __CUDA_ARCH__ >= GGML_CUDA_CC_HOPPER
+
#ifdef __CUDA_ARCH_LIST__
constexpr bool ggml_cuda_has_arch_impl(int) {
return false;
@@ -890,8 +897,10 @@ static __device__ __forceinline__ void ggml_cuda_mad(half2 & acc, const half2 v,
// Due to the strict aliasing rule the compiler can do incorrect optimizations if src and dst have different types.
// The function is intended for copies between registers and SRAM/VRAM to make the compiler emit the right instructions.
// If dst and src point at different address spaces then they are guaranteed to not be aliased.
+// With PDL the source may be written by the previous kernel, __restrict__ would let the compiler hoist the load
+// above ggml_cuda_pdl_sync() as a read-only (non-coherent) load, see GGML_CUDA_RESTRICT.
template <int nbytes, int alignment = 0>
-static __device__ __forceinline__ void ggml_cuda_memcpy_1(void * __restrict__ dst, const void * __restrict__ src) {
+static __device__ __forceinline__ void ggml_cuda_memcpy_1(void * GGML_CUDA_RESTRICT dst, const void * GGML_CUDA_RESTRICT src) {
static_assert(
nbytes <= ggml_cuda_get_max_cpy_bytes() || alignment == 0,
"You are misusing the alignment parameter for ggml_cuda_memcpy_1. "
@@ -1603,13 +1612,6 @@ bool ggml_cuda_kernel_can_use_pdl(const void * kernel);
#endif //defined(GGML_CUDA_USE_PDL)
-// PDL and __restrict__ need to be mutually exclusive, see https://github.com/ggml-org/llama.cpp/pull/24030
-# if (defined(GGML_CUDA_USE_PDL) && __CUDA_ARCH__ >= GGML_CUDA_CC_HOPPER)
-# define GGML_CUDA_RESTRICT
-# else
-# define GGML_CUDA_RESTRICT __restrict__
-# endif // defined(GGML_CUDA_USE_PDL) && __CUDA_ARCH__ >= GGML_CUDA_CC_HOPPER
-
template<typename Kernel, typename... Args>
static __inline__ void ggml_cuda_kernel_launch(Kernel kernel, const ggml_cuda_kernel_launch_params & launch_params, Args&&... args) {
#if defined(GGML_CUDA_USE_PDL)
diff --git a/ggml/src/ggml-cuda/fattn-common.cuh b/ggml/src/ggml-cuda/fattn-common.cuh
index 065330903..b54802f9e 100644
--- a/ggml/src/ggml-cuda/fattn-common.cuh
+++ b/ggml/src/ggml-cuda/fattn-common.cuh
@@ -68,7 +68,7 @@ using fattn_kernel_t = void (*)(
const fattn_bias_indexed bias);
typedef float (*vec_dot_KQ_t)(
- const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8 , const void * __restrict__ Q_ds);
+ const char * GGML_CUDA_RESTRICT K_c, const void * GGML_CUDA_RESTRICT Q_v, const int * GGML_CUDA_RESTRICT Q_q8 , const void * GGML_CUDA_RESTRICT Q_ds);
template <typename T_src, typename T_dst>
using fattn_load_tile_t = void (*)(
@@ -117,7 +117,7 @@ static inline ggml_cuda_flash_attn_ext_f16_extra_data ggml_cuda_flash_attn_ext_g
template <int D, int nthreads>
static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_f16(
- const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8 , const void * __restrict__ Q_ds_v) {
+ const char * GGML_CUDA_RESTRICT K_c, const void * GGML_CUDA_RESTRICT Q_v, const int * GGML_CUDA_RESTRICT Q_q8 , const void * GGML_CUDA_RESTRICT Q_ds_v) {
const half2 * K_h2 = (const half2 *) K_c;
GGML_UNUSED(Q_q8);
@@ -147,7 +147,7 @@ static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_f16(
template <int D, int nthreads>
static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_bf16(
- const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8 , const void * __restrict__ Q_ds_v) {
+ const char * GGML_CUDA_RESTRICT K_c, const void * GGML_CUDA_RESTRICT Q_v, const int * GGML_CUDA_RESTRICT Q_q8 , const void * GGML_CUDA_RESTRICT Q_ds_v) {
const nv_bfloat162 * K_bf16 = (const nv_bfloat162 *) K_c;
GGML_UNUSED(Q_q8);
@@ -178,7 +178,7 @@ static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_bf16(
template <int D, int nthreads>
static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_f8_e4m3(
- const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
+ const char * GGML_CUDA_RESTRICT K_c, const void * GGML_CUDA_RESTRICT Q_v, const int * GGML_CUDA_RESTRICT Q_q8, const void * GGML_CUDA_RESTRICT Q_ds_v) {
const uint8_t * K_f8 = (const uint8_t *) K_c;
GGML_UNUSED(Q_q8);
@@ -206,7 +206,7 @@ static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_f8_e4m3(
template<int D, int nthreads>
static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_q4_0(
- const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
+ const char * GGML_CUDA_RESTRICT K_c, const void * GGML_CUDA_RESTRICT Q_v, const int * GGML_CUDA_RESTRICT Q_q8, const void * GGML_CUDA_RESTRICT Q_ds_v) {
const block_q4_0 * K_q4_0 = (const block_q4_0 *) K_c;
GGML_UNUSED(Q_v);
@@ -237,7 +237,7 @@ static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_q4_0(
template<int D, int nthreads>
static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_q4_1(
- const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
+ const char * GGML_CUDA_RESTRICT K_c, const void * GGML_CUDA_RESTRICT Q_v, const int * GGML_CUDA_RESTRICT Q_q8, const void * GGML_CUDA_RESTRICT Q_ds_v) {
const block_q4_1 * K_q4_1 = (const block_q4_1 *) K_c;
GGML_UNUSED(Q_v);
@@ -270,7 +270,7 @@ static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_q4_1(
template<int D, int nthreads>
static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_q5_0(
- const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
+ const char * GGML_CUDA_RESTRICT K_c, const void * GGML_CUDA_RESTRICT Q_v, const int * GGML_CUDA_RESTRICT Q_q8, const void * GGML_CUDA_RESTRICT Q_ds_v) {
const block_q5_0 * K_q5_0 = (const block_q5_0 *) K_c;
GGML_UNUSED(Q_v);
@@ -315,7 +315,7 @@ static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_q5_0(
template<int D, int nthreads>
static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_q5_1(
- const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
+ const char * GGML_CUDA_RESTRICT K_c, const void * GGML_CUDA_RESTRICT Q_v, const int * GGML_CUDA_RESTRICT Q_q8, const void * GGML_CUDA_RESTRICT Q_ds_v) {
const block_q5_1 * K_q5_1 = (const block_q5_1 *) K_c;
GGML_UNUSED(Q_v);
@@ -361,7 +361,7 @@ static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_q5_1(
template <int D, int nthreads>
static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_q8_0(
- const char * __restrict__ K_c, const void * __restrict__ Q_v, const int * __restrict__ Q_q8, const void * __restrict__ Q_ds_v) {
+ const char * GGML_CUDA_RESTRICT K_c, const void * GGML_CUDA_RESTRICT Q_v, const int * GGML_CUDA_RESTRICT Q_q8, const void * GGML_CUDA_RESTRICT Q_ds_v) {
const block_q8_0 * K_q8_0 = (const block_q8_0 *) K_c;
GGML_UNUSED(Q_v);
@@ -389,7 +389,7 @@ static __device__ __forceinline__ float vec_dot_fattn_vec_KQ_q8_0(
template <typename Tds, int ni>
static __device__ __forceinline__ void quantize_q8_1_to_shared(
- const float * __restrict__ x, const float scale, int * __restrict__ yq32, void * __restrict__ yds) {
+ const float * GGML_CUDA_RESTRICT x, const float scale, int * GGML_CUDA_RESTRICT yq32, void * GGML_CUDA_RESTRICT yds) {
float vals[sizeof(int)] = {0.0f};
#pragma unroll
@@ -434,7 +434,7 @@ static __device__ __forceinline__ void quantize_q8_1_to_shared(
typedef void (*dequantize_V_t)(const void *, void *, const int64_t);
template <typename T, int ne>
-static __device__ __forceinline__ void dequantize_V_f16(const void * __restrict__ vx, void * __restrict__ dst, const int64_t i0) {
+static __device__ __forceinline__ void dequantize_V_f16(const void * GGML_CUDA_RESTRICT vx, void * GGML_CUDA_RESTRICT dst, const int64_t i0) {
if constexpr (std::is_same_v<T, half>) {
ggml_cuda_memcpy_1<ne*sizeof(half)>(dst, (const half *) vx + i0);
} else if constexpr (std::is_same_v<T, float>) {
@@ -452,7 +452,7 @@ static __device__ __forceinline__ void dequantize_V_f16(const void * __restrict_
}
template <typename T, int ne>
-static __device__ __forceinline__ void dequantize_V_bf16(const void * __restrict__ vx, void * __restrict__ dst, const int64_t i0) {
+static __device__ __forceinline__ void dequantize_V_bf16(const void * GGML_CUDA_RESTRICT vx, void * GGML_CUDA_RESTRICT dst, const int64_t i0) {
static_assert(std::is_same_v<T, float>, "BF16 V dequantization only supports float output");
static_assert(ne % 2 == 0, "bad ne");
__align__(16) nv_bfloat162 tmp[ne/2];
@@ -465,7 +465,7 @@ static __device__ __forceinline__ void dequantize_V_bf16(const void * __restrict
}
template <typename T, int ne>
-static __device__ __forceinline__ void dequantize_V_f8_e4m3(const void * __restrict__ vx, void * __restrict__ dst, const int64_t i0) {
+static __device__ __forceinline__ void dequantize_V_f8_e4m3(const void * GGML_CUDA_RESTRICT vx, void * GGML_CUDA_RESTRICT dst, const int64_t i0) {
static_assert(ne == 2 || ne == 4, "bad ne");
static_assert(std::is_same_v<T, half> || std::is_same_v<T, float>, "unsupported type");
ggml_fp8_e4m3_t tmp[ne];
@@ -478,7 +478,7 @@ static __device__ __forceinline__ void dequantize_V_f8_e4m3(const void * __restr
}
template <typename T, int ne>
-static __device__ __forceinline__ void dequantize_V_q4_0(const void * __restrict__ vx, void * __restrict__ dst, const int64_t i0) {
+static __device__ __forceinline__ void dequantize_V_q4_0(const void * GGML_CUDA_RESTRICT vx, void * GGML_CUDA_RESTRICT dst, const int64_t i0) {
const block_q4_0 * x = (const block_q4_0 *) vx;
const int64_t ib = i0 / QK4_0;
@@ -523,7 +523,7 @@ static __device__ __forceinline__ void dequantize_V_q4_0(const void * __restrict
}
template <typename T, int ne>
-static __device__ __forceinline__ void dequantize_V_q4_1(const void * __restrict__ vx, void * __restrict__ dst, const int64_t i0) {
+static __device__ __forceinline__ void dequantize_V_q4_1(const void * GGML_CUDA_RESTRICT vx, void * GGML_CUDA_RESTRICT dst, const int64_t i0) {
const block_q4_1 * x = (const block_q4_1 *) vx;
const int64_t ib = i0 / QK4_1;
@@ -564,7 +564,7 @@ static __device__ __forceinline__ void dequantize_V_q4_1(const void * __restrict
}
template <typename T, int ne>
-static __device__ __forceinline__ void dequantize_V_q5_0(const void * __restrict__ vx, void * __restrict__ dst, const int64_t i0) {
+static __device__ __forceinline__ void dequantize_V_q5_0(const void * GGML_CUDA_RESTRICT vx, void * GGML_CUDA_RESTRICT dst, const int64_t i0) {
const block_q5_0 * x = (const block_q5_0 *) vx;
const int64_t ib = i0 / QK5_0;
@@ -615,7 +615,7 @@ static __device__ __forceinline__ void dequantize_V_q5_0(const void * __restrict
}
template <typename T, int ne>
-static __device__ __forceinline__ void dequantize_V_q5_1(const void * __restrict__ vx, void * __restrict__ dst, const int64_t i0) {
+static __device__ __forceinline__ void dequantize_V_q5_1(const void * GGML_CUDA_RESTRICT vx, void * GGML_CUDA_RESTRICT dst, const int64_t i0) {
const block_q5_1 * x = (const block_q5_1 *) vx;
const int64_t ib = i0 / QK5_1;
@@ -666,7 +666,7 @@ static __device__ __forceinline__ void dequantize_V_q5_1(const void * __restrict
}
template <typename T, int ne>
-static __device__ __forceinline__ void dequantize_V_q8_0(const void * __restrict__ vx, void * __restrict__ dst, const int64_t i0) {
+static __device__ __forceinline__ void dequantize_V_q8_0(const void * GGML_CUDA_RESTRICT vx, void * GGML_CUDA_RESTRICT dst, const int64_t i0) {
const block_q8_0 * x = (const block_q8_0 *) vx;
const int64_t ib = i0 / QK8_0;