21{
22 float result = 0.0f;
23 for (size_t b = 0; b < blocks; ++b) {
26 int32_t weighted = 0;
27#if defined(__AVX2__)
28 __m256i lanes = _mm256_setzero_si256();
29 const __m128i zero = _mm_setzero_si128();
30 const __m128i masks = _mm_set1_epi64x((int64_t)UINT64_C(0x8040201008040201));
31#endif
32 for (
size_t g = 0; g <
QK_K / 16; ++g) {
33 const unsigned high = w->
high[g / 2] >> (4 * (g % 2));
34 const unsigned a = w->
low[2 * g] | ((high & 3) << 8);
35 const unsigned c = w->
low[2 * g + 1] | ((high & 12) << 6);
37 const unsigned sa = w->
signs[2 * g], sc = w->
signs[2 * g + 1];
38 const int scale = 1 + 2 * ((w->
scales[g / 2] >> (4 * (g % 2))) & 15);
39 const int8_t *row = x->
qs + g * 16;
40#if defined(__AVX2__)
41 const __m128i grid = _mm_set_epi64x((int64_t)high_grid, (int64_t)low_grid);
42 const __m128i signs = _mm_set_epi64x((int64_t)(sc * UINT64_C(0x0101010101010101)),
43 (int64_t)(sa * UINT64_C(0x0101010101010101)));
44 const __m128i positive = _mm_cmpeq_epi8(_mm_and_si128(signs, masks), zero);
45 const __m128i signed_grid = _mm_blendv_epi8(_mm_sub_epi8(zero, grid), grid, positive);
46 const __m256i
dot = _mm256_madd_epi16(_mm256_cvtepi8_epi16(signed_grid),
47 _mm256_cvtepi8_epi16(_mm_loadu_si128((const __m128i *)row)));
48 lanes = _mm256_add_epi32(lanes, _mm256_mullo_epi32(
dot, _mm256_set1_epi32(scale)));
49#else
51 for (size_t j = 0; j < 8; ++j) {
52 const int lo = (int)((low_grid >> (8 * j)) & 255);
53 const int hi = (int)((high_grid >> (8 * j)) & 255);
54 dot += row[j] * ((sa & (1u << j)) ? -lo : lo);
55 dot += row[j + 8] * ((sc & (1u << j)) ? -hi : hi);
56 }
57 weighted += scale *
dot;
58#endif
59 }
60#if defined(__AVX2__)
61 int32_t sums[8];
62 _mm256_storeu_si256((__m256i *)sums, lanes);
63 for (size_t j = 0; j < 8; ++j) weighted += sums[j];
64#endif
66 const volatile float contribution = scale * (float)weighted;
67 result += contribution;
68 }
69 return result * 0.125f;
70}
static double dot(const float *a, const float *b, size_t n)
#define CK_FP16_TO_FP32(x)
static const uint64_t ck_iq2_s_codebook[1024]