ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cuda / convert.cuh
| 1 | #pragma once |
| 2 | #include "common.cuh" |
| 3 | |
| 4 | #define CUDA_DEQUANTIZE_BLOCK_SIZE 256 |
| 5 | |
| 6 | template<typename T> |
| 7 | using to_t_cuda_t = void (*)(const void * x, T * y, int64_t k, cudaStream_t stream); |
| 8 | |
| 9 | typedef to_t_cuda_t<float> to_fp32_cuda_t; |
| 10 | typedef to_t_cuda_t<half> to_fp16_cuda_t; |
| 11 | typedef to_t_cuda_t<nv_bfloat16> to_bf16_cuda_t; |
| 12 | |
| 13 | to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type); |
| 14 | |
| 15 | to_bf16_cuda_t ggml_get_to_bf16_cuda(ggml_type type); |
| 16 | |
| 17 | to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type); |
| 18 | |
| 19 | // TODO more general support for non-contiguous inputs |
| 20 | |
| 21 | template<typename T> |
| 22 | using to_t_nc_cuda_t = void (*)(const void * x, T * y, |
| 23 | int64_t ne00, int64_t ne01, int64_t ne02, int64_t ne03, |
| 24 | int64_t s01, int64_t s02, int64_t s03, cudaStream_t stream); |
| 25 | |
| 26 | typedef to_t_nc_cuda_t<float> to_fp32_nc_cuda_t; |
| 27 | typedef to_t_nc_cuda_t<half> to_fp16_nc_cuda_t; |
| 28 | typedef to_t_nc_cuda_t<nv_bfloat16> to_bf16_nc_cuda_t; |
| 29 | |
| 30 | to_fp32_nc_cuda_t ggml_get_to_fp32_nc_cuda(ggml_type type); |
| 31 | to_fp16_nc_cuda_t ggml_get_to_fp16_nc_cuda(ggml_type type); |
| 32 | to_bf16_nc_cuda_t ggml_get_to_bf16_nc_cuda(ggml_type type); |
| 33 | |
| 34 | template<typename dst_t, typename src_t> |
| 35 | __host__ __device__ inline dst_t ggml_cuda_cast(src_t x) { |
| 36 | if constexpr (std::is_same_v<dst_t, src_t>) { |
| 37 | return x; |
| 38 | } else if constexpr(std::is_same_v<dst_t, nv_bfloat16>) { |
| 39 | return __float2bfloat16(float(x)); |
| 40 | } else if constexpr(std::is_same_v<src_t, nv_bfloat16>) { |
| 41 | return __bfloat162float(x); |
| 42 | } else if constexpr(std::is_same_v<src_t, float2> && std::is_same_v<dst_t, half2>) { |
| 43 | return __float22half2_rn(x); |
| 44 | } else if constexpr(std::is_same_v<src_t, nv_bfloat162> && std::is_same_v<dst_t, float2>) { |
| 45 | #ifdef GGML_USE_HIP |
| 46 | return make_float2(__bfloat162float(__low2bfloat16(x)), __bfloat162float(__high2bfloat16(x))); |
| 47 | #else |
| 48 | #if !defined(GGML_USE_MUSA) && __CUDA_ARCH__ >= 800 |
| 49 | return __bfloat1622float2(x); |
| 50 | #else |
| 51 | return make_float2(__bfloat162float(x.x), __bfloat162float(x.y)); |
| 52 | #endif // !defined(GGML_USE_MUSA) && __CUDA_ARCH__ >= 800 |
| 53 | #endif // GGML_USE_HIP |
| 54 | } else if constexpr(std::is_same_v<src_t, float2> && std::is_same_v<dst_t, nv_bfloat162>) { |
| 55 | // bypass compile error on cuda 12.0.1 |
| 56 | #ifdef GGML_USE_HIP |
| 57 | return __float22bfloat162_rn(x); |
| 58 | #else |
| 59 | return {x.x, x.y}; |
| 60 | #endif // GGML_USE_HIP |
| 61 | } else if constexpr(std::is_same_v<dst_t, int32_t>) { |
| 62 | return int32_t(x); |
| 63 | } else { |
| 64 | return float(x); |
| 65 | } |
| 66 | } |