ggml-cuda.cu 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. static __global__ void dequantize_block_q4_0(const void * vx, float * y) {
  26. const block_q4_0 * x = (const block_q4_0 *) vx;
  27. const int i = blockIdx.x;
  28. const float d = x[i].d;
  29. const uint8_t * pp = x[i].qs;
  30. for (int l = 0; l < QK4_0; l += 2) {
  31. const uint8_t vi = pp[l/2];
  32. const int8_t vi0 = vi & 0xf;
  33. const int8_t vi1 = vi >> 4;
  34. const float v0 = (vi0 - 8)*d;
  35. const float v1 = (vi1 - 8)*d;
  36. y[i*QK4_0 + l + 0] = v0;
  37. y[i*QK4_0 + l + 1] = v1;
  38. }
  39. }
  40. static __global__ void dequantize_block_q4_1(const void * vx, float * y) {
  41. const block_q4_1 * x = (const block_q4_1 *) vx;
  42. const int i = blockIdx.x;
  43. const float d = x[i].d;
  44. const float m = x[i].m;
  45. const uint8_t * pp = x[i].qs;
  46. for (int l = 0; l < QK4_1; l += 2) {
  47. const uint8_t vi = pp[l/2];
  48. const int8_t vi0 = vi & 0xf;
  49. const int8_t vi1 = vi >> 4;
  50. const float v0 = vi0*d + m;
  51. const float v1 = vi1*d + m;
  52. y[i*QK4_1 + l + 0] = v0;
  53. y[i*QK4_1 + l + 1] = v1;
  54. }
  55. }
  56. static __global__ void dequantize_block_q4_2(const void * vx, float * y) {
  57. const block_q4_2 * x = (const block_q4_2 *) vx;
  58. const int i = blockIdx.x;
  59. const float d = x[i].d;
  60. const uint8_t * pp = x[i].qs;
  61. for (int l = 0; l < QK4_2; l += 2) {
  62. const uint8_t vi = pp[l/2];
  63. const int8_t vi0 = vi & 0xf;
  64. const int8_t vi1 = vi >> 4;
  65. const float v0 = (vi0 - 8)*d;
  66. const float v1 = (vi1 - 8)*d;
  67. y[i*QK4_2 + l + 0] = v0;
  68. y[i*QK4_2 + l + 1] = v1;
  69. }
  70. }
  71. extern "C" {
  72. __host__ void dequantize_row_q4_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  73. const int nb = k / QK4_0;
  74. dequantize_block_q4_0<<<nb, 1, 0, stream>>>(vx, y);
  75. }
  76. __host__ void dequantize_row_q4_1_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  77. const int nb = k / QK4_1;
  78. dequantize_block_q4_1<<<nb, 1, 0, stream>>>(vx, y);
  79. }
  80. __host__ void dequantize_row_q4_2_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
  81. const int nb = k / QK4_2;
  82. dequantize_block_q4_2<<<nb, 1, 0, stream>>>(vx, y);
  83. }
  84. }