ggml-cuda.cu 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <cuda_fp16.h>
  4. #include <atomic>
  5. #include "ggml-cuda.h"
  6. typedef uint16_t ggml_fp16_t;
  7. static_assert(sizeof(__half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  8. #define QK4_0 32
  9. typedef struct {
  10. float d; // delta
  11. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  12. } block_q4_0;
  13. static_assert(sizeof(block_q4_0) == sizeof(float) + QK4_0 / 2, "wrong q4_0 block size/padding");
  14. #define QK4_1 32
  15. typedef struct {
  16. float d; // delta
  17. float m; // min
  18. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  19. } block_q4_1;
  20. static_assert(sizeof(block_q4_1) == sizeof(float) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  21. #define QK4_2 16
  22. typedef struct {
  23. __half d; // delta
  24. uint8_t qs[QK4_2 / 2]; // nibbles / quants
  25. } block_q4_2;
  26. static_assert(sizeof(block_q4_2) == sizeof(ggml_fp16_t) + QK4_2 / 2, "wrong q4_2 block size/padding");
  27. #define QK4_3 16
  28. typedef struct {
  29. __half d; // delta
  30. __half m; // min
  31. uint8_t qs[QK4_3 / 2]; // nibbles / quants
  32. } block_q4_3;
  33. static_assert(sizeof(block_q4_3) == 2 * sizeof(ggml_fp16_t) + QK4_3 / 2, "wrong q4_3 block size/padding");
  34. #define QK5_0 32
  35. typedef struct {
  36. __half d; // delta
  37. uint8_t qh[4]; // 5-th bit of quants
  38. uint8_t qs[QK5_0 / 2]; // nibbles / quants
  39. } block_q5_0;
  40. static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
  41. #define QK5_1 32
  42. typedef struct {
  43. __half d; // delta
  44. __half m; // min
  45. uint32_t qh; // 5-th bit of quants
  46. uint8_t qs[QK5_1 / 2]; // nibbles / quants
  47. } block_q5_1;
  48. static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
  49. #define QK8_0 32
  50. typedef struct {
  51. float d; // delta
  52. int8_t qs[QK8_0]; // quants
  53. } block_q8_0;
  54. static_assert(sizeof(block_q8_0) == sizeof(float) + QK8_0, "wrong q8_0 block size/padding");
  55. static __global__ void dequantize_block_q4_0(const void * vx, float * y) {
  56. const block_q4_0 * x = (const block_q4_0 *) vx;
  57. const int i = blockIdx.x;
  58. const float d = x[i].d;
  59. const uint8_t * pp = x[i].qs;
  60. for (int l = 0; l < QK4_0; l += 2) {
  61. const uint8_t vi = pp[l/2];
  62. const int8_t vi0 = vi & 0xf;
  63. const int8_t vi1 = vi >> 4;
  64. const float v0 = (vi0 - 8)*d;
  65. const float v1 = (vi1 - 8)*d;
  66. y[i*QK4_0 + l + 0] = v0;
  67. y[i*QK4_0 + l + 1] = v1;
  68. }
  69. }
  70. static __global__ void dequantize_block_q4_1(const void * vx, float * y) {
  71. const block_q4_1 * x = (const block_q4_1 *) vx;
  72. const int i = blockIdx.x;
  73. const float d = x[i].d;
  74. const float m = x[i].m;
  75. const uint8_t * pp = x[i].qs;
  76. for (int l = 0; l < QK4_1; l += 2) {
  77. const uint8_t vi = pp[l/2];
  78. const int8_t vi0 = vi & 0xf;
  79. const int8_t vi1 = vi >> 4;
  80. const float v0 = vi0*d + m;
  81. const float v1 = vi1*d + m;
  82. y[i*QK4_1 + l + 0] = v0;
  83. y[i*QK4_1 + l + 1] = v1;
  84. }
  85. }
  86. static __global__ void dequantize_block_q4_2(const void * vx, float * y) {
  87. const block_q4_2 * x = (const block_q4_2 *) vx;
  88. const int i = blockIdx.x;
  89. const float d = x[i].d;
  90. const uint8_t * pp = x[i].qs;
  91. for (int l = 0; l < QK4_2; l += 2) {
  92. const uint8_t vi = pp[l/2];
  93. const int8_t vi0 = vi & 0xf;
  94. const int8_t vi1 = vi >> 4;
  95. const float v0 = (vi0 - 8)*d;
  96. const float v1 = (vi1 - 8)*d;
  97. y[i*QK4_2 + l + 0] = v0;
  98. y[i*QK4_2 + l + 1] = v1;
  99. }
  100. }
  101. static __global__ void dequantize_block_q4_3(const void * vx, float * y) {
  102. const block_q4_3 * x = (const block_q4_3 *) vx;
  103. const int i = blockIdx.x;
  104. const float d = x[i].d;
  105. const float m = x[i].m;
  106. const uint8_t * pp = x[i].qs;
  107. for (int l = 0; l < QK4_3; l += 2) {
  108. const uint8_t vi = pp[l/2];
  109. const int8_t vi0 = vi & 0xf;
  110. const int8_t vi1 = vi >> 4;
  111. const float v0 = vi0*d + m;
  112. const float v1 = vi1*d + m;
  113. y[i*QK4_3 + l + 0] = v0;
  114. y[i*QK4_3 + l + 1] = v1;
  115. }
  116. }
  117. static __global__ void dequantize_block_q5_0(const void * vx, float * y) {
  118. const block_q5_0 * x = (const block_q5_0 *) vx;
  119. const int i = blockIdx.x;
  120. const float d = x[i].d;
  121. const uint8_t * pp = x[i].qs;
  122. uint32_t qh;
  123. memcpy(&qh, x[i].qh, sizeof(qh));
  124. for (int l = 0; l < QK5_0; l += 2) {
  125. const uint8_t vi = pp[l/2];
  126. const int8_t vh0 = ((qh & (1 << (l + 0))) >> (l + 0)) << 4;
  127. const int8_t vh1 = ((qh & (1 << (l + 1))) >> (l + 1)) << 4;
  128. const int8_t vi0 = ((vi & 0xf) | vh0);
  129. const int8_t vi1 = ((vi >> 4) | vh1);
  130. const float v0 = (vi0 - 16)*d;
  131. const float v1 = (vi1 - 16)*d;
  132. y[i*QK5_0 + l + 0] = v0;
  133. y[i*QK5_0 + l + 1] = v1;
  134. }
  135. }
  136. static __global__ void dequantize_block_q5_1(const void * vx, float * y) {
  137. const block_q5_1 * x = (const block_q5_1 *) vx;
  138. const int i = blockIdx.x;
  139. const float d = x[i].d;
  140. const float m = x[i].m;
  141. const uint8_t * pp = x[i].qs;
  142. const uint32_t qh = x[i].qh;
  143. for (int l = 0; l < QK5_1; l += 2) {
  144. const uint8_t vi = pp[l/2];
  145. const int8_t vh0 = ((qh & (1 << (l + 0))) >> (l + 0)) << 4;
  146. const int8_t vh1 = ((qh & (1 << (l + 1))) >> (l + 1)) << 4;
  147. const int8_t vi0 = (vi & 0xf) | vh0;
  148. const int8_t vi1 = (vi >> 4) | vh1;
  149. const float v0 = vi0*d + m;
  150. const float v1 = vi1*d + m;
  151. y[i*QK5_1 + l + 0] = v0;
  152. y[i*QK5_1 + l + 1] = v1;
  153. }
  154. }
  155. static __global__ void dequantize_block_q8_0(const void * vx, float * y) {
  156. const block_q8_0 * x = (const block_q8_0 *) vx;
  157. const int i = blockIdx.x;
  158. const float d = x[i].d;
  159. const int8_t * pp = x[i].qs;
  160. for (int l = 0; l < QK8_0; l++) {
  161. const int8_t vi = pp[l];
  162. y[i*QK8_0 + l] = vi*d;
  163. }
  164. }
  165. void dequantize_row_q4_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  166. const int nb = k / QK4_0;
  167. dequantize_block_q4_0<<<nb, 1, 0, stream>>>(vx, y);
  168. }
  169. void dequantize_row_q4_1_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  170. const int nb = k / QK4_1;
  171. dequantize_block_q4_1<<<nb, 1, 0, stream>>>(vx, y);
  172. }
  173. void dequantize_row_q4_2_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  174. const int nb = k / QK4_2;
  175. dequantize_block_q4_2<<<nb, 1, 0, stream>>>(vx, y);
  176. }
  177. void dequantize_row_q4_3_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  178. const int nb = k / QK4_3;
  179. dequantize_block_q4_3<<<nb, 1, 0, stream>>>(vx, y);
  180. }
  181. void dequantize_row_q5_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  182. const int nb = k / QK5_0;
  183. dequantize_block_q5_0<<<nb, 1, 0, stream>>>(vx, y);
  184. }
  185. void dequantize_row_q5_1_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  186. const int nb = k / QK5_1;
  187. dequantize_block_q5_1<<<nb, 1, 0, stream>>>(vx, y);
  188. }
  189. void dequantize_row_q8_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  190. const int nb = k / QK8_0;
  191. dequantize_block_q8_0<<<nb, 1, 0, stream>>>(vx, y);
  192. }
  193. // buffer pool for cuda
  194. #define MAX_CUDA_BUFFERS 16
  195. struct scoped_spin_lock {
  196. std::atomic_flag& lock;
  197. scoped_spin_lock(std::atomic_flag& lock) : lock(lock) {
  198. while (lock.test_and_set(std::memory_order_acquire)) {
  199. ; // spin
  200. }
  201. }
  202. ~scoped_spin_lock() {
  203. lock.clear(std::memory_order_release);
  204. }
  205. scoped_spin_lock(const scoped_spin_lock&) = delete;
  206. scoped_spin_lock& operator=(const scoped_spin_lock&) = delete;
  207. };
  208. struct cuda_buffer {
  209. void * ptr = nullptr;
  210. size_t size = 0;
  211. };
  212. static cuda_buffer g_cuda_buffer_pool[MAX_CUDA_BUFFERS];
  213. static std::atomic_flag g_cuda_pool_lock = ATOMIC_FLAG_INIT;
  214. void * ggml_cuda_pool_malloc(size_t size, size_t * actual_size) {
  215. scoped_spin_lock lock(g_cuda_pool_lock);
  216. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  217. cuda_buffer& b = g_cuda_buffer_pool[i];
  218. if (b.size >= size && b.ptr != nullptr) {
  219. void * ptr = b.ptr;
  220. *actual_size = b.size;
  221. b.ptr = nullptr;
  222. b.size = 0;
  223. return ptr;
  224. }
  225. }
  226. void * ptr;
  227. CUDA_CHECK(cudaMalloc((void **) &ptr, size));
  228. *actual_size = size;
  229. return ptr;
  230. }
  231. void ggml_cuda_pool_free(void * ptr, size_t size) {
  232. scoped_spin_lock lock(g_cuda_pool_lock);
  233. for (int i = 0; i < MAX_CUDA_BUFFERS; ++i) {
  234. cuda_buffer& b = g_cuda_buffer_pool[i];
  235. if (b.ptr == nullptr) {
  236. b.ptr = ptr;
  237. b.size = size;
  238. return;
  239. }
  240. }
  241. fprintf(stderr, "WARNING: cuda buffer pool full, increase MAX_CUDA_BUFFERS\n");
  242. CUDA_CHECK(cudaFree(ptr));
  243. }
  244. cublasHandle_t g_cublasH = NULL;
  245. cudaStream_t g_cudaStream = NULL;
  246. void ggml_init_cublas(void) {
  247. if (g_cublasH == NULL) {
  248. // create cublas handle, bind a stream
  249. CUBLAS_CHECK(cublasCreate(&g_cublasH));
  250. CUDA_CHECK(cudaStreamCreateWithFlags(&g_cudaStream, cudaStreamNonBlocking));
  251. CUBLAS_CHECK(cublasSetStream(g_cublasH, g_cudaStream));
  252. // configure logging to stdout
  253. // CUBLAS_CHECK(cublasLoggerConfigure(1, 1, 0, NULL));
  254. }
  255. }