Skip to content

Commit b7aca16

Browse files
committed
Merge branch 'iqX_k-cpu+metal' into iqX_k-cpu+metal+cuda
2 parents 31e127a + 79dff56 commit b7aca16

9 files changed

Lines changed: 74 additions & 67 deletions

File tree

‎ggml/src/ggml-common.h‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,32 +1189,35 @@ GGML_TABLE_BEGIN(int8_t, kvalues_iq4nl, 16)
11891189
-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113,
11901190
GGML_TABLE_END()
11911191

1192-
// IQ2_K..IQ6_K using ik_llama codebooks for now. IQ4_K reuses kvalues_iq4nl
11931192
GGML_TABLE_BEGIN(int8_t, kvalues_iq2k, 4)
1194-
-31, -13, 1, 17,
1193+
-17, -4, 7, 23,
11951194
GGML_TABLE_END()
11961195

11971196
GGML_TABLE_BEGIN(int8_t, kvalues_iq3k, 8)
1198-
-63, -40, -23, -10, 1, 13, 28, 47,
1197+
-55, -37, -23, -12, -1, 10, 23, 40,
1198+
GGML_TABLE_END()
1199+
1200+
GGML_TABLE_BEGIN(int8_t, kvalues_iq4k, 16)
1201+
-127, -96, -77, -61, -47, -35, -23, -12, 0, 11, 23, 36, 50, 66, 85, 103,
11991202
GGML_TABLE_END()
12001203

12011204
GGML_TABLE_BEGIN(int8_t, kvalues_iq5k, 32)
1202-
-126, -114, -103, -92, -83, -74, -65, -57, -50, -43, -36, -30, -24, -18, -12, -6,
1203-
-1, 5, 11, 17, 23, 29, 36, 43, 51, 59, 68, 77, 87, 97, 109, 121,
1205+
-115, -105, -94, -83, -74, -66, -59, -52, -45, -39, -33, -27, -22, -16, -11, -6,
1206+
-1, 5, 10, 15, 21, 27, 33, 39, 46, 54, 61, 69, 78, 86, 103, 111,
12041207
GGML_TABLE_END()
12051208

12061209
GGML_TABLE_BEGIN(int8_t, kvalues_iq6k, 64)
1207-
-127, -121, -115, -109, -104, -98, -93, -88, -84, -79, -74, -70, -66, -62, -58, -54,
1208-
-51, -47, -44, -40, -37, -34, -31, -28, -25, -22, -19, -16, -13, -11, -8, -5,
1209-
-2, 0, 3, 6, 9, 12, 14, 17, 20, 23, 27, 30, 33, 36, 40, 44,
1210-
47, 51, 55, 59, 63, 68, 72, 77, 82, 87, 92, 98, 103, 109, 115, 121,
1210+
-121, -116, -109, -103, -99, -93, -88, -84, -80, -75, -71, -67, -63, -59, -55, -52,
1211+
-48, -45, -42, -38, -35, -32, -29, -26, -24, -21, -18, -15, -13, -10, -7, -5,
1212+
-2, 1, 3, 6, 8, 11, 14, 17, 20, 22, 25, 28, 32, 35, 38, 42,
1213+
45, 49, 52, 56, 60, 65, 69, 74, 78, 83, 88, 94, 97, 105, 110, 115,
12111214
GGML_TABLE_END()
12121215

1213-
#define IQ2K_PHASE 5
1214-
#define IQ3K_PHASE 4
1215-
#define IQ4K_PHASE 4
1216-
#define IQ5K_PHASE 2
1217-
#define IQ6K_PHASE 1
1216+
#define IQ2K_PHASE 8
1217+
#define IQ3K_PHASE 23
1218+
#define IQ4K_PHASE 19
1219+
#define IQ5K_PHASE 15
1220+
#define IQ6K_PHASE 12
12181221

12191222
// e2m1 values (doubled), shared by MXFP4 and NVFP4
12201223
// ref: https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf

‎ggml/src/ggml-cpu/arch/arm/quants.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4340,7 +4340,7 @@ void ggml_vec_dot_iq4_k_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const vo
43404340
const int nb = n / QK_K;
43414341

43424342
#ifdef __ARM_NEON
4343-
const int8x16_t values = vld1q_s8(kvalues_iq4nl);
4343+
const int8x16_t values = vld1q_s8(kvalues_iq4k);
43444344
const uint8x16_t m4b = vdupq_n_u8(0x0f);
43454345

43464346
float sumf = 0;

‎ggml/src/ggml-cpu/arch/x86/quants.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4082,7 +4082,7 @@ void ggml_vec_dot_iq4_k_q8_K(int n, float * GGML_RESTRICT s, size_t bs, const vo
40824082

40834083
#if defined __AVX2__
40844084

4085-
const __m256i values = iqk_values_tbl(kvalues_iq4nl, 16);
4085+
const __m256i values = iqk_values_tbl(kvalues_iq4k, 16);
40864086
const __m256i m4 = _mm256_set1_epi8(0x0f);
40874087

40884088
__m256 accum = _mm256_setzero_ps();

‎ggml/src/ggml-cpu/quants.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,8 +1434,8 @@ void ggml_vec_dot_iq4_k_q8_K_generic(int n, float * GGML_RESTRICT s, size_t bs,
14341434
const int ph2 = x[ibl].extra & (1 << (ib + 1)) ? IQ4K_PHASE : 0;
14351435
int sumi1 = 0, sumi2 = 0;
14361436
for (int j = 0; j < 16; ++j) {
1437-
sumi1 += q8[j + 0] * (kvalues_iq4nl[qs[j] & 0xf] + ph1);
1438-
sumi2 += q8[j +16] * (kvalues_iq4nl[qs[j] >> 4] + ph2);
1437+
sumi1 += q8[j + 0] * (kvalues_iq4k[qs[j] & 0xf] + ph1);
1438+
sumi2 += q8[j +16] * (kvalues_iq4k[qs[j] >> 4] + ph2);
14391439
}
14401440

14411441
sum += ls1*sumi1 + ls2*sumi2;

‎ggml/src/ggml-metal/kernels/common.h‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,34 @@ constexpr constant static float kvalues_mxfp4_f[16] = {
4646
};
4747

4848
constexpr constant static float kvalues_iq2k_f[4] = {
49-
-31.f, -13.f, 1.f, 17.f
49+
-17.f, -4.f, 7.f, 23.f
5050
};
5151

5252
constexpr constant static float kvalues_iq3k_f[8] = {
53-
-63.f, -40.f, -23.f, -10.f, 1.f, 13.f, 28.f, 47.f
53+
-55.f, -37.f, -23.f, -12.f, -1.f, 10.f, 23.f, 40.f
54+
};
55+
56+
constexpr constant static float kvalues_iq4k_f[16] = {
57+
-127.f, -96.f, -77.f, -61.f, -47.f, -35.f, -23.f, -12.f, 0.f, 11.f, 23.f, 36.f, 50.f, 66.f, 85.f, 103.f
5458
};
5559

5660
constexpr constant static float kvalues_iq5k_f[32] = {
57-
-126.f, -114.f, -103.f, -92.f, -83.f, -74.f, -65.f, -57.f, -50.f, -43.f, -36.f, -30.f, -24.f, -18.f, -12.f, -6.f,
58-
-1.f, 5.f, 11.f, 17.f, 23.f, 29.f, 36.f, 43.f, 51.f, 59.f, 68.f, 77.f, 87.f, 97.f, 109.f, 121.f
61+
-115.f, -105.f, -94.f, -83.f, -74.f, -66.f, -59.f, -52.f, -45.f, -39.f, -33.f, -27.f, -22.f, -16.f, -11.f, -6.f,
62+
-1.f, 5.f, 10.f, 15.f, 21.f, 27.f, 33.f, 39.f, 46.f, 54.f, 61.f, 69.f, 78.f, 86.f, 103.f, 111.f
5963
};
6064

6165
constexpr constant static float kvalues_iq6k_f[64] = {
62-
-127.f, -121.f, -115.f, -109.f, -104.f, -98.f, -93.f, -88.f, -84.f, -79.f, -74.f, -70.f, -66.f, -62.f, -58.f, -54.f,
63-
-51.f, -47.f, -44.f, -40.f, -37.f, -34.f, -31.f, -28.f, -25.f, -22.f, -19.f, -16.f, -13.f, -11.f, -8.f, -5.f,
64-
-2.f, 0.f, 3.f, 6.f, 9.f, 12.f, 14.f, 17.f, 20.f, 23.f, 27.f, 30.f, 33.f, 36.f, 40.f, 44.f,
65-
47.f, 51.f, 55.f, 59.f, 63.f, 68.f, 72.f, 77.f, 82.f, 87.f, 92.f, 98.f, 103.f, 109.f, 115.f, 121.f
66+
-121.f, -116.f, -109.f, -103.f, -99.f, -93.f, -88.f, -84.f, -80.f, -75.f, -71.f, -67.f, -63.f, -59.f, -55.f, -52.f,
67+
-48.f, -45.f, -42.f, -38.f, -35.f, -32.f, -29.f, -26.f, -24.f, -21.f, -18.f, -15.f, -13.f, -10.f, -7.f, -5.f,
68+
-2.f, 1.f, 3.f, 6.f, 8.f, 11.f, 14.f, 17.f, 20.f, 22.f, 25.f, 28.f, 32.f, 35.f, 38.f, 42.f,
69+
45.f, 49.f, 52.f, 56.f, 60.f, 65.f, 69.f, 74.f, 78.f, 83.f, 88.f, 94.f, 97.f, 105.f, 110.f, 115.f
6670
};
6771

68-
constexpr constant static float IQ2K_PHASE_F = 5.f;
69-
constexpr constant static float IQ3K_PHASE_F = 4.f;
70-
constexpr constant static float IQ4K_PHASE_F = 4.f;
71-
constexpr constant static float IQ5K_PHASE_F = 2.f;
72-
constexpr constant static float IQ6K_PHASE_F = 1.f;
72+
constexpr constant static float IQ2K_PHASE_F = 8.f;
73+
constexpr constant static float IQ3K_PHASE_F = 23.f;
74+
constexpr constant static float IQ4K_PHASE_F = 19.f;
75+
constexpr constant static float IQ5K_PHASE_F = 15.f;
76+
constexpr constant static float IQ6K_PHASE_F = 12.f;
7377

7478
static inline int best_index_int8(int n, constant float * val, float x) {
7579
if (x <= val[0]) return 0;

‎ggml/src/ggml-metal/kernels/dequantize.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ void dequantize_iq4_k(device const block_iq4_k * xb, short il, thread type4x4 &
764764

765765
const short shift = 4 * (il % 2);
766766
for (int i = 0; i < 16; ++i) {
767-
reg[i / 4][i % 4] = dl * (kvalues_iq4nl_f[(qs[i] >> shift) & 0xf] + ph);
767+
reg[i / 4][i % 4] = dl * (kvalues_iq4k_f[(qs[i] >> shift) & 0xf] + ph);
768768
}
769769
}
770770

‎ggml/src/ggml-metal/kernels/mul_mv.metal‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3532,7 +3532,7 @@ void kernel_mul_mv_iq4_k_f32_impl(
35323532
const short ib = it/2;
35333533
const short il = it%2;
35343534

3535-
shmem_f32[tiisg] = kvalues_iq4nl_f[tiisg%16];
3535+
shmem_f32[tiisg] = kvalues_iq4k_f[tiisg%16];
35363536
threadgroup_barrier(mem_flags::mem_threadgroup);
35373537

35383538
float4 yl[4];

‎ggml/src/ggml-quants.c‎

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,8 +2832,8 @@ void dequantize_row_iq4_k(const block_iq4_k * GGML_RESTRICT x, float * GGML_REST
28322832
const int ph1 = x[i].extra & (1 << (ib + 0)) ? IQ4K_PHASE : 0;
28332833
const int ph2 = x[i].extra & (1 << (ib + 1)) ? IQ4K_PHASE : 0;
28342834
for (int j = 0; j < 16; ++j) {
2835-
y[j+ 0] = dl1 * (kvalues_iq4nl[qs[j] & 0xf] + ph1);
2836-
y[j+16] = dl2 * (kvalues_iq4nl[qs[j] >> 4] + ph2);
2835+
y[j+ 0] = dl1 * (kvalues_iq4k[qs[j] & 0xf] + ph1);
2836+
y[j+16] = dl2 * (kvalues_iq4k[qs[j] >> 4] + ph2);
28372837
}
28382838

28392839
y += 32;
@@ -5239,23 +5239,22 @@ void quantize_row_iq4_xs_ref(const float * GGML_RESTRICT x, block_iq4_xs * GGML_
52395239

52405240
// ============================ IQK superblock quants
52415241

5242-
static const uint8_t kindex_iq6k[248] = {
5243-
0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3,
5244-
3, 3, 3, 3, 67, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 69,
5245-
6, 6, 6, 6, 70, 7, 7, 7, 7, 8, 8, 8, 8, 72, 9, 9,
5246-
9, 9, 73, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13,
5247-
13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 79, 16, 16, 16, 17, 17,
5248-
17, 81, 18, 18, 18, 19, 19, 19, 83, 20, 20, 84, 21, 21, 85, 22,
5249-
22, 86, 23, 23, 87, 24, 24, 88, 25, 25, 89, 26, 26, 90, 27, 27,
5250-
91, 28, 28, 29, 29, 93, 30, 30, 94, 31, 31, 95, 32, 32, 33, 33,
5251-
97, 34, 34, 98, 35, 35, 99, 36, 36, 100, 37, 37, 38, 38, 102, 39,
5252-
39, 103, 40, 40, 104, 41, 41, 41, 42, 42, 42, 106, 43, 43, 107, 44,
5253-
44, 108, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 111, 48, 48, 48,
5254-
49, 49, 49, 49, 50, 50, 50, 50, 51, 51, 51, 51, 52, 52, 52, 52,
5255-
116, 53, 53, 53, 53, 54, 54, 54, 54, 118, 55, 55, 55, 55, 119, 56,
5256-
56, 56, 56, 120, 57, 57, 57, 57, 121, 58, 58, 58, 58, 58, 59, 59,
5257-
59, 59, 59, 123, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 61, 62,
5258-
62, 62, 62, 62, 62, 63, 63, 63,
5242+
static const uint8_t kindex_iq6k[236] = {
5243+
0, 0, 64, 1, 1, 1, 1, 1, 65, 2, 2, 2, 2, 2, 2, 3,
5244+
3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 69, 6,
5245+
6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 72, 9, 9, 9, 9,
5246+
10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13,
5247+
14, 14, 14, 78, 15, 15, 15, 16, 16, 16, 80, 17, 17, 81, 18, 18,
5248+
18, 19, 19, 19, 83, 20, 20, 84, 21, 21, 85, 22, 22, 86, 23, 23,
5249+
24, 24, 88, 25, 25, 89, 26, 26, 90, 27, 27, 28, 28, 92, 29, 29,
5250+
93, 30, 30, 31, 31, 95, 32, 32, 96, 33, 33, 34, 34, 98, 35, 35,
5251+
36, 36, 100, 37, 37, 101, 38, 38, 102, 39, 39, 103, 40, 40, 41, 41,
5252+
105, 42, 42, 106, 43, 43, 43, 44, 44, 44, 108, 45, 45, 109, 46, 46,
5253+
46, 47, 47, 47, 111, 48, 48, 48, 49, 49, 49, 113, 50, 50, 50, 51,
5254+
51, 51, 51, 52, 52, 52, 52, 116, 53, 53, 53, 53, 54, 54, 54, 54,
5255+
118, 55, 55, 55, 55, 56, 56, 56, 56, 120, 57, 57, 57, 57, 121, 58,
5256+
58, 58, 58, 58, 59, 59, 59, 59, 123, 60, 60, 60, 60, 60, 61, 61,
5257+
61, 61, 61, 61, 125, 62, 62, 62, 62, 126, 63, 63,
52595258
};
52605259

52615260
static inline int best_index_iqk(int nlevels, const int8_t * val, const uint8_t * index, float x) {
@@ -5590,7 +5589,7 @@ size_t quantize_iq4_k(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst,
55905589
block_iq4_k * iq4 = (block_iq4_k *)qrow;
55915590
for (int ibl = 0; ibl < nblock; ++ibl) {
55925591
const float * qw = quant_weights ? quant_weights + QK_K * ibl : NULL;
5593-
quantize_row_iqk_impl(src + QK_K * ibl, qw, 16, kvalues_iq4nl, NULL, IQ4K_PHASE, -32, 31, 1, 0, 7,
5592+
quantize_row_iqk_impl(src + QK_K * ibl, qw, 16, kvalues_iq4k, NULL, IQ4K_PHASE, -32, 31, 1, 0, 7,
55945593
&iq4[ibl].d, &iq4[ibl].extra, ls, L);
55955594
memset(iq4[ibl].scales_h, 0, sizeof(iq4[ibl].scales_h));
55965595
for (int ib = 0; ib < QK_K / 16; ++ib) {

‎gguf-py/gguf/quants.py‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,8 +1379,8 @@ def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray:
13791379

13801380

13811381
class IQ2_K(__Quant, qtype=GGMLQuantizationType.IQ2_K):
1382-
kvalues = (-31, -13, 1, 17)
1383-
phase = 5
1382+
kvalues = (-17, -4, 7, 23)
1383+
phase = 8
13841384

13851385
@classmethod
13861386
def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray:
@@ -1409,8 +1409,8 @@ def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray:
14091409

14101410

14111411
class IQ3_K(__Quant, qtype=GGMLQuantizationType.IQ3_K):
1412-
kvalues = (-63, -40, -23, -10, 1, 13, 28, 47)
1413-
phase = 4
1412+
kvalues = (-55, -37, -23, -12, -1, 10, 23, 40)
1413+
phase = 23
14141414

14151415
@classmethod
14161416
def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray:
@@ -1448,7 +1448,8 @@ def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray:
14481448

14491449

14501450
class IQ4_K(__Quant, qtype=GGMLQuantizationType.IQ4_K):
1451-
phase = 4
1451+
kvalues = (-127, -96, -77, -61, -47, -35, -23, -12, 0, 11, 23, 36, 50, 66, 85, 103)
1452+
phase = 19
14521453

14531454
@classmethod
14541455
def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray:
@@ -1474,16 +1475,16 @@ def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray:
14741475
qs = qs.reshape((n_blocks, -1, 16, 1)) >> np.array([0, 4], dtype=np.uint8).reshape((1, 1, 1, 2))
14751476
qs = qs.transpose(0, 1, 3, 2).reshape((n_blocks, -1, 1)) & np.uint8(0x0F)
14761477

1477-
kvalues = np.array(IQ4_NL.kvalues, dtype=np.int8).reshape((1, 1, -1))
1478+
kvalues = np.array(cls.kvalues, dtype=np.int8).reshape((1, 1, -1))
14781479
qs = np.take_along_axis(kvalues, qs, axis=-1).astype(np.float32).reshape((n_blocks, -1, 16))
14791480

14801481
return (dl * (qs + ph)).reshape((n_blocks, -1))
14811482

14821483

14831484
class IQ5_K(__Quant, qtype=GGMLQuantizationType.IQ5_K):
1484-
kvalues = (-126, -114, -103, -92, -83, -74, -65, -57, -50, -43, -36, -30, -24, -18, -12, -6,
1485-
-1, 5, 11, 17, 23, 29, 36, 43, 51, 59, 68, 77, 87, 97, 109, 121)
1486-
phase = 2
1485+
kvalues = (-115, -105, -94, -83, -74, -66, -59, -52, -45, -39, -33, -27, -22, -16, -11, -6,
1486+
-1, 5, 10, 15, 21, 27, 33, 39, 46, 54, 61, 69, 78, 86, 103, 111)
1487+
phase = 15
14871488

14881489
@classmethod
14891490
def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray:
@@ -1520,11 +1521,11 @@ def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray:
15201521

15211522

15221523
class IQ6_K(__Quant, qtype=GGMLQuantizationType.IQ6_K):
1523-
kvalues = (-127, -121, -115, -109, -104, -98, -93, -88, -84, -79, -74, -70, -66, -62, -58, -54,
1524-
-51, -47, -44, -40, -37, -34, -31, -28, -25, -22, -19, -16, -13, -11, -8, -5,
1525-
-2, 0, 3, 6, 9, 12, 14, 17, 20, 23, 27, 30, 33, 36, 40, 44,
1526-
47, 51, 55, 59, 63, 68, 72, 77, 82, 87, 92, 98, 103, 109, 115, 121)
1527-
phase = 1
1524+
kvalues = (-121, -116, -109, -103, -99, -93, -88, -84, -80, -75, -71, -67, -63, -59, -55, -52,
1525+
-48, -45, -42, -38, -35, -32, -29, -26, -24, -21, -18, -15, -13, -10, -7, -5,
1526+
-2, 1, 3, 6, 8, 11, 14, 17, 20, 22, 25, 28, 32, 35, 38, 42,
1527+
45, 49, 52, 56, 60, 65, 69, 74, 78, 83, 88, 94, 97, 105, 110, 115)
1528+
phase = 12
15281529

15291530
@classmethod
15301531
def dequantize_blocks(cls, blocks: np.ndarray) -> np.ndarray:

0 commit comments

Comments
 (0)