Skip to content

Commit 90d8b8d

Browse files
WIP
1 parent fe1c92c commit 90d8b8d

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

ggml/src/ggml-cuda/common.cuh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,26 @@ static __device__ __forceinline__ int ggml_cuda_dp4a(const int a, const int b, i
545545
#endif // defined(GGML_USE_HIP)
546546
}
547547

548+
static __device__ __forceinline__ void ggml_cuda_fma(float & acc, const float v, const float u) {
549+
acc += v*u;
550+
}
551+
552+
static __device__ __forceinline__ void ggml_cuda_fma(float & acc, const half2 v, const half2 u) {
553+
#if defined(GGML_USE_HIP) && defined(GCN)
554+
asm volatile("v_dot2_f32_f16 %0, %1, %2, %0" : "=v"(acc) : "v"(v), "v"(u));
555+
#else
556+
#ifdef FAST_FP16_AVAILABLE
557+
const float2 tmp = __half22float2(v*u);
558+
acc += tmp.x + tmp.y;
559+
#else
560+
const float2 tmpv = __half22float2(v);
561+
const float2 tmpu = __half22float2(u);
562+
acc += tmpv.x * tmpu.x;
563+
acc += tmpv.y * tmpu.y;
564+
#endif // FAST_FP16_AVAILABLE
565+
#endif // defined(GGML_USE_HIP) && defined(GCN)
566+
}
567+
548568
static __device__ __forceinline__ float ggml_cuda_e8m0_to_fp32(uint8_t x) {
549569
#if CUDART_VERSION >= 12080
550570
const nv_bfloat16 e = __nv_cvt_e8m0_to_bf16raw(x);

ggml/src/ggml-cuda/fattn-tile.cu

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,7 @@ static __global__ void flash_attn_tile(
304304
for (int i_KQ_0 = 0; i_KQ_0 < kq_stride; i_KQ_0 += warp_size) {
305305
#pragma unroll
306306
for (int j_KQ_0 = 0; j_KQ_0 < ncols; j_KQ_0 += nwarps) {
307-
#ifdef FAST_FP16_AVAILABLE
308-
const float2 tmp = __half22float2(K_k[i_KQ_0/warp_size] * Q_k[j_KQ_0/nwarps]);
309-
sum[i_KQ_0/warp_size][j_KQ_0/nwarps] += tmp.x + tmp.y;
310-
#else
311-
sum[i_KQ_0/warp_size][j_KQ_0/nwarps] += K_k[i_KQ_0/warp_size] * Q_k[j_KQ_0/nwarps];
312-
#endif // FAST_FP16_AVAILABLE
307+
ggml_cuda_fma(sum[i_KQ_0/warp_size][j_KQ_0/nwarps], K_k[i_KQ_0/warp_size], Q_k[j_KQ_0/nwarps]);
313308
}
314309
}
315310
}

0 commit comments

Comments
 (0)