Skip to content

Commit 4178180

Browse files
committed
CUDA/HIP: add expicit conversion operator to support older versions of rocm
1 parent ef249d9 commit 4178180

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

ggml/src/ggml-cuda/convert.cu

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __
3131
dequantize_kernel(vx, ib, iqs, v);
3232

3333
const int64_t iy0 = ((i03*ne02 + i02)*ne01 + i01)*ne00 + iybs + iqs;
34-
y[iy0 + 0] = float(v.x);
35-
y[iy0 + y_offset] = float(v.y);
34+
y[iy0 + 0] = ggml_cuda_convert_val<float, dst_t>(v.x);
35+
y[iy0 + y_offset] = ggml_cuda_convert_val<float, dst_t>(v.y);
3636
}
3737

3838
template <bool need_check>
@@ -630,7 +630,7 @@ static __global__ void convert_unary(
630630

631631
const int64_t ix = i03*s03 + i02*s02 + i01*s01 + i00;
632632
const int64_t iy = ((i03*ne02 + i02)*ne01 + i01)*ne00 + i00;
633-
y[iy] = float(x[ix]);
633+
y[iy] = ggml_cuda_convert_val<src_t, dst_t>(x[ix]);
634634
}
635635

636636
template <typename src_t, typename dst_t>

ggml/src/ggml-cuda/convert.cuh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,28 @@ typedef to_t_nc_cuda_t<nv_bfloat16> to_bf16_nc_cuda_t;
2929
to_fp32_nc_cuda_t ggml_get_to_fp32_nc_cuda(ggml_type type);
3030
to_fp16_nc_cuda_t ggml_get_to_fp16_nc_cuda(ggml_type type);
3131
to_bf16_nc_cuda_t ggml_get_to_bf16_nc_cuda(ggml_type type);
32+
33+
34+
template<typename src_t, typename dest_t>
35+
__host__ __device__ inline dest_t ggml_cuda_convert_val(src_t x)
36+
{
37+
return float(x);
38+
}
39+
40+
template<>
41+
__host__ __device__ inline float ggml_cuda_convert_val<nv_bfloat16, float>(nv_bfloat16 x)
42+
{
43+
return __bfloat162float(x);
44+
}
45+
46+
template<>
47+
__host__ __device__ inline nv_bfloat16 ggml_cuda_convert_val<nv_bfloat16, nv_bfloat16>(nv_bfloat16 x)
48+
{
49+
return x;
50+
}
51+
52+
template<>
53+
__host__ __device__ inline nv_bfloat16 ggml_cuda_convert_val<float, nv_bfloat16>(float x)
54+
{
55+
return __float2bfloat16(x);
56+
}

ggml/src/ggml-cuda/cpy-utils.cuh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#pragma once
22

33
#include "ggml-common.h"
4+
#include "convert.cuh"
45

56
template<typename src_t, typename dst_t>
67
static __device__ __forceinline__ void convert_flt(const src_t * src, dst_t * dst) {
78
if constexpr (std::is_same_v<src_t, dst_t>) {
89
*dst = *src;
910
} else {
10-
*dst = float(*src);
11+
*dst = ggml_cuda_convert_val<src_t, dst_t>(*src);
1112
}
1213
}
1314

0 commit comments

Comments
 (0)