mirror of
https://github.com/ikawrakow/ik_llama.cpp.git
synced 2026-02-24 07:04:11 +00:00
WIP
This commit is contained in:
@@ -1839,7 +1839,7 @@ static void mul_mat_iq3_s_r4_q8_k(int n, const void * vx, size_t bx, const DataI
|
||||
}
|
||||
}
|
||||
|
||||
inline float convert_to_q8_k_r8(int k, const __m256i * qx, const int16_t * scales, uint32_t * block, int8_t * q8_k) {
|
||||
inline float convert_to_q8_k_r8(int k, int d0, const __m256i * qx, const int16_t * scales, uint32_t * block, int8_t * q8_k) {
|
||||
auto max_i16 = _mm256_setzero_si256();
|
||||
for (int ib32 = 0; ib32 < 8; ++ib32) {
|
||||
auto q16_l = _mm256_cvtepi8_epi16(_mm256_castsi256_si128(qx[ib32]));
|
||||
@@ -1855,7 +1855,7 @@ inline float convert_to_q8_k_r8(int k, const __m256i * qx, const int16_t * scale
|
||||
max4 = _mm_max_ps(max4, _mm_movehl_ps(max4, max4));
|
||||
max4 = _mm_max_ss(max4, _mm_movehdup_ps(max4));
|
||||
bool needs_scaling = true;
|
||||
float dnew = _mm_cvtss_f32(max4) / 127;
|
||||
float dnew = _mm_cvtss_f32(max4) / d0;
|
||||
if (dnew < 1.f) {
|
||||
dnew = 1.f; needs_scaling = false;
|
||||
}
|
||||
@@ -1980,7 +1980,7 @@ void iqk_convert_iq2_xs_q8_k_r8(int n, const void * vx, size_t bx, void * vy, in
|
||||
DequantizerIQ2XS::sign_values_helper(q2l, sign_helper, qx+0);
|
||||
DequantizerIQ2XS::sign_values_helper(q2h, sign_helper, qx+4);
|
||||
#endif
|
||||
float dnew = convert_to_q8_k_r8(k, qx, helper.val, block, y[i].qs);
|
||||
float dnew = convert_to_q8_k_r8(k, 124, qx, helper.val, block, y[i].qs);
|
||||
y[i].d[k] = GGML_FP32_TO_FP16(d*dnew);
|
||||
}
|
||||
}
|
||||
@@ -2293,60 +2293,8 @@ void iqk_convert_iq2_s_q8_k_r8(int n, const void * vx, size_t bx, void * vy, int
|
||||
helper.vec = DequantizerIQ2S::make_scales(x8[k][i].scales);
|
||||
DequantizerIQ2S::prepare(x8[k][i].qs+ 0, x8[k][i].qh+0, (const uint16_t *)(x8[k][i].qs + QK_K/8) + 0, sh, qx+0);
|
||||
DequantizerIQ2S::prepare(x8[k][i].qs+16, x8[k][i].qh+4, (const uint16_t *)(x8[k][i].qs + QK_K/8) + 8, sh, qx+4);
|
||||
auto max_i16 = _mm256_setzero_si256();
|
||||
for (int ib32 = 0; ib32 < 8; ++ib32) {
|
||||
auto q16_l = _mm256_cvtepi8_epi16(_mm256_castsi256_si128(qx[ib32]));
|
||||
auto q16_h = _mm256_cvtepi8_epi16(_mm256_extracti128_si256(qx[ib32], 1));
|
||||
q16_l = _mm256_mullo_epi16(q16_l, _mm256_set1_epi16(helper.val[2*ib32+0]));
|
||||
q16_h = _mm256_mullo_epi16(q16_h, _mm256_set1_epi16(helper.val[2*ib32+1]));
|
||||
max_i16 = _mm256_max_epi16(max_i16, _mm256_sign_epi16(q16_l, q16_l));
|
||||
max_i16 = _mm256_max_epi16(max_i16, _mm256_sign_epi16(q16_h, q16_h));
|
||||
}
|
||||
auto max_q32 = _mm256_cvtepi16_epi32(_mm_max_epi16(_mm256_castsi256_si128(max_i16), _mm256_extracti128_si256(max_i16, 1)));
|
||||
auto imax4 = _mm_max_epi32(_mm256_castsi256_si128(max_q32), _mm256_extracti128_si256(max_q32, 1));
|
||||
auto max4 = _mm_cvtepi32_ps(imax4);
|
||||
max4 = _mm_max_ps(max4, _mm_movehl_ps(max4, max4));
|
||||
max4 = _mm_max_ss(max4, _mm_movehdup_ps(max4));
|
||||
bool needs_scaling = true;
|
||||
float dnew = _mm_cvtss_f32(max4) / 127;
|
||||
if (dnew < 1.f) {
|
||||
dnew = 1.f; needs_scaling = false;
|
||||
}
|
||||
d *= dnew;
|
||||
y[i].d[k] = GGML_FP32_TO_FP16(d);
|
||||
auto scale = _mm256_set1_ps(std::abs(dnew) > 1e-9f ? 1/dnew : 0.f);
|
||||
for (int ib32 = 0; ib32 < 8; ++ib32) {
|
||||
auto q16_l = _mm256_cvtepi8_epi16(_mm256_castsi256_si128(qx[ib32]));
|
||||
auto q16_h = _mm256_cvtepi8_epi16(_mm256_extracti128_si256(qx[ib32], 1));
|
||||
q16_l = _mm256_mullo_epi16(q16_l, _mm256_set1_epi16(helper.val[2*ib32+0]));
|
||||
q16_h = _mm256_mullo_epi16(q16_h, _mm256_set1_epi16(helper.val[2*ib32+1]));
|
||||
if (needs_scaling) {
|
||||
auto i0 = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(q16_l));
|
||||
auto i1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(q16_l, 1));
|
||||
auto i2 = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(q16_h));
|
||||
auto i3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(q16_h, 1));
|
||||
i0 = _mm256_cvtps_epi32(_mm256_round_ps(_mm256_mul_ps(scale, _mm256_cvtepi32_ps(i0)), _MM_ROUND_NEAREST));
|
||||
i1 = _mm256_cvtps_epi32(_mm256_round_ps(_mm256_mul_ps(scale, _mm256_cvtepi32_ps(i1)), _MM_ROUND_NEAREST));
|
||||
i2 = _mm256_cvtps_epi32(_mm256_round_ps(_mm256_mul_ps(scale, _mm256_cvtepi32_ps(i2)), _MM_ROUND_NEAREST));
|
||||
i3 = _mm256_cvtps_epi32(_mm256_round_ps(_mm256_mul_ps(scale, _mm256_cvtepi32_ps(i3)), _MM_ROUND_NEAREST));
|
||||
i0 = _mm256_packs_epi32(i0, i1);
|
||||
i2 = _mm256_packs_epi32(i2, i3);
|
||||
i0 = _mm256_packs_epi16(i0, i2);
|
||||
i0 = _mm256_permutevar8x32_epi32(i0, _mm256_setr_epi32(0, 4, 1, 5, 2, 6, 3, 7));
|
||||
_mm256_storeu_si256((__m256i *)block, i0);
|
||||
} else {
|
||||
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 16, 17, 18, 19, 20, 21, 22, 23, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31
|
||||
auto i0 = _mm256_packs_epi16(q16_l, q16_h);
|
||||
auto i0_l = _mm256_castsi256_si128(i0);
|
||||
auto i0_h = _mm256_extracti128_si256(i0, 1);
|
||||
_mm_storeu_si128((__m128i *)block+0, _mm_unpacklo_epi64(i0_l, i0_h));
|
||||
_mm_storeu_si128((__m128i *)block+1, _mm_unpackhi_epi64(i0_l, i0_h));
|
||||
}
|
||||
auto qs = (uint32_t *)y[i].qs + 64*ib32;
|
||||
for (int l = 0; l < 8; ++l) {
|
||||
qs[8*l + k] = block[l];
|
||||
}
|
||||
}
|
||||
float dnew = convert_to_q8_k_r8(k, 124, qx, helper.val, block, y[i].qs);
|
||||
y[i].d[k] = GGML_FP32_TO_FP16(d*dnew);
|
||||
}
|
||||
}
|
||||
y += nb;
|
||||
|
||||
Reference in New Issue
Block a user