From 7da3c043e4b9405d129190b90fa1908d73d98173 Mon Sep 17 00:00:00 2001 From: Iwan Kawrakow Date: Sun, 15 Jun 2025 15:58:24 +0300 Subject: [PATCH] iq2_xs: repack to q8_k_r8 --- ggml/src/ggml.c | 4 --- ggml/src/iqk/iqk_gemm_iquants.cpp | 58 ++++++++++++++++++++++++++----- ggml/src/iqk/iqk_mul_mat.cpp | 2 +- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c index f91acdad..773871ce 100644 --- a/ggml/src/ggml.c +++ b/ggml/src/ggml.c @@ -1080,11 +1080,7 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = { .from_float = quantize_row_iq2_xxs, .from_float_ref = (ggml_from_float_t)quantize_row_iq2_xxs_ref, .vec_dot = ggml_vec_dot_iq2_xxs_q8_K, -#ifdef __AVX2__ - .vec_dot_type = GGML_TYPE_Q8_2_X4, -#else .vec_dot_type = GGML_TYPE_Q8_K, -#endif .nrows = 1, .row_meta_size = 0, }, diff --git a/ggml/src/iqk/iqk_gemm_iquants.cpp b/ggml/src/iqk/iqk_gemm_iquants.cpp index cfe4b3c4..3862fa6a 100644 --- a/ggml/src/iqk/iqk_gemm_iquants.cpp +++ b/ggml/src/iqk/iqk_gemm_iquants.cpp @@ -1944,6 +1944,46 @@ void iqk_convert_iq2_xxs_q8_0_r8(int n, const void * vx, size_t bx, void * vy, i } } +void iqk_convert_iq2_xxs_q8_k_r8(int n, const void * vx, size_t bx, void * vy, int nrc_x) { + GGML_ASSERT(n%QK_K == 0); + GGML_ASSERT(nrc_x%8 == 0); + + int nb = n/QK_K; + + const block_iq2_xxs * x8[8]; + + block_q8_k_r8 * y = (block_q8_k_r8 *)vy; + + int16_t ls[16]; + EvenSignHelper esh; + + uint32_t block[8]; + uint32_t aux32[2]; + const uint8_t * aux8 = (const uint8_t *)aux32; + + __m256i values[8]; + + for (int ix = 0; ix < nrc_x; ix += 8) { + for (int k = 0; k < 8; ++k) x8[k] = (const block_iq2_xxs *)((const char *)vx + (ix + k)*bx); + for (int i = 0; i < nb; ++i) { + // TODO: simdify + for (int k = 0; k < 8; ++k) { + float d = 0.125f * GGML_FP16_TO_FP32(x8[k][i].d); + for (int ib32 = 0; ib32 < 8; ++ib32) { + std::memcpy(aux32, x8[k][i].qs + 4*ib32, 2*sizeof(uint32_t)); + ls[2*ib32+0] = (2*(aux32[1] >> 28) + 1); + ls[2*ib32+1] = ls[2*ib32+0]; + values[ib32] = _mm256_set_epi64x(iq2xxs_grid[aux8[3]], iq2xxs_grid[aux8[2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]); + esh.sign_value(aux32[1], values[ib32]); + } + float dnew = convert_to_q8_k_r8(k, 124, values, ls, block, y[i].qs); + y[i].d[k] = GGML_FP32_TO_FP16(d*dnew); + } + } + y += nb; + } +} + void iqk_convert_iq2_xs_q8_k_r8(int n, const void * vx, size_t bx, void * vy, int nrc_x) { GGML_ASSERT(n%QK_K == 0); GGML_ASSERT(nrc_x%8 == 0); @@ -2544,14 +2584,14 @@ bool iqk_set_kernels_iquants(int ne00, int typeA, int typeB, std::array= 32 ? GGML_TYPE_F32 : type; case GGML_TYPE_IQ3_KT : return nrc_y >= 32 ? GGML_TYPE_F32 : type; case GGML_TYPE_IQ4_KT : return nrc_y >= 32 ? GGML_TYPE_F32 : type; - case GGML_TYPE_IQ2_XXS: return nrc_y >= 32 ? GGML_TYPE_Q8_0_R8 : type; + case GGML_TYPE_IQ2_XXS: return nrc_y >= 32 ? GGML_TYPE_Q8_K_R8 : type; case GGML_TYPE_IQ2_XS : return nrc_y >= 32 ? GGML_TYPE_Q8_K_R8 : type; case GGML_TYPE_IQ2_S : return nrc_y >= 16 ? GGML_TYPE_Q8_K_R8 : type; case GGML_TYPE_IQ3_XXS: return nrc_y >= 32 ? GGML_TYPE_Q8_0_R8 : type;