Skip to content

Commit 3839a9a

Browse files
committed
ggml : adding Q5_0 mode
1 parent 2e01d68 commit 3839a9a

File tree

6 files changed

+339
-7
lines changed

6 files changed

+339
-7
lines changed

ggml-cuda.cu

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ typedef struct {
3737
} block_q4_3;
3838
static_assert(sizeof(block_q4_3) == 2 * sizeof(ggml_fp16_t) + QK4_3 / 2, "wrong q4_3 block size/padding");
3939

40+
#define QK5_0 32
41+
typedef struct {
42+
__half d; // delta
43+
uint32_t qh; // 5-th bit of quants
44+
uint8_t qs[QK5_0 / 2]; // nibbles / quants
45+
} block_q5_0;
46+
static_assert(sizeof(block_q5_0) == sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
47+
4048
#define QK5_1 32
4149
typedef struct {
4250
__half d; // delta
@@ -147,6 +155,34 @@ static __global__ void dequantize_block_q4_3(const void * vx, float * y) {
147155
}
148156
}
149157

158+
static __global__ void dequantize_block_q5_0(const void * vx, float * y) {
159+
const block_q5_0 * x = (const block_q5_0 *) vx;
160+
161+
const int i = blockIdx.x;
162+
163+
const float d = x[i].d;
164+
165+
const uint8_t * pp = x[i].qs;
166+
167+
const uint32_t qh = x[i].qh;
168+
169+
for (int l = 0; l < QK5_0; l += 2) {
170+
const uint8_t vi = pp[l/2];
171+
172+
const int8_t vh0 = ((qh & (1 << (l + 0))) >> (l + 0)) << 4;
173+
const int8_t vh1 = ((qh & (1 << (l + 1))) >> (l + 1)) << 4;
174+
175+
const int8_t vi0 = ((vi & 0xf) | vh0);
176+
const int8_t vi1 = ((vi >> 4) | vh1);
177+
178+
const float v0 = (vi0 - 16)*d;
179+
const float v1 = (vi1 - 16)*d;
180+
181+
y[i*QK5_0 + l + 0] = v0;
182+
y[i*QK5_0 + l + 1] = v1;
183+
}
184+
}
185+
150186
static __global__ void dequantize_block_q5_1(const void * vx, float * y) {
151187
const block_q5_1 * x = (const block_q5_1 *) vx;
152188

@@ -212,6 +248,11 @@ void dequantize_row_q4_3_cuda(const void * vx, float * y, int k, cudaStream_t st
212248
dequantize_block_q4_3<<<nb, 1, 0, stream>>>(vx, y);
213249
}
214250

251+
void dequantize_row_q5_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
252+
const int nb = k / QK5_0;
253+
dequantize_block_q5_0<<<nb, 1, 0, stream>>>(vx, y);
254+
}
255+
215256
void dequantize_row_q5_1_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
216257
const int nb = k / QK5_1;
217258
dequantize_block_q5_1<<<nb, 1, 0, stream>>>(vx, y);

ggml-cuda.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void dequantize_row_q4_0_cuda(const void * vx, float * y, int k, cudaStream_t st
3535
void dequantize_row_q4_1_cuda(const void * vx, float * y, int k, cudaStream_t stream);
3636
void dequantize_row_q4_2_cuda(const void * vx, float * y, int k, cudaStream_t stream);
3737
void dequantize_row_q4_3_cuda(const void * vx, float * y, int k, cudaStream_t stream);
38+
void dequantize_row_q5_0_cuda(const void * vx, float * y, int k, cudaStream_t stream);
3839
void dequantize_row_q5_1_cuda(const void * vx, float * y, int k, cudaStream_t stream);
3940
void dequantize_row_q8_0_cuda(const void * vx, float * y, int k, cudaStream_t stream);
4041

0 commit comments

Comments
 (0)