Skip to content

Commit ed01e0c

Browse files
committed
ggml : define helper constants only for CUDA and SYCL
ggml-ci
1 parent 54ebe70 commit ed01e0c

File tree

1 file changed

+76
-51
lines changed

1 file changed

+76
-51
lines changed

ggml-common.h

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,95 @@ typedef sycl::half2 ggml_half2;
6060
#define static_assert(cond, msg) struct global_scope_noop_trick
6161
#endif
6262
#endif
63-
#endif
63+
#endif // __cplusplus
6464

6565
// QK = number of values after dequantization
66+
// QK_K = super-block size
67+
68+
#ifdef GGML_QKK_64
69+
#define QK_K 64
70+
#define K_SCALE_SIZE 4
71+
#else
72+
#define QK_K 256
73+
#define K_SCALE_SIZE 12
74+
#endif // GGML_QKK_64
75+
76+
#if defined(GGML_COMMON_DECL_CUDA) || defined(GGML_COMMON_DECL_HIP)
6677
// QR = QK / number of values before dequantization
6778
// QI = number of 32 bit integers before dequantization
6879

69-
#define QK4_0 32
7080
#define QI4_0 (QK4_0 / (4 * QR4_0))
7181
#define QR4_0 2
82+
83+
#define QI4_1 (QK4_1 / (4 * QR4_1))
84+
#define QR4_1 2
85+
86+
#define QI5_0 (QK5_0 / (4 * QR5_0))
87+
#define QR5_0 2
88+
89+
#define QI5_1 (QK5_1 / (4 * QR5_1))
90+
#define QR5_1 2
91+
92+
#define QI8_0 (QK8_0 / (4 * QR8_0))
93+
#define QR8_0 1
94+
95+
#define QI8_1 (QK8_1 / (4 * QR8_1))
96+
#define QR8_1 1
97+
98+
#define QI2_K (QK_K / (4*QR2_K))
99+
#define QR2_K 4
100+
101+
#define QI3_K (QK_K / (4*QR3_K))
102+
#define QR3_K 4
103+
104+
#define QI4_K (QK_K / (4*QR4_K))
105+
#define QR4_K 2
106+
107+
#define QI5_K (QK_K / (4*QR5_K))
108+
#define QR5_K 2
109+
110+
#define QI6_K (QK_K / (4*QR6_K))
111+
#define QR6_K 2
112+
113+
#define QI2_XXS (QK_K / (4*QR2_XXS))
114+
#define QR2_XXS 8
115+
116+
#define QI2_XS (QK_K / (4*QR2_XS))
117+
#define QR2_XS 8
118+
119+
#define QI2_S (QK_K / (4*QR2_S))
120+
#define QR2_S 8
121+
122+
#define QI3_XXS (QK_K / (4*QR3_XXS))
123+
#define QR3_XXS 8
124+
125+
#define QI3_XS (QK_K / (4*QR3_XS))
126+
#define QR3_XS 8
127+
128+
#define QI1_S (QK_K / (4*QR1_S))
129+
#define QR1_S 8
130+
131+
#define QI4_NL (QK4_NL / (4*QR4_NL))
132+
#define QR4_NL 2
133+
134+
#if QK_K == 64
135+
#define QI4_XS QI4_NL
136+
#define QR4_XS QR4_NL
137+
#else
138+
#define QI4_XS (QK_K / (4*QR4_XS))
139+
#define QR4_XS 8
140+
#endif
141+
142+
#endif // GGML_COMMON_DECL_CUDA || GGML_COMMON_DECL_HIP
143+
144+
#define QK4_0 32
72145
typedef struct {
73146
ggml_half d; // delta
74147
uint8_t qs[QK4_0 / 2]; // nibbles / quants
75148
} block_q4_0;
76149
static_assert(sizeof(block_q4_0) == sizeof(ggml_half) + QK4_0 / 2, "wrong q4_0 block size/padding");
77150

78151
#define QK4_1 32
79-
#define QI4_1 (QK4_1 / (4 * QR4_1))
80-
#define QR4_1 2
81152
typedef struct {
82153
union {
83154
struct {
@@ -91,8 +162,6 @@ typedef struct {
91162
static_assert(sizeof(block_q4_1) == 2 * sizeof(ggml_half) + QK4_1 / 2, "wrong q4_1 block size/padding");
92163

93164
#define QK5_0 32
94-
#define QI5_0 (QK5_0 / (4 * QR5_0))
95-
#define QR5_0 2
96165
typedef struct {
97166
ggml_half d; // delta
98167
uint8_t qh[4]; // 5-th bit of quants
@@ -101,8 +170,6 @@ typedef struct {
101170
static_assert(sizeof(block_q5_0) == sizeof(ggml_half) + sizeof(uint32_t) + QK5_0 / 2, "wrong q5_0 block size/padding");
102171

103172
#define QK5_1 32
104-
#define QI5_1 (QK5_1 / (4 * QR5_1))
105-
#define QR5_1 2
106173
typedef struct {
107174
union {
108175
struct {
@@ -117,17 +184,13 @@ typedef struct {
117184
static_assert(sizeof(block_q5_1) == 2 * sizeof(ggml_half) + sizeof(uint32_t) + QK5_1 / 2, "wrong q5_1 block size/padding");
118185

119186
#define QK8_0 32
120-
#define QI8_0 (QK8_0 / (4 * QR8_0))
121-
#define QR8_0 1
122187
typedef struct {
123188
ggml_half d; // delta
124189
int8_t qs[QK8_0]; // quants
125190
} block_q8_0;
126191
static_assert(sizeof(block_q8_0) == sizeof(ggml_half) + QK8_0, "wrong q8_0 block size/padding");
127192

128193
#define QK8_1 32
129-
#define QI8_1 (QK8_1 / (4 * QR8_1))
130-
#define QR8_1 1
131194
typedef struct {
132195
union {
133196
struct {
@@ -144,21 +207,10 @@ static_assert(sizeof(block_q8_1) == 2*sizeof(ggml_half) + QK8_1, "wrong q8_1 blo
144207
// Super-block quantization structures
145208
//
146209

147-
// Super-block size
148-
#ifdef GGML_QKK_64
149-
#define QK_K 64
150-
#define K_SCALE_SIZE 4
151-
#else
152-
#define QK_K 256
153-
#define K_SCALE_SIZE 12
154-
#endif
155-
156210
// 2-bit quantization
157211
// weight is represented as x = a * q + b
158212
// 16 blocks of 16 elements each
159213
// Effectively 2.625 bits per weight
160-
#define QI2_K (QK_K / (4*QR2_K))
161-
#define QR2_K 4
162214
typedef struct {
163215
uint8_t scales[QK_K/16]; // scales and mins, quantized with 4 bits
164216
uint8_t qs[QK_K/4]; // quants
@@ -176,8 +228,6 @@ static_assert(sizeof(block_q2_K) == 2*sizeof(ggml_half) + QK_K/16 + QK_K/4, "wro
176228
// weight is represented as x = a * q
177229
// 16 blocks of 16 elements each
178230
// Effectively 3.4375 bits per weight
179-
#define QI3_K (QK_K / (4*QR3_K))
180-
#define QR3_K 4
181231
#ifdef GGML_QKK_64
182232
typedef struct {
183233
uint8_t hmask[QK_K/8]; // quants - high bit
@@ -200,8 +250,6 @@ static_assert(sizeof(block_q3_K) == sizeof(ggml_half) + QK_K / 4 + QK_K / 8 + 12
200250
// 8 blocks of 32 elements each
201251
// weight is represented as x = a * q + b
202252
// Effectively 4.5 bits per weight
203-
#define QI4_K (QK_K / (4*QR4_K))
204-
#define QR4_K 2
205253
#ifdef GGML_QKK_64
206254
typedef struct {
207255
ggml_half d[2]; // super-block scales/mins
@@ -228,8 +276,6 @@ static_assert(sizeof(block_q4_K) == 2*sizeof(ggml_half) + K_SCALE_SIZE + QK_K/2,
228276
// 8 blocks of 32 elements each
229277
// weight is represented as x = a * q + b
230278
// Effectively 5.5 bits per weight
231-
#define QI5_K (QK_K / (4*QR5_K))
232-
#define QR5_K 2
233279
#ifdef GGML_QKK_64
234280
typedef struct {
235281
ggml_half d; // super-block scale
@@ -258,8 +304,6 @@ static_assert(sizeof(block_q5_K) == 2*sizeof(ggml_half) + K_SCALE_SIZE + QK_K/2
258304
// weight is represented as x = a * q
259305
// 16 blocks of 16 elements each
260306
// Effectively 6.5625 bits per weight
261-
#define QI6_K (QK_K / (4*QR6_K))
262-
#define QR6_K 2
263307
typedef struct {
264308
uint8_t ql[QK_K/2]; // quants, lower 4 bits
265309
uint8_t qh[QK_K/4]; // quants, upper 2 bits
@@ -279,17 +323,13 @@ static_assert(sizeof(block_q8_K) == sizeof(float) + QK_K + QK_K/16*sizeof(int16_
279323
// (Almost) "true" 2-bit quantization.
280324
// Due to the need to use blocks as per ggml design, it ends up using
281325
// 2.0625 bpw because of the 16-bit scale for each block of 256.
282-
#define QI2_XXS (QK_K / (4*QR2_XXS))
283-
#define QR2_XXS 8
284326
typedef struct {
285327
ggml_half d;
286328
uint16_t qs[QK_K/8];
287329
} block_iq2_xxs;
288330
static_assert(sizeof(block_iq2_xxs) == sizeof(ggml_half) + QK_K/8*sizeof(uint16_t), "wrong iq2_xxs block size/padding");
289331

290332
// 2.3125 bpw quants
291-
#define QI2_XS (QK_K / (4*QR2_XS))
292-
#define QR2_XS 8
293333
typedef struct {
294334
ggml_half d;
295335
uint16_t qs[QK_K/8];
@@ -298,8 +338,6 @@ typedef struct {
298338
static_assert(sizeof(block_iq2_xs) == sizeof(ggml_half) + QK_K/8*sizeof(uint16_t) + QK_K/32, "wrong iq2_xs block size/padding");
299339

300340
// 2.5625 bpw quants
301-
#define QI2_S (QK_K / (4*QR2_S))
302-
#define QR2_S 8
303341
typedef struct {
304342
ggml_half d;
305343
uint8_t qs[QK_K/4];
@@ -311,8 +349,6 @@ static_assert(sizeof(block_iq2_s) == sizeof(ggml_half) + QK_K/4 + QK_K/16, "wron
311349
// (Almost) "true" 3-bit quantization.
312350
// Due to the need to use blocks as per ggml design, it ends up using
313351
// 3.0625 bpw because of the 16-bit scale for each block of 256.
314-
#define QI3_XXS (QK_K / (4*QR3_XXS))
315-
#define QR3_XXS 8
316352
typedef struct {
317353
ggml_half d;
318354
uint8_t qs[3*QK_K/8];
@@ -325,8 +361,6 @@ static_assert(sizeof(block_iq3_xxs) == sizeof(ggml_half) + 3*(QK_K/8), "wrong iq
325361
#else
326362
#define IQ3S_N_SCALE QK_K/64
327363
#endif
328-
#define QI3_XS (QK_K / (4*QR3_XS))
329-
#define QR3_XS 8
330364
typedef struct {
331365
ggml_half d;
332366
uint8_t qs[QK_K/4];
@@ -336,8 +370,6 @@ typedef struct {
336370
} block_iq3_s;
337371
static_assert(sizeof(block_iq3_s) == sizeof(ggml_half) + 13*(QK_K/32) + IQ3S_N_SCALE, "wrong iq3_s block size/padding");
338372

339-
#define QI1_S (QK_K / (4*QR1_S))
340-
#define QR1_S 8
341373
typedef struct {
342374
ggml_half d;
343375
uint8_t qs[QK_K/8];
@@ -347,22 +379,15 @@ static_assert(sizeof(block_iq1_s) == sizeof(ggml_half) + QK_K/8 + QK_K/16, "wron
347379

348380
// Non-linear quants
349381
#define QK4_NL 32
350-
#define QI4_NL (QK4_NL / (4*QR4_NL))
351-
#define QR4_NL 2
352382
typedef struct {
353383
ggml_half d;
354384
uint8_t qs[QK4_NL/2];
355385
} block_iq4_nl;
356386
static_assert(sizeof(block_iq4_nl) == sizeof(ggml_half) + QK4_NL/2, "wrong iq4_nl block size/padding");
357387

358388
#if QK_K == 64
359-
#define block_iq4_xs block_iq4_nl
360-
#define QI4_XS QI4_NL
361-
#define QR4_XS QR4_NL
362-
//typedef struct block_iq4_nl block_iq4_xs;
389+
typedef struct block_iq4_nl block_iq4_xs;
363390
#else
364-
#define QI4_XS (QK_K / (4*QR4_XS))
365-
#define QR4_XS 8
366391
typedef struct {
367392
ggml_half d;
368393
uint16_t scales_h;

0 commit comments

Comments
 (0)