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