Skip to content

Commit d90844f

Browse files
committed
ggml : add Q5_0 quantization (cuBLAS only)
1 parent 4afcc37 commit d90844f

File tree

8 files changed

+328
-6
lines changed

8 files changed

+328
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ build-em/
1515
build-debug/
1616
build-release/
1717
build-static/
18+
build-cublas/
1819
build-no-accel/
1920
build-sanitize-addr/
2021
build-sanitize-thread/

examples/quantize/quantize.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ int main(int argc, char ** argv) {
1717
fprintf(stderr, " type = %d - q4_2\n", LLAMA_FTYPE_MOSTLY_Q4_2);
1818
fprintf(stderr, " type = %d - q4_3\n", LLAMA_FTYPE_MOSTLY_Q4_3);
1919
fprintf(stderr, " type = %d - q8_0\n", LLAMA_FTYPE_MOSTLY_Q8_0);
20+
fprintf(stderr, " type = %d - q5_0\n", LLAMA_FTYPE_MOSTLY_Q5_0);
2021
return 1;
2122
}
2223

ggml-cuda.cu

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ 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+
__half m; // min
44+
int32_t qh; // 5-th bit of quants
45+
uint8_t qs[QK5_0 / 2]; // nibbles / quants
46+
} block_q5_0;
47+
static_assert(sizeof(block_q5_0) == 2 * sizeof(ggml_fp16_t) + sizeof(int32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
48+
4049
#define QK8_0 32
4150
typedef struct {
4251
float d; // delta
@@ -138,6 +147,35 @@ static __global__ void dequantize_block_q4_3(const void * vx, float * y) {
138147
}
139148
}
140149

150+
static __global__ void dequantize_block_q5_0(const void * vx, float * y) {
151+
const block_q5_0 * x = (const block_q5_0 *) vx;
152+
153+
const int i = blockIdx.x;
154+
155+
const float d = x[i].d;
156+
const float m = x[i].m;
157+
158+
const uint8_t * pp = x[i].qs;
159+
160+
const uint32_t qh = x[i].qh;
161+
162+
for (int l = 0; l < QK5_0; l += 2) {
163+
const uint8_t vi = pp[l/2];
164+
165+
const int8_t vh0 = ((qh & (1 << (l + 0))) >> (l + 0)) << 4;
166+
const int8_t vh1 = ((qh & (1 << (l + 1))) >> (l + 1)) << 4;
167+
168+
const int8_t vi0 = (vi & 0xf) | vh0;
169+
const int8_t vi1 = (vi >> 4) | vh1;
170+
171+
const float v0 = vi0*d + m;
172+
const float v1 = vi1*d + m;
173+
174+
y[i*QK5_0 + l + 0] = v0;
175+
y[i*QK5_0 + l + 1] = v1;
176+
}
177+
}
178+
141179
static __global__ void dequantize_block_q8_0(const void * vx, float * y) {
142180
const block_q8_0 * x = (const block_q8_0 *) vx;
143181

@@ -174,6 +212,11 @@ void dequantize_row_q4_3_cuda(const void * vx, float * y, int k, cudaStream_t st
174212
dequantize_block_q4_3<<<nb, 1, 0, stream>>>(vx, y);
175213
}
176214

215+
void dequantize_row_q5_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
216+
const int nb = k / QK5_0;
217+
dequantize_block_q5_0<<<nb, 1, 0, stream>>>(vx, y);
218+
}
219+
177220
void dequantize_row_q8_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
178221
const int nb = k / QK8_0;
179222
dequantize_block_q8_0<<<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_q8_0_cuda(const void * vx, float * y, int k, cudaStream_t stream);
3940

4041
#ifdef __cplusplus

0 commit comments

Comments
 (0)