Skip to content

Commit 574406d

Browse files
ggerganovsw
andauthored
ggml : add Q5_0 and Q5_1 quantization (#1187)
* ggml : add Q5_0 quantization (cuBLAS only) * ggml : fix Q5_0 qh -> uint32_t * ggml : fix q5_0 histogram stats * ggml : q5_0 scalar dot product * ggml : q5_0 ARM NEON dot * ggml : q5_0 more efficient ARM NEON using uint64_t masks * ggml : rename Q5_0 -> Q5_1 * ggml : adding Q5_0 mode * quantize : add Q5_0 and Q5_1 to map * ggml : AVX2 optimizations for Q5_0, Q5_1 (#1195) --------- Co-authored-by: Stephan Walter <[email protected]>
1 parent 87a6f84 commit 574406d

File tree

8 files changed

+711
-30
lines changed

8 files changed

+711
-30
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ static const std::map<std::string, enum llama_ftype> LLAMA_FTYPE_MAP = {
1010
{"q4_1", LLAMA_FTYPE_MOSTLY_Q4_1},
1111
{"q4_2", LLAMA_FTYPE_MOSTLY_Q4_2},
1212
{"q4_3", LLAMA_FTYPE_MOSTLY_Q4_3},
13+
{"q5_0", LLAMA_FTYPE_MOSTLY_Q5_0},
14+
{"q5_1", LLAMA_FTYPE_MOSTLY_Q5_1},
1315
{"q8_0", LLAMA_FTYPE_MOSTLY_Q8_0},
1416
};
1517

ggml-cuda.cu

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ 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+
uint8_t qh[4]; // 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+
48+
#define QK5_1 32
49+
typedef struct {
50+
__half d; // delta
51+
__half m; // min
52+
uint32_t qh; // 5-th bit of quants
53+
uint8_t qs[QK5_1 / 2]; // nibbles / quants
54+
} block_q5_1;
55+
static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_fp16_t) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
56+
4057
#define QK8_0 32
4158
typedef struct {
4259
float d; // delta
@@ -138,6 +155,64 @@ static __global__ void dequantize_block_q4_3(const void * vx, float * y) {
138155
}
139156
}
140157

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+
uint32_t qh;
168+
memcpy(&qh, x[i].qh, sizeof(qh));
169+
170+
for (int l = 0; l < QK5_0; l += 2) {
171+
const uint8_t vi = pp[l/2];
172+
173+
const int8_t vh0 = ((qh & (1 << (l + 0))) >> (l + 0)) << 4;
174+
const int8_t vh1 = ((qh & (1 << (l + 1))) >> (l + 1)) << 4;
175+
176+
const int8_t vi0 = ((vi & 0xf) | vh0);
177+
const int8_t vi1 = ((vi >> 4) | vh1);
178+
179+
const float v0 = (vi0 - 16)*d;
180+
const float v1 = (vi1 - 16)*d;
181+
182+
y[i*QK5_0 + l + 0] = v0;
183+
y[i*QK5_0 + l + 1] = v1;
184+
}
185+
}
186+
187+
static __global__ void dequantize_block_q5_1(const void * vx, float * y) {
188+
const block_q5_1 * x = (const block_q5_1 *) vx;
189+
190+
const int i = blockIdx.x;
191+
192+
const float d = x[i].d;
193+
const float m = x[i].m;
194+
195+
const uint8_t * pp = x[i].qs;
196+
197+
const uint32_t qh = x[i].qh;
198+
199+
for (int l = 0; l < QK5_1; l += 2) {
200+
const uint8_t vi = pp[l/2];
201+
202+
const int8_t vh0 = ((qh & (1 << (l + 0))) >> (l + 0)) << 4;
203+
const int8_t vh1 = ((qh & (1 << (l + 1))) >> (l + 1)) << 4;
204+
205+
const int8_t vi0 = (vi & 0xf) | vh0;
206+
const int8_t vi1 = (vi >> 4) | vh1;
207+
208+
const float v0 = vi0*d + m;
209+
const float v1 = vi1*d + m;
210+
211+
y[i*QK5_1 + l + 0] = v0;
212+
y[i*QK5_1 + l + 1] = v1;
213+
}
214+
}
215+
141216
static __global__ void dequantize_block_q8_0(const void * vx, float * y) {
142217
const block_q8_0 * x = (const block_q8_0 *) vx;
143218

@@ -174,6 +249,16 @@ void dequantize_row_q4_3_cuda(const void * vx, float * y, int k, cudaStream_t st
174249
dequantize_block_q4_3<<<nb, 1, 0, stream>>>(vx, y);
175250
}
176251

252+
void dequantize_row_q5_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
253+
const int nb = k / QK5_0;
254+
dequantize_block_q5_0<<<nb, 1, 0, stream>>>(vx, y);
255+
}
256+
257+
void dequantize_row_q5_1_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
258+
const int nb = k / QK5_1;
259+
dequantize_block_q5_1<<<nb, 1, 0, stream>>>(vx, y);
260+
}
261+
177262
void dequantize_row_q8_0_cuda(const void * vx, float * y, int k, cudaStream_t stream) {
178263
const int nb = k / QK8_0;
179264
dequantize_block_q8_0<<<nb, 1, 0, stream>>>(vx, y);

ggml-cuda.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ 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);
39+
void dequantize_row_q5_1_cuda(const void * vx, float * y, int k, cudaStream_t stream);
3840
void dequantize_row_q8_0_cuda(const void * vx, float * y, int k, cudaStream_t stream);
3941

4042
#ifdef __cplusplus

0 commit comments

Comments
 (0)