common.cuh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /**
  2. * llama.cpp - commit 8962422b1c6f9b8b15f5aeaea42600bcc2d44177 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #pragma once
  27. #include "ggml.h"
  28. #include "ggml-cuda.h"
  29. #include <cstdint>
  30. #include <memory>
  31. #if defined(GGML_USE_HIPBLAS)
  32. #define GGML_COMMON_DECL_HIP
  33. #define GGML_COMMON_IMPL_HIP
  34. #else
  35. #define GGML_COMMON_DECL_CUDA
  36. #define GGML_COMMON_IMPL_CUDA
  37. #if defined(GGML_USE_MUSA)
  38. #define GGML_COMMON_DECL_MUSA
  39. #define GGML_COMMON_IMPL_MUSA
  40. #endif
  41. #endif
  42. #include "ggml-common.h"
  43. #include <cstdio>
  44. #include <array>
  45. #include <cassert>
  46. #include <cfloat>
  47. #include <string>
  48. #include <vector>
  49. #if defined(GGML_USE_HIPBLAS)
  50. #include "vendors/hip.h"
  51. #elif defined(GGML_USE_MUSA)
  52. #include "vendors/musa.h"
  53. #else
  54. #include "vendors/cuda.h"
  55. #endif // defined(GGML_USE_HIPBLAS)
  56. #define STRINGIZE_IMPL(...) #__VA_ARGS__
  57. #define STRINGIZE(...) STRINGIZE_IMPL(__VA_ARGS__)
  58. #define WARP_SIZE 32
  59. #define CUDART_HMAX 11070 // CUDA 11.7, min. ver. for which __hmax and __hmax2 are known to work (may be higher than needed)
  60. #define CUDART_HMASK 12000 // CUDA 12.0, min. ver. for half2 -> uint mask comparisons
  61. #define CC_PASCAL 600
  62. #define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
  63. #define CC_VOLTA 700
  64. #define CC_TURING 750
  65. #define CC_AMPERE 800
  66. #define CC_OFFSET_AMD 1000000
  67. #define CC_RDNA1 (CC_OFFSET_AMD + 1010)
  68. #define CC_RDNA2 (CC_OFFSET_AMD + 1030)
  69. #define CC_RDNA3 (CC_OFFSET_AMD + 1100)
  70. #define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
  71. #if defined(_MSC_VER)
  72. #pragma warning(disable: 4244 4267) // possible loss of data
  73. #endif
  74. #define GGML_CUDA_MAX_STREAMS 8
  75. [[noreturn]]
  76. void ggml_cuda_error(const char * stmt, const char * func, const char * file, int line, const char * msg);
  77. #define CUDA_CHECK_GEN(err, success, error_fn) \
  78. do { \
  79. auto err_ = (err); \
  80. if (err_ != (success)) { \
  81. ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
  82. } \
  83. } while (0)
  84. #define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
  85. #if CUDART_VERSION >= 12000 || defined(GGML_USE_MUSA)
  86. static const char * cublas_get_error_str(const cublasStatus_t err) {
  87. return cublasGetStatusString(err);
  88. }
  89. #else
  90. static const char * cublas_get_error_str(const cublasStatus_t err) {
  91. switch (err) {
  92. case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
  93. case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
  94. case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
  95. case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
  96. case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
  97. case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
  98. case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
  99. case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
  100. case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
  101. default: return "unknown error";
  102. }
  103. }
  104. #endif // CUDART_VERSION >= 12000
  105. #define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
  106. #if !defined(GGML_USE_HIPBLAS)
  107. static const char * cu_get_error_str(CUresult err) {
  108. const char * err_str;
  109. cuGetErrorString(err, &err_str);
  110. return err_str;
  111. }
  112. #define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
  113. #endif
  114. #if CUDART_VERSION >= 11100 || defined(GGML_USE_MUSA)
  115. #define GGML_CUDA_ASSUME(x) __builtin_assume(x)
  116. #else
  117. #define GGML_CUDA_ASSUME(x)
  118. #endif // CUDART_VERSION >= 11100
  119. #ifdef GGML_CUDA_F16
  120. typedef half dfloat; // dequantize float
  121. typedef half2 dfloat2;
  122. #else
  123. typedef float dfloat; // dequantize float
  124. typedef float2 dfloat2;
  125. #endif // GGML_CUDA_F16
  126. #if (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) || __CUDA_ARCH__ >= CC_PASCAL
  127. #define FP16_AVAILABLE
  128. #endif // (defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) || __CUDA_ARCH__ >= CC_PASCAL
  129. #if defined(FP16_AVAILABLE) && __CUDA_ARCH__ != 610
  130. #define FAST_FP16_AVAILABLE
  131. #endif // defined(FP16_AVAILABLE) && __CUDA_ARCH__ != 610
  132. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_VOLTA
  133. #define FP16_MMA_AVAILABLE
  134. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_VOLTA
  135. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_TURING
  136. #define INT8_MMA_AVAILABLE
  137. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_TURING
  138. static constexpr bool fast_fp16_available(const int cc) {
  139. return cc >= CC_PASCAL && cc != 610;
  140. }
  141. static constexpr bool fp16_mma_available(const int cc) {
  142. return cc < CC_OFFSET_AMD && cc >= CC_VOLTA;
  143. }
  144. static constexpr bool int8_mma_available(const int cc) {
  145. return cc < CC_OFFSET_AMD && cc >= CC_TURING;
  146. }
  147. [[noreturn]]
  148. static __device__ void no_device_code(
  149. const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
  150. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  151. printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
  152. file_name, line, function_name, arch);
  153. GGML_UNUSED(arch_list);
  154. #else
  155. printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
  156. file_name, line, function_name, arch, arch_list);
  157. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  158. __trap();
  159. GGML_UNUSED(no_device_code); // suppress unused function warning
  160. }
  161. #ifdef __CUDA_ARCH__
  162. #define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
  163. #else
  164. #define NO_DEVICE_CODE //GGML_ABORT("NO_DEVICE_CODE not valid in host code.")
  165. #endif // __CUDA_ARCH__
  166. static __device__ __forceinline__ float warp_reduce_sum(float x) {
  167. #pragma unroll
  168. for (int mask = 16; mask > 0; mask >>= 1) {
  169. x += __shfl_xor_sync(0xffffffff, x, mask, 32);
  170. }
  171. return x;
  172. }
  173. static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
  174. #pragma unroll
  175. for (int mask = 16; mask > 0; mask >>= 1) {
  176. a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
  177. a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
  178. }
  179. return a;
  180. }
  181. static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
  182. #ifdef FP16_AVAILABLE
  183. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  184. #pragma unroll
  185. for (int mask = 16; mask > 0; mask >>= 1) {
  186. const half2 a_other = __shfl_xor_sync(0xffffffff, a, mask, 32);
  187. reinterpret_cast<half&>(a.x) += __low2half(a_other);
  188. reinterpret_cast<half&>(a.y) += __high2half(a_other);
  189. }
  190. return a;
  191. #else
  192. #pragma unroll
  193. for (int mask = 16; mask > 0; mask >>= 1) {
  194. a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
  195. }
  196. return a;
  197. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  198. #else
  199. NO_DEVICE_CODE;
  200. return a;
  201. #endif // FP16_AVAILABLE
  202. }
  203. static __device__ __forceinline__ float warp_reduce_max(float x) {
  204. #pragma unroll
  205. for (int mask = 16; mask > 0; mask >>= 1) {
  206. x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  207. }
  208. return x;
  209. }
  210. static __device__ __forceinline__ half ggml_cuda_hmax(const half a, const half b) {
  211. #ifdef FP16_AVAILABLE
  212. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && CUDART_VERSION < CUDART_HMAX
  213. return __float2half(fmaxf(__half2float(a), __half2float(b)));
  214. #else
  215. return __hmax(a, b);
  216. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && CUDART_VERSION < CUDART_HMAX
  217. #else
  218. NO_DEVICE_CODE;
  219. GGML_UNUSED(b);
  220. return a;
  221. #endif // FP16_AVAILABLE
  222. }
  223. static __device__ __forceinline__ half2 ggml_cuda_hmax2(const half2 a, const half2 b) {
  224. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  225. #if CUDART_VERSION >= CUDART_HMAX
  226. return __hmax2(a, b);
  227. #else
  228. half2 ret;
  229. reinterpret_cast<half&>(ret.x) = __float2half(fmaxf( __low2float(a), __low2float(b)));
  230. reinterpret_cast<half&>(ret.y) = __float2half(fmaxf(__high2float(a), __high2float(b)));
  231. return ret;
  232. #endif // CUDART_VERSION >= CUDART_HMAX
  233. #else
  234. GGML_UNUSED(a);
  235. GGML_UNUSED(b);
  236. NO_DEVICE_CODE;
  237. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
  238. }
  239. static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
  240. #if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  241. #pragma unroll
  242. for (int mask = 16; mask > 0; mask >>= 1) {
  243. x = ggml_cuda_hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
  244. }
  245. return x;
  246. #else
  247. GGML_UNUSED(x);
  248. NO_DEVICE_CODE;
  249. #endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
  250. }
  251. #if CUDART_VERSION < CUDART_HMASK
  252. static __device__ __forceinline__ uint32_t __hgt2_mask(const half2 a, const half2 b) {
  253. const uint32_t mask_low = 0x0000FFFF * (float( __low2half(a)) > float( __low2half(b)));
  254. const uint32_t mask_high = 0xFFFF0000 * (float(__high2half(a)) > float(__high2half(b)));
  255. return mask_low | mask_high;
  256. }
  257. #endif // CUDART_VERSION < CUDART_HMASK
  258. static __device__ __forceinline__ int ggml_cuda_dp4a(const int a, const int b, int c) {
  259. #if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  260. #if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(RDNA2)
  261. c = __builtin_amdgcn_sdot4(a, b, c, false);
  262. #elif defined(RDNA3)
  263. c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
  264. #elif defined(__gfx1010__) || defined(__gfx900__)
  265. int tmp1;
  266. int tmp2;
  267. asm("\n \
  268. v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0 src1_sel:BYTE_0 \n \
  269. v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_1 src1_sel:BYTE_1 \n \
  270. v_add3_u32 %0, %1, %2, %0 \n \
  271. v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_2 src1_sel:BYTE_2 \n \
  272. v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_3 src1_sel:BYTE_3 \n \
  273. v_add3_u32 %0, %1, %2, %0 \n \
  274. "
  275. : "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
  276. : "v"(a), "v"(b)
  277. );
  278. #else
  279. const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
  280. const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
  281. c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
  282. #endif
  283. return c;
  284. #else // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  285. #if __CUDA_ARCH__ >= MIN_CC_DP4A
  286. return __dp4a(a, b, c);
  287. #else // __CUDA_ARCH__ >= MIN_CC_DP4A
  288. const int8_t * a8 = (const int8_t *) &a;
  289. const int8_t * b8 = (const int8_t *) &b;
  290. return c + a8[0]*b8[0] + a8[1]*b8[1] + a8[2]*b8[2] + a8[3]*b8[3];
  291. #endif // __CUDA_ARCH__ >= MIN_CC_DP4A
  292. #endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
  293. }
  294. // TODO: move to ggml-common.h
  295. static constexpr __device__ int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  296. typedef void (*dequantize_kernel_t)(const void * vx, const int64_t ib, const int iqs, dfloat2 & v);
  297. static __device__ __forceinline__ float get_alibi_slope(
  298. const float max_bias, const uint32_t h, const uint32_t n_head_log2, const float m0, const float m1
  299. ) {
  300. if (max_bias <= 0.0f) {
  301. return 1.0f;
  302. }
  303. const float base = h < n_head_log2 ? m0 : m1;
  304. const int exph = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
  305. return powf(base, exph);
  306. }
  307. template <ggml_type type>
  308. struct ggml_cuda_type_traits;
  309. template<>
  310. struct ggml_cuda_type_traits<GGML_TYPE_F16> {
  311. static constexpr int qk = 1;
  312. static constexpr int qr = 1;
  313. };
  314. template<>
  315. struct ggml_cuda_type_traits<GGML_TYPE_Q4_0> {
  316. static constexpr int qk = QK4_0;
  317. static constexpr int qr = QR4_0;
  318. static constexpr int qi = QI4_0;
  319. };
  320. template<>
  321. struct ggml_cuda_type_traits<GGML_TYPE_Q4_1> {
  322. static constexpr int qk = QK4_1;
  323. static constexpr int qr = QR4_1;
  324. static constexpr int qi = QI4_1;
  325. };
  326. template<>
  327. struct ggml_cuda_type_traits<GGML_TYPE_Q5_0> {
  328. static constexpr int qk = QK5_0;
  329. static constexpr int qr = QR5_0;
  330. static constexpr int qi = QI5_0;
  331. };
  332. template<>
  333. struct ggml_cuda_type_traits<GGML_TYPE_Q5_1> {
  334. static constexpr int qk = QK5_1;
  335. static constexpr int qr = QR5_1;
  336. static constexpr int qi = QI5_1;
  337. };
  338. template<>
  339. struct ggml_cuda_type_traits<GGML_TYPE_Q8_0> {
  340. static constexpr int qk = QK8_0;
  341. static constexpr int qr = QR8_0;
  342. static constexpr int qi = QI8_0;
  343. };
  344. template<>
  345. struct ggml_cuda_type_traits<GGML_TYPE_Q2_K> {
  346. static constexpr int qk = QK_K;
  347. static constexpr int qr = QR2_K;
  348. static constexpr int qi = QI2_K;
  349. };
  350. template<>
  351. struct ggml_cuda_type_traits<GGML_TYPE_Q3_K> {
  352. static constexpr int qk = QK_K;
  353. static constexpr int qr = QR3_K;
  354. static constexpr int qi = QI3_K;
  355. };
  356. template<>
  357. struct ggml_cuda_type_traits<GGML_TYPE_Q4_K> {
  358. static constexpr int qk = QK_K;
  359. static constexpr int qr = QR4_K;
  360. static constexpr int qi = QI4_K;
  361. };
  362. template<>
  363. struct ggml_cuda_type_traits<GGML_TYPE_Q5_K> {
  364. static constexpr int qk = QK_K;
  365. static constexpr int qr = QR5_K;
  366. static constexpr int qi = QI5_K;
  367. };
  368. template<>
  369. struct ggml_cuda_type_traits<GGML_TYPE_Q6_K> {
  370. static constexpr int qk = QK_K;
  371. static constexpr int qr = QR6_K;
  372. static constexpr int qi = QI6_K;
  373. };
  374. template<>
  375. struct ggml_cuda_type_traits<GGML_TYPE_IQ2_XXS> {
  376. static constexpr int qk = QK_K;
  377. static constexpr int qr = QR2_XXS;
  378. static constexpr int qi = QI2_XXS;
  379. };
  380. template<>
  381. struct ggml_cuda_type_traits<GGML_TYPE_IQ2_XS> {
  382. static constexpr int qk = QK_K;
  383. static constexpr int qr = QR2_XS;
  384. static constexpr int qi = QI2_XS;
  385. };
  386. template<>
  387. struct ggml_cuda_type_traits<GGML_TYPE_IQ2_S> {
  388. static constexpr int qk = QK_K;
  389. static constexpr int qr = QR2_S;
  390. static constexpr int qi = QI2_S;
  391. };
  392. template<>
  393. struct ggml_cuda_type_traits<GGML_TYPE_IQ3_XXS> {
  394. static constexpr int qk = QK_K;
  395. static constexpr int qr = QR3_XXS;
  396. static constexpr int qi = QI3_XXS;
  397. };
  398. template<>
  399. struct ggml_cuda_type_traits<GGML_TYPE_IQ1_S> {
  400. static constexpr int qk = QK_K;
  401. static constexpr int qr = QR1_S;
  402. static constexpr int qi = QI1_S;
  403. };
  404. template<>
  405. struct ggml_cuda_type_traits<GGML_TYPE_IQ1_M> {
  406. static constexpr int qk = QK_K;
  407. static constexpr int qr = QR1_M;
  408. static constexpr int qi = QI1_M;
  409. };
  410. template<>
  411. struct ggml_cuda_type_traits<GGML_TYPE_IQ4_NL> {
  412. static constexpr int qk = QK4_NL;
  413. static constexpr int qr = QR4_NL;
  414. static constexpr int qi = QI4_NL;
  415. };
  416. template<>
  417. struct ggml_cuda_type_traits<GGML_TYPE_IQ4_XS> {
  418. static constexpr int qk = QK_K;
  419. static constexpr int qr = QR4_XS;
  420. static constexpr int qi = QI4_XS;
  421. };
  422. template<>
  423. struct ggml_cuda_type_traits<GGML_TYPE_IQ3_S> {
  424. static constexpr int qk = QK_K;
  425. static constexpr int qr = QR3_S;
  426. static constexpr int qi = QI3_S;
  427. };
  428. //////////////////////
  429. struct ggml_cuda_device_info {
  430. int device_count;
  431. struct cuda_device_info {
  432. int cc; // compute capability
  433. int nsm; // number of streaming multiprocessors
  434. size_t smpb; // max. shared memory per block
  435. size_t smpbo; // max. shared memory per block (with opt-in)
  436. bool vmm; // virtual memory support
  437. size_t vmm_granularity; // granularity of virtual memory
  438. size_t total_vram;
  439. };
  440. cuda_device_info devices[GGML_CUDA_MAX_DEVICES] = {};
  441. std::array<float, GGML_CUDA_MAX_DEVICES> default_tensor_split = {};
  442. };
  443. const ggml_cuda_device_info & ggml_cuda_info();
  444. void ggml_cuda_set_device(int device);
  445. int ggml_cuda_get_device();
  446. struct ggml_cuda_pool {
  447. virtual ~ggml_cuda_pool() = default;
  448. virtual void * alloc(size_t size, size_t * actual_size) = 0;
  449. virtual void free(void * ptr, size_t size) = 0;
  450. };
  451. template<typename T>
  452. struct ggml_cuda_pool_alloc {
  453. ggml_cuda_pool * pool = nullptr;
  454. T * ptr = nullptr;
  455. size_t actual_size = 0;
  456. ggml_cuda_pool_alloc() = default;
  457. explicit ggml_cuda_pool_alloc(ggml_cuda_pool & pool) : pool(&pool) {
  458. }
  459. ggml_cuda_pool_alloc(ggml_cuda_pool & pool, size_t size) : pool(&pool) {
  460. alloc(size);
  461. }
  462. ~ggml_cuda_pool_alloc() {
  463. if (ptr != nullptr) {
  464. pool->free(ptr, actual_size);
  465. }
  466. }
  467. // size is in number of elements
  468. T * alloc(size_t size) {
  469. GGML_ASSERT(pool != nullptr);
  470. GGML_ASSERT(ptr == nullptr);
  471. ptr = (T *) pool->alloc(size * sizeof(T), &this->actual_size);
  472. return ptr;
  473. }
  474. T * alloc(ggml_cuda_pool & pool, size_t size) {
  475. this->pool = &pool;
  476. return alloc(size);
  477. }
  478. T * get() {
  479. return ptr;
  480. }
  481. ggml_cuda_pool_alloc(const ggml_cuda_pool_alloc &) = delete;
  482. ggml_cuda_pool_alloc(ggml_cuda_pool_alloc &&) = delete;
  483. ggml_cuda_pool_alloc& operator=(const ggml_cuda_pool_alloc &) = delete;
  484. ggml_cuda_pool_alloc& operator=(ggml_cuda_pool_alloc &&) = delete;
  485. };
  486. // backend interface
  487. struct ggml_tensor_extra_gpu {
  488. void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
  489. cudaEvent_t events[GGML_CUDA_MAX_DEVICES][GGML_CUDA_MAX_STREAMS]; // events for synchronizing multiple GPUs
  490. };
  491. #if (CUDART_VERSION >= 12000) && defined(GGML_CUDA_USE_GRAPHS)
  492. #define USE_CUDA_GRAPH
  493. #endif
  494. struct ggml_graph_node_properties {
  495. void * node_address;
  496. ggml_op node_op;
  497. int64_t ne[GGML_MAX_DIMS];
  498. size_t nb[GGML_MAX_DIMS];
  499. void * src_address[GGML_MAX_SRC];
  500. };
  501. struct ggml_cuda_graph {
  502. #ifdef USE_CUDA_GRAPH
  503. ~ggml_cuda_graph() {
  504. if (instance != nullptr) {
  505. CUDA_CHECK(cudaGraphExecDestroy(instance));
  506. }
  507. if (graph != nullptr) {
  508. CUDA_CHECK(cudaGraphDestroy(graph));
  509. }
  510. }
  511. cudaGraph_t graph = nullptr;
  512. cudaGraphExec_t instance = nullptr;
  513. size_t num_nodes = 0;
  514. std::vector<cudaGraphNode_t> nodes;
  515. std::vector<cudaKernelNodeParams> params;
  516. bool disable_due_to_gpu_arch = false;
  517. bool disable_due_to_too_many_updates = false;
  518. bool disable_due_to_failed_graph_capture = false;
  519. int number_consecutive_updates = 0;
  520. std::vector<ggml_graph_node_properties> ggml_graph_properties;
  521. std::vector<char **> updated_kernel_arg;
  522. #endif
  523. };
  524. struct ggml_backend_cuda_context {
  525. int device;
  526. std::string name;
  527. cudaEvent_t copy_event = nullptr;
  528. cudaStream_t streams[GGML_CUDA_MAX_DEVICES][GGML_CUDA_MAX_STREAMS] = { { nullptr } };
  529. cublasHandle_t cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
  530. std::unique_ptr<ggml_cuda_graph> cuda_graph;
  531. explicit ggml_backend_cuda_context(int device) :
  532. device(device),
  533. name(GGML_CUDA_NAME + std::to_string(device)) {
  534. }
  535. ~ggml_backend_cuda_context() {
  536. if (copy_event != nullptr) {
  537. CUDA_CHECK(cudaEventDestroy(copy_event));
  538. }
  539. for (int i = 0; i < GGML_CUDA_MAX_DEVICES; ++i) {
  540. for (int j = 0; j < GGML_CUDA_MAX_STREAMS; ++j) {
  541. if (streams[i][j] != nullptr) {
  542. CUDA_CHECK(cudaStreamDestroy(streams[i][j]));
  543. }
  544. }
  545. if (cublas_handles[i] != nullptr) {
  546. CUBLAS_CHECK(cublasDestroy(cublas_handles[i]));
  547. }
  548. }
  549. }
  550. cudaStream_t stream(int device, int stream) {
  551. if (streams[device][stream] == nullptr) {
  552. ggml_cuda_set_device(device);
  553. CUDA_CHECK(cudaStreamCreateWithFlags(&streams[device][stream], cudaStreamNonBlocking));
  554. }
  555. return streams[device][stream];
  556. }
  557. cudaStream_t stream() {
  558. return stream(device, 0);
  559. }
  560. cublasHandle_t cublas_handle(int device) {
  561. if (cublas_handles[device] == nullptr) {
  562. ggml_cuda_set_device(device);
  563. CUBLAS_CHECK(cublasCreate(&cublas_handles[device]));
  564. CUBLAS_CHECK(cublasSetMathMode(cublas_handles[device], CUBLAS_TF32_TENSOR_OP_MATH));
  565. }
  566. return cublas_handles[device];
  567. }
  568. cublasHandle_t cublas_handle() {
  569. return cublas_handle(device);
  570. }
  571. // pool
  572. std::unique_ptr<ggml_cuda_pool> pools[GGML_CUDA_MAX_DEVICES];
  573. static std::unique_ptr<ggml_cuda_pool> new_pool_for_device(int device);
  574. ggml_cuda_pool & pool(int device) {
  575. if (pools[device] == nullptr) {
  576. pools[device] = new_pool_for_device(device);
  577. }
  578. return *pools[device];
  579. }
  580. ggml_cuda_pool & pool() {
  581. return pool(device);
  582. }
  583. };