diff --git a/metal/kernels/moe.metal b/metal/kernels/moe.metal index bfabfcad66..edc0cba87d 100644 --- a/metal/kernels/moe.metal +++ b/metal/kernels/moe.metal @@ -447,6 +447,7 @@ GU_SET(T_IQ2_XS) GU_SET(T_IQ2_S) GU_SET(T_IQ3_S) GU_SET(T_IQ4_XS) // the IQ3_S pack's gate/up in one layer +GU_SET(T_IQ1_M) // IQ1_M's expert gate/up in the GSQ packs (ported from the 0.1.45 line) GU_SET(T_Q4_K) GU_SET(T_Q5_K) GU_SET(T_Q6_K) GU_SET(T_Q8_0) // the UD-Q4/5/6_K_XL packs DN_SET(T_Q5_1) DN_SET(T_Q8_0) DN_SET(T_Q5_0) // the UD-IQ2_XS / Q2_K_XL / Q3_K_XL / IQ4_XS packs' down experts (generic Dec: d * (code - 16)) diff --git a/metal/kernels/moe_mm.metal b/metal/kernels/moe_mm.metal index a92a19f77f..669d421651 100644 --- a/metal/kernels/moe_mm.metal +++ b/metal/kernels/moe_mm.metal @@ -267,7 +267,7 @@ kernel void mm_combine(device const float* YE [[buffer(0)]], device const int* p #define MMDN(F, M, N, K, S) MMDN_(F, M, N, K, S) #define MM_GU_SET(F) MMGU(F, 32, 64, 64, 4) MMGU(F, 64, 64, 64, 4) MMGU(F, 16, 64, 64, 2) MMGU(F, 32, 32, 64, 2) #define MM_DN_SET(F) MMDN(F, 32, 64, 64, 4) MMDN(F, 64, 64, 64, 4) MMDN(F, 16, 64, 64, 2) MMDN(F, 32, 32, 64, 2) -MM_GU_SET(T_Q2_0) MM_GU_SET(T_IQ3_XXS) MM_GU_SET(T_IQ2_XXS) MM_GU_SET(T_IQ2_XS) MM_GU_SET(T_IQ2_S) MM_GU_SET(T_IQ3_S) +MM_GU_SET(T_Q2_0) MM_GU_SET(T_IQ3_XXS) MM_GU_SET(T_IQ2_XXS) MM_GU_SET(T_IQ2_XS) MM_GU_SET(T_IQ2_S) MM_GU_SET(T_IQ3_S) MM_GU_SET(T_IQ1_M) MM_DN_SET(T_Q2_0) MM_DN_SET(T_IQ4_NL) MM_DN_SET(T_Q5_0) // ---- v2: activations gathered once per layer as fp16 rows, read by matmul2d from device memory --------------------- @@ -413,6 +413,7 @@ MMDN2(T_IQ3_S, 16, 64, 16, 2) MMDN2(T_IQ3_S, 16, 64, 32, 2) MMDN2(T_IQ3_S, 16, 3 MMGU2(T_IQ4_XS, 16, 64, 32, 2) MMGU2(T_IQ4_XS, 16, 64, 64, 2) MMGU2(T_IQ4_XS, 32, 64, 32, 2) MMGU2(T_IQ4_XS, 16, 32, 32, 2) MMGU2(T_IQ4_XS, 64, 64, 32, 4) MM_GU2_SET(T_Q2_0) MM_GU2_SET(T_IQ3_XXS) MM_GU2_SET(T_IQ2_XXS) MM_GU2_SET(T_IQ2_XS) MM_GU2_SET(T_IQ2_S) MM_GU2_SET(T_IQ3_S) +MM_GU2_SET(T_IQ1_M) // IQ1_M's expert gate/up in the GSQ packs (ported from the 0.1.45 line) MM_GU2_SET(T_TQK6) MM_GU2_SET(T_TQ2T) // Gyro: the trellis gate/up (units of 16 weights: TK 16 or a multiple) MM_GU2_SET(T_TQP5) MM_GU2_SET(T_TQP6) MM_GU2_SET(T_TQP7) // the pair trellis gate/up (same 16-weight units) MM_DN2_SET(T_Q2_0) diff --git a/metal/kernels/quant.metal b/metal/kernels/quant.metal index 32c3b87c2c..c2454bf613 100644 --- a/metal/kernels/quant.metal +++ b/metal/kernels/quant.metal @@ -518,6 +518,30 @@ template <> inline float deq(device const uchar* b, int ui, thread floa return rdh(b) * (0.5f + float((sc >> (4 * h)) & 15)) * 0.25f; } +// IQ1_M: qs[32], qh[16], scales[8] (56 bytes); the fp16 scale is packed in the four u16 scales' nibbles. One unit = +// one 32-group half of 16 (grids l = 2h, 2h + 1 of group ib = ui >> 1, h = ui & 1), each grid's delta bit in qh. The +// delta (+-1/8) folds into q[j], the unit's scale is dl (dl1's nibble for the first unit half, dl2's for the second). +// Ported from the 0.1.45 line's deq: there a packed grid_iq1m constant stores nibble j = grid-row byte j + 1 +// (the rows' bytes are -1/0/1), so here the same value is the row's signed byte straight out of the module's +// iq1s_grid constant (the 0.1.45 threadgroup-table path is gone: TAB = 1, and load_tab is a no-op). +template <> inline float deq(device const uchar* b, int ui, thread float* q, threadgroup const uint*) { + const int ib = ui >> 1, h = ui & 1; + device const ushort* sc = (device const ushort*)(b + 48); + const float d = float(as_type(ushort((sc[0] >> 12) | ((sc[1] >> 8) & 0x0f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000)))); + const float dl = d * (2 * ((sc[ib >> 1] >> (6 * (ib & 1) + 3 * h)) & 7) + 1); + const uint qh = b[32 + 2 * ib + h]; + const int qb = 4 * ib + 2 * h; + const ulong g0 = iq1s_grid[uint(b[qb]) | ((qh << 8) & 0x700u)]; + const ulong g1 = iq1s_grid[uint(b[qb + 1]) | ((qh << 4) & 0x700u)]; + const float d0 = (qh & 0x08) ? -0.125f : 0.125f, d1 = (qh & 0x80) ? -0.125f : 0.125f; + for (int j = 0; j < 8; ++j) { + const uint b0 = uint((g0 >> (8 * j)) & 0xffu), b1 = uint((g1 >> (8 * j)) & 0xffu); + q[j] = float(int(b0) - ((b0 & 0x80u) ? 256 : 0)) + d0; + q[8 + j] = float(int(b1) - ((b1 & 0x80u) ? 256 : 0)) + d1; + } + return dl; +} + // IQ3_S: qs[64] low 8 bits of 9-bit indices, qh[8], signs[32], scales[4] (nibble per 32). template <> inline float deq(device const uchar* b, int ui, thread float* q, threadgroup const uint* tab) { const int ib = ui >> 1, h = ui & 1;