ggml-cuda.cu 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <stdint.h>
  2. #include <cuda_fp16.h>
  3. #include "ggml-cuda.h"
  4. typedef uint16_t ggml_fp16_t;
  5. static_assert(sizeof(__half) == sizeof(ggml_fp16_t), "wrong fp16 size");
  6. #define QK4_0 32
  7. typedef struct {
  8. float d; // delta
  9. uint8_t qs[QK4_0 / 2]; // nibbles / quants
  10. } block_q4_0;
  11. static_assert(sizeof(block_q4_0) == sizeof(float) + QK4_0 / 2, "wrong q4_0 block size/padding");
  12. #define QK4_1 32
  13. typedef struct {
  14. float d; // delta
  15. float m; // min
  16. uint8_t qs[QK4_1 / 2]; // nibbles / quants
  17. } block_q4_1;
  18. static_assert(sizeof(block_q4_1) == sizeof(float) * 2 + QK4_1 / 2, "wrong q4_1 block size/padding");
  19. #define QK4_2 16
  20. typedef struct {
  21. __half d; // delta
  22. uint8_t qs[QK4_2 / 2]; // nibbles / quants
  23. } block_q4_2;
  24. static_assert(sizeof(block_q4_2) == sizeof(ggml_fp16_t) + QK4_2 / 2, "wrong q4_2 block size/padding");
  25. #define QK4_3 16
  26. typedef struct {
  27. __half d; // delta
  28. __half m; // min
  29. uint8_t qs[QK4_3 / 2]; // nibbles / quants
  30. } block_q4_3;
  31. static_assert(sizeof(block_q4_3) == 2 * sizeof(ggml_fp16_t) + QK4_3 / 2, "wrong q4_3 block size/padding");
  32. static __global__ void dequantize_block_q4_0(const void * vx, float * y) {
  33. const block_q4_0 * x = (const block_q4_0 *) vx;
  34. const int i = blockIdx.x;
  35. const float d = x[i].d;
  36. const uint8_t * pp = x[i].qs;
  37. for (int l = 0; l < QK4_0; l += 2) {
  38. const uint8_t vi = pp[l/2];
  39. const int8_t vi0 = vi & 0xf;
  40. const int8_t vi1 = vi >> 4;
  41. const float v0 = (vi0 - 8)*d;
  42. const float v1 = (vi1 - 8)*d;
  43. y[i*QK4_0 + l + 0] = v0;
  44. y[i*QK4_0 + l + 1] = v1;
  45. }
  46. }
  47. static __global__ void dequantize_block_q4_1(const void * vx, float * y) {
  48. const block_q4_1 * x = (const block_q4_1 *) vx;
  49. const int i = blockIdx.x;
  50. const float d = x[i].d;
  51. const float m = x[i].m;
  52. const uint8_t * pp = x[i].qs;
  53. for (int l = 0; l < QK4_1; l += 2) {
  54. const uint8_t vi = pp[l/2];
  55. const int8_t vi0 = vi & 0xf;
  56. const int8_t vi1 = vi >> 4;
  57. const float v0 = vi0*d + m;
  58. const float v1 = vi1*d + m;
  59. y[i*QK4_1 + l + 0] = v0;
  60. y[i*QK4_1 + l + 1] = v1;
  61. }
  62. }
  63. static __global__ void dequantize_block_q4_2(const void * vx, float * y) {
  64. const block_q4_2 * x = (const block_q4_2 *) vx;
  65. const int i = blockIdx.x;
  66. const float d = x[i].d;
  67. const uint8_t * pp = x[i].qs;
  68. for (int l = 0; l < QK4_2; l += 2) {
  69. const uint8_t vi = pp[l/2];
  70. const int8_t vi0 = vi & 0xf;
  71. const int8_t vi1 = vi >> 4;
  72. const float v0 = (vi0 - 8)*d;
  73. const float v1 = (vi1 - 8)*d;
  74. y[i*QK4_2 + l + 0] = v0;
  75. y[i*QK4_2 + l + 1] = v1;
  76. }
  77. }
  78. static __global__ void dequantize_block_q4_3(const void * vx, float * y) {
  79. const block_q4_3 * x = (const block_q4_3 *) vx;
  80. const int i = blockIdx.x;
  81. const float d = x[i].d;
  82. const float m = x[i].m;
  83. const uint8_t * pp = x[i].qs;
  84. for (int l = 0; l < QK4_3; l += 2) {
  85. const uint8_t vi = pp[l/2];
  86. const int8_t vi0 = vi & 0xf;
  87. const int8_t vi1 = vi >> 4;
  88. const float v0 = vi0*d + m;
  89. const float v1 = vi1*d + m;
  90. y[i*QK4_3 + l + 0] = v0;
  91. y[i*QK4_3 + l + 1] = v1;
  92. }
  93. }
  94. extern "C" {
  95. __host__ void dequantize_row_q4_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  96. const int nb = k / QK4_0;
  97. dequantize_block_q4_0<<<nb, 1, 0, stream>>>(vx, y);
  98. }
  99. __host__ void dequantize_row_q4_1_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  100. const int nb = k / QK4_1;
  101. dequantize_block_q4_1<<<nb, 1, 0, stream>>>(vx, y);
  102. }
  103. __host__ void dequantize_row_q4_2_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  104. const int nb = k / QK4_2;
  105. dequantize_block_q4_2<<<nb, 1, 0, stream>>>(vx, y);
  106. }
  107. __host__ void dequantize_row_q4_3_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  108. const int nb = k / QK4_3;
  109. dequantize_block_q4_3<<<nb, 1, 0, stream>>>(vx, y);
  110. }
  111. }